[DFAJumpThreading] Remove incoming StartBlock from all phis when unfolding select...
[llvm-project.git] / clang / lib / Format / TokenAnnotator.cpp
blob729e7e370bf62ead495b045f50febeabc49ce505
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 //===----------------------------------------------------------------------===//
15 #include "TokenAnnotator.h"
16 #include "FormatToken.h"
17 #include "clang/Basic/SourceManager.h"
18 #include "clang/Basic/TokenKinds.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/Support/Debug.h"
22 #define DEBUG_TYPE "format-token-annotator"
24 namespace clang {
25 namespace format {
27 namespace {
29 /// Returns \c true if the line starts with a token that can start a statement
30 /// with an initializer.
31 static bool startsWithInitStatement(const AnnotatedLine &Line) {
32 return Line.startsWith(tok::kw_for) || Line.startsWith(tok::kw_if) ||
33 Line.startsWith(tok::kw_switch);
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.
47 static bool canBeObjCSelectorComponent(const FormatToken &Tok) {
48 return Tok.Tok.getIdentifierInfo();
51 /// With `Left` being '(', check if we're at either `[...](` or
52 /// `[...]<...>(`, where the [ opens a lambda capture list.
53 static 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;
61 // Check for `[...]`.
62 return Left->Previous && Left->Previous->is(tok::r_square) &&
63 Left->Previous->MatchingParen &&
64 Left->Previous->MatchingParen->is(TT_LambdaLSquare);
67 /// Returns \c true if the token is followed by a boolean condition, \c false
68 /// otherwise.
69 static 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);
74 /// Returns \c true if the token starts a C++ attribute, \c false otherwise.
75 static 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;
99 if (AttrTok->is(tok::ellipsis))
100 return true;
101 AttrTok = AttrTok->Next;
103 return AttrTok && AttrTok->startsSequence(tok::r_square, tok::r_square);
106 /// A parser that gathers additional information about tokens.
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.
111 class AnnotatingParser {
112 public:
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();
122 private:
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;
137 bool parseAngle() {
138 if (!CurrentToken || !CurrentToken->Previous)
139 return false;
140 if (NonTemplateLess.count(CurrentToken->Previous) > 0)
141 return false;
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;
157 FormatToken *Left = CurrentToken->Previous;
158 Left->ParentBracket = Contexts.back().ContextKind;
159 ScopedContextCreator ContextCreator(*this, tok::less, 12);
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;
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;
171 if (Style.Language == FormatStyle::LK_Java &&
172 CurrentToken->is(tok::question)) {
173 next();
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;
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);
206 if (CurrentToken->Next && CurrentToken->Next->Tok.isLiteral())
207 return false;
208 next();
209 return true;
211 if (CurrentToken->is(tok::question) &&
212 Style.Language == FormatStyle::LK_Java) {
213 next();
214 continue;
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;
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;
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);
244 if (!consumeToken())
245 return false;
247 return false;
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;
266 next();
268 return false;
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);
281 // FIXME: This is a bit of a hack. Do better.
282 Contexts.back().ColonIsForRangeExpr =
283 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
285 if (OpeningParen.Previous &&
286 OpeningParen.Previous->is(TT_UntouchableMacroFunc)) {
287 OpeningParen.Finalized = true;
288 return parseUntouchableParens();
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;
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!");
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;
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;
393 if (StartsObjCMethodExpr) {
394 Contexts.back().ColonIsObjCMethodExpr = true;
395 OpeningParen.setType(TT_ObjCMethodExpr);
398 // MightBeFunctionType and ProbablyFunctionType are used for
399 // function pointer and reference types as well as Objective-C
400 // block types:
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;
434 if (CurrentToken->Previous->is(TT_PointerOrReference) &&
435 CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
436 tok::coloncolon)) {
437 ProbablyFunctionType = true;
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);
453 OpeningParen.MatchingParen = CurrentToken;
454 CurrentToken->MatchingParen = &OpeningParen;
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);
470 if (StartsObjCMethodExpr) {
471 CurrentToken->setType(TT_ObjCMethodExpr);
472 if (Contexts.back().FirstObjCSelectorName) {
473 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
474 Contexts.back().LongestObjCSelectorName;
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);
486 if (OpeningParen.Previous &&
487 OpeningParen.Previous->is(TT_LeadingJavaAnnotation)) {
488 CurrentToken->setType(TT_LeadingJavaAnnotation);
490 if (OpeningParen.Previous &&
491 OpeningParen.Previous->is(TT_AttributeSquare)) {
492 CurrentToken->setType(TT_AttributeSquare);
495 if (!HasMultipleLines)
496 OpeningParen.setPackingKind(PPK_Inconclusive);
497 else if (HasMultipleParametersOnALine)
498 OpeningParen.setPackingKind(PPK_BinPacked);
499 else
500 OpeningParen.setPackingKind(PPK_OnePerLine);
502 next();
503 return true;
505 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
506 return false;
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;
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;
524 if (CurrentToken->isOneOf(tok::semi, tok::colon)) {
525 MightBeObjCForRangeLoop = false;
526 if (PossibleObjCForInToken) {
527 PossibleObjCForInToken->setType(TT_Unknown);
528 PossibleObjCForInToken = nullptr;
531 if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in)) {
532 PossibleObjCForInToken = CurrentToken;
533 PossibleObjCForInToken->setType(TT_ObjCForIn);
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;
540 FormatToken *Tok = CurrentToken;
541 if (!consumeToken())
542 return false;
543 updateParameterCount(&OpeningParen, Tok);
544 if (CurrentToken && CurrentToken->HasUnescapedNewline)
545 HasMultipleLines = true;
547 return false;
550 bool isCSharpAttributeSpecifier(const FormatToken &Tok) {
551 if (!Style.isCSharp())
552 return false;
554 // `identifier[i]` is not an attribute.
555 if (Tok.Previous && Tok.Previous->is(tok::identifier))
556 return false;
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;
565 const FormatToken *AttrTok = Tok.Next;
566 if (!AttrTok)
567 return false;
569 // Just an empty declaration e.g. string [].
570 if (AttrTok->is(tok::r_square))
571 return false;
573 // Move along the tokens inbetween the '[' and ']' e.g. [STAThread].
574 while (AttrTok && AttrTok->isNot(tok::r_square))
575 AttrTok = AttrTok->Next;
577 if (!AttrTok)
578 return false;
580 // Allow an attribute to be the only content of a file.
581 AttrTok = AttrTok->Next;
582 if (!AttrTok)
583 return true;
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;
592 // incase its a [XXX] retval func(....
593 if (AttrTok->Next &&
594 AttrTok->Next->startsSequence(tok::identifier, tok::l_paren)) {
595 return true;
598 return false;
601 bool parseSquare() {
602 if (!CurrentToken)
603 return false;
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();
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);
621 const bool IsInnerSquare = Contexts.back().InCpp11AttributeSpecifier;
622 const bool IsCpp11AttributeSpecifier =
623 isCppAttribute(Style.isCpp(), *Left) || IsInnerSquare;
625 // Treat C# Attributes [STAThread] much like C++ attributes [[...]].
626 bool IsCSharpAttributeSpecifier =
627 isCSharpAttributeSpecifier(*Left) ||
628 Contexts.back().InCSharpAttributeSpecifier;
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) >
645 prec::Unknown));
646 bool ColonFound = false;
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:
676 // optional Aaa aaa = 1 [
677 // (aaa) = aaa
678 // ];
680 // extensions 123 [
681 // (aaa) = aaa
682 // ];
684 // or text proto extensions (in options):
686 // option (Aaa.options) = {
687 // [type.type/type] {
688 // key: value
689 // }
690 // }
692 // or repeated fields (in options):
694 // option (Aaa.options) = {
695 // keys: [ 1, 2, 3 ]
696 // }
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;
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);
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;
727 Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
728 Contexts.back().InCpp11AttributeSpecifier = IsCpp11AttributeSpecifier;
729 Contexts.back().InCSharpAttributeSpecifier = IsCSharpAttributeSpecifier;
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;
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);
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);
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);
767 // An arrow after an ObjC method expression is not a lambda arrow.
768 if (CurrentToken->getType() == TT_ObjCMethodExpr &&
769 CurrentToken->Next &&
770 CurrentToken->Next->is(TT_TrailingReturnArrow)) {
771 CurrentToken->Next->overwriteFixedType(TT_Unknown);
773 Left->MatchingParen = CurrentToken;
774 CurrentToken->MatchingParen = Left;
775 // FirstObjCSelectorName is set when a colon is found. This does
776 // not work, however, when the method has no parameters.
777 // Here, we set FirstObjCSelectorName when the end of the method call is
778 // reached, in case it was not set already.
779 if (!Contexts.back().FirstObjCSelectorName) {
780 FormatToken *Previous = CurrentToken->getPreviousNonComment();
781 if (Previous && Previous->is(TT_SelectorName)) {
782 Previous->ObjCSelectorNameParts = 1;
783 Contexts.back().FirstObjCSelectorName = Previous;
785 } else {
786 Left->ParameterCount =
787 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
789 if (Contexts.back().FirstObjCSelectorName) {
790 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
791 Contexts.back().LongestObjCSelectorName;
792 if (Left->BlockParameterCount > 1)
793 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
795 next();
796 return true;
798 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
799 return false;
800 if (CurrentToken->is(tok::colon)) {
801 if (IsCpp11AttributeSpecifier &&
802 CurrentToken->endsSequence(tok::colon, tok::identifier,
803 tok::kw_using)) {
804 // Remember that this is a [[using ns: foo]] C++ attribute, so we
805 // don't add a space before the colon (unlike other colons).
806 CurrentToken->setType(TT_AttributeColon);
807 } else if (!Style.isVerilog() && !Line.InPragmaDirective &&
808 Left->isOneOf(TT_ArraySubscriptLSquare,
809 TT_DesignatedInitializerLSquare)) {
810 Left->setType(TT_ObjCMethodExpr);
811 StartsObjCMethodExpr = true;
812 Contexts.back().ColonIsObjCMethodExpr = true;
813 if (Parent && Parent->is(tok::r_paren)) {
814 // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
815 Parent->setType(TT_CastRParen);
818 ColonFound = true;
820 if (CurrentToken->is(tok::comma) && Left->is(TT_ObjCMethodExpr) &&
821 !ColonFound) {
822 Left->setType(TT_ArrayInitializerLSquare);
824 FormatToken *Tok = CurrentToken;
825 if (!consumeToken())
826 return false;
827 updateParameterCount(Left, Tok);
829 return false;
832 bool couldBeInStructArrayInitializer() const {
833 if (Contexts.size() < 2)
834 return false;
835 // We want to back up no more then 2 context levels i.e.
836 // . { { <-
837 const auto End = std::next(Contexts.rbegin(), 2);
838 auto Last = Contexts.rbegin();
839 unsigned Depth = 0;
840 for (; Last != End; ++Last)
841 if (Last->ContextKind == tok::l_brace)
842 ++Depth;
843 return Depth == 2 && Last->ContextKind != tok::l_brace;
846 bool parseBrace() {
847 if (!CurrentToken)
848 return true;
850 assert(CurrentToken->Previous);
851 FormatToken &OpeningBrace = *CurrentToken->Previous;
852 assert(OpeningBrace.is(tok::l_brace));
853 OpeningBrace.ParentBracket = Contexts.back().ContextKind;
855 if (Contexts.back().CaretFound)
856 OpeningBrace.overwriteFixedType(TT_ObjCBlockLBrace);
857 Contexts.back().CaretFound = false;
859 ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
860 Contexts.back().ColonIsDictLiteral = true;
861 if (OpeningBrace.is(BK_BracedInit))
862 Contexts.back().IsExpression = true;
863 if (Style.isJavaScript() && OpeningBrace.Previous &&
864 OpeningBrace.Previous->is(TT_JsTypeColon)) {
865 Contexts.back().IsExpression = false;
867 if (Style.isVerilog() &&
868 (!OpeningBrace.getPreviousNonComment() ||
869 OpeningBrace.getPreviousNonComment()->isNot(Keywords.kw_apostrophe))) {
870 Contexts.back().VerilogMayBeConcatenation = true;
873 unsigned CommaCount = 0;
874 while (CurrentToken) {
875 if (CurrentToken->is(tok::r_brace)) {
876 assert(!Scopes.empty());
877 assert(Scopes.back() == getScopeType(OpeningBrace));
878 Scopes.pop_back();
879 assert(OpeningBrace.Optional == CurrentToken->Optional);
880 OpeningBrace.MatchingParen = CurrentToken;
881 CurrentToken->MatchingParen = &OpeningBrace;
882 if (Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
883 if (OpeningBrace.ParentBracket == tok::l_brace &&
884 couldBeInStructArrayInitializer() && CommaCount > 0) {
885 Contexts.back().ContextType = Context::StructArrayInitializer;
888 next();
889 return true;
891 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
892 return false;
893 updateParameterCount(&OpeningBrace, CurrentToken);
894 if (CurrentToken->isOneOf(tok::colon, tok::l_brace, tok::less)) {
895 FormatToken *Previous = CurrentToken->getPreviousNonComment();
896 if (Previous->is(TT_JsTypeOptionalQuestion))
897 Previous = Previous->getPreviousNonComment();
898 if ((CurrentToken->is(tok::colon) &&
899 (!Contexts.back().ColonIsDictLiteral || !Style.isCpp())) ||
900 Style.Language == FormatStyle::LK_Proto ||
901 Style.Language == FormatStyle::LK_TextProto) {
902 OpeningBrace.setType(TT_DictLiteral);
903 if (Previous->Tok.getIdentifierInfo() ||
904 Previous->is(tok::string_literal)) {
905 Previous->setType(TT_SelectorName);
908 if (CurrentToken->is(tok::colon) && OpeningBrace.is(TT_Unknown))
909 OpeningBrace.setType(TT_DictLiteral);
910 else if (Style.isJavaScript())
911 OpeningBrace.overwriteFixedType(TT_DictLiteral);
913 if (CurrentToken->is(tok::comma)) {
914 if (Style.isJavaScript())
915 OpeningBrace.overwriteFixedType(TT_DictLiteral);
916 ++CommaCount;
918 if (!consumeToken())
919 return false;
921 return true;
924 void updateParameterCount(FormatToken *Left, FormatToken *Current) {
925 // For ObjC methods, the number of parameters is calculated differently as
926 // method declarations have a different structure (the parameters are not
927 // inside a bracket scope).
928 if (Current->is(tok::l_brace) && Current->is(BK_Block))
929 ++Left->BlockParameterCount;
930 if (Current->is(tok::comma)) {
931 ++Left->ParameterCount;
932 if (!Left->Role)
933 Left->Role.reset(new CommaSeparatedList(Style));
934 Left->Role->CommaFound(Current);
935 } else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
936 Left->ParameterCount = 1;
940 bool parseConditional() {
941 while (CurrentToken) {
942 if (CurrentToken->is(tok::colon)) {
943 CurrentToken->setType(TT_ConditionalExpr);
944 next();
945 return true;
947 if (!consumeToken())
948 return false;
950 return false;
953 bool parseTemplateDeclaration() {
954 if (CurrentToken && CurrentToken->is(tok::less)) {
955 CurrentToken->setType(TT_TemplateOpener);
956 next();
957 if (!parseAngle())
958 return false;
959 if (CurrentToken)
960 CurrentToken->Previous->ClosesTemplateDeclaration = true;
961 return true;
963 return false;
966 bool consumeToken() {
967 FormatToken *Tok = CurrentToken;
968 next();
969 // In Verilog primitives' state tables, `:`, `?`, and `-` aren't normal
970 // operators.
971 if (Tok->is(TT_VerilogTableItem))
972 return true;
973 switch (Tok->Tok.getKind()) {
974 case tok::plus:
975 case tok::minus:
976 if (!Tok->Previous && Line.MustBeDeclaration)
977 Tok->setType(TT_ObjCMethodSpecifier);
978 break;
979 case tok::colon:
980 if (!Tok->Previous)
981 return false;
982 // Goto labels and case labels are already identified in
983 // UnwrappedLineParser.
984 if (Tok->isTypeFinalized())
985 break;
986 // Colons from ?: are handled in parseConditional().
987 if (Style.isJavaScript()) {
988 if (Contexts.back().ColonIsForRangeExpr || // colon in for loop
989 (Contexts.size() == 1 && // switch/case labels
990 !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) ||
991 Contexts.back().ContextKind == tok::l_paren || // function params
992 Contexts.back().ContextKind == tok::l_square || // array type
993 (!Contexts.back().IsExpression &&
994 Contexts.back().ContextKind == tok::l_brace) || // object type
995 (Contexts.size() == 1 &&
996 Line.MustBeDeclaration)) { // method/property declaration
997 Contexts.back().IsExpression = false;
998 Tok->setType(TT_JsTypeColon);
999 break;
1001 } else if (Style.isCSharp()) {
1002 if (Contexts.back().InCSharpAttributeSpecifier) {
1003 Tok->setType(TT_AttributeColon);
1004 break;
1006 if (Contexts.back().ContextKind == tok::l_paren) {
1007 Tok->setType(TT_CSharpNamedArgumentColon);
1008 break;
1010 } else if (Style.isVerilog() && Tok->isNot(TT_BinaryOperator)) {
1011 // The distribution weight operators are labeled
1012 // TT_BinaryOperator by the lexer.
1013 if (Keywords.isVerilogEnd(*Tok->Previous) ||
1014 Keywords.isVerilogBegin(*Tok->Previous)) {
1015 Tok->setType(TT_VerilogBlockLabelColon);
1016 } else if (Contexts.back().ContextKind == tok::l_square) {
1017 Tok->setType(TT_BitFieldColon);
1018 } else if (Contexts.back().ColonIsDictLiteral) {
1019 Tok->setType(TT_DictLiteral);
1020 } else if (Contexts.size() == 1) {
1021 // In Verilog a case label doesn't have the case keyword. We
1022 // assume a colon following an expression is a case label.
1023 // Colons from ?: are annotated in parseConditional().
1024 Tok->setType(TT_CaseLabelColon);
1025 if (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))
1026 --Line.Level;
1028 break;
1030 if (Line.First->isOneOf(Keywords.kw_module, Keywords.kw_import) ||
1031 Line.First->startsSequence(tok::kw_export, Keywords.kw_module) ||
1032 Line.First->startsSequence(tok::kw_export, Keywords.kw_import)) {
1033 Tok->setType(TT_ModulePartitionColon);
1034 } else if (Contexts.back().ColonIsDictLiteral ||
1035 Style.Language == FormatStyle::LK_Proto ||
1036 Style.Language == FormatStyle::LK_TextProto) {
1037 Tok->setType(TT_DictLiteral);
1038 if (Style.Language == FormatStyle::LK_TextProto) {
1039 if (FormatToken *Previous = Tok->getPreviousNonComment())
1040 Previous->setType(TT_SelectorName);
1042 } else if (Contexts.back().ColonIsObjCMethodExpr ||
1043 Line.startsWith(TT_ObjCMethodSpecifier)) {
1044 Tok->setType(TT_ObjCMethodExpr);
1045 const FormatToken *BeforePrevious = Tok->Previous->Previous;
1046 // Ensure we tag all identifiers in method declarations as
1047 // TT_SelectorName.
1048 bool UnknownIdentifierInMethodDeclaration =
1049 Line.startsWith(TT_ObjCMethodSpecifier) &&
1050 Tok->Previous->is(tok::identifier) && Tok->Previous->is(TT_Unknown);
1051 if (!BeforePrevious ||
1052 // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
1053 !(BeforePrevious->is(TT_CastRParen) ||
1054 (BeforePrevious->is(TT_ObjCMethodExpr) &&
1055 BeforePrevious->is(tok::colon))) ||
1056 BeforePrevious->is(tok::r_square) ||
1057 Contexts.back().LongestObjCSelectorName == 0 ||
1058 UnknownIdentifierInMethodDeclaration) {
1059 Tok->Previous->setType(TT_SelectorName);
1060 if (!Contexts.back().FirstObjCSelectorName) {
1061 Contexts.back().FirstObjCSelectorName = Tok->Previous;
1062 } else if (Tok->Previous->ColumnWidth >
1063 Contexts.back().LongestObjCSelectorName) {
1064 Contexts.back().LongestObjCSelectorName =
1065 Tok->Previous->ColumnWidth;
1067 Tok->Previous->ParameterIndex =
1068 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1069 ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1071 } else if (Contexts.back().ColonIsForRangeExpr) {
1072 Tok->setType(TT_RangeBasedForLoopColon);
1073 } else if (Contexts.back().ContextType == Context::C11GenericSelection) {
1074 Tok->setType(TT_GenericSelectionColon);
1075 } else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) {
1076 Tok->setType(TT_BitFieldColon);
1077 } else if (Contexts.size() == 1 &&
1078 !Line.First->isOneOf(tok::kw_enum, tok::kw_case,
1079 tok::kw_default)) {
1080 FormatToken *Prev = Tok->getPreviousNonComment();
1081 if (!Prev)
1082 break;
1083 if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept) ||
1084 Prev->ClosesRequiresClause) {
1085 Tok->setType(TT_CtorInitializerColon);
1086 } else if (Prev->is(tok::kw_try)) {
1087 // Member initializer list within function try block.
1088 FormatToken *PrevPrev = Prev->getPreviousNonComment();
1089 if (!PrevPrev)
1090 break;
1091 if (PrevPrev && PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept))
1092 Tok->setType(TT_CtorInitializerColon);
1093 } else {
1094 Tok->setType(TT_InheritanceColon);
1096 } else if (canBeObjCSelectorComponent(*Tok->Previous) && Tok->Next &&
1097 (Tok->Next->isOneOf(tok::r_paren, tok::comma) ||
1098 (canBeObjCSelectorComponent(*Tok->Next) && Tok->Next->Next &&
1099 Tok->Next->Next->is(tok::colon)))) {
1100 // This handles a special macro in ObjC code where selectors including
1101 // the colon are passed as macro arguments.
1102 Tok->setType(TT_ObjCMethodExpr);
1103 } else if (Contexts.back().ContextKind == tok::l_paren &&
1104 !Line.InPragmaDirective) {
1105 Tok->setType(TT_InlineASMColon);
1107 break;
1108 case tok::pipe:
1109 case tok::amp:
1110 // | and & in declarations/type expressions represent union and
1111 // intersection types, respectively.
1112 if (Style.isJavaScript() && !Contexts.back().IsExpression)
1113 Tok->setType(TT_JsTypeOperator);
1114 break;
1115 case tok::kw_if:
1116 if (CurrentToken &&
1117 CurrentToken->isOneOf(tok::kw_constexpr, tok::identifier)) {
1118 next();
1120 [[fallthrough]];
1121 case tok::kw_while:
1122 if (CurrentToken && CurrentToken->is(tok::l_paren)) {
1123 next();
1124 if (!parseParens(/*LookForDecls=*/true))
1125 return false;
1127 break;
1128 case tok::kw_for:
1129 if (Style.isJavaScript()) {
1130 // x.for and {for: ...}
1131 if ((Tok->Previous && Tok->Previous->is(tok::period)) ||
1132 (Tok->Next && Tok->Next->is(tok::colon))) {
1133 break;
1135 // JS' for await ( ...
1136 if (CurrentToken && CurrentToken->is(Keywords.kw_await))
1137 next();
1139 if (Style.isCpp() && CurrentToken && CurrentToken->is(tok::kw_co_await))
1140 next();
1141 Contexts.back().ColonIsForRangeExpr = true;
1142 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1143 return false;
1144 next();
1145 if (!parseParens())
1146 return false;
1147 break;
1148 case tok::l_paren:
1149 // When faced with 'operator()()', the kw_operator handler incorrectly
1150 // marks the first l_paren as a OverloadedOperatorLParen. Here, we make
1151 // the first two parens OverloadedOperators and the second l_paren an
1152 // OverloadedOperatorLParen.
1153 if (Tok->Previous && Tok->Previous->is(tok::r_paren) &&
1154 Tok->Previous->MatchingParen &&
1155 Tok->Previous->MatchingParen->is(TT_OverloadedOperatorLParen)) {
1156 Tok->Previous->setType(TT_OverloadedOperator);
1157 Tok->Previous->MatchingParen->setType(TT_OverloadedOperator);
1158 Tok->setType(TT_OverloadedOperatorLParen);
1161 if (Style.isVerilog()) {
1162 // Identify the parameter list and port list in a module instantiation.
1163 // This is still needed when we already have
1164 // UnwrappedLineParser::parseVerilogHierarchyHeader because that
1165 // function is only responsible for the definition, not the
1166 // instantiation.
1167 auto IsInstancePort = [&]() {
1168 const FormatToken *Prev = Tok->getPreviousNonComment();
1169 const FormatToken *PrevPrev;
1170 // In the following example all 4 left parentheses will be treated as
1171 // 'TT_VerilogInstancePortLParen'.
1173 // module_x instance_1(port_1); // Case A.
1174 // module_x #(parameter_1) // Case B.
1175 // instance_2(port_1), // Case C.
1176 // instance_3(port_1); // Case D.
1177 if (!Prev || !(PrevPrev = Prev->getPreviousNonComment()))
1178 return false;
1179 // Case A.
1180 if (Keywords.isVerilogIdentifier(*Prev) &&
1181 Keywords.isVerilogIdentifier(*PrevPrev)) {
1182 return true;
1184 // Case B.
1185 if (Prev->is(Keywords.kw_verilogHash) &&
1186 Keywords.isVerilogIdentifier(*PrevPrev)) {
1187 return true;
1189 // Case C.
1190 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::r_paren))
1191 return true;
1192 // Case D.
1193 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::comma)) {
1194 const FormatToken *PrevParen = PrevPrev->getPreviousNonComment();
1195 if (PrevParen->is(tok::r_paren) && PrevParen->MatchingParen &&
1196 PrevParen->MatchingParen->is(TT_VerilogInstancePortLParen)) {
1197 return true;
1200 return false;
1203 if (IsInstancePort())
1204 Tok->setFinalizedType(TT_VerilogInstancePortLParen);
1207 if (!parseParens())
1208 return false;
1209 if (Line.MustBeDeclaration && Contexts.size() == 1 &&
1210 !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
1211 !Tok->isOneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen)) {
1212 if (const auto *Previous = Tok->Previous;
1213 !Previous ||
1214 (!Previous->isAttribute() &&
1215 !Previous->isOneOf(TT_RequiresClause, TT_LeadingJavaAnnotation))) {
1216 Line.MightBeFunctionDecl = true;
1219 break;
1220 case tok::l_square:
1221 if (!parseSquare())
1222 return false;
1223 break;
1224 case tok::l_brace:
1225 if (Style.Language == FormatStyle::LK_TextProto) {
1226 FormatToken *Previous = Tok->getPreviousNonComment();
1227 if (Previous && Previous->getType() != TT_DictLiteral)
1228 Previous->setType(TT_SelectorName);
1230 Scopes.push_back(getScopeType(*Tok));
1231 if (!parseBrace())
1232 return false;
1233 break;
1234 case tok::less:
1235 if (parseAngle()) {
1236 Tok->setType(TT_TemplateOpener);
1237 // In TT_Proto, we must distignuish between:
1238 // map<key, value>
1239 // msg < item: data >
1240 // msg: < item: data >
1241 // In TT_TextProto, map<key, value> does not occur.
1242 if (Style.Language == FormatStyle::LK_TextProto ||
1243 (Style.Language == FormatStyle::LK_Proto && Tok->Previous &&
1244 Tok->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) {
1245 Tok->setType(TT_DictLiteral);
1246 FormatToken *Previous = Tok->getPreviousNonComment();
1247 if (Previous && Previous->getType() != TT_DictLiteral)
1248 Previous->setType(TT_SelectorName);
1250 } else {
1251 Tok->setType(TT_BinaryOperator);
1252 NonTemplateLess.insert(Tok);
1253 CurrentToken = Tok;
1254 next();
1256 break;
1257 case tok::r_paren:
1258 case tok::r_square:
1259 return false;
1260 case tok::r_brace:
1261 // Don't pop scope when encountering unbalanced r_brace.
1262 if (!Scopes.empty())
1263 Scopes.pop_back();
1264 // Lines can start with '}'.
1265 if (Tok->Previous)
1266 return false;
1267 break;
1268 case tok::greater:
1269 if (Style.Language != FormatStyle::LK_TextProto)
1270 Tok->setType(TT_BinaryOperator);
1271 if (Tok->Previous && Tok->Previous->is(TT_TemplateCloser))
1272 Tok->SpacesRequiredBefore = 1;
1273 break;
1274 case tok::kw_operator:
1275 if (Style.Language == FormatStyle::LK_TextProto ||
1276 Style.Language == FormatStyle::LK_Proto) {
1277 break;
1279 while (CurrentToken &&
1280 !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
1281 if (CurrentToken->isOneOf(tok::star, tok::amp))
1282 CurrentToken->setType(TT_PointerOrReference);
1283 auto Next = CurrentToken->getNextNonComment();
1284 if (!Next)
1285 break;
1286 if (Next->is(tok::less))
1287 next();
1288 else
1289 consumeToken();
1290 if (!CurrentToken)
1291 break;
1292 auto Previous = CurrentToken->getPreviousNonComment();
1293 assert(Previous);
1294 if (CurrentToken->is(tok::comma) && Previous->isNot(tok::kw_operator))
1295 break;
1296 if (Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator, tok::comma,
1297 tok::star, tok::arrow, tok::amp, tok::ampamp) ||
1298 // User defined literal.
1299 Previous->TokenText.startswith("\"\"")) {
1300 Previous->setType(TT_OverloadedOperator);
1301 if (CurrentToken->isOneOf(tok::less, tok::greater))
1302 break;
1305 if (CurrentToken && CurrentToken->is(tok::l_paren))
1306 CurrentToken->setType(TT_OverloadedOperatorLParen);
1307 if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator))
1308 CurrentToken->Previous->setType(TT_OverloadedOperator);
1309 break;
1310 case tok::question:
1311 if (Style.isJavaScript() && Tok->Next &&
1312 Tok->Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
1313 tok::r_brace, tok::r_square)) {
1314 // Question marks before semicolons, colons, etc. indicate optional
1315 // types (fields, parameters), e.g.
1316 // function(x?: string, y?) {...}
1317 // class X { y?; }
1318 Tok->setType(TT_JsTypeOptionalQuestion);
1319 break;
1321 // Declarations cannot be conditional expressions, this can only be part
1322 // of a type declaration.
1323 if (Line.MustBeDeclaration && !Contexts.back().IsExpression &&
1324 Style.isJavaScript()) {
1325 break;
1327 if (Style.isCSharp()) {
1328 // `Type?)`, `Type?>`, `Type? name;` and `Type? name =` can only be
1329 // nullable types.
1331 // `Type?)`, `Type?>`, `Type? name;`
1332 if (Tok->Next &&
1333 (Tok->Next->startsSequence(tok::question, tok::r_paren) ||
1334 Tok->Next->startsSequence(tok::question, tok::greater) ||
1335 Tok->Next->startsSequence(tok::question, tok::identifier,
1336 tok::semi))) {
1337 Tok->setType(TT_CSharpNullable);
1338 break;
1341 // `Type? name =`
1342 if (Tok->Next && Tok->Next->is(tok::identifier) && Tok->Next->Next &&
1343 Tok->Next->Next->is(tok::equal)) {
1344 Tok->setType(TT_CSharpNullable);
1345 break;
1348 // Line.MustBeDeclaration will be true for `Type? name;`.
1349 // But not
1350 // cond ? "A" : "B";
1351 // cond ? id : "B";
1352 // cond ? cond2 ? "A" : "B" : "C";
1353 if (!Contexts.back().IsExpression && Line.MustBeDeclaration &&
1354 (!Tok->Next ||
1355 !Tok->Next->isOneOf(tok::identifier, tok::string_literal) ||
1356 !Tok->Next->Next ||
1357 !Tok->Next->Next->isOneOf(tok::colon, tok::question))) {
1358 Tok->setType(TT_CSharpNullable);
1359 break;
1362 parseConditional();
1363 break;
1364 case tok::kw_template:
1365 parseTemplateDeclaration();
1366 break;
1367 case tok::comma:
1368 switch (Contexts.back().ContextType) {
1369 case Context::CtorInitializer:
1370 Tok->setType(TT_CtorInitializerComma);
1371 break;
1372 case Context::InheritanceList:
1373 Tok->setType(TT_InheritanceComma);
1374 break;
1375 case Context::VerilogInstancePortList:
1376 Tok->setFinalizedType(TT_VerilogInstancePortComma);
1377 break;
1378 default:
1379 if (Style.isVerilog() && Contexts.size() == 1 &&
1380 Line.startsWith(Keywords.kw_assign)) {
1381 Tok->setFinalizedType(TT_VerilogAssignComma);
1382 } else if (Contexts.back().FirstStartOfName &&
1383 (Contexts.size() == 1 || startsWithInitStatement(Line))) {
1384 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true;
1385 Line.IsMultiVariableDeclStmt = true;
1387 break;
1389 if (Contexts.back().ContextType == Context::ForEachMacro)
1390 Contexts.back().IsExpression = true;
1391 break;
1392 case tok::kw_default:
1393 // Unindent case labels.
1394 if (Style.isVerilog() && Keywords.isVerilogEndOfLabel(*Tok) &&
1395 (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))) {
1396 --Line.Level;
1398 break;
1399 case tok::identifier:
1400 if (Tok->isOneOf(Keywords.kw___has_include,
1401 Keywords.kw___has_include_next)) {
1402 parseHasInclude();
1404 if (Style.isCSharp() && Tok->is(Keywords.kw_where) && Tok->Next &&
1405 Tok->Next->isNot(tok::l_paren)) {
1406 Tok->setType(TT_CSharpGenericTypeConstraint);
1407 parseCSharpGenericTypeConstraint();
1408 if (!Tok->getPreviousNonComment())
1409 Line.IsContinuation = true;
1411 break;
1412 case tok::arrow:
1413 if (Tok->Previous && Tok->Previous->is(tok::kw_noexcept))
1414 Tok->setType(TT_TrailingReturnArrow);
1415 break;
1416 default:
1417 break;
1419 return true;
1422 void parseCSharpGenericTypeConstraint() {
1423 int OpenAngleBracketsCount = 0;
1424 while (CurrentToken) {
1425 if (CurrentToken->is(tok::less)) {
1426 // parseAngle is too greedy and will consume the whole line.
1427 CurrentToken->setType(TT_TemplateOpener);
1428 ++OpenAngleBracketsCount;
1429 next();
1430 } else if (CurrentToken->is(tok::greater)) {
1431 CurrentToken->setType(TT_TemplateCloser);
1432 --OpenAngleBracketsCount;
1433 next();
1434 } else if (CurrentToken->is(tok::comma) && OpenAngleBracketsCount == 0) {
1435 // We allow line breaks after GenericTypeConstraintComma's
1436 // so do not flag commas in Generics as GenericTypeConstraintComma's.
1437 CurrentToken->setType(TT_CSharpGenericTypeConstraintComma);
1438 next();
1439 } else if (CurrentToken->is(Keywords.kw_where)) {
1440 CurrentToken->setType(TT_CSharpGenericTypeConstraint);
1441 next();
1442 } else if (CurrentToken->is(tok::colon)) {
1443 CurrentToken->setType(TT_CSharpGenericTypeConstraintColon);
1444 next();
1445 } else {
1446 next();
1451 void parseIncludeDirective() {
1452 if (CurrentToken && CurrentToken->is(tok::less)) {
1453 next();
1454 while (CurrentToken) {
1455 // Mark tokens up to the trailing line comments as implicit string
1456 // literals.
1457 if (CurrentToken->isNot(tok::comment) &&
1458 !CurrentToken->TokenText.startswith("//")) {
1459 CurrentToken->setType(TT_ImplicitStringLiteral);
1461 next();
1466 void parseWarningOrError() {
1467 next();
1468 // We still want to format the whitespace left of the first token of the
1469 // warning or error.
1470 next();
1471 while (CurrentToken) {
1472 CurrentToken->setType(TT_ImplicitStringLiteral);
1473 next();
1477 void parsePragma() {
1478 next(); // Consume "pragma".
1479 if (CurrentToken &&
1480 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option,
1481 Keywords.kw_region)) {
1482 bool IsMarkOrRegion =
1483 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_region);
1484 next();
1485 next(); // Consume first token (so we fix leading whitespace).
1486 while (CurrentToken) {
1487 if (IsMarkOrRegion || CurrentToken->Previous->is(TT_BinaryOperator))
1488 CurrentToken->setType(TT_ImplicitStringLiteral);
1489 next();
1494 void parseHasInclude() {
1495 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1496 return;
1497 next(); // '('
1498 parseIncludeDirective();
1499 next(); // ')'
1502 LineType parsePreprocessorDirective() {
1503 bool IsFirstToken = CurrentToken->IsFirst;
1504 LineType Type = LT_PreprocessorDirective;
1505 next();
1506 if (!CurrentToken)
1507 return Type;
1509 if (Style.isJavaScript() && IsFirstToken) {
1510 // JavaScript files can contain shebang lines of the form:
1511 // #!/usr/bin/env node
1512 // Treat these like C++ #include directives.
1513 while (CurrentToken) {
1514 // Tokens cannot be comments here.
1515 CurrentToken->setType(TT_ImplicitStringLiteral);
1516 next();
1518 return LT_ImportStatement;
1521 if (CurrentToken->is(tok::numeric_constant)) {
1522 CurrentToken->SpacesRequiredBefore = 1;
1523 return Type;
1525 // Hashes in the middle of a line can lead to any strange token
1526 // sequence.
1527 if (!CurrentToken->Tok.getIdentifierInfo())
1528 return Type;
1529 // In Verilog macro expansions start with a backtick just like preprocessor
1530 // directives. Thus we stop if the word is not a preprocessor directive.
1531 if (Style.isVerilog() && !Keywords.isVerilogPPDirective(*CurrentToken))
1532 return LT_Invalid;
1533 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1534 case tok::pp_include:
1535 case tok::pp_include_next:
1536 case tok::pp_import:
1537 next();
1538 parseIncludeDirective();
1539 Type = LT_ImportStatement;
1540 break;
1541 case tok::pp_error:
1542 case tok::pp_warning:
1543 parseWarningOrError();
1544 break;
1545 case tok::pp_pragma:
1546 parsePragma();
1547 break;
1548 case tok::pp_if:
1549 case tok::pp_elif:
1550 Contexts.back().IsExpression = true;
1551 next();
1552 parseLine();
1553 break;
1554 default:
1555 break;
1557 while (CurrentToken) {
1558 FormatToken *Tok = CurrentToken;
1559 next();
1560 if (Tok->is(tok::l_paren)) {
1561 parseParens();
1562 } else if (Tok->isOneOf(Keywords.kw___has_include,
1563 Keywords.kw___has_include_next)) {
1564 parseHasInclude();
1567 return Type;
1570 public:
1571 LineType parseLine() {
1572 if (!CurrentToken)
1573 return LT_Invalid;
1574 NonTemplateLess.clear();
1575 if (!Line.InMacroBody && CurrentToken->is(tok::hash)) {
1576 // We were not yet allowed to use C++17 optional when this was being
1577 // written. So we used LT_Invalid to mark that the line is not a
1578 // preprocessor directive.
1579 auto Type = parsePreprocessorDirective();
1580 if (Type != LT_Invalid)
1581 return Type;
1584 // Directly allow to 'import <string-literal>' to support protocol buffer
1585 // definitions (github.com/google/protobuf) or missing "#" (either way we
1586 // should not break the line).
1587 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
1588 if ((Style.Language == FormatStyle::LK_Java &&
1589 CurrentToken->is(Keywords.kw_package)) ||
1590 (!Style.isVerilog() && Info &&
1591 Info->getPPKeywordID() == tok::pp_import && CurrentToken->Next &&
1592 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
1593 tok::kw_static))) {
1594 next();
1595 parseIncludeDirective();
1596 return LT_ImportStatement;
1599 // If this line starts and ends in '<' and '>', respectively, it is likely
1600 // part of "#define <a/b.h>".
1601 if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
1602 parseIncludeDirective();
1603 return LT_ImportStatement;
1606 // In .proto files, top-level options and package statements are very
1607 // similar to import statements and should not be line-wrapped.
1608 if (Style.Language == FormatStyle::LK_Proto && Line.Level == 0 &&
1609 CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) {
1610 next();
1611 if (CurrentToken && CurrentToken->is(tok::identifier)) {
1612 while (CurrentToken)
1613 next();
1614 return LT_ImportStatement;
1618 bool KeywordVirtualFound = false;
1619 bool ImportStatement = false;
1621 // import {...} from '...';
1622 if (Style.isJavaScript() && CurrentToken->is(Keywords.kw_import))
1623 ImportStatement = true;
1625 while (CurrentToken) {
1626 if (CurrentToken->is(tok::kw_virtual))
1627 KeywordVirtualFound = true;
1628 if (Style.isJavaScript()) {
1629 // export {...} from '...';
1630 // An export followed by "from 'some string';" is a re-export from
1631 // another module identified by a URI and is treated as a
1632 // LT_ImportStatement (i.e. prevent wraps on it for long URIs).
1633 // Just "export {...};" or "export class ..." should not be treated as
1634 // an import in this sense.
1635 if (Line.First->is(tok::kw_export) &&
1636 CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
1637 CurrentToken->Next->isStringLiteral()) {
1638 ImportStatement = true;
1640 if (isClosureImportStatement(*CurrentToken))
1641 ImportStatement = true;
1643 if (!consumeToken())
1644 return LT_Invalid;
1646 if (KeywordVirtualFound)
1647 return LT_VirtualFunctionDecl;
1648 if (ImportStatement)
1649 return LT_ImportStatement;
1651 if (Line.startsWith(TT_ObjCMethodSpecifier)) {
1652 if (Contexts.back().FirstObjCSelectorName) {
1653 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
1654 Contexts.back().LongestObjCSelectorName;
1656 return LT_ObjCMethodDecl;
1659 for (const auto &ctx : Contexts)
1660 if (ctx.ContextType == Context::StructArrayInitializer)
1661 return LT_ArrayOfStructInitializer;
1663 return LT_Other;
1666 private:
1667 bool isClosureImportStatement(const FormatToken &Tok) {
1668 // FIXME: Closure-library specific stuff should not be hard-coded but be
1669 // configurable.
1670 return Tok.TokenText == "goog" && Tok.Next && Tok.Next->is(tok::period) &&
1671 Tok.Next->Next &&
1672 (Tok.Next->Next->TokenText == "module" ||
1673 Tok.Next->Next->TokenText == "provide" ||
1674 Tok.Next->Next->TokenText == "require" ||
1675 Tok.Next->Next->TokenText == "requireType" ||
1676 Tok.Next->Next->TokenText == "forwardDeclare") &&
1677 Tok.Next->Next->Next && Tok.Next->Next->Next->is(tok::l_paren);
1680 void resetTokenMetadata() {
1681 if (!CurrentToken)
1682 return;
1684 // Reset token type in case we have already looked at it and then
1685 // recovered from an error (e.g. failure to find the matching >).
1686 if (!CurrentToken->isTypeFinalized() &&
1687 !CurrentToken->isOneOf(
1688 TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
1689 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
1690 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
1691 TT_NamespaceMacro, TT_OverloadedOperator, TT_RegexLiteral,
1692 TT_TemplateString, TT_ObjCStringLiteral, TT_UntouchableMacroFunc,
1693 TT_StatementAttributeLikeMacro, TT_FunctionLikeOrFreestandingMacro,
1694 TT_ClassLBrace, TT_EnumLBrace, TT_RecordLBrace, TT_StructLBrace,
1695 TT_UnionLBrace, TT_RequiresClause,
1696 TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
1697 TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
1698 TT_BracedListLBrace)) {
1699 CurrentToken->setType(TT_Unknown);
1701 CurrentToken->Role.reset();
1702 CurrentToken->MatchingParen = nullptr;
1703 CurrentToken->FakeLParens.clear();
1704 CurrentToken->FakeRParens = 0;
1707 void next() {
1708 if (!CurrentToken)
1709 return;
1711 CurrentToken->NestingLevel = Contexts.size() - 1;
1712 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
1713 modifyContext(*CurrentToken);
1714 determineTokenType(*CurrentToken);
1715 CurrentToken = CurrentToken->Next;
1717 resetTokenMetadata();
1720 /// A struct to hold information valid in a specific context, e.g.
1721 /// a pair of parenthesis.
1722 struct Context {
1723 Context(tok::TokenKind ContextKind, unsigned BindingStrength,
1724 bool IsExpression)
1725 : ContextKind(ContextKind), BindingStrength(BindingStrength),
1726 IsExpression(IsExpression) {}
1728 tok::TokenKind ContextKind;
1729 unsigned BindingStrength;
1730 bool IsExpression;
1731 unsigned LongestObjCSelectorName = 0;
1732 bool ColonIsForRangeExpr = false;
1733 bool ColonIsDictLiteral = false;
1734 bool ColonIsObjCMethodExpr = false;
1735 FormatToken *FirstObjCSelectorName = nullptr;
1736 FormatToken *FirstStartOfName = nullptr;
1737 bool CanBeExpression = true;
1738 bool CaretFound = false;
1739 bool InCpp11AttributeSpecifier = false;
1740 bool InCSharpAttributeSpecifier = false;
1741 bool VerilogAssignmentFound = false;
1742 // Whether the braces may mean concatenation instead of structure or array
1743 // literal.
1744 bool VerilogMayBeConcatenation = false;
1745 enum {
1746 Unknown,
1747 // Like the part after `:` in a constructor.
1748 // Context(...) : IsExpression(IsExpression)
1749 CtorInitializer,
1750 // Like in the parentheses in a foreach.
1751 ForEachMacro,
1752 // Like the inheritance list in a class declaration.
1753 // class Input : public IO
1754 InheritanceList,
1755 // Like in the braced list.
1756 // int x[] = {};
1757 StructArrayInitializer,
1758 // Like in `static_cast<int>`.
1759 TemplateArgument,
1760 // C11 _Generic selection.
1761 C11GenericSelection,
1762 // Like in the outer parentheses in `ffnand ff1(.q());`.
1763 VerilogInstancePortList,
1764 } ContextType = Unknown;
1767 /// Puts a new \c Context onto the stack \c Contexts for the lifetime
1768 /// of each instance.
1769 struct ScopedContextCreator {
1770 AnnotatingParser &P;
1772 ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind,
1773 unsigned Increase)
1774 : P(P) {
1775 P.Contexts.push_back(Context(ContextKind,
1776 P.Contexts.back().BindingStrength + Increase,
1777 P.Contexts.back().IsExpression));
1780 ~ScopedContextCreator() {
1781 if (P.Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
1782 if (P.Contexts.back().ContextType == Context::StructArrayInitializer) {
1783 P.Contexts.pop_back();
1784 P.Contexts.back().ContextType = Context::StructArrayInitializer;
1785 return;
1788 P.Contexts.pop_back();
1792 void modifyContext(const FormatToken &Current) {
1793 auto AssignmentStartsExpression = [&]() {
1794 if (Current.getPrecedence() != prec::Assignment)
1795 return false;
1797 if (Line.First->isOneOf(tok::kw_using, tok::kw_return))
1798 return false;
1799 if (Line.First->is(tok::kw_template)) {
1800 assert(Current.Previous);
1801 if (Current.Previous->is(tok::kw_operator)) {
1802 // `template ... operator=` cannot be an expression.
1803 return false;
1806 // `template` keyword can start a variable template.
1807 const FormatToken *Tok = Line.First->getNextNonComment();
1808 assert(Tok); // Current token is on the same line.
1809 if (Tok->isNot(TT_TemplateOpener)) {
1810 // Explicit template instantiations do not have `<>`.
1811 return false;
1814 // This is the default value of a template parameter, determine if it's
1815 // type or non-type.
1816 if (Contexts.back().ContextKind == tok::less) {
1817 assert(Current.Previous->Previous);
1818 return !Current.Previous->Previous->isOneOf(tok::kw_typename,
1819 tok::kw_class);
1822 Tok = Tok->MatchingParen;
1823 if (!Tok)
1824 return false;
1825 Tok = Tok->getNextNonComment();
1826 if (!Tok)
1827 return false;
1829 if (Tok->isOneOf(tok::kw_class, tok::kw_enum, tok::kw_struct,
1830 tok::kw_using)) {
1831 return false;
1834 return true;
1837 // Type aliases use `type X = ...;` in TypeScript and can be exported
1838 // using `export type ...`.
1839 if (Style.isJavaScript() &&
1840 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
1841 Line.startsWith(tok::kw_export, Keywords.kw_type,
1842 tok::identifier))) {
1843 return false;
1846 return !Current.Previous || Current.Previous->isNot(tok::kw_operator);
1849 if (AssignmentStartsExpression()) {
1850 Contexts.back().IsExpression = true;
1851 if (!Line.startsWith(TT_UnaryOperator)) {
1852 for (FormatToken *Previous = Current.Previous;
1853 Previous && Previous->Previous &&
1854 !Previous->Previous->isOneOf(tok::comma, tok::semi);
1855 Previous = Previous->Previous) {
1856 if (Previous->isOneOf(tok::r_square, tok::r_paren, tok::greater)) {
1857 Previous = Previous->MatchingParen;
1858 if (!Previous)
1859 break;
1861 if (Previous->opensScope())
1862 break;
1863 if (Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
1864 Previous->isOneOf(tok::star, tok::amp, tok::ampamp) &&
1865 Previous->Previous && Previous->Previous->isNot(tok::equal)) {
1866 Previous->setType(TT_PointerOrReference);
1870 } else if (Current.is(tok::lessless) &&
1871 (!Current.Previous ||
1872 Current.Previous->isNot(tok::kw_operator))) {
1873 Contexts.back().IsExpression = true;
1874 } else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
1875 Contexts.back().IsExpression = true;
1876 } else if (Current.is(TT_TrailingReturnArrow)) {
1877 Contexts.back().IsExpression = false;
1878 } else if (Current.is(Keywords.kw_assert)) {
1879 Contexts.back().IsExpression = Style.Language == FormatStyle::LK_Java;
1880 } else if (Current.Previous &&
1881 Current.Previous->is(TT_CtorInitializerColon)) {
1882 Contexts.back().IsExpression = true;
1883 Contexts.back().ContextType = Context::CtorInitializer;
1884 } else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
1885 Contexts.back().ContextType = Context::InheritanceList;
1886 } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
1887 for (FormatToken *Previous = Current.Previous;
1888 Previous && Previous->isOneOf(tok::star, tok::amp);
1889 Previous = Previous->Previous) {
1890 Previous->setType(TT_PointerOrReference);
1892 if (Line.MustBeDeclaration &&
1893 Contexts.front().ContextType != Context::CtorInitializer) {
1894 Contexts.back().IsExpression = false;
1896 } else if (Current.is(tok::kw_new)) {
1897 Contexts.back().CanBeExpression = false;
1898 } else if (Current.is(tok::semi) ||
1899 (Current.is(tok::exclaim) && Current.Previous &&
1900 Current.Previous->isNot(tok::kw_operator))) {
1901 // This should be the condition or increment in a for-loop.
1902 // But not operator !() (can't use TT_OverloadedOperator here as its not
1903 // been annotated yet).
1904 Contexts.back().IsExpression = true;
1908 static FormatToken *untilMatchingParen(FormatToken *Current) {
1909 // Used when `MatchingParen` is not yet established.
1910 int ParenLevel = 0;
1911 while (Current) {
1912 if (Current->is(tok::l_paren))
1913 ++ParenLevel;
1914 if (Current->is(tok::r_paren))
1915 --ParenLevel;
1916 if (ParenLevel < 1)
1917 break;
1918 Current = Current->Next;
1920 return Current;
1923 static bool isDeductionGuide(FormatToken &Current) {
1924 // Look for a deduction guide template<T> A(...) -> A<...>;
1925 if (Current.Previous && Current.Previous->is(tok::r_paren) &&
1926 Current.startsSequence(tok::arrow, tok::identifier, tok::less)) {
1927 // Find the TemplateCloser.
1928 FormatToken *TemplateCloser = Current.Next->Next;
1929 int NestingLevel = 0;
1930 while (TemplateCloser) {
1931 // Skip over an expressions in parens A<(3 < 2)>;
1932 if (TemplateCloser->is(tok::l_paren)) {
1933 // No Matching Paren yet so skip to matching paren
1934 TemplateCloser = untilMatchingParen(TemplateCloser);
1935 if (!TemplateCloser)
1936 break;
1938 if (TemplateCloser->is(tok::less))
1939 ++NestingLevel;
1940 if (TemplateCloser->is(tok::greater))
1941 --NestingLevel;
1942 if (NestingLevel < 1)
1943 break;
1944 TemplateCloser = TemplateCloser->Next;
1946 // Assuming we have found the end of the template ensure its followed
1947 // with a semi-colon.
1948 if (TemplateCloser && TemplateCloser->Next &&
1949 TemplateCloser->Next->is(tok::semi) &&
1950 Current.Previous->MatchingParen) {
1951 // Determine if the identifier `A` prior to the A<..>; is the same as
1952 // prior to the A(..)
1953 FormatToken *LeadingIdentifier =
1954 Current.Previous->MatchingParen->Previous;
1956 return LeadingIdentifier &&
1957 LeadingIdentifier->TokenText == Current.Next->TokenText;
1960 return false;
1963 void determineTokenType(FormatToken &Current) {
1964 if (Current.isNot(TT_Unknown)) {
1965 // The token type is already known.
1966 return;
1969 if ((Style.isJavaScript() || Style.isCSharp()) &&
1970 Current.is(tok::exclaim)) {
1971 if (Current.Previous) {
1972 bool IsIdentifier =
1973 Style.isJavaScript()
1974 ? Keywords.IsJavaScriptIdentifier(
1975 *Current.Previous, /* AcceptIdentifierName= */ true)
1976 : Current.Previous->is(tok::identifier);
1977 if (IsIdentifier ||
1978 Current.Previous->isOneOf(
1979 tok::kw_default, tok::kw_namespace, tok::r_paren, tok::r_square,
1980 tok::r_brace, tok::kw_false, tok::kw_true, Keywords.kw_type,
1981 Keywords.kw_get, Keywords.kw_init, Keywords.kw_set) ||
1982 Current.Previous->Tok.isLiteral()) {
1983 Current.setType(TT_NonNullAssertion);
1984 return;
1987 if (Current.Next &&
1988 Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
1989 Current.setType(TT_NonNullAssertion);
1990 return;
1994 // Line.MightBeFunctionDecl can only be true after the parentheses of a
1995 // function declaration have been found. In this case, 'Current' is a
1996 // trailing token of this declaration and thus cannot be a name.
1997 if (Current.is(Keywords.kw_instanceof)) {
1998 Current.setType(TT_BinaryOperator);
1999 } else if (isStartOfName(Current) &&
2000 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
2001 Contexts.back().FirstStartOfName = &Current;
2002 Current.setType(TT_StartOfName);
2003 } else if (Current.is(tok::semi)) {
2004 // Reset FirstStartOfName after finding a semicolon so that a for loop
2005 // with multiple increment statements is not confused with a for loop
2006 // having multiple variable declarations.
2007 Contexts.back().FirstStartOfName = nullptr;
2008 } else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
2009 AutoFound = true;
2010 } else if (Current.is(tok::arrow) &&
2011 Style.Language == FormatStyle::LK_Java) {
2012 Current.setType(TT_TrailingReturnArrow);
2013 } else if (Current.is(tok::arrow) && AutoFound &&
2014 Line.MightBeFunctionDecl && Current.NestingLevel == 0 &&
2015 !Current.Previous->isOneOf(tok::kw_operator, tok::identifier)) {
2016 // not auto operator->() -> xxx;
2017 Current.setType(TT_TrailingReturnArrow);
2018 } else if (Current.is(tok::arrow) && Current.Previous &&
2019 Current.Previous->is(tok::r_brace)) {
2020 // Concept implicit conversion constraint needs to be treated like
2021 // a trailing return type ... } -> <type>.
2022 Current.setType(TT_TrailingReturnArrow);
2023 } else if (isDeductionGuide(Current)) {
2024 // Deduction guides trailing arrow " A(...) -> A<T>;".
2025 Current.setType(TT_TrailingReturnArrow);
2026 } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
2027 Current.setType(determineStarAmpUsage(
2028 Current,
2029 Contexts.back().CanBeExpression && Contexts.back().IsExpression,
2030 Contexts.back().ContextType == Context::TemplateArgument));
2031 } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret) ||
2032 (Style.isVerilog() && Current.is(tok::pipe))) {
2033 Current.setType(determinePlusMinusCaretUsage(Current));
2034 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
2035 Contexts.back().CaretFound = true;
2036 } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
2037 Current.setType(determineIncrementUsage(Current));
2038 } else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
2039 Current.setType(TT_UnaryOperator);
2040 } else if (Current.is(tok::question)) {
2041 if (Style.isJavaScript() && Line.MustBeDeclaration &&
2042 !Contexts.back().IsExpression) {
2043 // In JavaScript, `interface X { foo?(): bar; }` is an optional method
2044 // on the interface, not a ternary expression.
2045 Current.setType(TT_JsTypeOptionalQuestion);
2046 } else {
2047 Current.setType(TT_ConditionalExpr);
2049 } else if (Current.isBinaryOperator() &&
2050 (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
2051 (Current.isNot(tok::greater) &&
2052 Style.Language != FormatStyle::LK_TextProto)) {
2053 if (Style.isVerilog()) {
2054 if (Current.is(tok::lessequal) && Contexts.size() == 1 &&
2055 !Contexts.back().VerilogAssignmentFound) {
2056 // In Verilog `<=` is assignment if in its own statement. It is a
2057 // statement instead of an expression, that is it can not be chained.
2058 Current.ForcedPrecedence = prec::Assignment;
2059 Current.setFinalizedType(TT_BinaryOperator);
2061 if (Current.getPrecedence() == prec::Assignment)
2062 Contexts.back().VerilogAssignmentFound = true;
2064 Current.setType(TT_BinaryOperator);
2065 } else if (Current.is(tok::comment)) {
2066 if (Current.TokenText.startswith("/*")) {
2067 if (Current.TokenText.endswith("*/")) {
2068 Current.setType(TT_BlockComment);
2069 } else {
2070 // The lexer has for some reason determined a comment here. But we
2071 // cannot really handle it, if it isn't properly terminated.
2072 Current.Tok.setKind(tok::unknown);
2074 } else {
2075 Current.setType(TT_LineComment);
2077 } else if (Current.is(tok::string_literal)) {
2078 if (Style.isVerilog() && Contexts.back().VerilogMayBeConcatenation &&
2079 Current.getPreviousNonComment() &&
2080 Current.getPreviousNonComment()->isOneOf(tok::comma, tok::l_brace) &&
2081 Current.getNextNonComment() &&
2082 Current.getNextNonComment()->isOneOf(tok::comma, tok::r_brace)) {
2083 Current.setType(TT_StringInConcatenation);
2085 } else if (Current.is(tok::l_paren)) {
2086 if (lParenStartsCppCast(Current))
2087 Current.setType(TT_CppCastLParen);
2088 } else if (Current.is(tok::r_paren)) {
2089 if (rParenEndsCast(Current))
2090 Current.setType(TT_CastRParen);
2091 if (Current.MatchingParen && Current.Next &&
2092 !Current.Next->isBinaryOperator() &&
2093 !Current.Next->isOneOf(tok::semi, tok::colon, tok::l_brace,
2094 tok::comma, tok::period, tok::arrow,
2095 tok::coloncolon, tok::kw_noexcept)) {
2096 if (FormatToken *AfterParen = Current.MatchingParen->Next;
2097 AfterParen && AfterParen->isNot(tok::caret)) {
2098 // Make sure this isn't the return type of an Obj-C block declaration.
2099 if (FormatToken *BeforeParen = Current.MatchingParen->Previous;
2100 BeforeParen && BeforeParen->is(tok::identifier) &&
2101 BeforeParen->isNot(TT_TypenameMacro) &&
2102 BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
2103 (!BeforeParen->Previous ||
2104 BeforeParen->Previous->ClosesTemplateDeclaration ||
2105 BeforeParen->Previous->ClosesRequiresClause)) {
2106 Current.setType(TT_FunctionAnnotationRParen);
2110 } else if (Current.is(tok::at) && Current.Next && !Style.isJavaScript() &&
2111 Style.Language != FormatStyle::LK_Java) {
2112 // In Java & JavaScript, "@..." is a decorator or annotation. In ObjC, it
2113 // marks declarations and properties that need special formatting.
2114 switch (Current.Next->Tok.getObjCKeywordID()) {
2115 case tok::objc_interface:
2116 case tok::objc_implementation:
2117 case tok::objc_protocol:
2118 Current.setType(TT_ObjCDecl);
2119 break;
2120 case tok::objc_property:
2121 Current.setType(TT_ObjCProperty);
2122 break;
2123 default:
2124 break;
2126 } else if (Current.is(tok::period)) {
2127 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
2128 if (PreviousNoComment &&
2129 PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) {
2130 Current.setType(TT_DesignatedInitializerPeriod);
2131 } else if (Style.Language == FormatStyle::LK_Java && Current.Previous &&
2132 Current.Previous->isOneOf(TT_JavaAnnotation,
2133 TT_LeadingJavaAnnotation)) {
2134 Current.setType(Current.Previous->getType());
2136 } else if (canBeObjCSelectorComponent(Current) &&
2137 // FIXME(bug 36976): ObjC return types shouldn't use
2138 // TT_CastRParen.
2139 Current.Previous && Current.Previous->is(TT_CastRParen) &&
2140 Current.Previous->MatchingParen &&
2141 Current.Previous->MatchingParen->Previous &&
2142 Current.Previous->MatchingParen->Previous->is(
2143 TT_ObjCMethodSpecifier)) {
2144 // This is the first part of an Objective-C selector name. (If there's no
2145 // colon after this, this is the only place which annotates the identifier
2146 // as a selector.)
2147 Current.setType(TT_SelectorName);
2148 } else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
2149 tok::kw_requires) &&
2150 Current.Previous &&
2151 !Current.Previous->isOneOf(tok::equal, tok::at,
2152 TT_CtorInitializerComma,
2153 TT_CtorInitializerColon) &&
2154 Line.MightBeFunctionDecl && Contexts.size() == 1) {
2155 // Line.MightBeFunctionDecl can only be true after the parentheses of a
2156 // function declaration have been found.
2157 Current.setType(TT_TrailingAnnotation);
2158 } else if ((Style.Language == FormatStyle::LK_Java ||
2159 Style.isJavaScript()) &&
2160 Current.Previous) {
2161 if (Current.Previous->is(tok::at) &&
2162 Current.isNot(Keywords.kw_interface)) {
2163 const FormatToken &AtToken = *Current.Previous;
2164 const FormatToken *Previous = AtToken.getPreviousNonComment();
2165 if (!Previous || Previous->is(TT_LeadingJavaAnnotation))
2166 Current.setType(TT_LeadingJavaAnnotation);
2167 else
2168 Current.setType(TT_JavaAnnotation);
2169 } else if (Current.Previous->is(tok::period) &&
2170 Current.Previous->isOneOf(TT_JavaAnnotation,
2171 TT_LeadingJavaAnnotation)) {
2172 Current.setType(Current.Previous->getType());
2177 /// Take a guess at whether \p Tok starts a name of a function or
2178 /// variable declaration.
2180 /// This is a heuristic based on whether \p Tok is an identifier following
2181 /// something that is likely a type.
2182 bool isStartOfName(const FormatToken &Tok) {
2183 // Handled in ExpressionParser for Verilog.
2184 if (Style.isVerilog())
2185 return false;
2187 if (Tok.isNot(tok::identifier) || !Tok.Previous)
2188 return false;
2190 if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
2191 Keywords.kw_as)) {
2192 return false;
2194 if (Style.isJavaScript() && Tok.Previous->is(Keywords.kw_in))
2195 return false;
2197 // Skip "const" as it does not have an influence on whether this is a name.
2198 FormatToken *PreviousNotConst = Tok.getPreviousNonComment();
2200 // For javascript const can be like "let" or "var"
2201 if (!Style.isJavaScript())
2202 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
2203 PreviousNotConst = PreviousNotConst->getPreviousNonComment();
2205 if (!PreviousNotConst)
2206 return false;
2208 if (PreviousNotConst->ClosesRequiresClause)
2209 return false;
2211 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
2212 PreviousNotConst->Previous &&
2213 PreviousNotConst->Previous->is(tok::hash);
2215 if (PreviousNotConst->is(TT_TemplateCloser)) {
2216 return PreviousNotConst && PreviousNotConst->MatchingParen &&
2217 PreviousNotConst->MatchingParen->Previous &&
2218 PreviousNotConst->MatchingParen->Previous->isNot(tok::period) &&
2219 PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
2222 if (PreviousNotConst->is(tok::r_paren) &&
2223 PreviousNotConst->is(TT_TypeDeclarationParen)) {
2224 return true;
2227 // If is a preprocess keyword like #define.
2228 if (IsPPKeyword)
2229 return false;
2231 // int a or auto a.
2232 if (PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto))
2233 return true;
2235 // *a or &a or &&a.
2236 if (PreviousNotConst->is(TT_PointerOrReference))
2237 return true;
2239 // MyClass a;
2240 if (PreviousNotConst->isSimpleTypeSpecifier())
2241 return true;
2243 // type[] a in Java
2244 if (Style.Language == FormatStyle::LK_Java &&
2245 PreviousNotConst->is(tok::r_square)) {
2246 return true;
2249 // const a = in JavaScript.
2250 return Style.isJavaScript() && PreviousNotConst->is(tok::kw_const);
2253 /// Determine whether '(' is starting a C++ cast.
2254 bool lParenStartsCppCast(const FormatToken &Tok) {
2255 // C-style casts are only used in C++.
2256 if (!Style.isCpp())
2257 return false;
2259 FormatToken *LeftOfParens = Tok.getPreviousNonComment();
2260 if (LeftOfParens && LeftOfParens->is(TT_TemplateCloser) &&
2261 LeftOfParens->MatchingParen) {
2262 auto *Prev = LeftOfParens->MatchingParen->getPreviousNonComment();
2263 if (Prev &&
2264 Prev->isOneOf(tok::kw_const_cast, tok::kw_dynamic_cast,
2265 tok::kw_reinterpret_cast, tok::kw_static_cast)) {
2266 // FIXME: Maybe we should handle identifiers ending with "_cast",
2267 // e.g. any_cast?
2268 return true;
2271 return false;
2274 /// Determine whether ')' is ending a cast.
2275 bool rParenEndsCast(const FormatToken &Tok) {
2276 // C-style casts are only used in C++, C# and Java.
2277 if (!Style.isCSharp() && !Style.isCpp() &&
2278 Style.Language != FormatStyle::LK_Java) {
2279 return false;
2282 // Empty parens aren't casts and there are no casts at the end of the line.
2283 if (Tok.Previous == Tok.MatchingParen || !Tok.Next || !Tok.MatchingParen)
2284 return false;
2286 if (Tok.MatchingParen->is(TT_OverloadedOperatorLParen))
2287 return false;
2289 FormatToken *LeftOfParens = Tok.MatchingParen->getPreviousNonComment();
2290 if (LeftOfParens) {
2291 // If there is a closing parenthesis left of the current
2292 // parentheses, look past it as these might be chained casts.
2293 if (LeftOfParens->is(tok::r_paren) &&
2294 LeftOfParens->isNot(TT_CastRParen)) {
2295 if (!LeftOfParens->MatchingParen ||
2296 !LeftOfParens->MatchingParen->Previous) {
2297 return false;
2299 LeftOfParens = LeftOfParens->MatchingParen->Previous;
2302 if (LeftOfParens->is(tok::r_square)) {
2303 // delete[] (void *)ptr;
2304 auto MayBeArrayDelete = [](FormatToken *Tok) -> FormatToken * {
2305 if (Tok->isNot(tok::r_square))
2306 return nullptr;
2308 Tok = Tok->getPreviousNonComment();
2309 if (!Tok || Tok->isNot(tok::l_square))
2310 return nullptr;
2312 Tok = Tok->getPreviousNonComment();
2313 if (!Tok || Tok->isNot(tok::kw_delete))
2314 return nullptr;
2315 return Tok;
2317 if (FormatToken *MaybeDelete = MayBeArrayDelete(LeftOfParens))
2318 LeftOfParens = MaybeDelete;
2321 // The Condition directly below this one will see the operator arguments
2322 // as a (void *foo) cast.
2323 // void operator delete(void *foo) ATTRIB;
2324 if (LeftOfParens->Tok.getIdentifierInfo() && LeftOfParens->Previous &&
2325 LeftOfParens->Previous->is(tok::kw_operator)) {
2326 return false;
2329 // If there is an identifier (or with a few exceptions a keyword) right
2330 // before the parentheses, this is unlikely to be a cast.
2331 if (LeftOfParens->Tok.getIdentifierInfo() &&
2332 !LeftOfParens->isOneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
2333 tok::kw_delete, tok::kw_throw)) {
2334 return false;
2337 // Certain other tokens right before the parentheses are also signals that
2338 // this cannot be a cast.
2339 if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
2340 TT_TemplateCloser, tok::ellipsis)) {
2341 return false;
2345 if (Tok.Next->is(tok::question))
2346 return false;
2348 // `foreach((A a, B b) in someList)` should not be seen as a cast.
2349 if (Tok.Next->is(Keywords.kw_in) && Style.isCSharp())
2350 return false;
2352 // Functions which end with decorations like volatile, noexcept are unlikely
2353 // to be casts.
2354 if (Tok.Next->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
2355 tok::kw_requires, tok::kw_throw, tok::arrow,
2356 Keywords.kw_override, Keywords.kw_final) ||
2357 isCppAttribute(Style.isCpp(), *Tok.Next)) {
2358 return false;
2361 // As Java has no function types, a "(" after the ")" likely means that this
2362 // is a cast.
2363 if (Style.Language == FormatStyle::LK_Java && Tok.Next->is(tok::l_paren))
2364 return true;
2366 // If a (non-string) literal follows, this is likely a cast.
2367 if (Tok.Next->isNot(tok::string_literal) &&
2368 (Tok.Next->Tok.isLiteral() ||
2369 Tok.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof))) {
2370 return true;
2373 // Heuristically try to determine whether the parentheses contain a type.
2374 auto IsQualifiedPointerOrReference = [](FormatToken *T) {
2375 // This is used to handle cases such as x = (foo *const)&y;
2376 assert(!T->isSimpleTypeSpecifier() && "Should have already been checked");
2377 // Strip trailing qualifiers such as const or volatile when checking
2378 // whether the parens could be a cast to a pointer/reference type.
2379 while (T) {
2380 if (T->is(TT_AttributeRParen)) {
2381 // Handle `x = (foo *__attribute__((foo)))&v;`:
2382 assert(T->is(tok::r_paren));
2383 assert(T->MatchingParen);
2384 assert(T->MatchingParen->is(tok::l_paren));
2385 assert(T->MatchingParen->is(TT_AttributeLParen));
2386 if (const auto *Tok = T->MatchingParen->Previous;
2387 Tok && Tok->isAttribute()) {
2388 T = Tok->Previous;
2389 continue;
2391 } else if (T->is(TT_AttributeSquare)) {
2392 // Handle `x = (foo *[[clang::foo]])&v;`:
2393 if (T->MatchingParen && T->MatchingParen->Previous) {
2394 T = T->MatchingParen->Previous;
2395 continue;
2397 } else if (T->canBePointerOrReferenceQualifier()) {
2398 T = T->Previous;
2399 continue;
2401 break;
2403 return T && T->is(TT_PointerOrReference);
2405 bool ParensAreType =
2406 !Tok.Previous ||
2407 Tok.Previous->isOneOf(TT_TemplateCloser, TT_TypeDeclarationParen) ||
2408 Tok.Previous->isSimpleTypeSpecifier() ||
2409 IsQualifiedPointerOrReference(Tok.Previous);
2410 bool ParensCouldEndDecl =
2411 Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
2412 if (ParensAreType && !ParensCouldEndDecl)
2413 return true;
2415 // At this point, we heuristically assume that there are no casts at the
2416 // start of the line. We assume that we have found most cases where there
2417 // are by the logic above, e.g. "(void)x;".
2418 if (!LeftOfParens)
2419 return false;
2421 // Certain token types inside the parentheses mean that this can't be a
2422 // cast.
2423 for (const FormatToken *Token = Tok.MatchingParen->Next; Token != &Tok;
2424 Token = Token->Next) {
2425 if (Token->is(TT_BinaryOperator))
2426 return false;
2429 // If the following token is an identifier or 'this', this is a cast. All
2430 // cases where this can be something else are handled above.
2431 if (Tok.Next->isOneOf(tok::identifier, tok::kw_this))
2432 return true;
2434 // Look for a cast `( x ) (`.
2435 if (Tok.Next->is(tok::l_paren) && Tok.Previous && Tok.Previous->Previous) {
2436 if (Tok.Previous->is(tok::identifier) &&
2437 Tok.Previous->Previous->is(tok::l_paren)) {
2438 return true;
2442 if (!Tok.Next->Next)
2443 return false;
2445 // If the next token after the parenthesis is a unary operator, assume
2446 // that this is cast, unless there are unexpected tokens inside the
2447 // parenthesis.
2448 const bool NextIsAmpOrStar = Tok.Next->isOneOf(tok::amp, tok::star);
2449 if (!(Tok.Next->isUnaryOperator() || NextIsAmpOrStar) ||
2450 Tok.Next->is(tok::plus) ||
2451 !Tok.Next->Next->isOneOf(tok::identifier, tok::numeric_constant)) {
2452 return false;
2454 if (NextIsAmpOrStar &&
2455 (Tok.Next->Next->is(tok::numeric_constant) || Line.InPPDirective)) {
2456 return false;
2458 // Search for unexpected tokens.
2459 for (FormatToken *Prev = Tok.Previous; Prev != Tok.MatchingParen;
2460 Prev = Prev->Previous) {
2461 if (!Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon))
2462 return false;
2464 return true;
2467 /// Returns true if the token is used as a unary operator.
2468 bool determineUnaryOperatorByUsage(const FormatToken &Tok) {
2469 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2470 if (!PrevToken)
2471 return true;
2473 // These keywords are deliberately not included here because they may
2474 // precede only one of unary star/amp and plus/minus but not both. They are
2475 // either included in determineStarAmpUsage or determinePlusMinusCaretUsage.
2477 // @ - It may be followed by a unary `-` in Objective-C literals. We don't
2478 // know how they can be followed by a star or amp.
2479 if (PrevToken->isOneOf(
2480 TT_ConditionalExpr, tok::l_paren, tok::comma, tok::colon, tok::semi,
2481 tok::equal, tok::question, tok::l_square, tok::l_brace,
2482 tok::kw_case, tok::kw_co_await, tok::kw_co_return, tok::kw_co_yield,
2483 tok::kw_delete, tok::kw_return, tok::kw_throw)) {
2484 return true;
2487 // We put sizeof here instead of only in determineStarAmpUsage. In the cases
2488 // where the unary `+` operator is overloaded, it is reasonable to write
2489 // things like `sizeof +x`. Like commit 446d6ec996c6c3.
2490 if (PrevToken->is(tok::kw_sizeof))
2491 return true;
2493 // A sequence of leading unary operators.
2494 if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator))
2495 return true;
2497 // There can't be two consecutive binary operators.
2498 if (PrevToken->is(TT_BinaryOperator))
2499 return true;
2501 return false;
2504 /// Return the type of the given token assuming it is * or &.
2505 TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression,
2506 bool InTemplateArgument) {
2507 if (Style.isJavaScript())
2508 return TT_BinaryOperator;
2510 // && in C# must be a binary operator.
2511 if (Style.isCSharp() && Tok.is(tok::ampamp))
2512 return TT_BinaryOperator;
2514 if (Style.isVerilog()) {
2515 // In Verilog, `*` can only be a binary operator. `&` can be either unary
2516 // or binary. `*` also includes `*>` in module path declarations in
2517 // specify blocks because merged tokens take the type of the first one by
2518 // default.
2519 if (Tok.is(tok::star))
2520 return TT_BinaryOperator;
2521 return determineUnaryOperatorByUsage(Tok) ? TT_UnaryOperator
2522 : TT_BinaryOperator;
2525 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2526 if (!PrevToken)
2527 return TT_UnaryOperator;
2528 if (PrevToken->is(TT_TypeName))
2529 return TT_PointerOrReference;
2531 const FormatToken *NextToken = Tok.getNextNonComment();
2533 if (InTemplateArgument && NextToken && NextToken->is(tok::kw_noexcept))
2534 return TT_BinaryOperator;
2536 if (!NextToken ||
2537 NextToken->isOneOf(tok::arrow, tok::equal, tok::comma, tok::r_paren,
2538 TT_RequiresClause) ||
2539 (NextToken->is(tok::kw_noexcept) && !IsExpression) ||
2540 NextToken->canBePointerOrReferenceQualifier() ||
2541 (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) {
2542 return TT_PointerOrReference;
2545 if (PrevToken->is(tok::coloncolon))
2546 return TT_PointerOrReference;
2548 if (PrevToken->is(tok::r_paren) && PrevToken->is(TT_TypeDeclarationParen))
2549 return TT_PointerOrReference;
2551 if (determineUnaryOperatorByUsage(Tok))
2552 return TT_UnaryOperator;
2554 if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
2555 return TT_PointerOrReference;
2556 if (NextToken->is(tok::kw_operator) && !IsExpression)
2557 return TT_PointerOrReference;
2558 if (NextToken->isOneOf(tok::comma, tok::semi))
2559 return TT_PointerOrReference;
2561 // After right braces, star tokens are likely to be pointers to struct,
2562 // union, or class.
2563 // struct {} *ptr;
2564 // This by itself is not sufficient to distinguish from multiplication
2565 // following a brace-initialized expression, as in:
2566 // int i = int{42} * 2;
2567 // In the struct case, the part of the struct declaration until the `{` and
2568 // the `}` are put on separate unwrapped lines; in the brace-initialized
2569 // case, the matching `{` is on the same unwrapped line, so check for the
2570 // presence of the matching brace to distinguish between those.
2571 if (PrevToken->is(tok::r_brace) && Tok.is(tok::star) &&
2572 !PrevToken->MatchingParen) {
2573 return TT_PointerOrReference;
2576 if (PrevToken->endsSequence(tok::r_square, tok::l_square, tok::kw_delete))
2577 return TT_UnaryOperator;
2579 if (PrevToken->Tok.isLiteral() ||
2580 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
2581 tok::kw_false, tok::r_brace)) {
2582 return TT_BinaryOperator;
2585 const FormatToken *NextNonParen = NextToken;
2586 while (NextNonParen && NextNonParen->is(tok::l_paren))
2587 NextNonParen = NextNonParen->getNextNonComment();
2588 if (NextNonParen && (NextNonParen->Tok.isLiteral() ||
2589 NextNonParen->isOneOf(tok::kw_true, tok::kw_false) ||
2590 NextNonParen->isUnaryOperator())) {
2591 return TT_BinaryOperator;
2594 // If we know we're in a template argument, there are no named declarations.
2595 // Thus, having an identifier on the right-hand side indicates a binary
2596 // operator.
2597 if (InTemplateArgument && NextToken->Tok.isAnyIdentifier())
2598 return TT_BinaryOperator;
2600 // "&&" followed by "(", "*", or "&" is quite unlikely to be two successive
2601 // unary "&".
2602 if (Tok.is(tok::ampamp) &&
2603 NextToken->isOneOf(tok::l_paren, tok::star, tok::amp)) {
2604 return TT_BinaryOperator;
2607 // This catches some cases where evaluation order is used as control flow:
2608 // aaa && aaa->f();
2609 if (NextToken->Tok.isAnyIdentifier()) {
2610 const FormatToken *NextNextToken = NextToken->getNextNonComment();
2611 if (NextNextToken && NextNextToken->is(tok::arrow))
2612 return TT_BinaryOperator;
2615 // It is very unlikely that we are going to find a pointer or reference type
2616 // definition on the RHS of an assignment.
2617 if (IsExpression && !Contexts.back().CaretFound)
2618 return TT_BinaryOperator;
2620 // Opeartors at class scope are likely pointer or reference members.
2621 if (!Scopes.empty() && Scopes.back() == ST_Class)
2622 return TT_PointerOrReference;
2624 // Tokens that indicate member access or chained operator& use.
2625 auto IsChainedOperatorAmpOrMember = [](const FormatToken *token) {
2626 return !token || token->isOneOf(tok::amp, tok::period, tok::arrow,
2627 tok::arrowstar, tok::periodstar);
2630 // It's more likely that & represents operator& than an uninitialized
2631 // reference.
2632 if (Tok.is(tok::amp) && PrevToken && PrevToken->Tok.isAnyIdentifier() &&
2633 IsChainedOperatorAmpOrMember(PrevToken->getPreviousNonComment()) &&
2634 NextToken && NextToken->Tok.isAnyIdentifier()) {
2635 if (auto NextNext = NextToken->getNextNonComment();
2636 NextNext &&
2637 (IsChainedOperatorAmpOrMember(NextNext) || NextNext->is(tok::semi))) {
2638 return TT_BinaryOperator;
2642 return TT_PointerOrReference;
2645 TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) {
2646 if (determineUnaryOperatorByUsage(Tok))
2647 return TT_UnaryOperator;
2649 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2650 if (!PrevToken)
2651 return TT_UnaryOperator;
2653 if (PrevToken->is(tok::at))
2654 return TT_UnaryOperator;
2656 // Fall back to marking the token as binary operator.
2657 return TT_BinaryOperator;
2660 /// Determine whether ++/-- are pre- or post-increments/-decrements.
2661 TokenType determineIncrementUsage(const FormatToken &Tok) {
2662 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2663 if (!PrevToken || PrevToken->is(TT_CastRParen))
2664 return TT_UnaryOperator;
2665 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
2666 return TT_TrailingUnaryOperator;
2668 return TT_UnaryOperator;
2671 SmallVector<Context, 8> Contexts;
2673 const FormatStyle &Style;
2674 AnnotatedLine &Line;
2675 FormatToken *CurrentToken;
2676 bool AutoFound;
2677 const AdditionalKeywords &Keywords;
2679 SmallVector<ScopeType> &Scopes;
2681 // Set of "<" tokens that do not open a template parameter list. If parseAngle
2682 // determines that a specific token can't be a template opener, it will make
2683 // same decision irrespective of the decisions for tokens leading up to it.
2684 // Store this information to prevent this from causing exponential runtime.
2685 llvm::SmallPtrSet<FormatToken *, 16> NonTemplateLess;
2688 static const int PrecedenceUnaryOperator = prec::PointerToMember + 1;
2689 static const int PrecedenceArrowAndPeriod = prec::PointerToMember + 2;
2691 /// Parses binary expressions by inserting fake parenthesis based on
2692 /// operator precedence.
2693 class ExpressionParser {
2694 public:
2695 ExpressionParser(const FormatStyle &Style, const AdditionalKeywords &Keywords,
2696 AnnotatedLine &Line)
2697 : Style(Style), Keywords(Keywords), Line(Line), Current(Line.First) {}
2699 /// Parse expressions with the given operator precedence.
2700 void parse(int Precedence = 0) {
2701 // Skip 'return' and ObjC selector colons as they are not part of a binary
2702 // expression.
2703 while (Current && (Current->is(tok::kw_return) ||
2704 (Current->is(tok::colon) &&
2705 Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)))) {
2706 next();
2709 if (!Current || Precedence > PrecedenceArrowAndPeriod)
2710 return;
2712 // Conditional expressions need to be parsed separately for proper nesting.
2713 if (Precedence == prec::Conditional) {
2714 parseConditionalExpr();
2715 return;
2718 // Parse unary operators, which all have a higher precedence than binary
2719 // operators.
2720 if (Precedence == PrecedenceUnaryOperator) {
2721 parseUnaryOperator();
2722 return;
2725 FormatToken *Start = Current;
2726 FormatToken *LatestOperator = nullptr;
2727 unsigned OperatorIndex = 0;
2728 // The first name of the current type in a port list.
2729 FormatToken *VerilogFirstOfType = nullptr;
2731 while (Current) {
2732 // In Verilog ports in a module header that don't have a type take the
2733 // type of the previous one. For example,
2734 // module a(output b,
2735 // c,
2736 // output d);
2737 // In this case there need to be fake parentheses around b and c.
2738 if (Style.isVerilog() && Precedence == prec::Comma) {
2739 VerilogFirstOfType =
2740 verilogGroupDecl(VerilogFirstOfType, LatestOperator);
2743 // Consume operators with higher precedence.
2744 parse(Precedence + 1);
2746 // Do not assign fake parenthesis to tokens that are part of an
2747 // unexpanded macro call. The line within the macro call contains
2748 // the parenthesis and commas, and we will not find operators within
2749 // that structure.
2750 if (Current && Current->MacroParent)
2751 break;
2753 int CurrentPrecedence = getCurrentPrecedence();
2755 if (Precedence == CurrentPrecedence && Current &&
2756 Current->is(TT_SelectorName)) {
2757 if (LatestOperator)
2758 addFakeParenthesis(Start, prec::Level(Precedence));
2759 Start = Current;
2762 if ((Style.isCSharp() || Style.isJavaScript() ||
2763 Style.Language == FormatStyle::LK_Java) &&
2764 Precedence == prec::Additive && Current) {
2765 // A string can be broken without parentheses around it when it is
2766 // already in a sequence of strings joined by `+` signs.
2767 FormatToken *Prev = Current->getPreviousNonComment();
2768 if (Prev && Prev->is(tok::string_literal) &&
2769 (Prev == Start || Prev->endsSequence(tok::string_literal, tok::plus,
2770 TT_StringInConcatenation))) {
2771 Prev->setType(TT_StringInConcatenation);
2775 // At the end of the line or when an operator with lower precedence is
2776 // found, insert fake parenthesis and return.
2777 if (!Current ||
2778 (Current->closesScope() &&
2779 (Current->MatchingParen || Current->is(TT_TemplateString))) ||
2780 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
2781 (CurrentPrecedence == prec::Conditional &&
2782 Precedence == prec::Assignment && Current->is(tok::colon))) {
2783 break;
2786 // Consume scopes: (), [], <> and {}
2787 // In addition to that we handle require clauses as scope, so that the
2788 // constraints in that are correctly indented.
2789 if (Current->opensScope() ||
2790 Current->isOneOf(TT_RequiresClause,
2791 TT_RequiresClauseInARequiresExpression)) {
2792 // In fragment of a JavaScript template string can look like '}..${' and
2793 // thus close a scope and open a new one at the same time.
2794 while (Current && (!Current->closesScope() || Current->opensScope())) {
2795 next();
2796 parse();
2798 next();
2799 } else {
2800 // Operator found.
2801 if (CurrentPrecedence == Precedence) {
2802 if (LatestOperator)
2803 LatestOperator->NextOperator = Current;
2804 LatestOperator = Current;
2805 Current->OperatorIndex = OperatorIndex;
2806 ++OperatorIndex;
2808 next(/*SkipPastLeadingComments=*/Precedence > 0);
2812 // Group variables of the same type.
2813 if (Style.isVerilog() && Precedence == prec::Comma && VerilogFirstOfType)
2814 addFakeParenthesis(VerilogFirstOfType, prec::Comma);
2816 if (LatestOperator && (Current || Precedence > 0)) {
2817 // The requires clauses do not neccessarily end in a semicolon or a brace,
2818 // but just go over to struct/class or a function declaration, we need to
2819 // intervene so that the fake right paren is inserted correctly.
2820 auto End =
2821 (Start->Previous &&
2822 Start->Previous->isOneOf(TT_RequiresClause,
2823 TT_RequiresClauseInARequiresExpression))
2824 ? [this]() {
2825 auto Ret = Current ? Current : Line.Last;
2826 while (!Ret->ClosesRequiresClause && Ret->Previous)
2827 Ret = Ret->Previous;
2828 return Ret;
2830 : nullptr;
2832 if (Precedence == PrecedenceArrowAndPeriod) {
2833 // Call expressions don't have a binary operator precedence.
2834 addFakeParenthesis(Start, prec::Unknown, End);
2835 } else {
2836 addFakeParenthesis(Start, prec::Level(Precedence), End);
2841 private:
2842 /// Gets the precedence (+1) of the given token for binary operators
2843 /// and other tokens that we treat like binary operators.
2844 int getCurrentPrecedence() {
2845 if (Current) {
2846 const FormatToken *NextNonComment = Current->getNextNonComment();
2847 if (Current->is(TT_ConditionalExpr))
2848 return prec::Conditional;
2849 if (NextNonComment && Current->is(TT_SelectorName) &&
2850 (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
2851 ((Style.Language == FormatStyle::LK_Proto ||
2852 Style.Language == FormatStyle::LK_TextProto) &&
2853 NextNonComment->is(tok::less)))) {
2854 return prec::Assignment;
2856 if (Current->is(TT_JsComputedPropertyName))
2857 return prec::Assignment;
2858 if (Current->is(TT_TrailingReturnArrow))
2859 return prec::Comma;
2860 if (Current->is(TT_FatArrow))
2861 return prec::Assignment;
2862 if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
2863 (Current->is(tok::comment) && NextNonComment &&
2864 NextNonComment->is(TT_SelectorName))) {
2865 return 0;
2867 if (Current->is(TT_RangeBasedForLoopColon))
2868 return prec::Comma;
2869 if ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) &&
2870 Current->is(Keywords.kw_instanceof)) {
2871 return prec::Relational;
2873 if (Style.isJavaScript() &&
2874 Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) {
2875 return prec::Relational;
2877 if (Current->is(TT_BinaryOperator) || Current->is(tok::comma))
2878 return Current->getPrecedence();
2879 if (Current->isOneOf(tok::period, tok::arrow) &&
2880 Current->isNot(TT_TrailingReturnArrow)) {
2881 return PrecedenceArrowAndPeriod;
2883 if ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) &&
2884 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
2885 Keywords.kw_throws)) {
2886 return 0;
2888 // In Verilog case labels are not on separate lines straight out of
2889 // UnwrappedLineParser. The colon is not part of an expression.
2890 if (Style.isVerilog() && Current->is(tok::colon))
2891 return 0;
2893 return -1;
2896 void addFakeParenthesis(FormatToken *Start, prec::Level Precedence,
2897 FormatToken *End = nullptr) {
2898 Start->FakeLParens.push_back(Precedence);
2899 if (Precedence > prec::Unknown)
2900 Start->StartsBinaryExpression = true;
2901 if (!End && Current)
2902 End = Current->getPreviousNonComment();
2903 if (End) {
2904 ++End->FakeRParens;
2905 if (Precedence > prec::Unknown)
2906 End->EndsBinaryExpression = true;
2910 /// Parse unary operator expressions and surround them with fake
2911 /// parentheses if appropriate.
2912 void parseUnaryOperator() {
2913 llvm::SmallVector<FormatToken *, 2> Tokens;
2914 while (Current && Current->is(TT_UnaryOperator)) {
2915 Tokens.push_back(Current);
2916 next();
2918 parse(PrecedenceArrowAndPeriod);
2919 for (FormatToken *Token : llvm::reverse(Tokens)) {
2920 // The actual precedence doesn't matter.
2921 addFakeParenthesis(Token, prec::Unknown);
2925 void parseConditionalExpr() {
2926 while (Current && Current->isTrailingComment())
2927 next();
2928 FormatToken *Start = Current;
2929 parse(prec::LogicalOr);
2930 if (!Current || Current->isNot(tok::question))
2931 return;
2932 next();
2933 parse(prec::Assignment);
2934 if (!Current || Current->isNot(TT_ConditionalExpr))
2935 return;
2936 next();
2937 parse(prec::Assignment);
2938 addFakeParenthesis(Start, prec::Conditional);
2941 void next(bool SkipPastLeadingComments = true) {
2942 if (Current)
2943 Current = Current->Next;
2944 while (Current &&
2945 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
2946 Current->isTrailingComment()) {
2947 Current = Current->Next;
2951 // Add fake parenthesis around declarations of the same type for example in a
2952 // module prototype. Return the first port / variable of the current type.
2953 FormatToken *verilogGroupDecl(FormatToken *FirstOfType,
2954 FormatToken *PreviousComma) {
2955 if (!Current)
2956 return nullptr;
2958 FormatToken *Start = Current;
2960 // Skip attributes.
2961 while (Start->startsSequence(tok::l_paren, tok::star)) {
2962 if (!(Start = Start->MatchingParen) ||
2963 !(Start = Start->getNextNonComment())) {
2964 return nullptr;
2968 FormatToken *Tok = Start;
2970 if (Tok->is(Keywords.kw_assign))
2971 Tok = Tok->getNextNonComment();
2973 // Skip any type qualifiers to find the first identifier. It may be either a
2974 // new type name or a variable name. There can be several type qualifiers
2975 // preceding a variable name, and we can not tell them apart by looking at
2976 // the word alone since a macro can be defined as either a type qualifier or
2977 // a variable name. Thus we use the last word before the dimensions instead
2978 // of the first word as the candidate for the variable or type name.
2979 FormatToken *First = nullptr;
2980 while (Tok) {
2981 FormatToken *Next = Tok->getNextNonComment();
2983 if (Tok->is(tok::hash)) {
2984 // Start of a macro expansion.
2985 First = Tok;
2986 Tok = Next;
2987 if (Tok)
2988 Tok = Tok->getNextNonComment();
2989 } else if (Tok->is(tok::hashhash)) {
2990 // Concatenation. Skip.
2991 Tok = Next;
2992 if (Tok)
2993 Tok = Tok->getNextNonComment();
2994 } else if (Keywords.isVerilogQualifier(*Tok) ||
2995 Keywords.isVerilogIdentifier(*Tok)) {
2996 First = Tok;
2997 Tok = Next;
2998 // The name may have dots like `interface_foo.modport_foo`.
2999 while (Tok && Tok->isOneOf(tok::period, tok::coloncolon) &&
3000 (Tok = Tok->getNextNonComment())) {
3001 if (Keywords.isVerilogIdentifier(*Tok))
3002 Tok = Tok->getNextNonComment();
3004 } else if (!Next) {
3005 Tok = nullptr;
3006 } else if (Tok->is(tok::l_paren)) {
3007 // Make sure the parenthesized list is a drive strength. Otherwise the
3008 // statement may be a module instantiation in which case we have already
3009 // found the instance name.
3010 if (Next->isOneOf(
3011 Keywords.kw_highz0, Keywords.kw_highz1, Keywords.kw_large,
3012 Keywords.kw_medium, Keywords.kw_pull0, Keywords.kw_pull1,
3013 Keywords.kw_small, Keywords.kw_strong0, Keywords.kw_strong1,
3014 Keywords.kw_supply0, Keywords.kw_supply1, Keywords.kw_weak0,
3015 Keywords.kw_weak1)) {
3016 Tok->setType(TT_VerilogStrength);
3017 Tok = Tok->MatchingParen;
3018 if (Tok) {
3019 Tok->setType(TT_VerilogStrength);
3020 Tok = Tok->getNextNonComment();
3022 } else {
3023 break;
3025 } else if (Tok->is(tok::hash)) {
3026 if (Next->is(tok::l_paren))
3027 Next = Next->MatchingParen;
3028 if (Next)
3029 Tok = Next->getNextNonComment();
3030 } else {
3031 break;
3035 // Find the second identifier. If it exists it will be the name.
3036 FormatToken *Second = nullptr;
3037 // Dimensions.
3038 while (Tok && Tok->is(tok::l_square) && (Tok = Tok->MatchingParen))
3039 Tok = Tok->getNextNonComment();
3040 if (Tok && (Tok->is(tok::hash) || Keywords.isVerilogIdentifier(*Tok)))
3041 Second = Tok;
3043 // If the second identifier doesn't exist and there are qualifiers, the type
3044 // is implied.
3045 FormatToken *TypedName = nullptr;
3046 if (Second) {
3047 TypedName = Second;
3048 if (First && First->is(TT_Unknown))
3049 First->setType(TT_VerilogDimensionedTypeName);
3050 } else if (First != Start) {
3051 // If 'First' is null, then this isn't a declaration, 'TypedName' gets set
3052 // to null as intended.
3053 TypedName = First;
3056 if (TypedName) {
3057 // This is a declaration with a new type.
3058 if (TypedName->is(TT_Unknown))
3059 TypedName->setType(TT_StartOfName);
3060 // Group variables of the previous type.
3061 if (FirstOfType && PreviousComma) {
3062 PreviousComma->setType(TT_VerilogTypeComma);
3063 addFakeParenthesis(FirstOfType, prec::Comma, PreviousComma->Previous);
3066 FirstOfType = TypedName;
3068 // Don't let higher precedence handle the qualifiers. For example if we
3069 // have:
3070 // parameter x = 0
3071 // We skip `parameter` here. This way the fake parentheses for the
3072 // assignment will be around `x = 0`.
3073 while (Current && Current != FirstOfType) {
3074 if (Current->opensScope()) {
3075 next();
3076 parse();
3078 next();
3082 return FirstOfType;
3085 const FormatStyle &Style;
3086 const AdditionalKeywords &Keywords;
3087 const AnnotatedLine &Line;
3088 FormatToken *Current;
3091 } // end anonymous namespace
3093 void TokenAnnotator::setCommentLineLevels(
3094 SmallVectorImpl<AnnotatedLine *> &Lines) const {
3095 const AnnotatedLine *NextNonCommentLine = nullptr;
3096 for (AnnotatedLine *Line : llvm::reverse(Lines)) {
3097 assert(Line->First);
3099 // If the comment is currently aligned with the line immediately following
3100 // it, that's probably intentional and we should keep it.
3101 if (NextNonCommentLine && !NextNonCommentLine->First->Finalized &&
3102 Line->isComment() && NextNonCommentLine->First->NewlinesBefore <= 1 &&
3103 NextNonCommentLine->First->OriginalColumn ==
3104 Line->First->OriginalColumn) {
3105 const bool PPDirectiveOrImportStmt =
3106 NextNonCommentLine->Type == LT_PreprocessorDirective ||
3107 NextNonCommentLine->Type == LT_ImportStatement;
3108 if (PPDirectiveOrImportStmt)
3109 Line->Type = LT_CommentAbovePPDirective;
3110 // Align comments for preprocessor lines with the # in column 0 if
3111 // preprocessor lines are not indented. Otherwise, align with the next
3112 // line.
3113 Line->Level = Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash &&
3114 PPDirectiveOrImportStmt
3116 : NextNonCommentLine->Level;
3117 } else {
3118 NextNonCommentLine = Line->First->isNot(tok::r_brace) ? Line : nullptr;
3121 setCommentLineLevels(Line->Children);
3125 static unsigned maxNestingDepth(const AnnotatedLine &Line) {
3126 unsigned Result = 0;
3127 for (const auto *Tok = Line.First; Tok; Tok = Tok->Next)
3128 Result = std::max(Result, Tok->NestingLevel);
3129 return Result;
3132 // Returns the name of a function with no return type, e.g. a constructor or
3133 // destructor.
3134 static FormatToken *getFunctionName(const AnnotatedLine &Line) {
3135 for (FormatToken *Tok = Line.getFirstNonComment(), *Name = nullptr; Tok;
3136 Tok = Tok->getNextNonComment()) {
3137 // Skip C++11 attributes both before and after the function name.
3138 if (Tok->is(tok::l_square) && Tok->is(TT_AttributeSquare)) {
3139 Tok = Tok->MatchingParen;
3140 if (!Tok)
3141 break;
3142 continue;
3145 // Make sure the name is followed by a pair of parentheses.
3146 if (Name) {
3147 return Tok->is(tok::l_paren) && Tok->isNot(TT_FunctionTypeLParen) &&
3148 Tok->MatchingParen
3149 ? Name
3150 : nullptr;
3153 // Skip keywords that may precede the constructor/destructor name.
3154 if (Tok->isOneOf(tok::kw_friend, tok::kw_inline, tok::kw_virtual,
3155 tok::kw_constexpr, tok::kw_consteval, tok::kw_explicit)) {
3156 continue;
3159 // A qualified name may start from the global namespace.
3160 if (Tok->is(tok::coloncolon)) {
3161 Tok = Tok->Next;
3162 if (!Tok)
3163 break;
3166 // Skip to the unqualified part of the name.
3167 while (Tok->startsSequence(tok::identifier, tok::coloncolon)) {
3168 assert(Tok->Next);
3169 Tok = Tok->Next->Next;
3170 if (!Tok)
3171 return nullptr;
3174 // Skip the `~` if a destructor name.
3175 if (Tok->is(tok::tilde)) {
3176 Tok = Tok->Next;
3177 if (!Tok)
3178 break;
3181 // Make sure the name is not already annotated, e.g. as NamespaceMacro.
3182 if (Tok->isNot(tok::identifier) || Tok->isNot(TT_Unknown))
3183 break;
3185 Name = Tok;
3188 return nullptr;
3191 // Checks if Tok is a constructor/destructor name qualified by its class name.
3192 static bool isCtorOrDtorName(const FormatToken *Tok) {
3193 assert(Tok && Tok->is(tok::identifier));
3194 const auto *Prev = Tok->Previous;
3196 if (Prev && Prev->is(tok::tilde))
3197 Prev = Prev->Previous;
3199 if (!Prev || !Prev->endsSequence(tok::coloncolon, tok::identifier))
3200 return false;
3202 assert(Prev->Previous);
3203 return Prev->Previous->TokenText == Tok->TokenText;
3206 void TokenAnnotator::annotate(AnnotatedLine &Line) {
3207 AnnotatingParser Parser(Style, Line, Keywords, Scopes);
3208 Line.Type = Parser.parseLine();
3210 for (auto &Child : Line.Children)
3211 annotate(*Child);
3213 // With very deep nesting, ExpressionParser uses lots of stack and the
3214 // formatting algorithm is very slow. We're not going to do a good job here
3215 // anyway - it's probably generated code being formatted by mistake.
3216 // Just skip the whole line.
3217 if (maxNestingDepth(Line) > 50)
3218 Line.Type = LT_Invalid;
3220 if (Line.Type == LT_Invalid)
3221 return;
3223 ExpressionParser ExprParser(Style, Keywords, Line);
3224 ExprParser.parse();
3226 if (Style.isCpp()) {
3227 auto *Tok = getFunctionName(Line);
3228 if (Tok && ((!Scopes.empty() && Scopes.back() == ST_Class) ||
3229 Line.endsWith(TT_FunctionLBrace) || isCtorOrDtorName(Tok))) {
3230 Tok->setFinalizedType(TT_CtorDtorDeclName);
3234 if (Line.startsWith(TT_ObjCMethodSpecifier))
3235 Line.Type = LT_ObjCMethodDecl;
3236 else if (Line.startsWith(TT_ObjCDecl))
3237 Line.Type = LT_ObjCDecl;
3238 else if (Line.startsWith(TT_ObjCProperty))
3239 Line.Type = LT_ObjCProperty;
3241 auto *First = Line.First;
3242 First->SpacesRequiredBefore = 1;
3243 First->CanBreakBefore = First->MustBreakBefore;
3245 if (First->is(tok::eof) && First->NewlinesBefore == 0 &&
3246 Style.InsertNewlineAtEOF) {
3247 First->NewlinesBefore = 1;
3251 // This function heuristically determines whether 'Current' starts the name of a
3252 // function declaration.
3253 static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
3254 const AnnotatedLine &Line,
3255 FormatToken *&ClosingParen) {
3256 assert(Current.Previous);
3258 if (Current.is(TT_FunctionDeclarationName))
3259 return true;
3261 if (!Current.Tok.getIdentifierInfo())
3262 return false;
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;
3276 continue;
3278 if (Next->startsSequence(tok::l_square, tok::r_square)) {
3279 // For operator[]().
3280 Next = Next->Next;
3281 continue;
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;
3289 if (Next->is(TT_TemplateOpener) && Next->MatchingParen) {
3290 Next = Next->MatchingParen;
3291 continue;
3294 break;
3296 return nullptr;
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;
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;
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;
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;
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 ClosingParen = Next->MatchingParen;
3348 assert(ClosingParen->is(tok::r_paren));
3349 // If the lines ends with "{", this is likely a function definition.
3350 if (Line.Last->is(tok::l_brace))
3351 return true;
3352 if (Next->Next == ClosingParen)
3353 return true; // Empty parentheses.
3354 // If there is an &/&& after the r_paren, this is likely a function.
3355 if (ClosingParen->Next && ClosingParen->Next->is(TT_PointerOrReference))
3356 return true;
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;
3373 for (const FormatToken *Tok = Next->Next; Tok && Tok != ClosingParen;
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;
3381 if (Tok->is(tok::kw_const) || Tok->isSimpleTypeSpecifier() ||
3382 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) {
3383 return true;
3385 if (Tok->isOneOf(tok::l_brace, tok::string_literal, TT_ObjCMethodExpr) ||
3386 Tok->Tok.isLiteral()) {
3387 return false;
3390 return false;
3393 bool TokenAnnotator::mustBreakForReturnType(const AnnotatedLine &Line) const {
3394 assert(Line.MightBeFunctionDecl);
3396 if ((Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_TopLevel ||
3397 Style.AlwaysBreakAfterReturnType ==
3398 FormatStyle::RTBS_TopLevelDefinitions) &&
3399 Line.Level > 0) {
3400 return false;
3403 switch (Style.AlwaysBreakAfterReturnType) {
3404 case FormatStyle::RTBS_None:
3405 return false;
3406 case FormatStyle::RTBS_All:
3407 case FormatStyle::RTBS_TopLevel:
3408 return true;
3409 case FormatStyle::RTBS_AllDefinitions:
3410 case FormatStyle::RTBS_TopLevelDefinitions:
3411 return Line.mightBeFunctionDefinition();
3414 return false;
3417 static bool mustBreakAfterAttributes(const FormatToken &Tok,
3418 const FormatStyle &Style) {
3419 switch (Style.BreakAfterAttributes) {
3420 case FormatStyle::ABS_Always:
3421 return true;
3422 case FormatStyle::ABS_Leave:
3423 return Tok.NewlinesBefore > 0;
3424 default:
3425 return false;
3429 void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
3430 for (AnnotatedLine *ChildLine : Line.Children)
3431 calculateFormattingInformation(*ChildLine);
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);
3444 bool LineIsFunctionDeclaration = false;
3445 FormatToken *ClosingParen = nullptr;
3446 for (FormatToken *Tok = Current, *AfterLastAttribute = nullptr; Tok;
3447 Tok = Tok->Next) {
3448 if (Tok->Previous->EndsCppAttributeGroup)
3449 AfterLastAttribute = Tok;
3450 if (const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
3451 IsCtorOrDtor ||
3452 isFunctionDeclarationName(Style.isCpp(), *Tok, Line, ClosingParen)) {
3453 if (!IsCtorOrDtor) {
3454 LineIsFunctionDeclaration = true;
3455 Tok->setFinalizedType(TT_FunctionDeclarationName);
3457 if (AfterLastAttribute &&
3458 mustBreakAfterAttributes(*AfterLastAttribute, Style)) {
3459 AfterLastAttribute->MustBreakBefore = true;
3460 Line.ReturnTypeWrapped = true;
3462 break;
3466 if (Style.isCpp()) {
3467 if (!LineIsFunctionDeclaration) {
3468 // Annotate */&/&& in `operator` function calls as binary operators.
3469 for (const auto *Tok = Line.First; Tok; Tok = Tok->Next) {
3470 if (Tok->isNot(tok::kw_operator))
3471 continue;
3472 do {
3473 Tok = Tok->Next;
3474 } while (Tok && Tok->isNot(TT_OverloadedOperatorLParen));
3475 if (!Tok)
3476 break;
3477 const auto *LeftParen = Tok;
3478 for (Tok = Tok->Next; Tok && Tok != LeftParen->MatchingParen;
3479 Tok = Tok->Next) {
3480 if (Tok->isNot(tok::identifier))
3481 continue;
3482 auto *Next = Tok->Next;
3483 const bool NextIsBinaryOperator =
3484 Next && Next->isOneOf(tok::star, tok::amp, tok::ampamp) &&
3485 Next->Next && Next->Next->is(tok::identifier);
3486 if (!NextIsBinaryOperator)
3487 continue;
3488 Next->setType(TT_BinaryOperator);
3489 Tok = Next;
3492 } else if (ClosingParen) {
3493 for (auto *Tok = ClosingParen->Next; Tok; Tok = Tok->Next) {
3494 if (Tok->is(tok::arrow)) {
3495 Tok->setType(TT_TrailingReturnArrow);
3496 break;
3498 if (Tok->isNot(TT_TrailingAnnotation))
3499 continue;
3500 const auto *Next = Tok->Next;
3501 if (!Next || Next->isNot(tok::l_paren))
3502 continue;
3503 Tok = Next->MatchingParen;
3504 if (!Tok)
3505 break;
3510 while (Current) {
3511 const FormatToken *Prev = Current->Previous;
3512 if (Current->is(TT_LineComment)) {
3513 if (Prev->is(BK_BracedInit) && Prev->opensScope()) {
3514 Current->SpacesRequiredBefore =
3515 (Style.Cpp11BracedListStyle && !Style.SpacesInParensOptions.Other)
3517 : 1;
3518 } else if (Prev->is(TT_VerilogMultiLineListLParen)) {
3519 Current->SpacesRequiredBefore = 0;
3520 } else {
3521 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
3524 // If we find a trailing comment, iterate backwards to determine whether
3525 // it seems to relate to a specific parameter. If so, break before that
3526 // parameter to avoid changing the comment's meaning. E.g. don't move 'b'
3527 // to the previous line in:
3528 // SomeFunction(a,
3529 // b, // comment
3530 // c);
3531 if (!Current->HasUnescapedNewline) {
3532 for (FormatToken *Parameter = Current->Previous; Parameter;
3533 Parameter = Parameter->Previous) {
3534 if (Parameter->isOneOf(tok::comment, tok::r_brace))
3535 break;
3536 if (Parameter->Previous && Parameter->Previous->is(tok::comma)) {
3537 if (Parameter->Previous->isNot(TT_CtorInitializerComma) &&
3538 Parameter->HasUnescapedNewline) {
3539 Parameter->MustBreakBefore = true;
3541 break;
3545 } else if (!Current->Finalized && Current->SpacesRequiredBefore == 0 &&
3546 spaceRequiredBefore(Line, *Current)) {
3547 Current->SpacesRequiredBefore = 1;
3550 const auto &Children = Prev->Children;
3551 if (!Children.empty() && Children.back()->Last->is(TT_LineComment)) {
3552 Current->MustBreakBefore = true;
3553 } else {
3554 Current->MustBreakBefore =
3555 Current->MustBreakBefore || mustBreakBefore(Line, *Current);
3556 if (!Current->MustBreakBefore && InFunctionDecl &&
3557 Current->is(TT_FunctionDeclarationName)) {
3558 Current->MustBreakBefore = mustBreakForReturnType(Line);
3562 Current->CanBreakBefore =
3563 Current->MustBreakBefore || canBreakBefore(Line, *Current);
3564 unsigned ChildSize = 0;
3565 if (Prev->Children.size() == 1) {
3566 FormatToken &LastOfChild = *Prev->Children[0]->Last;
3567 ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit
3568 : LastOfChild.TotalLength + 1;
3570 if (Current->MustBreakBefore || Prev->Children.size() > 1 ||
3571 (Prev->Children.size() == 1 &&
3572 Prev->Children[0]->First->MustBreakBefore) ||
3573 Current->IsMultiline) {
3574 Current->TotalLength = Prev->TotalLength + Style.ColumnLimit;
3575 } else {
3576 Current->TotalLength = Prev->TotalLength + Current->ColumnWidth +
3577 ChildSize + Current->SpacesRequiredBefore;
3580 if (Current->is(TT_CtorInitializerColon))
3581 InFunctionDecl = false;
3583 // FIXME: Only calculate this if CanBreakBefore is true once static
3584 // initializers etc. are sorted out.
3585 // FIXME: Move magic numbers to a better place.
3587 // Reduce penalty for aligning ObjC method arguments using the colon
3588 // alignment as this is the canonical way (still prefer fitting everything
3589 // into one line if possible). Trying to fit a whole expression into one
3590 // line should not force other line breaks (e.g. when ObjC method
3591 // expression is a part of other expression).
3592 Current->SplitPenalty = splitPenalty(Line, *Current, InFunctionDecl);
3593 if (Style.Language == FormatStyle::LK_ObjC &&
3594 Current->is(TT_SelectorName) && Current->ParameterIndex > 0) {
3595 if (Current->ParameterIndex == 1)
3596 Current->SplitPenalty += 5 * Current->BindingStrength;
3597 } else {
3598 Current->SplitPenalty += 20 * Current->BindingStrength;
3601 Current = Current->Next;
3604 calculateUnbreakableTailLengths(Line);
3605 unsigned IndentLevel = Line.Level;
3606 for (Current = Line.First; Current; Current = Current->Next) {
3607 if (Current->Role)
3608 Current->Role->precomputeFormattingInfos(Current);
3609 if (Current->MatchingParen &&
3610 Current->MatchingParen->opensBlockOrBlockTypeList(Style) &&
3611 IndentLevel > 0) {
3612 --IndentLevel;
3614 Current->IndentLevel = IndentLevel;
3615 if (Current->opensBlockOrBlockTypeList(Style))
3616 ++IndentLevel;
3619 LLVM_DEBUG({ printDebugInfo(Line); });
3622 void TokenAnnotator::calculateUnbreakableTailLengths(
3623 AnnotatedLine &Line) const {
3624 unsigned UnbreakableTailLength = 0;
3625 FormatToken *Current = Line.Last;
3626 while (Current) {
3627 Current->UnbreakableTailLength = UnbreakableTailLength;
3628 if (Current->CanBreakBefore ||
3629 Current->isOneOf(tok::comment, tok::string_literal)) {
3630 UnbreakableTailLength = 0;
3631 } else {
3632 UnbreakableTailLength +=
3633 Current->ColumnWidth + Current->SpacesRequiredBefore;
3635 Current = Current->Previous;
3639 void TokenAnnotator::calculateArrayInitializerColumnList(
3640 AnnotatedLine &Line) const {
3641 if (Line.First == Line.Last)
3642 return;
3643 auto *CurrentToken = Line.First;
3644 CurrentToken->ArrayInitializerLineStart = true;
3645 unsigned Depth = 0;
3646 while (CurrentToken && CurrentToken != Line.Last) {
3647 if (CurrentToken->is(tok::l_brace)) {
3648 CurrentToken->IsArrayInitializer = true;
3649 if (CurrentToken->Next)
3650 CurrentToken->Next->MustBreakBefore = true;
3651 CurrentToken =
3652 calculateInitializerColumnList(Line, CurrentToken->Next, Depth + 1);
3653 } else {
3654 CurrentToken = CurrentToken->Next;
3659 FormatToken *TokenAnnotator::calculateInitializerColumnList(
3660 AnnotatedLine &Line, FormatToken *CurrentToken, unsigned Depth) const {
3661 while (CurrentToken && CurrentToken != Line.Last) {
3662 if (CurrentToken->is(tok::l_brace))
3663 ++Depth;
3664 else if (CurrentToken->is(tok::r_brace))
3665 --Depth;
3666 if (Depth == 2 && CurrentToken->isOneOf(tok::l_brace, tok::comma)) {
3667 CurrentToken = CurrentToken->Next;
3668 if (!CurrentToken)
3669 break;
3670 CurrentToken->StartsColumn = true;
3671 CurrentToken = CurrentToken->Previous;
3673 CurrentToken = CurrentToken->Next;
3675 return CurrentToken;
3678 unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
3679 const FormatToken &Tok,
3680 bool InFunctionDecl) const {
3681 const FormatToken &Left = *Tok.Previous;
3682 const FormatToken &Right = Tok;
3684 if (Left.is(tok::semi))
3685 return 0;
3687 // Language specific handling.
3688 if (Style.Language == FormatStyle::LK_Java) {
3689 if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
3690 return 1;
3691 if (Right.is(Keywords.kw_implements))
3692 return 2;
3693 if (Left.is(tok::comma) && Left.NestingLevel == 0)
3694 return 3;
3695 } else if (Style.isJavaScript()) {
3696 if (Right.is(Keywords.kw_function) && Left.isNot(tok::comma))
3697 return 100;
3698 if (Left.is(TT_JsTypeColon))
3699 return 35;
3700 if ((Left.is(TT_TemplateString) && Left.TokenText.endswith("${")) ||
3701 (Right.is(TT_TemplateString) && Right.TokenText.startswith("}"))) {
3702 return 100;
3704 // Prefer breaking call chains (".foo") over empty "{}", "[]" or "()".
3705 if (Left.opensScope() && Right.closesScope())
3706 return 200;
3707 } else if (Style.isProto()) {
3708 if (Right.is(tok::l_square))
3709 return 1;
3710 if (Right.is(tok::period))
3711 return 500;
3714 if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
3715 return 1;
3716 if (Right.is(tok::l_square)) {
3717 if (Left.is(tok::r_square))
3718 return 200;
3719 // Slightly prefer formatting local lambda definitions like functions.
3720 if (Right.is(TT_LambdaLSquare) && Left.is(tok::equal))
3721 return 35;
3722 if (!Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
3723 TT_ArrayInitializerLSquare,
3724 TT_DesignatedInitializerLSquare, TT_AttributeSquare)) {
3725 return 500;
3729 if (Left.is(tok::coloncolon))
3730 return 500;
3731 if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
3732 Right.is(tok::kw_operator)) {
3733 if (Line.startsWith(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
3734 return 3;
3735 if (Left.is(TT_StartOfName))
3736 return 110;
3737 if (InFunctionDecl && Right.NestingLevel == 0)
3738 return Style.PenaltyReturnTypeOnItsOwnLine;
3739 return 200;
3741 if (Right.is(TT_PointerOrReference))
3742 return 190;
3743 if (Right.is(TT_TrailingReturnArrow))
3744 return 110;
3745 if (Left.is(tok::equal) && Right.is(tok::l_brace))
3746 return 160;
3747 if (Left.is(TT_CastRParen))
3748 return 100;
3749 if (Left.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union))
3750 return 5000;
3751 if (Left.is(tok::comment))
3752 return 1000;
3754 if (Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
3755 TT_CtorInitializerColon)) {
3756 return 2;
3759 if (Right.isMemberAccess()) {
3760 // Breaking before the "./->" of a chained call/member access is reasonably
3761 // cheap, as formatting those with one call per line is generally
3762 // desirable. In particular, it should be cheaper to break before the call
3763 // than it is to break inside a call's parameters, which could lead to weird
3764 // "hanging" indents. The exception is the very last "./->" to support this
3765 // frequent pattern:
3767 // aaaaaaaa.aaaaaaaa.bbbbbbb().ccccccccccccccccccccc(
3768 // dddddddd);
3770 // which might otherwise be blown up onto many lines. Here, clang-format
3771 // won't produce "hanging" indents anyway as there is no other trailing
3772 // call.
3774 // Also apply higher penalty is not a call as that might lead to a wrapping
3775 // like:
3777 // aaaaaaa
3778 // .aaaaaaaaa.bbbbbbbb(cccccccc);
3779 return !Right.NextOperator || !Right.NextOperator->Previous->closesScope()
3780 ? 150
3781 : 35;
3784 if (Right.is(TT_TrailingAnnotation) &&
3785 (!Right.Next || Right.Next->isNot(tok::l_paren))) {
3786 // Moving trailing annotations to the next line is fine for ObjC method
3787 // declarations.
3788 if (Line.startsWith(TT_ObjCMethodSpecifier))
3789 return 10;
3790 // Generally, breaking before a trailing annotation is bad unless it is
3791 // function-like. It seems to be especially preferable to keep standard
3792 // annotations (i.e. "const", "final" and "override") on the same line.
3793 // Use a slightly higher penalty after ")" so that annotations like
3794 // "const override" are kept together.
3795 bool is_short_annotation = Right.TokenText.size() < 10;
3796 return (Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
3799 // In for-loops, prefer breaking at ',' and ';'.
3800 if (Line.startsWith(tok::kw_for) && Left.is(tok::equal))
3801 return 4;
3803 // In Objective-C method expressions, prefer breaking before "param:" over
3804 // breaking after it.
3805 if (Right.is(TT_SelectorName))
3806 return 0;
3807 if (Left.is(tok::colon) && Left.is(TT_ObjCMethodExpr))
3808 return Line.MightBeFunctionDecl ? 50 : 500;
3810 // In Objective-C type declarations, avoid breaking after the category's
3811 // open paren (we'll prefer breaking after the protocol list's opening
3812 // angle bracket, if present).
3813 if (Line.Type == LT_ObjCDecl && Left.is(tok::l_paren) && Left.Previous &&
3814 Left.Previous->isOneOf(tok::identifier, tok::greater)) {
3815 return 500;
3818 if (Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
3819 return Style.PenaltyBreakOpenParenthesis;
3820 if (Left.is(tok::l_paren) && InFunctionDecl &&
3821 Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign) {
3822 return 100;
3824 if (Left.is(tok::l_paren) && Left.Previous &&
3825 (Left.Previous->isOneOf(tok::kw_for, tok::kw__Generic) ||
3826 Left.Previous->isIf())) {
3827 return 1000;
3829 if (Left.is(tok::equal) && InFunctionDecl)
3830 return 110;
3831 if (Right.is(tok::r_brace))
3832 return 1;
3833 if (Left.is(TT_TemplateOpener))
3834 return 100;
3835 if (Left.opensScope()) {
3836 // If we aren't aligning after opening parens/braces we can always break
3837 // here unless the style does not want us to place all arguments on the
3838 // next line.
3839 if (Style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign &&
3840 (Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
3841 return 0;
3843 if (Left.is(tok::l_brace) && !Style.Cpp11BracedListStyle)
3844 return 19;
3845 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
3846 : 19;
3848 if (Left.is(TT_JavaAnnotation))
3849 return 50;
3851 if (Left.is(TT_UnaryOperator))
3852 return 60;
3853 if (Left.isOneOf(tok::plus, tok::comma) && Left.Previous &&
3854 Left.Previous->isLabelString() &&
3855 (Left.NextOperator || Left.OperatorIndex != 0)) {
3856 return 50;
3858 if (Right.is(tok::plus) && Left.isLabelString() &&
3859 (Right.NextOperator || Right.OperatorIndex != 0)) {
3860 return 25;
3862 if (Left.is(tok::comma))
3863 return 1;
3864 if (Right.is(tok::lessless) && Left.isLabelString() &&
3865 (Right.NextOperator || Right.OperatorIndex != 1)) {
3866 return 25;
3868 if (Right.is(tok::lessless)) {
3869 // Breaking at a << is really cheap.
3870 if (Left.isNot(tok::r_paren) || Right.OperatorIndex > 0) {
3871 // Slightly prefer to break before the first one in log-like statements.
3872 return 2;
3874 return 1;
3876 if (Left.ClosesTemplateDeclaration)
3877 return Style.PenaltyBreakTemplateDeclaration;
3878 if (Left.ClosesRequiresClause)
3879 return 0;
3880 if (Left.is(TT_ConditionalExpr))
3881 return prec::Conditional;
3882 prec::Level Level = Left.getPrecedence();
3883 if (Level == prec::Unknown)
3884 Level = Right.getPrecedence();
3885 if (Level == prec::Assignment)
3886 return Style.PenaltyBreakAssignment;
3887 if (Level != prec::Unknown)
3888 return Level;
3890 return 3;
3893 bool TokenAnnotator::spaceRequiredBeforeParens(const FormatToken &Right) const {
3894 if (Style.SpaceBeforeParens == FormatStyle::SBPO_Always)
3895 return true;
3896 if (Right.is(TT_OverloadedOperatorLParen) &&
3897 Style.SpaceBeforeParensOptions.AfterOverloadedOperator) {
3898 return true;
3900 if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
3901 Right.ParameterCount > 0) {
3902 return true;
3904 return false;
3907 bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
3908 const FormatToken &Left,
3909 const FormatToken &Right) const {
3910 if (Left.is(tok::kw_return) &&
3911 !Right.isOneOf(tok::semi, tok::r_paren, tok::hashhash)) {
3912 return true;
3914 if (Left.is(tok::kw_throw) && Right.is(tok::l_paren) && Right.MatchingParen &&
3915 Right.MatchingParen->is(TT_CastRParen)) {
3916 return true;
3918 if (Left.is(Keywords.kw_assert) && Style.Language == FormatStyle::LK_Java)
3919 return true;
3920 if (Style.ObjCSpaceAfterProperty && Line.Type == LT_ObjCProperty &&
3921 Left.Tok.getObjCKeywordID() == tok::objc_property) {
3922 return true;
3924 if (Right.is(tok::hashhash))
3925 return Left.is(tok::hash);
3926 if (Left.isOneOf(tok::hashhash, tok::hash))
3927 return Right.is(tok::hash);
3928 if ((Left.is(tok::l_paren) && Right.is(tok::r_paren)) ||
3929 (Left.is(tok::l_brace) && Left.isNot(BK_Block) &&
3930 Right.is(tok::r_brace) && Right.isNot(BK_Block))) {
3931 return Style.SpacesInParensOptions.InEmptyParentheses;
3933 if (Style.SpacesInParensOptions.InConditionalStatements) {
3934 const FormatToken *LeftParen = nullptr;
3935 if (Left.is(tok::l_paren))
3936 LeftParen = &Left;
3937 else if (Right.is(tok::r_paren) && Right.MatchingParen)
3938 LeftParen = Right.MatchingParen;
3939 if (LeftParen) {
3940 if (LeftParen->is(TT_ConditionLParen))
3941 return true;
3942 if (LeftParen->Previous && isKeywordWithCondition(*LeftParen->Previous))
3943 return true;
3947 // trailing return type 'auto': []() -> auto {}, auto foo() -> auto {}
3948 if (Left.is(tok::kw_auto) && Right.isOneOf(TT_LambdaLBrace, TT_FunctionLBrace,
3949 // function return type 'auto'
3950 TT_FunctionTypeLParen)) {
3951 return true;
3954 // auto{x} auto(x)
3955 if (Left.is(tok::kw_auto) && Right.isOneOf(tok::l_paren, tok::l_brace))
3956 return false;
3958 // operator co_await(x)
3959 if (Right.is(tok::l_paren) && Left.is(tok::kw_co_await) && Left.Previous &&
3960 Left.Previous->is(tok::kw_operator)) {
3961 return false;
3963 // co_await (x), co_yield (x), co_return (x)
3964 if (Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
3965 !Right.isOneOf(tok::semi, tok::r_paren)) {
3966 return true;
3969 if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) {
3970 return (Right.is(TT_CastRParen) ||
3971 (Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
3972 ? Style.SpacesInParensOptions.InCStyleCasts
3973 : Style.SpacesInParensOptions.Other;
3975 if (Right.isOneOf(tok::semi, tok::comma))
3976 return false;
3977 if (Right.is(tok::less) && Line.Type == LT_ObjCDecl) {
3978 bool IsLightweightGeneric = Right.MatchingParen &&
3979 Right.MatchingParen->Next &&
3980 Right.MatchingParen->Next->is(tok::colon);
3981 return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList;
3983 if (Right.is(tok::less) && Left.is(tok::kw_template))
3984 return Style.SpaceAfterTemplateKeyword;
3985 if (Left.isOneOf(tok::exclaim, tok::tilde))
3986 return false;
3987 if (Left.is(tok::at) &&
3988 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
3989 tok::numeric_constant, tok::l_paren, tok::l_brace,
3990 tok::kw_true, tok::kw_false)) {
3991 return false;
3993 if (Left.is(tok::colon))
3994 return Left.isNot(TT_ObjCMethodExpr);
3995 if (Left.is(tok::coloncolon))
3996 return false;
3997 if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less)) {
3998 if (Style.Language == FormatStyle::LK_TextProto ||
3999 (Style.Language == FormatStyle::LK_Proto &&
4000 (Left.is(TT_DictLiteral) || Right.is(TT_DictLiteral)))) {
4001 // Format empty list as `<>`.
4002 if (Left.is(tok::less) && Right.is(tok::greater))
4003 return false;
4004 return !Style.Cpp11BracedListStyle;
4006 // Don't attempt to format operator<(), as it is handled later.
4007 if (Right.isNot(TT_OverloadedOperatorLParen))
4008 return false;
4010 if (Right.is(tok::ellipsis)) {
4011 return Left.Tok.isLiteral() || (Left.is(tok::identifier) && Left.Previous &&
4012 Left.Previous->is(tok::kw_case));
4014 if (Left.is(tok::l_square) && Right.is(tok::amp))
4015 return Style.SpacesInSquareBrackets;
4016 if (Right.is(TT_PointerOrReference)) {
4017 if (Left.is(tok::r_paren) && Line.MightBeFunctionDecl) {
4018 if (!Left.MatchingParen)
4019 return true;
4020 FormatToken *TokenBeforeMatchingParen =
4021 Left.MatchingParen->getPreviousNonComment();
4022 if (!TokenBeforeMatchingParen || Left.isNot(TT_TypeDeclarationParen))
4023 return true;
4025 // Add a space if the previous token is a pointer qualifier or the closing
4026 // parenthesis of __attribute__(()) expression and the style requires spaces
4027 // after pointer qualifiers.
4028 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_After ||
4029 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
4030 (Left.is(TT_AttributeRParen) ||
4031 Left.canBePointerOrReferenceQualifier())) {
4032 return true;
4034 if (Left.Tok.isLiteral())
4035 return true;
4036 // for (auto a = 0, b = 0; const auto & c : {1, 2, 3})
4037 if (Left.isTypeOrIdentifier() && Right.Next && Right.Next->Next &&
4038 Right.Next->Next->is(TT_RangeBasedForLoopColon)) {
4039 return getTokenPointerOrReferenceAlignment(Right) !=
4040 FormatStyle::PAS_Left;
4042 return !Left.isOneOf(TT_PointerOrReference, tok::l_paren) &&
4043 (getTokenPointerOrReferenceAlignment(Right) !=
4044 FormatStyle::PAS_Left ||
4045 (Line.IsMultiVariableDeclStmt &&
4046 (Left.NestingLevel == 0 ||
4047 (Left.NestingLevel == 1 && startsWithInitStatement(Line)))));
4049 if (Right.is(TT_FunctionTypeLParen) && Left.isNot(tok::l_paren) &&
4050 (Left.isNot(TT_PointerOrReference) ||
4051 (getTokenPointerOrReferenceAlignment(Left) != FormatStyle::PAS_Right &&
4052 !Line.IsMultiVariableDeclStmt))) {
4053 return true;
4055 if (Left.is(TT_PointerOrReference)) {
4056 // Add a space if the next token is a pointer qualifier and the style
4057 // requires spaces before pointer qualifiers.
4058 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Before ||
4059 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
4060 Right.canBePointerOrReferenceQualifier()) {
4061 return true;
4063 // & 1
4064 if (Right.Tok.isLiteral())
4065 return true;
4066 // & /* comment
4067 if (Right.is(TT_BlockComment))
4068 return true;
4069 // foo() -> const Bar * override/final
4070 // S::foo() & noexcept/requires
4071 if (Right.isOneOf(Keywords.kw_override, Keywords.kw_final, tok::kw_noexcept,
4072 TT_RequiresClause) &&
4073 Right.isNot(TT_StartOfName)) {
4074 return true;
4076 // & {
4077 if (Right.is(tok::l_brace) && Right.is(BK_Block))
4078 return true;
4079 // for (auto a = 0, b = 0; const auto& c : {1, 2, 3})
4080 if (Left.Previous && Left.Previous->isTypeOrIdentifier() && Right.Next &&
4081 Right.Next->is(TT_RangeBasedForLoopColon)) {
4082 return getTokenPointerOrReferenceAlignment(Left) !=
4083 FormatStyle::PAS_Right;
4085 if (Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
4086 tok::l_paren)) {
4087 return false;
4089 if (getTokenPointerOrReferenceAlignment(Left) == FormatStyle::PAS_Right)
4090 return false;
4091 // FIXME: Setting IsMultiVariableDeclStmt for the whole line is error-prone,
4092 // because it does not take into account nested scopes like lambdas.
4093 // In multi-variable declaration statements, attach */& to the variable
4094 // independently of the style. However, avoid doing it if we are in a nested
4095 // scope, e.g. lambda. We still need to special-case statements with
4096 // initializers.
4097 if (Line.IsMultiVariableDeclStmt &&
4098 (Left.NestingLevel == Line.First->NestingLevel ||
4099 ((Left.NestingLevel == Line.First->NestingLevel + 1) &&
4100 startsWithInitStatement(Line)))) {
4101 return false;
4103 return Left.Previous && !Left.Previous->isOneOf(
4104 tok::l_paren, tok::coloncolon, tok::l_square);
4106 // Ensure right pointer alignment with ellipsis e.g. int *...P
4107 if (Left.is(tok::ellipsis) && Left.Previous &&
4108 Left.Previous->isOneOf(tok::star, tok::amp, tok::ampamp)) {
4109 return Style.PointerAlignment != FormatStyle::PAS_Right;
4112 if (Right.is(tok::star) && Left.is(tok::l_paren))
4113 return false;
4114 if (Left.is(tok::star) && Right.isOneOf(tok::star, tok::amp, tok::ampamp))
4115 return false;
4116 if (Right.isOneOf(tok::star, tok::amp, tok::ampamp)) {
4117 const FormatToken *Previous = &Left;
4118 while (Previous && Previous->isNot(tok::kw_operator)) {
4119 if (Previous->is(tok::identifier) || Previous->isSimpleTypeSpecifier()) {
4120 Previous = Previous->getPreviousNonComment();
4121 continue;
4123 if (Previous->is(TT_TemplateCloser) && Previous->MatchingParen) {
4124 Previous = Previous->MatchingParen->getPreviousNonComment();
4125 continue;
4127 if (Previous->is(tok::coloncolon)) {
4128 Previous = Previous->getPreviousNonComment();
4129 continue;
4131 break;
4133 // Space between the type and the * in:
4134 // operator void*()
4135 // operator char*()
4136 // operator void const*()
4137 // operator void volatile*()
4138 // operator /*comment*/ const char*()
4139 // operator volatile /*comment*/ char*()
4140 // operator Foo*()
4141 // operator C<T>*()
4142 // operator std::Foo*()
4143 // operator C<T>::D<U>*()
4144 // dependent on PointerAlignment style.
4145 if (Previous) {
4146 if (Previous->endsSequence(tok::kw_operator))
4147 return Style.PointerAlignment != FormatStyle::PAS_Left;
4148 if (Previous->is(tok::kw_const) || Previous->is(tok::kw_volatile)) {
4149 return (Style.PointerAlignment != FormatStyle::PAS_Left) ||
4150 (Style.SpaceAroundPointerQualifiers ==
4151 FormatStyle::SAPQ_After) ||
4152 (Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both);
4156 if (Style.isCSharp() && Left.is(Keywords.kw_is) && Right.is(tok::l_square))
4157 return true;
4158 const auto SpaceRequiredForArrayInitializerLSquare =
4159 [](const FormatToken &LSquareTok, const FormatStyle &Style) {
4160 return Style.SpacesInContainerLiterals ||
4161 ((Style.Language == FormatStyle::LK_Proto ||
4162 Style.Language == FormatStyle::LK_TextProto) &&
4163 !Style.Cpp11BracedListStyle &&
4164 LSquareTok.endsSequence(tok::l_square, tok::colon,
4165 TT_SelectorName));
4167 if (Left.is(tok::l_square)) {
4168 return (Left.is(TT_ArrayInitializerLSquare) && Right.isNot(tok::r_square) &&
4169 SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
4170 (Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare,
4171 TT_LambdaLSquare) &&
4172 Style.SpacesInSquareBrackets && Right.isNot(tok::r_square));
4174 if (Right.is(tok::r_square)) {
4175 return Right.MatchingParen &&
4176 ((Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
4177 SpaceRequiredForArrayInitializerLSquare(*Right.MatchingParen,
4178 Style)) ||
4179 (Style.SpacesInSquareBrackets &&
4180 Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
4181 TT_StructuredBindingLSquare,
4182 TT_LambdaLSquare)));
4184 if (Right.is(tok::l_square) &&
4185 !Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4186 TT_DesignatedInitializerLSquare,
4187 TT_StructuredBindingLSquare, TT_AttributeSquare) &&
4188 !Left.isOneOf(tok::numeric_constant, TT_DictLiteral) &&
4189 !(Left.isNot(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
4190 Right.is(TT_ArraySubscriptLSquare))) {
4191 return false;
4193 if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
4194 return !Left.Children.empty(); // No spaces in "{}".
4195 if ((Left.is(tok::l_brace) && Left.isNot(BK_Block)) ||
4196 (Right.is(tok::r_brace) && Right.MatchingParen &&
4197 Right.MatchingParen->isNot(BK_Block))) {
4198 return !Style.Cpp11BracedListStyle || Style.SpacesInParensOptions.Other;
4200 if (Left.is(TT_BlockComment)) {
4201 // No whitespace in x(/*foo=*/1), except for JavaScript.
4202 return Style.isJavaScript() || !Left.TokenText.endswith("=*/");
4205 // Space between template and attribute.
4206 // e.g. template <typename T> [[nodiscard]] ...
4207 if (Left.is(TT_TemplateCloser) && Right.is(TT_AttributeSquare))
4208 return true;
4209 // Space before parentheses common for all languages
4210 if (Right.is(tok::l_paren)) {
4211 if (Left.is(TT_TemplateCloser) && Right.isNot(TT_FunctionTypeLParen))
4212 return spaceRequiredBeforeParens(Right);
4213 if (Left.isOneOf(TT_RequiresClause,
4214 TT_RequiresClauseInARequiresExpression)) {
4215 return Style.SpaceBeforeParensOptions.AfterRequiresInClause ||
4216 spaceRequiredBeforeParens(Right);
4218 if (Left.is(TT_RequiresExpression)) {
4219 return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
4220 spaceRequiredBeforeParens(Right);
4222 if (Left.is(TT_AttributeRParen) ||
4223 (Left.is(tok::r_square) && Left.is(TT_AttributeSquare))) {
4224 return true;
4226 if (Left.is(TT_ForEachMacro)) {
4227 return Style.SpaceBeforeParensOptions.AfterForeachMacros ||
4228 spaceRequiredBeforeParens(Right);
4230 if (Left.is(TT_IfMacro)) {
4231 return Style.SpaceBeforeParensOptions.AfterIfMacros ||
4232 spaceRequiredBeforeParens(Right);
4234 if (Style.SpaceBeforeParens == FormatStyle::SBPO_Custom &&
4235 Left.isOneOf(tok::kw_new, tok::kw_delete) &&
4236 Right.isNot(TT_OverloadedOperatorLParen) &&
4237 !(Line.MightBeFunctionDecl && Left.is(TT_FunctionDeclarationName))) {
4238 if (Style.SpaceBeforeParensOptions.AfterPlacementOperator ==
4239 FormatStyle::SpaceBeforeParensCustom::APO_Always ||
4240 (Style.SpaceBeforeParensOptions.AfterPlacementOperator ==
4241 FormatStyle::SpaceBeforeParensCustom::APO_Leave &&
4242 Right.hasWhitespaceBefore())) {
4243 return true;
4245 return false;
4247 if (Line.Type == LT_ObjCDecl)
4248 return true;
4249 if (Left.is(tok::semi))
4250 return true;
4251 if (Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while, tok::kw_switch,
4252 tok::kw_case, TT_ForEachMacro, TT_ObjCForIn) ||
4253 Left.isIf(Line.Type != LT_PreprocessorDirective) ||
4254 Right.is(TT_ConditionLParen)) {
4255 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4256 spaceRequiredBeforeParens(Right);
4259 // TODO add Operator overloading specific Options to
4260 // SpaceBeforeParensOptions
4261 if (Right.is(TT_OverloadedOperatorLParen))
4262 return spaceRequiredBeforeParens(Right);
4263 // Function declaration or definition
4264 if (Line.MightBeFunctionDecl && (Left.is(TT_FunctionDeclarationName))) {
4265 if (Line.mightBeFunctionDefinition()) {
4266 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
4267 spaceRequiredBeforeParens(Right);
4268 } else {
4269 return Style.SpaceBeforeParensOptions.AfterFunctionDeclarationName ||
4270 spaceRequiredBeforeParens(Right);
4273 // Lambda
4274 if (Line.Type != LT_PreprocessorDirective && Left.is(tok::r_square) &&
4275 Left.MatchingParen && Left.MatchingParen->is(TT_LambdaLSquare)) {
4276 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
4277 spaceRequiredBeforeParens(Right);
4279 if (!Left.Previous || Left.Previous->isNot(tok::period)) {
4280 if (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) {
4281 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4282 spaceRequiredBeforeParens(Right);
4284 if (Left.isOneOf(tok::kw_new, tok::kw_delete)) {
4285 return ((!Line.MightBeFunctionDecl || !Left.Previous) &&
4286 Style.SpaceBeforeParens != FormatStyle::SBPO_Never) ||
4287 spaceRequiredBeforeParens(Right);
4290 if (Left.is(tok::r_square) && Left.MatchingParen &&
4291 Left.MatchingParen->Previous &&
4292 Left.MatchingParen->Previous->is(tok::kw_delete)) {
4293 return (Style.SpaceBeforeParens != FormatStyle::SBPO_Never) ||
4294 spaceRequiredBeforeParens(Right);
4297 // Handle builtins like identifiers.
4298 if (Line.Type != LT_PreprocessorDirective &&
4299 (Left.Tok.getIdentifierInfo() || Left.is(tok::r_paren))) {
4300 return spaceRequiredBeforeParens(Right);
4302 return false;
4304 if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
4305 return false;
4306 if (Right.is(TT_UnaryOperator)) {
4307 return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
4308 (Left.isNot(tok::colon) || Left.isNot(TT_ObjCMethodExpr));
4310 // No space between the variable name and the initializer list.
4311 // A a1{1};
4312 // Verilog doesn't have such syntax, but it has word operators that are C++
4313 // identifiers like `a inside {b, c}`. So the rule is not applicable.
4314 if (!Style.isVerilog() &&
4315 (Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
4316 tok::r_paren) ||
4317 Left.isSimpleTypeSpecifier()) &&
4318 Right.is(tok::l_brace) && Right.getNextNonComment() &&
4319 Right.isNot(BK_Block)) {
4320 return false;
4322 if (Left.is(tok::period) || Right.is(tok::period))
4323 return false;
4324 // u#str, U#str, L#str, u8#str
4325 // uR#str, UR#str, LR#str, u8R#str
4326 if (Right.is(tok::hash) && Left.is(tok::identifier) &&
4327 (Left.TokenText == "L" || Left.TokenText == "u" ||
4328 Left.TokenText == "U" || Left.TokenText == "u8" ||
4329 Left.TokenText == "LR" || Left.TokenText == "uR" ||
4330 Left.TokenText == "UR" || Left.TokenText == "u8R")) {
4331 return false;
4333 if (Left.is(TT_TemplateCloser) && Left.MatchingParen &&
4334 Left.MatchingParen->Previous &&
4335 (Left.MatchingParen->Previous->is(tok::period) ||
4336 Left.MatchingParen->Previous->is(tok::coloncolon))) {
4337 // Java call to generic function with explicit type:
4338 // A.<B<C<...>>>DoSomething();
4339 // A::<B<C<...>>>DoSomething(); // With a Java 8 method reference.
4340 return false;
4342 if (Left.is(TT_TemplateCloser) && Right.is(tok::l_square))
4343 return false;
4344 if (Left.is(tok::l_brace) && Left.endsSequence(TT_DictLiteral, tok::at)) {
4345 // Objective-C dictionary literal -> no space after opening brace.
4346 return false;
4348 if (Right.is(tok::r_brace) && Right.MatchingParen &&
4349 Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at)) {
4350 // Objective-C dictionary literal -> no space before closing brace.
4351 return false;
4353 if (Right.getType() == TT_TrailingAnnotation &&
4354 Right.isOneOf(tok::amp, tok::ampamp) &&
4355 Left.isOneOf(tok::kw_const, tok::kw_volatile) &&
4356 (!Right.Next || Right.Next->is(tok::semi))) {
4357 // Match const and volatile ref-qualifiers without any additional
4358 // qualifiers such as
4359 // void Fn() const &;
4360 return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
4363 return true;
4366 bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
4367 const FormatToken &Right) const {
4368 const FormatToken &Left = *Right.Previous;
4370 // If the token is finalized don't touch it (as it could be in a
4371 // clang-format-off section).
4372 if (Left.Finalized)
4373 return Right.hasWhitespaceBefore();
4375 // Never ever merge two words.
4376 if (Keywords.isWordLike(Right) && Keywords.isWordLike(Left))
4377 return true;
4379 // Leave a space between * and /* to avoid C4138 `comment end` found outside
4380 // of comment.
4381 if (Left.is(tok::star) && Right.is(tok::comment))
4382 return true;
4384 if (Style.isCpp()) {
4385 if (Left.is(TT_OverloadedOperator) &&
4386 Right.isOneOf(TT_TemplateOpener, TT_TemplateCloser)) {
4387 return true;
4389 // Space between UDL and dot: auto b = 4s .count();
4390 if (Right.is(tok::period) && Left.is(tok::numeric_constant))
4391 return true;
4392 // Space between import <iostream>.
4393 // or import .....;
4394 if (Left.is(Keywords.kw_import) && Right.isOneOf(tok::less, tok::ellipsis))
4395 return true;
4396 // Space between `module :` and `import :`.
4397 if (Left.isOneOf(Keywords.kw_module, Keywords.kw_import) &&
4398 Right.is(TT_ModulePartitionColon)) {
4399 return true;
4401 // No space between import foo:bar but keep a space between import :bar;
4402 if (Left.is(tok::identifier) && Right.is(TT_ModulePartitionColon))
4403 return false;
4404 // No space between :bar;
4405 if (Left.is(TT_ModulePartitionColon) &&
4406 Right.isOneOf(tok::identifier, tok::kw_private)) {
4407 return false;
4409 if (Left.is(tok::ellipsis) && Right.is(tok::identifier) &&
4410 Line.First->is(Keywords.kw_import)) {
4411 return false;
4413 // Space in __attribute__((attr)) ::type.
4414 if (Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
4415 Right.is(tok::coloncolon)) {
4416 return true;
4419 if (Left.is(tok::kw_operator))
4420 return Right.is(tok::coloncolon);
4421 if (Right.is(tok::l_brace) && Right.is(BK_BracedInit) &&
4422 !Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
4423 return true;
4425 if (Left.is(tok::less) && Left.is(TT_OverloadedOperator) &&
4426 Right.is(TT_TemplateOpener)) {
4427 return true;
4429 } else if (Style.Language == FormatStyle::LK_Proto ||
4430 Style.Language == FormatStyle::LK_TextProto) {
4431 if (Right.is(tok::period) &&
4432 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
4433 Keywords.kw_repeated, Keywords.kw_extend)) {
4434 return true;
4436 if (Right.is(tok::l_paren) &&
4437 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)) {
4438 return true;
4440 if (Right.isOneOf(tok::l_brace, tok::less) && Left.is(TT_SelectorName))
4441 return true;
4442 // Slashes occur in text protocol extension syntax: [type/type] { ... }.
4443 if (Left.is(tok::slash) || Right.is(tok::slash))
4444 return false;
4445 if (Left.MatchingParen &&
4446 Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
4447 Right.isOneOf(tok::l_brace, tok::less)) {
4448 return !Style.Cpp11BracedListStyle;
4450 // A percent is probably part of a formatting specification, such as %lld.
4451 if (Left.is(tok::percent))
4452 return false;
4453 // Preserve the existence of a space before a percent for cases like 0x%04x
4454 // and "%d %d"
4455 if (Left.is(tok::numeric_constant) && Right.is(tok::percent))
4456 return Right.hasWhitespaceBefore();
4457 } else if (Style.isJson()) {
4458 if (Right.is(tok::colon) && Left.is(tok::string_literal))
4459 return Style.SpaceBeforeJsonColon;
4460 } else if (Style.isCSharp()) {
4461 // Require spaces around '{' and before '}' unless they appear in
4462 // interpolated strings. Interpolated strings are merged into a single token
4463 // so cannot have spaces inserted by this function.
4465 // No space between 'this' and '['
4466 if (Left.is(tok::kw_this) && Right.is(tok::l_square))
4467 return false;
4469 // No space between 'new' and '('
4470 if (Left.is(tok::kw_new) && Right.is(tok::l_paren))
4471 return false;
4473 // Space before { (including space within '{ {').
4474 if (Right.is(tok::l_brace))
4475 return true;
4477 // Spaces inside braces.
4478 if (Left.is(tok::l_brace) && Right.isNot(tok::r_brace))
4479 return true;
4481 if (Left.isNot(tok::l_brace) && Right.is(tok::r_brace))
4482 return true;
4484 // Spaces around '=>'.
4485 if (Left.is(TT_FatArrow) || Right.is(TT_FatArrow))
4486 return true;
4488 // No spaces around attribute target colons
4489 if (Left.is(TT_AttributeColon) || Right.is(TT_AttributeColon))
4490 return false;
4492 // space between type and variable e.g. Dictionary<string,string> foo;
4493 if (Left.is(TT_TemplateCloser) && Right.is(TT_StartOfName))
4494 return true;
4496 // spaces inside square brackets.
4497 if (Left.is(tok::l_square) || Right.is(tok::r_square))
4498 return Style.SpacesInSquareBrackets;
4500 // No space before ? in nullable types.
4501 if (Right.is(TT_CSharpNullable))
4502 return false;
4504 // No space before null forgiving '!'.
4505 if (Right.is(TT_NonNullAssertion))
4506 return false;
4508 // No space between consecutive commas '[,,]'.
4509 if (Left.is(tok::comma) && Right.is(tok::comma))
4510 return false;
4512 // space after var in `var (key, value)`
4513 if (Left.is(Keywords.kw_var) && Right.is(tok::l_paren))
4514 return true;
4516 // space between keywords and paren e.g. "using ("
4517 if (Right.is(tok::l_paren)) {
4518 if (Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when,
4519 Keywords.kw_lock)) {
4520 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4521 spaceRequiredBeforeParens(Right);
4525 // space between method modifier and opening parenthesis of a tuple return
4526 // type
4527 if (Left.isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected,
4528 tok::kw_virtual, tok::kw_extern, tok::kw_static,
4529 Keywords.kw_internal, Keywords.kw_abstract,
4530 Keywords.kw_sealed, Keywords.kw_override,
4531 Keywords.kw_async, Keywords.kw_unsafe) &&
4532 Right.is(tok::l_paren)) {
4533 return true;
4535 } else if (Style.isJavaScript()) {
4536 if (Left.is(TT_FatArrow))
4537 return true;
4538 // for await ( ...
4539 if (Right.is(tok::l_paren) && Left.is(Keywords.kw_await) && Left.Previous &&
4540 Left.Previous->is(tok::kw_for)) {
4541 return true;
4543 if (Left.is(Keywords.kw_async) && Right.is(tok::l_paren) &&
4544 Right.MatchingParen) {
4545 const FormatToken *Next = Right.MatchingParen->getNextNonComment();
4546 // An async arrow function, for example: `x = async () => foo();`,
4547 // as opposed to calling a function called async: `x = async();`
4548 if (Next && Next->is(TT_FatArrow))
4549 return true;
4551 if ((Left.is(TT_TemplateString) && Left.TokenText.endswith("${")) ||
4552 (Right.is(TT_TemplateString) && Right.TokenText.startswith("}"))) {
4553 return false;
4555 // In tagged template literals ("html`bar baz`"), there is no space between
4556 // the tag identifier and the template string.
4557 if (Keywords.IsJavaScriptIdentifier(Left,
4558 /* AcceptIdentifierName= */ false) &&
4559 Right.is(TT_TemplateString)) {
4560 return false;
4562 if (Right.is(tok::star) &&
4563 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)) {
4564 return false;
4566 if (Right.isOneOf(tok::l_brace, tok::l_square) &&
4567 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
4568 Keywords.kw_extends, Keywords.kw_implements)) {
4569 return true;
4571 if (Right.is(tok::l_paren)) {
4572 // JS methods can use some keywords as names (e.g. `delete()`).
4573 if (Line.MustBeDeclaration && Left.Tok.getIdentifierInfo())
4574 return false;
4575 // Valid JS method names can include keywords, e.g. `foo.delete()` or
4576 // `bar.instanceof()`. Recognize call positions by preceding period.
4577 if (Left.Previous && Left.Previous->is(tok::period) &&
4578 Left.Tok.getIdentifierInfo()) {
4579 return false;
4581 // Additional unary JavaScript operators that need a space after.
4582 if (Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
4583 tok::kw_void)) {
4584 return true;
4587 // `foo as const;` casts into a const type.
4588 if (Left.endsSequence(tok::kw_const, Keywords.kw_as))
4589 return false;
4590 if ((Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
4591 tok::kw_const) ||
4592 // "of" is only a keyword if it appears after another identifier
4593 // (e.g. as "const x of y" in a for loop), or after a destructuring
4594 // operation (const [x, y] of z, const {a, b} of c).
4595 (Left.is(Keywords.kw_of) && Left.Previous &&
4596 (Left.Previous->is(tok::identifier) ||
4597 Left.Previous->isOneOf(tok::r_square, tok::r_brace)))) &&
4598 (!Left.Previous || Left.Previous->isNot(tok::period))) {
4599 return true;
4601 if (Left.isOneOf(tok::kw_for, Keywords.kw_as) && Left.Previous &&
4602 Left.Previous->is(tok::period) && Right.is(tok::l_paren)) {
4603 return false;
4605 if (Left.is(Keywords.kw_as) &&
4606 Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)) {
4607 return true;
4609 if (Left.is(tok::kw_default) && Left.Previous &&
4610 Left.Previous->is(tok::kw_export)) {
4611 return true;
4613 if (Left.is(Keywords.kw_is) && Right.is(tok::l_brace))
4614 return true;
4615 if (Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
4616 return false;
4617 if (Left.is(TT_JsTypeOperator) || Right.is(TT_JsTypeOperator))
4618 return false;
4619 if ((Left.is(tok::l_brace) || Right.is(tok::r_brace)) &&
4620 Line.First->isOneOf(Keywords.kw_import, tok::kw_export)) {
4621 return false;
4623 if (Left.is(tok::ellipsis))
4624 return false;
4625 if (Left.is(TT_TemplateCloser) &&
4626 !Right.isOneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
4627 Keywords.kw_implements, Keywords.kw_extends)) {
4628 // Type assertions ('<type>expr') are not followed by whitespace. Other
4629 // locations that should have whitespace following are identified by the
4630 // above set of follower tokens.
4631 return false;
4633 if (Right.is(TT_NonNullAssertion))
4634 return false;
4635 if (Left.is(TT_NonNullAssertion) &&
4636 Right.isOneOf(Keywords.kw_as, Keywords.kw_in)) {
4637 return true; // "x! as string", "x! in y"
4639 } else if (Style.Language == FormatStyle::LK_Java) {
4640 if (Left.is(tok::r_square) && Right.is(tok::l_brace))
4641 return true;
4642 if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren)) {
4643 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4644 spaceRequiredBeforeParens(Right);
4646 if ((Left.isOneOf(tok::kw_static, tok::kw_public, tok::kw_private,
4647 tok::kw_protected) ||
4648 Left.isOneOf(Keywords.kw_final, Keywords.kw_abstract,
4649 Keywords.kw_native)) &&
4650 Right.is(TT_TemplateOpener)) {
4651 return true;
4653 } else if (Style.isVerilog()) {
4654 // An escaped identifier ends with whitespace.
4655 if (Style.isVerilog() && Left.is(tok::identifier) &&
4656 Left.TokenText[0] == '\\') {
4657 return true;
4659 // Add space between things in a primitive's state table unless in a
4660 // transition like `(0?)`.
4661 if ((Left.is(TT_VerilogTableItem) &&
4662 !Right.isOneOf(tok::r_paren, tok::semi)) ||
4663 (Right.is(TT_VerilogTableItem) && Left.isNot(tok::l_paren))) {
4664 const FormatToken *Next = Right.getNextNonComment();
4665 return !(Next && Next->is(tok::r_paren));
4667 // Don't add space within a delay like `#0`.
4668 if (Left.isNot(TT_BinaryOperator) &&
4669 Left.isOneOf(Keywords.kw_verilogHash, Keywords.kw_verilogHashHash)) {
4670 return false;
4672 // Add space after a delay.
4673 if (Right.isNot(tok::semi) &&
4674 (Left.endsSequence(tok::numeric_constant, Keywords.kw_verilogHash) ||
4675 Left.endsSequence(tok::numeric_constant,
4676 Keywords.kw_verilogHashHash) ||
4677 (Left.is(tok::r_paren) && Left.MatchingParen &&
4678 Left.MatchingParen->endsSequence(tok::l_paren, tok::at)))) {
4679 return true;
4681 // Don't add embedded spaces in a number literal like `16'h1?ax` or an array
4682 // literal like `'{}`.
4683 if (Left.is(Keywords.kw_apostrophe) ||
4684 (Left.is(TT_VerilogNumberBase) && Right.is(tok::numeric_constant))) {
4685 return false;
4687 // Don't add spaces between two at signs. Like in a coverage event.
4688 // Don't add spaces between at and a sensitivity list like
4689 // `@(posedge clk)`.
4690 if (Left.is(tok::at) && Right.isOneOf(tok::l_paren, tok::star, tok::at))
4691 return false;
4692 // Add space between the type name and dimension like `logic [1:0]`.
4693 if (Right.is(tok::l_square) &&
4694 Left.isOneOf(TT_VerilogDimensionedTypeName, Keywords.kw_function)) {
4695 return true;
4697 // Don't add spaces between a casting type and the quote or repetition count
4698 // and the brace.
4699 if ((Right.is(Keywords.kw_apostrophe) ||
4700 (Right.is(BK_BracedInit) && Right.is(tok::l_brace))) &&
4701 !(Left.isOneOf(Keywords.kw_assign, Keywords.kw_unique) ||
4702 Keywords.isVerilogWordOperator(Left)) &&
4703 (Left.isOneOf(tok::r_square, tok::r_paren, tok::r_brace,
4704 tok::numeric_constant) ||
4705 Keywords.isWordLike(Left))) {
4706 return false;
4708 // Don't add spaces in imports like `import foo::*;`.
4709 if ((Right.is(tok::star) && Left.is(tok::coloncolon)) ||
4710 (Left.is(tok::star) && Right.is(tok::semi))) {
4711 return false;
4713 // Add space in attribute like `(* ASYNC_REG = "TRUE" *)`.
4714 if (Left.endsSequence(tok::star, tok::l_paren) && Right.is(tok::identifier))
4715 return true;
4716 // Add space before drive strength like in `wire (strong1, pull0)`.
4717 if (Right.is(tok::l_paren) && Right.is(TT_VerilogStrength))
4718 return true;
4719 // Don't add space in a streaming concatenation like `{>>{j}}`.
4720 if ((Left.is(tok::l_brace) &&
4721 Right.isOneOf(tok::lessless, tok::greatergreater)) ||
4722 (Left.endsSequence(tok::lessless, tok::l_brace) ||
4723 Left.endsSequence(tok::greatergreater, tok::l_brace))) {
4724 return false;
4727 if (Left.is(TT_ImplicitStringLiteral))
4728 return Right.hasWhitespaceBefore();
4729 if (Line.Type == LT_ObjCMethodDecl) {
4730 if (Left.is(TT_ObjCMethodSpecifier))
4731 return true;
4732 if (Left.is(tok::r_paren) && Left.isNot(TT_AttributeRParen) &&
4733 canBeObjCSelectorComponent(Right)) {
4734 // Don't space between ')' and <id> or ')' and 'new'. 'new' is not a
4735 // keyword in Objective-C, and '+ (instancetype)new;' is a standard class
4736 // method declaration.
4737 return false;
4740 if (Line.Type == LT_ObjCProperty &&
4741 (Right.is(tok::equal) || Left.is(tok::equal))) {
4742 return false;
4745 if (Right.is(TT_TrailingReturnArrow) || Left.is(TT_TrailingReturnArrow))
4746 return true;
4748 if (Left.is(tok::comma) && Right.isNot(TT_OverloadedOperatorLParen) &&
4749 // In an unexpanded macro call we only find the parentheses and commas
4750 // in a line; the commas and closing parenthesis do not require a space.
4751 (Left.Children.empty() || !Left.MacroParent)) {
4752 return true;
4754 if (Right.is(tok::comma))
4755 return false;
4756 if (Right.is(TT_ObjCBlockLParen))
4757 return true;
4758 if (Right.is(TT_CtorInitializerColon))
4759 return Style.SpaceBeforeCtorInitializerColon;
4760 if (Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
4761 return false;
4762 if (Right.is(TT_RangeBasedForLoopColon) &&
4763 !Style.SpaceBeforeRangeBasedForLoopColon) {
4764 return false;
4766 if (Left.is(TT_BitFieldColon)) {
4767 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
4768 Style.BitFieldColonSpacing == FormatStyle::BFCS_After;
4770 if (Right.is(tok::colon)) {
4771 if (Right.is(TT_CaseLabelColon))
4772 return Style.SpaceBeforeCaseColon;
4773 if (Right.is(TT_GotoLabelColon))
4774 return false;
4775 // `private:` and `public:`.
4776 if (!Right.getNextNonComment())
4777 return false;
4778 if (Right.is(TT_ObjCMethodExpr))
4779 return false;
4780 if (Left.is(tok::question))
4781 return false;
4782 if (Right.is(TT_InlineASMColon) && Left.is(tok::coloncolon))
4783 return false;
4784 if (Right.is(TT_DictLiteral))
4785 return Style.SpacesInContainerLiterals;
4786 if (Right.is(TT_AttributeColon))
4787 return false;
4788 if (Right.is(TT_CSharpNamedArgumentColon))
4789 return false;
4790 if (Right.is(TT_GenericSelectionColon))
4791 return false;
4792 if (Right.is(TT_BitFieldColon)) {
4793 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
4794 Style.BitFieldColonSpacing == FormatStyle::BFCS_Before;
4796 return true;
4798 // Do not merge "- -" into "--".
4799 if ((Left.isOneOf(tok::minus, tok::minusminus) &&
4800 Right.isOneOf(tok::minus, tok::minusminus)) ||
4801 (Left.isOneOf(tok::plus, tok::plusplus) &&
4802 Right.isOneOf(tok::plus, tok::plusplus))) {
4803 return true;
4805 if (Left.is(TT_UnaryOperator)) {
4806 if (Right.isNot(tok::l_paren)) {
4807 // The alternative operators for ~ and ! are "compl" and "not".
4808 // If they are used instead, we do not want to combine them with
4809 // the token to the right, unless that is a left paren.
4810 if (Left.is(tok::exclaim) && Left.TokenText == "not")
4811 return true;
4812 if (Left.is(tok::tilde) && Left.TokenText == "compl")
4813 return true;
4814 // Lambda captures allow for a lone &, so "&]" needs to be properly
4815 // handled.
4816 if (Left.is(tok::amp) && Right.is(tok::r_square))
4817 return Style.SpacesInSquareBrackets;
4819 return (Style.SpaceAfterLogicalNot && Left.is(tok::exclaim)) ||
4820 Right.is(TT_BinaryOperator);
4823 // If the next token is a binary operator or a selector name, we have
4824 // incorrectly classified the parenthesis as a cast. FIXME: Detect correctly.
4825 if (Left.is(TT_CastRParen)) {
4826 return Style.SpaceAfterCStyleCast ||
4827 Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
4830 auto ShouldAddSpacesInAngles = [this, &Right]() {
4831 if (this->Style.SpacesInAngles == FormatStyle::SIAS_Always)
4832 return true;
4833 if (this->Style.SpacesInAngles == FormatStyle::SIAS_Leave)
4834 return Right.hasWhitespaceBefore();
4835 return false;
4838 if (Left.is(tok::greater) && Right.is(tok::greater)) {
4839 if (Style.Language == FormatStyle::LK_TextProto ||
4840 (Style.Language == FormatStyle::LK_Proto && Left.is(TT_DictLiteral))) {
4841 return !Style.Cpp11BracedListStyle;
4843 return Right.is(TT_TemplateCloser) && Left.is(TT_TemplateCloser) &&
4844 ((Style.Standard < FormatStyle::LS_Cpp11) ||
4845 ShouldAddSpacesInAngles());
4847 if (Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
4848 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
4849 (Right.is(tok::period) && Right.isNot(TT_DesignatedInitializerPeriod))) {
4850 return false;
4852 if (!Style.SpaceBeforeAssignmentOperators && Left.isNot(TT_TemplateCloser) &&
4853 Right.getPrecedence() == prec::Assignment) {
4854 return false;
4856 if (Style.Language == FormatStyle::LK_Java && Right.is(tok::coloncolon) &&
4857 (Left.is(tok::identifier) || Left.is(tok::kw_this))) {
4858 return false;
4860 if (Right.is(tok::coloncolon) && Left.is(tok::identifier)) {
4861 // Generally don't remove existing spaces between an identifier and "::".
4862 // The identifier might actually be a macro name such as ALWAYS_INLINE. If
4863 // this turns out to be too lenient, add analysis of the identifier itself.
4864 return Right.hasWhitespaceBefore();
4866 if (Right.is(tok::coloncolon) &&
4867 !Left.isOneOf(tok::l_brace, tok::comment, tok::l_paren)) {
4868 // Put a space between < and :: in vector< ::std::string >
4869 return (Left.is(TT_TemplateOpener) &&
4870 ((Style.Standard < FormatStyle::LS_Cpp11) ||
4871 ShouldAddSpacesInAngles())) ||
4872 !(Left.isOneOf(tok::l_paren, tok::r_paren, tok::l_square,
4873 tok::kw___super, TT_TemplateOpener,
4874 TT_TemplateCloser)) ||
4875 (Left.is(tok::l_paren) && Style.SpacesInParensOptions.Other);
4877 if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser)))
4878 return ShouldAddSpacesInAngles();
4879 // Space before TT_StructuredBindingLSquare.
4880 if (Right.is(TT_StructuredBindingLSquare)) {
4881 return !Left.isOneOf(tok::amp, tok::ampamp) ||
4882 getTokenReferenceAlignment(Left) != FormatStyle::PAS_Right;
4884 // Space before & or && following a TT_StructuredBindingLSquare.
4885 if (Right.Next && Right.Next->is(TT_StructuredBindingLSquare) &&
4886 Right.isOneOf(tok::amp, tok::ampamp)) {
4887 return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
4889 if ((Right.is(TT_BinaryOperator) && Left.isNot(tok::l_paren)) ||
4890 (Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) &&
4891 Right.isNot(tok::r_paren))) {
4892 return true;
4894 if (Right.is(TT_TemplateOpener) && Left.is(tok::r_paren) &&
4895 Left.MatchingParen &&
4896 Left.MatchingParen->is(TT_OverloadedOperatorLParen)) {
4897 return false;
4899 if (Right.is(tok::less) && Left.isNot(tok::l_paren) &&
4900 Line.Type == LT_ImportStatement) {
4901 return true;
4903 if (Right.is(TT_TrailingUnaryOperator))
4904 return false;
4905 if (Left.is(TT_RegexLiteral))
4906 return false;
4907 return spaceRequiredBetween(Line, Left, Right);
4910 // Returns 'true' if 'Tok' is a brace we'd want to break before in Allman style.
4911 static bool isAllmanBrace(const FormatToken &Tok) {
4912 return Tok.is(tok::l_brace) && Tok.is(BK_Block) &&
4913 !Tok.isOneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral);
4916 // Returns 'true' if 'Tok' is a function argument.
4917 static bool IsFunctionArgument(const FormatToken &Tok) {
4918 return Tok.MatchingParen && Tok.MatchingParen->Next &&
4919 Tok.MatchingParen->Next->isOneOf(tok::comma, tok::r_paren);
4922 static bool
4923 isItAnEmptyLambdaAllowed(const FormatToken &Tok,
4924 FormatStyle::ShortLambdaStyle ShortLambdaOption) {
4925 return Tok.Children.empty() && ShortLambdaOption != FormatStyle::SLS_None;
4928 static bool isAllmanLambdaBrace(const FormatToken &Tok) {
4929 return Tok.is(tok::l_brace) && Tok.is(BK_Block) &&
4930 !Tok.isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
4933 bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
4934 const FormatToken &Right) const {
4935 const FormatToken &Left = *Right.Previous;
4936 if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0)
4937 return true;
4939 if (Style.isCSharp()) {
4940 if (Left.is(TT_FatArrow) && Right.is(tok::l_brace) &&
4941 Style.BraceWrapping.AfterFunction) {
4942 return true;
4944 if (Right.is(TT_CSharpNamedArgumentColon) ||
4945 Left.is(TT_CSharpNamedArgumentColon)) {
4946 return false;
4948 if (Right.is(TT_CSharpGenericTypeConstraint))
4949 return true;
4950 if (Right.Next && Right.Next->is(TT_FatArrow) &&
4951 (Right.is(tok::numeric_constant) ||
4952 (Right.is(tok::identifier) && Right.TokenText == "_"))) {
4953 return true;
4956 // Break after C# [...] and before public/protected/private/internal.
4957 if (Left.is(TT_AttributeSquare) && Left.is(tok::r_square) &&
4958 (Right.isAccessSpecifier(/*ColonRequired=*/false) ||
4959 Right.is(Keywords.kw_internal))) {
4960 return true;
4962 // Break between ] and [ but only when there are really 2 attributes.
4963 if (Left.is(TT_AttributeSquare) && Right.is(TT_AttributeSquare) &&
4964 Left.is(tok::r_square) && Right.is(tok::l_square)) {
4965 return true;
4968 } else if (Style.isJavaScript()) {
4969 // FIXME: This might apply to other languages and token kinds.
4970 if (Right.is(tok::string_literal) && Left.is(tok::plus) && Left.Previous &&
4971 Left.Previous->is(tok::string_literal)) {
4972 return true;
4974 if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) && Line.Level == 0 &&
4975 Left.Previous && Left.Previous->is(tok::equal) &&
4976 Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
4977 tok::kw_const) &&
4978 // kw_var/kw_let are pseudo-tokens that are tok::identifier, so match
4979 // above.
4980 !Line.First->isOneOf(Keywords.kw_var, Keywords.kw_let)) {
4981 // Object literals on the top level of a file are treated as "enum-style".
4982 // Each key/value pair is put on a separate line, instead of bin-packing.
4983 return true;
4985 if (Left.is(tok::l_brace) && Line.Level == 0 &&
4986 (Line.startsWith(tok::kw_enum) ||
4987 Line.startsWith(tok::kw_const, tok::kw_enum) ||
4988 Line.startsWith(tok::kw_export, tok::kw_enum) ||
4989 Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum))) {
4990 // JavaScript top-level enum key/value pairs are put on separate lines
4991 // instead of bin-packing.
4992 return true;
4994 if (Right.is(tok::r_brace) && Left.is(tok::l_brace) && Left.Previous &&
4995 Left.Previous->is(TT_FatArrow)) {
4996 // JS arrow function (=> {...}).
4997 switch (Style.AllowShortLambdasOnASingleLine) {
4998 case FormatStyle::SLS_All:
4999 return false;
5000 case FormatStyle::SLS_None:
5001 return true;
5002 case FormatStyle::SLS_Empty:
5003 return !Left.Children.empty();
5004 case FormatStyle::SLS_Inline:
5005 // allow one-lining inline (e.g. in function call args) and empty arrow
5006 // functions.
5007 return (Left.NestingLevel == 0 && Line.Level == 0) &&
5008 !Left.Children.empty();
5010 llvm_unreachable("Unknown FormatStyle::ShortLambdaStyle enum");
5013 if (Right.is(tok::r_brace) && Left.is(tok::l_brace) &&
5014 !Left.Children.empty()) {
5015 // Support AllowShortFunctionsOnASingleLine for JavaScript.
5016 return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None ||
5017 Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Empty ||
5018 (Left.NestingLevel == 0 && Line.Level == 0 &&
5019 Style.AllowShortFunctionsOnASingleLine &
5020 FormatStyle::SFS_InlineOnly);
5022 } else if (Style.Language == FormatStyle::LK_Java) {
5023 if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next &&
5024 Right.Next->is(tok::string_literal)) {
5025 return true;
5027 } else if (Style.isVerilog()) {
5028 // Break between assignments.
5029 if (Left.is(TT_VerilogAssignComma))
5030 return true;
5031 // Break between ports of different types.
5032 if (Left.is(TT_VerilogTypeComma))
5033 return true;
5034 // Break between ports in a module instantiation and after the parameter
5035 // list.
5036 if (Style.VerilogBreakBetweenInstancePorts &&
5037 (Left.is(TT_VerilogInstancePortComma) ||
5038 (Left.is(tok::r_paren) && Keywords.isVerilogIdentifier(Right) &&
5039 Left.MatchingParen &&
5040 Left.MatchingParen->is(TT_VerilogInstancePortLParen)))) {
5041 return true;
5043 // Break after labels. In Verilog labels don't have the 'case' keyword, so
5044 // it is hard to identify them in UnwrappedLineParser.
5045 if (!Keywords.isVerilogBegin(Right) && Keywords.isVerilogEndOfLabel(Left))
5046 return true;
5047 } else if (Style.Language == FormatStyle::LK_Cpp ||
5048 Style.Language == FormatStyle::LK_ObjC ||
5049 Style.Language == FormatStyle::LK_Proto ||
5050 Style.Language == FormatStyle::LK_TableGen ||
5051 Style.Language == FormatStyle::LK_TextProto) {
5052 if (Left.isStringLiteral() && Right.isStringLiteral())
5053 return true;
5056 // Basic JSON newline processing.
5057 if (Style.isJson()) {
5058 // Always break after a JSON record opener.
5059 // {
5060 // }
5061 if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace))
5062 return true;
5063 // Always break after a JSON array opener based on BreakArrays.
5064 if ((Left.is(TT_ArrayInitializerLSquare) && Left.is(tok::l_square) &&
5065 Right.isNot(tok::r_square)) ||
5066 Left.is(tok::comma)) {
5067 if (Right.is(tok::l_brace))
5068 return true;
5069 // scan to the right if an we see an object or an array inside
5070 // then break.
5071 for (const auto *Tok = &Right; Tok; Tok = Tok->Next) {
5072 if (Tok->isOneOf(tok::l_brace, tok::l_square))
5073 return true;
5074 if (Tok->isOneOf(tok::r_brace, tok::r_square))
5075 break;
5077 return Style.BreakArrays;
5081 if (Line.startsWith(tok::kw_asm) && Right.is(TT_InlineASMColon) &&
5082 Style.BreakBeforeInlineASMColon == FormatStyle::BBIAS_Always) {
5083 return true;
5086 // If the last token before a '}', ']', or ')' is a comma or a trailing
5087 // comment, the intention is to insert a line break after it in order to make
5088 // shuffling around entries easier. Import statements, especially in
5089 // JavaScript, can be an exception to this rule.
5090 if (Style.JavaScriptWrapImports || Line.Type != LT_ImportStatement) {
5091 const FormatToken *BeforeClosingBrace = nullptr;
5092 if ((Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
5093 (Style.isJavaScript() && Left.is(tok::l_paren))) &&
5094 Left.isNot(BK_Block) && Left.MatchingParen) {
5095 BeforeClosingBrace = Left.MatchingParen->Previous;
5096 } else if (Right.MatchingParen &&
5097 (Right.MatchingParen->isOneOf(tok::l_brace,
5098 TT_ArrayInitializerLSquare) ||
5099 (Style.isJavaScript() &&
5100 Right.MatchingParen->is(tok::l_paren)))) {
5101 BeforeClosingBrace = &Left;
5103 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
5104 BeforeClosingBrace->isTrailingComment())) {
5105 return true;
5109 if (Right.is(tok::comment)) {
5110 return Left.isNot(BK_BracedInit) && Left.isNot(TT_CtorInitializerColon) &&
5111 (Right.NewlinesBefore > 0 && Right.HasUnescapedNewline);
5113 if (Left.isTrailingComment())
5114 return true;
5115 if (Left.IsUnterminatedLiteral)
5116 return true;
5117 if (Right.is(TT_RequiresClause)) {
5118 switch (Style.RequiresClausePosition) {
5119 case FormatStyle::RCPS_OwnLine:
5120 case FormatStyle::RCPS_WithFollowing:
5121 return true;
5122 default:
5123 break;
5126 // Can break after template<> declaration
5127 if (Left.ClosesTemplateDeclaration && Left.MatchingParen &&
5128 Left.MatchingParen->NestingLevel == 0) {
5129 // Put concepts on the next line e.g.
5130 // template<typename T>
5131 // concept ...
5132 if (Right.is(tok::kw_concept))
5133 return Style.BreakBeforeConceptDeclarations == FormatStyle::BBCDS_Always;
5134 return Style.AlwaysBreakTemplateDeclarations == FormatStyle::BTDS_Yes;
5136 if (Left.ClosesRequiresClause && Right.isNot(tok::semi)) {
5137 switch (Style.RequiresClausePosition) {
5138 case FormatStyle::RCPS_OwnLine:
5139 case FormatStyle::RCPS_WithPreceding:
5140 return true;
5141 default:
5142 break;
5145 if (Style.PackConstructorInitializers == FormatStyle::PCIS_Never) {
5146 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon &&
5147 (Left.is(TT_CtorInitializerComma) ||
5148 Right.is(TT_CtorInitializerColon))) {
5149 return true;
5152 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
5153 Left.isOneOf(TT_CtorInitializerColon, TT_CtorInitializerComma)) {
5154 return true;
5157 if (Style.PackConstructorInitializers < FormatStyle::PCIS_CurrentLine &&
5158 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma &&
5159 Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) {
5160 return true;
5162 if (Style.PackConstructorInitializers == FormatStyle::PCIS_NextLineOnly) {
5163 if ((Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon ||
5164 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) &&
5165 Right.is(TT_CtorInitializerColon)) {
5166 return true;
5169 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
5170 Left.is(TT_CtorInitializerColon)) {
5171 return true;
5174 // Break only if we have multiple inheritance.
5175 if (Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma &&
5176 Right.is(TT_InheritanceComma)) {
5177 return true;
5179 if (Style.BreakInheritanceList == FormatStyle::BILS_AfterComma &&
5180 Left.is(TT_InheritanceComma)) {
5181 return true;
5183 if (Right.is(tok::string_literal) && Right.TokenText.startswith("R\"")) {
5184 // Multiline raw string literals are special wrt. line breaks. The author
5185 // has made a deliberate choice and might have aligned the contents of the
5186 // string literal accordingly. Thus, we try keep existing line breaks.
5187 return Right.IsMultiline && Right.NewlinesBefore > 0;
5189 if ((Left.is(tok::l_brace) || (Left.is(tok::less) && Left.Previous &&
5190 Left.Previous->is(tok::equal))) &&
5191 Right.NestingLevel == 1 && Style.Language == FormatStyle::LK_Proto) {
5192 // Don't put enums or option definitions onto single lines in protocol
5193 // buffers.
5194 return true;
5196 if (Right.is(TT_InlineASMBrace))
5197 return Right.HasUnescapedNewline;
5199 if (isAllmanBrace(Left) || isAllmanBrace(Right)) {
5200 auto *FirstNonComment = Line.getFirstNonComment();
5201 bool AccessSpecifier =
5202 FirstNonComment &&
5203 FirstNonComment->isOneOf(Keywords.kw_internal, tok::kw_public,
5204 tok::kw_private, tok::kw_protected);
5206 if (Style.BraceWrapping.AfterEnum) {
5207 if (Line.startsWith(tok::kw_enum) ||
5208 Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
5209 return true;
5211 // Ensure BraceWrapping for `public enum A {`.
5212 if (AccessSpecifier && FirstNonComment->Next &&
5213 FirstNonComment->Next->is(tok::kw_enum)) {
5214 return true;
5218 // Ensure BraceWrapping for `public interface A {`.
5219 if (Style.BraceWrapping.AfterClass &&
5220 ((AccessSpecifier && FirstNonComment->Next &&
5221 FirstNonComment->Next->is(Keywords.kw_interface)) ||
5222 Line.startsWith(Keywords.kw_interface))) {
5223 return true;
5226 // Don't attempt to interpret struct return types as structs.
5227 if (Right.isNot(TT_FunctionLBrace)) {
5228 return (Line.startsWith(tok::kw_class) &&
5229 Style.BraceWrapping.AfterClass) ||
5230 (Line.startsWith(tok::kw_struct) &&
5231 Style.BraceWrapping.AfterStruct);
5235 if (Left.is(TT_ObjCBlockLBrace) &&
5236 Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never) {
5237 return true;
5240 // Ensure wrapping after __attribute__((XX)) and @interface etc.
5241 if (Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
5242 Right.is(TT_ObjCDecl)) {
5243 return true;
5246 if (Left.is(TT_LambdaLBrace)) {
5247 if (IsFunctionArgument(Left) &&
5248 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline) {
5249 return false;
5252 if (Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_None ||
5253 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline ||
5254 (!Left.Children.empty() &&
5255 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Empty)) {
5256 return true;
5260 if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace) &&
5261 Left.isOneOf(tok::star, tok::amp, tok::ampamp, TT_TemplateCloser)) {
5262 return true;
5265 // Put multiple Java annotation on a new line.
5266 if ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) &&
5267 Left.is(TT_LeadingJavaAnnotation) &&
5268 Right.isNot(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) &&
5269 (Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) {
5270 return true;
5273 if (Right.is(TT_ProtoExtensionLSquare))
5274 return true;
5276 // In text proto instances if a submessage contains at least 2 entries and at
5277 // least one of them is a submessage, like A { ... B { ... } ... },
5278 // put all of the entries of A on separate lines by forcing the selector of
5279 // the submessage B to be put on a newline.
5281 // Example: these can stay on one line:
5282 // a { scalar_1: 1 scalar_2: 2 }
5283 // a { b { key: value } }
5285 // and these entries need to be on a new line even if putting them all in one
5286 // line is under the column limit:
5287 // a {
5288 // scalar: 1
5289 // b { key: value }
5290 // }
5292 // We enforce this by breaking before a submessage field that has previous
5293 // siblings, *and* breaking before a field that follows a submessage field.
5295 // Be careful to exclude the case [proto.ext] { ... } since the `]` is
5296 // the TT_SelectorName there, but we don't want to break inside the brackets.
5298 // Another edge case is @submessage { key: value }, which is a common
5299 // substitution placeholder. In this case we want to keep `@` and `submessage`
5300 // together.
5302 // We ensure elsewhere that extensions are always on their own line.
5303 if ((Style.Language == FormatStyle::LK_Proto ||
5304 Style.Language == FormatStyle::LK_TextProto) &&
5305 Right.is(TT_SelectorName) && Right.isNot(tok::r_square) && Right.Next) {
5306 // Keep `@submessage` together in:
5307 // @submessage { key: value }
5308 if (Left.is(tok::at))
5309 return false;
5310 // Look for the scope opener after selector in cases like:
5311 // selector { ...
5312 // selector: { ...
5313 // selector: @base { ...
5314 FormatToken *LBrace = Right.Next;
5315 if (LBrace && LBrace->is(tok::colon)) {
5316 LBrace = LBrace->Next;
5317 if (LBrace && LBrace->is(tok::at)) {
5318 LBrace = LBrace->Next;
5319 if (LBrace)
5320 LBrace = LBrace->Next;
5323 if (LBrace &&
5324 // The scope opener is one of {, [, <:
5325 // selector { ... }
5326 // selector [ ... ]
5327 // selector < ... >
5329 // In case of selector { ... }, the l_brace is TT_DictLiteral.
5330 // In case of an empty selector {}, the l_brace is not TT_DictLiteral,
5331 // so we check for immediately following r_brace.
5332 ((LBrace->is(tok::l_brace) &&
5333 (LBrace->is(TT_DictLiteral) ||
5334 (LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
5335 LBrace->is(TT_ArrayInitializerLSquare) || LBrace->is(tok::less))) {
5336 // If Left.ParameterCount is 0, then this submessage entry is not the
5337 // first in its parent submessage, and we want to break before this entry.
5338 // If Left.ParameterCount is greater than 0, then its parent submessage
5339 // might contain 1 or more entries and we want to break before this entry
5340 // if it contains at least 2 entries. We deal with this case later by
5341 // detecting and breaking before the next entry in the parent submessage.
5342 if (Left.ParameterCount == 0)
5343 return true;
5344 // However, if this submessage is the first entry in its parent
5345 // submessage, Left.ParameterCount might be 1 in some cases.
5346 // We deal with this case later by detecting an entry
5347 // following a closing paren of this submessage.
5350 // If this is an entry immediately following a submessage, it will be
5351 // preceded by a closing paren of that submessage, like in:
5352 // left---. .---right
5353 // v v
5354 // sub: { ... } key: value
5355 // If there was a comment between `}` an `key` above, then `key` would be
5356 // put on a new line anyways.
5357 if (Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
5358 return true;
5361 return false;
5364 bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
5365 const FormatToken &Right) const {
5366 const FormatToken &Left = *Right.Previous;
5367 // Language-specific stuff.
5368 if (Style.isCSharp()) {
5369 if (Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) ||
5370 Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon)) {
5371 return false;
5373 // Only break after commas for generic type constraints.
5374 if (Line.First->is(TT_CSharpGenericTypeConstraint))
5375 return Left.is(TT_CSharpGenericTypeConstraintComma);
5376 // Keep nullable operators attached to their identifiers.
5377 if (Right.is(TT_CSharpNullable))
5378 return false;
5379 } else if (Style.Language == FormatStyle::LK_Java) {
5380 if (Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
5381 Keywords.kw_implements)) {
5382 return false;
5384 if (Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
5385 Keywords.kw_implements)) {
5386 return true;
5388 } else if (Style.isJavaScript()) {
5389 const FormatToken *NonComment = Right.getPreviousNonComment();
5390 if (NonComment &&
5391 NonComment->isOneOf(
5392 tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
5393 tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
5394 tok::kw_static, tok::kw_public, tok::kw_private, tok::kw_protected,
5395 Keywords.kw_readonly, Keywords.kw_override, Keywords.kw_abstract,
5396 Keywords.kw_get, Keywords.kw_set, Keywords.kw_async,
5397 Keywords.kw_await)) {
5398 return false; // Otherwise automatic semicolon insertion would trigger.
5400 if (Right.NestingLevel == 0 &&
5401 (Left.Tok.getIdentifierInfo() ||
5402 Left.isOneOf(tok::r_square, tok::r_paren)) &&
5403 Right.isOneOf(tok::l_square, tok::l_paren)) {
5404 return false; // Otherwise automatic semicolon insertion would trigger.
5406 if (NonComment && NonComment->is(tok::identifier) &&
5407 NonComment->TokenText == "asserts") {
5408 return false;
5410 if (Left.is(TT_FatArrow) && Right.is(tok::l_brace))
5411 return false;
5412 if (Left.is(TT_JsTypeColon))
5413 return true;
5414 // Don't wrap between ":" and "!" of a strict prop init ("field!: type;").
5415 if (Left.is(tok::exclaim) && Right.is(tok::colon))
5416 return false;
5417 // Look for is type annotations like:
5418 // function f(): a is B { ... }
5419 // Do not break before is in these cases.
5420 if (Right.is(Keywords.kw_is)) {
5421 const FormatToken *Next = Right.getNextNonComment();
5422 // If `is` is followed by a colon, it's likely that it's a dict key, so
5423 // ignore it for this check.
5424 // For example this is common in Polymer:
5425 // Polymer({
5426 // is: 'name',
5427 // ...
5428 // });
5429 if (!Next || Next->isNot(tok::colon))
5430 return false;
5432 if (Left.is(Keywords.kw_in))
5433 return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None;
5434 if (Right.is(Keywords.kw_in))
5435 return Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None;
5436 if (Right.is(Keywords.kw_as))
5437 return false; // must not break before as in 'x as type' casts
5438 if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
5439 // extends and infer can appear as keywords in conditional types:
5440 // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#conditional-types
5441 // do not break before them, as the expressions are subject to ASI.
5442 return false;
5444 if (Left.is(Keywords.kw_as))
5445 return true;
5446 if (Left.is(TT_NonNullAssertion))
5447 return true;
5448 if (Left.is(Keywords.kw_declare) &&
5449 Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
5450 Keywords.kw_function, tok::kw_class, tok::kw_enum,
5451 Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
5452 Keywords.kw_let, tok::kw_const)) {
5453 // See grammar for 'declare' statements at:
5454 // https://github.com/Microsoft/TypeScript/blob/main/doc/spec-ARCHIVED.md#A.10
5455 return false;
5457 if (Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
5458 Right.isOneOf(tok::identifier, tok::string_literal)) {
5459 return false; // must not break in "module foo { ...}"
5461 if (Right.is(TT_TemplateString) && Right.closesScope())
5462 return false;
5463 // Don't split tagged template literal so there is a break between the tag
5464 // identifier and template string.
5465 if (Left.is(tok::identifier) && Right.is(TT_TemplateString))
5466 return false;
5467 if (Left.is(TT_TemplateString) && Left.opensScope())
5468 return true;
5471 if (Left.is(tok::at))
5472 return false;
5473 if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
5474 return false;
5475 if (Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
5476 return Right.isNot(tok::l_paren);
5477 if (Right.is(TT_PointerOrReference)) {
5478 return Line.IsMultiVariableDeclStmt ||
5479 (getTokenPointerOrReferenceAlignment(Right) ==
5480 FormatStyle::PAS_Right &&
5481 (!Right.Next || Right.Next->isNot(TT_FunctionDeclarationName)));
5483 if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
5484 Right.is(tok::kw_operator)) {
5485 return true;
5487 if (Left.is(TT_PointerOrReference))
5488 return false;
5489 if (Right.isTrailingComment()) {
5490 // We rely on MustBreakBefore being set correctly here as we should not
5491 // change the "binding" behavior of a comment.
5492 // The first comment in a braced lists is always interpreted as belonging to
5493 // the first list element. Otherwise, it should be placed outside of the
5494 // list.
5495 return Left.is(BK_BracedInit) ||
5496 (Left.is(TT_CtorInitializerColon) && Right.NewlinesBefore > 0 &&
5497 Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon);
5499 if (Left.is(tok::question) && Right.is(tok::colon))
5500 return false;
5501 if (Right.is(TT_ConditionalExpr) || Right.is(tok::question))
5502 return Style.BreakBeforeTernaryOperators;
5503 if (Left.is(TT_ConditionalExpr) || Left.is(tok::question))
5504 return !Style.BreakBeforeTernaryOperators;
5505 if (Left.is(TT_InheritanceColon))
5506 return Style.BreakInheritanceList == FormatStyle::BILS_AfterColon;
5507 if (Right.is(TT_InheritanceColon))
5508 return Style.BreakInheritanceList != FormatStyle::BILS_AfterColon;
5509 if (Right.is(TT_ObjCMethodExpr) && Right.isNot(tok::r_square) &&
5510 Left.isNot(TT_SelectorName)) {
5511 return true;
5514 if (Right.is(tok::colon) &&
5515 !Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon)) {
5516 return false;
5518 if (Left.is(tok::colon) && Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) {
5519 if (Style.Language == FormatStyle::LK_Proto ||
5520 Style.Language == FormatStyle::LK_TextProto) {
5521 if (!Style.AlwaysBreakBeforeMultilineStrings && Right.isStringLiteral())
5522 return false;
5523 // Prevent cases like:
5525 // submessage:
5526 // { key: valueeeeeeeeeeee }
5528 // when the snippet does not fit into one line.
5529 // Prefer:
5531 // submessage: {
5532 // key: valueeeeeeeeeeee
5533 // }
5535 // instead, even if it is longer by one line.
5537 // Note that this allows the "{" to go over the column limit
5538 // when the column limit is just between ":" and "{", but that does
5539 // not happen too often and alternative formattings in this case are
5540 // not much better.
5542 // The code covers the cases:
5544 // submessage: { ... }
5545 // submessage: < ... >
5546 // repeated: [ ... ]
5547 if (((Right.is(tok::l_brace) || Right.is(tok::less)) &&
5548 Right.is(TT_DictLiteral)) ||
5549 Right.is(TT_ArrayInitializerLSquare)) {
5550 return false;
5553 return true;
5555 if (Right.is(tok::r_square) && Right.MatchingParen &&
5556 Right.MatchingParen->is(TT_ProtoExtensionLSquare)) {
5557 return false;
5559 if (Right.is(TT_SelectorName) || (Right.is(tok::identifier) && Right.Next &&
5560 Right.Next->is(TT_ObjCMethodExpr))) {
5561 return Left.isNot(tok::period); // FIXME: Properly parse ObjC calls.
5563 if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty)
5564 return true;
5565 if (Right.is(tok::kw_concept))
5566 return Style.BreakBeforeConceptDeclarations != FormatStyle::BBCDS_Never;
5567 if (Right.is(TT_RequiresClause))
5568 return true;
5569 if (Left.ClosesTemplateDeclaration || Left.is(TT_FunctionAnnotationRParen))
5570 return true;
5571 if (Left.ClosesRequiresClause)
5572 return true;
5573 if (Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
5574 TT_OverloadedOperator)) {
5575 return false;
5577 if (Left.is(TT_RangeBasedForLoopColon))
5578 return true;
5579 if (Right.is(TT_RangeBasedForLoopColon))
5580 return false;
5581 if (Left.is(TT_TemplateCloser) && Right.is(TT_TemplateOpener))
5582 return true;
5583 if ((Left.is(tok::greater) && Right.is(tok::greater)) ||
5584 (Left.is(tok::less) && Right.is(tok::less))) {
5585 return false;
5587 if (Right.is(TT_BinaryOperator) &&
5588 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None &&
5589 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All ||
5590 Right.getPrecedence() != prec::Assignment)) {
5591 return true;
5593 if (Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator) ||
5594 Left.is(tok::kw_operator)) {
5595 return false;
5597 if (Left.is(tok::equal) && !Right.isOneOf(tok::kw_default, tok::kw_delete) &&
5598 Line.Type == LT_VirtualFunctionDecl && Left.NestingLevel == 0) {
5599 return false;
5601 if (Left.is(tok::equal) && Right.is(tok::l_brace) &&
5602 !Style.Cpp11BracedListStyle) {
5603 return false;
5605 if (Left.is(TT_AttributeLParen) ||
5606 (Left.is(tok::l_paren) && Left.is(TT_TypeDeclarationParen))) {
5607 return false;
5609 if (Left.is(tok::l_paren) && Left.Previous &&
5610 (Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen))) {
5611 return false;
5613 if (Right.is(TT_ImplicitStringLiteral))
5614 return false;
5616 if (Right.is(TT_TemplateCloser))
5617 return false;
5618 if (Right.is(tok::r_square) && Right.MatchingParen &&
5619 Right.MatchingParen->is(TT_LambdaLSquare)) {
5620 return false;
5623 // We only break before r_brace if there was a corresponding break before
5624 // the l_brace, which is tracked by BreakBeforeClosingBrace.
5625 if (Right.is(tok::r_brace)) {
5626 return Right.MatchingParen && (Right.MatchingParen->is(BK_Block) ||
5627 (Right.isBlockIndentedInitRBrace(Style)));
5630 // We only break before r_paren if we're in a block indented context.
5631 if (Right.is(tok::r_paren)) {
5632 if (Style.AlignAfterOpenBracket != FormatStyle::BAS_BlockIndent ||
5633 !Right.MatchingParen) {
5634 return false;
5636 auto Next = Right.Next;
5637 if (Next && Next->is(tok::r_paren))
5638 Next = Next->Next;
5639 if (Next && Next->is(tok::l_paren))
5640 return false;
5641 const FormatToken *Previous = Right.MatchingParen->Previous;
5642 return !(Previous && (Previous->is(tok::kw_for) || Previous->isIf()));
5645 // Allow breaking after a trailing annotation, e.g. after a method
5646 // declaration.
5647 if (Left.is(TT_TrailingAnnotation)) {
5648 return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
5649 tok::less, tok::coloncolon);
5652 if (Right.isAttribute())
5653 return true;
5655 if (Right.is(tok::l_square) && Right.is(TT_AttributeSquare))
5656 return Left.isNot(TT_AttributeSquare);
5658 if (Left.is(tok::identifier) && Right.is(tok::string_literal))
5659 return true;
5661 if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
5662 return true;
5664 if (Left.is(TT_CtorInitializerColon)) {
5665 return Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
5666 (!Right.isTrailingComment() || Right.NewlinesBefore > 0);
5668 if (Right.is(TT_CtorInitializerColon))
5669 return Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon;
5670 if (Left.is(TT_CtorInitializerComma) &&
5671 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
5672 return false;
5674 if (Right.is(TT_CtorInitializerComma) &&
5675 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
5676 return true;
5678 if (Left.is(TT_InheritanceComma) &&
5679 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) {
5680 return false;
5682 if (Right.is(TT_InheritanceComma) &&
5683 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) {
5684 return true;
5686 if (Left.is(TT_ArrayInitializerLSquare))
5687 return true;
5688 if (Right.is(tok::kw_typename) && Left.isNot(tok::kw_const))
5689 return true;
5690 if ((Left.isBinaryOperator() || Left.is(TT_BinaryOperator)) &&
5691 !Left.isOneOf(tok::arrowstar, tok::lessless) &&
5692 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All &&
5693 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None ||
5694 Left.getPrecedence() == prec::Assignment)) {
5695 return true;
5697 if ((Left.is(TT_AttributeSquare) && Right.is(tok::l_square)) ||
5698 (Left.is(tok::r_square) && Right.is(TT_AttributeSquare))) {
5699 return false;
5702 auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
5703 if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace)) {
5704 if (isAllmanLambdaBrace(Left))
5705 return !isItAnEmptyLambdaAllowed(Left, ShortLambdaOption);
5706 if (isAllmanLambdaBrace(Right))
5707 return !isItAnEmptyLambdaAllowed(Right, ShortLambdaOption);
5710 if (Right.is(tok::kw_noexcept) && Right.is(TT_TrailingAnnotation)) {
5711 switch (Style.AllowBreakBeforeNoexceptSpecifier) {
5712 case FormatStyle::BBNSS_Never:
5713 return false;
5714 case FormatStyle::BBNSS_Always:
5715 return true;
5716 case FormatStyle::BBNSS_OnlyWithParen:
5717 return Right.Next && Right.Next->is(tok::l_paren);
5721 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
5722 tok::kw_class, tok::kw_struct, tok::comment) ||
5723 Right.isMemberAccess() ||
5724 Right.isOneOf(TT_TrailingReturnArrow, tok::lessless, tok::colon,
5725 tok::l_square, tok::at) ||
5726 (Left.is(tok::r_paren) &&
5727 Right.isOneOf(tok::identifier, tok::kw_const)) ||
5728 (Left.is(tok::l_paren) && Right.isNot(tok::r_paren)) ||
5729 (Left.is(TT_TemplateOpener) && Right.isNot(TT_TemplateCloser));
5732 void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) const {
5733 llvm::errs() << "AnnotatedTokens(L=" << Line.Level << ", P=" << Line.PPLevel
5734 << ", T=" << Line.Type << ", C=" << Line.IsContinuation
5735 << "):\n";
5736 const FormatToken *Tok = Line.First;
5737 while (Tok) {
5738 llvm::errs() << " M=" << Tok->MustBreakBefore
5739 << " C=" << Tok->CanBreakBefore
5740 << " T=" << getTokenTypeName(Tok->getType())
5741 << " S=" << Tok->SpacesRequiredBefore
5742 << " F=" << Tok->Finalized << " B=" << Tok->BlockParameterCount
5743 << " BK=" << Tok->getBlockKind() << " P=" << Tok->SplitPenalty
5744 << " Name=" << Tok->Tok.getName() << " L=" << Tok->TotalLength
5745 << " PPK=" << Tok->getPackingKind() << " FakeLParens=";
5746 for (prec::Level LParen : Tok->FakeLParens)
5747 llvm::errs() << LParen << "/";
5748 llvm::errs() << " FakeRParens=" << Tok->FakeRParens;
5749 llvm::errs() << " II=" << Tok->Tok.getIdentifierInfo();
5750 llvm::errs() << " Text='" << Tok->TokenText << "'\n";
5751 if (!Tok->Next)
5752 assert(Tok == Line.Last);
5753 Tok = Tok->Next;
5755 llvm::errs() << "----\n";
5758 FormatStyle::PointerAlignmentStyle
5759 TokenAnnotator::getTokenReferenceAlignment(const FormatToken &Reference) const {
5760 assert(Reference.isOneOf(tok::amp, tok::ampamp));
5761 switch (Style.ReferenceAlignment) {
5762 case FormatStyle::RAS_Pointer:
5763 return Style.PointerAlignment;
5764 case FormatStyle::RAS_Left:
5765 return FormatStyle::PAS_Left;
5766 case FormatStyle::RAS_Right:
5767 return FormatStyle::PAS_Right;
5768 case FormatStyle::RAS_Middle:
5769 return FormatStyle::PAS_Middle;
5771 assert(0); //"Unhandled value of ReferenceAlignment"
5772 return Style.PointerAlignment;
5775 FormatStyle::PointerAlignmentStyle
5776 TokenAnnotator::getTokenPointerOrReferenceAlignment(
5777 const FormatToken &PointerOrReference) const {
5778 if (PointerOrReference.isOneOf(tok::amp, tok::ampamp)) {
5779 switch (Style.ReferenceAlignment) {
5780 case FormatStyle::RAS_Pointer:
5781 return Style.PointerAlignment;
5782 case FormatStyle::RAS_Left:
5783 return FormatStyle::PAS_Left;
5784 case FormatStyle::RAS_Right:
5785 return FormatStyle::PAS_Right;
5786 case FormatStyle::RAS_Middle:
5787 return FormatStyle::PAS_Middle;
5790 assert(PointerOrReference.is(tok::star));
5791 return Style.PointerAlignment;
5794 } // namespace format
5795 } // namespace clang