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