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