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
535 if (!HasMultipleLines)
536 OpeningParen.setPackingKind(PPK_Inconclusive);
537 else if (HasMultipleParametersOnALine)
538 OpeningParen.setPackingKind(PPK_BinPacked);
539 else
540 OpeningParen.setPackingKind(PPK_OnePerLine);
541
542 next();
543 return true;
544 }
545 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
546 return false;
547
548 if (CurrentToken->is(tok::l_brace) && OpeningParen.is(TT_ObjCBlockLParen))
549 OpeningParen.setType(TT_Unknown);
550 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
551 !CurrentToken->Next->HasUnescapedNewline &&
552 !CurrentToken->Next->isTrailingComment()) {
553 HasMultipleParametersOnALine = true;
554 }
555 bool ProbablyFunctionTypeLParen =
556 (CurrentToken->is(tok::l_paren) && CurrentToken->Next &&
557 CurrentToken->Next->isOneOf(tok::star, tok::amp, tok::caret));
558 if ((Prev.isOneOf(tok::kw_const, tok::kw_auto) ||
559 Prev.isTypeName(LangOpts)) &&
560 !(CurrentToken->is(tok::l_brace) ||
561 (CurrentToken->is(tok::l_paren) && !ProbablyFunctionTypeLParen))) {
562 Contexts.back().IsExpression = false;
563 }
564 if (CurrentToken->isOneOf(tok::semi, tok::colon)) {
565 MightBeObjCForRangeLoop = false;
566 if (PossibleObjCForInToken) {
567 PossibleObjCForInToken->setType(TT_Unknown);
568 PossibleObjCForInToken = nullptr;
569 }
570 }
571 if (IsIf && CurrentToken->is(tok::semi)) {
572 for (auto *Tok = OpeningParen.Next;
573 Tok != CurrentToken &&
574 Tok->isNoneOf(tok::equal, tok::l_paren, tok::l_brace);
575 Tok = Tok->Next) {
576 if (Tok->isPointerOrReference())
577 Tok->setFinalizedType(TT_PointerOrReference);
578 }
579 }
580 if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in)) {
581 PossibleObjCForInToken = CurrentToken;
582 PossibleObjCForInToken->setType(TT_ObjCForIn);
583 }
584 // When we discover a 'new', we set CanBeExpression to 'false' in order to
585 // parse the type correctly. Reset that after a comma.
586 if (CurrentToken->is(tok::comma))
587 Contexts.back().CanBeExpression = true;
588
589 if (Style.isTableGen()) {
590 if (CurrentToken->is(tok::comma)) {
591 if (Contexts.back().IsTableGenCondOpe)
592 CurrentToken->setType(TT_TableGenCondOperatorComma);
593 next();
594 } else if (CurrentToken->is(tok::colon)) {
595 if (Contexts.back().IsTableGenCondOpe)
596 CurrentToken->setType(TT_TableGenCondOperatorColon);
597 next();
598 }
599 // In TableGen there must be Values in parens.
600 if (!parseTableGenValue())
601 return false;
602 continue;
603 }
604
605 FormatToken *Tok = CurrentToken;
606 if (!consumeToken())
607 return false;
608 updateParameterCount(&OpeningParen, Tok);
609 if (CurrentToken && CurrentToken->HasUnescapedNewline)
610 HasMultipleLines = true;
611 }
612 return false;
613 }
614
615 bool isCSharpAttributeSpecifier(const FormatToken &Tok) {
616 if (!Style.isCSharp())
617 return false;
618
619 // `identifier[i]` is not an attribute.
620 if (Tok.Previous && Tok.Previous->is(tok::identifier))
621 return false;
622
623 // Chains of [] in `identifier[i][j][k]` are not attributes.
624 if (Tok.Previous && Tok.Previous->is(tok::r_square)) {
625 auto *MatchingParen = Tok.Previous->MatchingParen;
626 if (!MatchingParen || MatchingParen->is(TT_ArraySubscriptLSquare))
627 return false;
628 }
629
630 const FormatToken *AttrTok = Tok.Next;
631 if (!AttrTok)
632 return false;
633
634 // Just an empty declaration e.g. string [].
635 if (AttrTok->is(tok::r_square))
636 return false;
637
638 // Move along the tokens inbetween the '[' and ']' e.g. [STAThread].
639 while (AttrTok && AttrTok->isNot(tok::r_square))
640 AttrTok = AttrTok->Next;
641
642 if (!AttrTok)
643 return false;
644
645 // Allow an attribute to be the only content of a file.
646 AttrTok = AttrTok->Next;
647 if (!AttrTok)
648 return true;
649
650 // Limit this to being an access modifier that follows.
651 if (AttrTok->isAccessSpecifierKeyword() ||
652 AttrTok->isOneOf(tok::comment, tok::kw_class, tok::kw_static,
653 tok::l_square, Keywords.kw_internal)) {
654 return true;
655 }
656
657 // incase its a [XXX] retval func(....
658 if (AttrTok->Next &&
659 AttrTok->Next->startsSequence(tok::identifier, tok::l_paren)) {
660 return true;
661 }
662
663 return false;
664 }
665
666 bool parseSquare() {
667 if (!CurrentToken)
668 return false;
669
670 // A '[' could be an index subscript (after an identifier or after
671 // ')' or ']'), it could be the start of an Objective-C method
672 // expression, it could the start of an Objective-C array literal,
673 // or it could be a C++ attribute specifier [[foo::bar]].
674 FormatToken *Left = CurrentToken->Previous;
675 Left->ParentBracket = Contexts.back().ContextKind;
676 FormatToken *Parent = Left->getPreviousNonComment();
677
678 // Cases where '>' is followed by '['.
679 // In C++, this can happen either in array of templates (foo<int>[10])
680 // or when array is a nested template type (unique_ptr<type1<type2>[]>).
681 bool CppArrayTemplates =
682 IsCpp && Parent && Parent->is(TT_TemplateCloser) &&
683 (Contexts.back().CanBeExpression || Contexts.back().IsExpression ||
684 Contexts.back().ContextType == Context::TemplateArgument);
685
686 const bool IsInnerSquare = Contexts.back().InCpp11AttributeSpecifier;
687 const bool IsCpp11AttributeSpecifier =
688 isCppAttribute(IsCpp, *Left) || IsInnerSquare;
689
690 // Treat C# Attributes [STAThread] much like C++ attributes [[...]].
691 bool IsCSharpAttributeSpecifier =
692 isCSharpAttributeSpecifier(*Left) ||
693 Contexts.back().InCSharpAttributeSpecifier;
694
695 bool InsideInlineASM = Line.startsWith(tok::kw_asm);
696 bool IsCppStructuredBinding = Left->isCppStructuredBinding(IsCpp);
697 bool StartsObjCMethodExpr =
698 !IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates &&
699 IsCpp && !IsCpp11AttributeSpecifier && !IsCSharpAttributeSpecifier &&
700 Contexts.back().CanBeExpression && Left->isNot(TT_LambdaLSquare) &&
701 CurrentToken->isNoneOf(tok::l_brace, tok::r_square) &&
702 (!Parent ||
703 Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
704 tok::kw_return, tok::kw_throw) ||
705 Parent->isUnaryOperator() ||
706 // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
707 Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
708 (getBinOpPrecedence(Parent->Tok.getKind(), true, true) >
710 bool ColonFound = false;
711
712 unsigned BindingIncrease = 1;
713 if (IsCppStructuredBinding) {
714 Left->setType(TT_StructuredBindingLSquare);
715 } else if (Left->is(TT_Unknown)) {
716 if (StartsObjCMethodExpr) {
717 Left->setType(TT_ObjCMethodExpr);
718 } else if (InsideInlineASM) {
719 Left->setType(TT_InlineASMSymbolicNameLSquare);
720 } else if (IsCpp11AttributeSpecifier) {
721 if (!IsInnerSquare) {
722 Left->setType(TT_AttributeLSquare);
723 if (Left->Previous)
724 Left->Previous->EndsCppAttributeGroup = false;
725 }
726 } else if (Style.isJavaScript() && Parent &&
727 Contexts.back().ContextKind == tok::l_brace &&
728 Parent->isOneOf(tok::l_brace, tok::comma)) {
729 Left->setType(TT_JsComputedPropertyName);
730 } else if (IsCpp && Contexts.back().ContextKind == tok::l_brace &&
731 Parent && Parent->isOneOf(tok::l_brace, tok::comma)) {
732 Left->setType(TT_DesignatedInitializerLSquare);
733 } else if (IsCSharpAttributeSpecifier) {
734 Left->setType(TT_AttributeLSquare);
735 } else if (CurrentToken->is(tok::r_square) && Parent &&
736 Parent->is(TT_TemplateCloser)) {
737 Left->setType(TT_ArraySubscriptLSquare);
738 } else if (Style.isProto()) {
739 // Square braces in LK_Proto can either be message field attributes:
740 //
741 // optional Aaa aaa = 1 [
742 // (aaa) = aaa
743 // ];
744 //
745 // extensions 123 [
746 // (aaa) = aaa
747 // ];
748 //
749 // or text proto extensions (in options):
750 //
751 // option (Aaa.options) = {
752 // [type.type/type] {
753 // key: value
754 // }
755 // }
756 //
757 // or repeated fields (in options):
758 //
759 // option (Aaa.options) = {
760 // keys: [ 1, 2, 3 ]
761 // }
762 //
763 // In the first and the third case we want to spread the contents inside
764 // the square braces; in the second we want to keep them inline.
765 Left->setType(TT_ArrayInitializerLSquare);
766 if (!Left->endsSequence(tok::l_square, tok::numeric_constant,
767 tok::equal) &&
768 !Left->endsSequence(tok::l_square, tok::numeric_constant,
769 tok::identifier) &&
770 !Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)) {
771 Left->setType(TT_ProtoExtensionLSquare);
772 BindingIncrease = 10;
773 }
774 } else if (!CppArrayTemplates && Parent &&
775 Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
776 tok::comma, tok::l_paren, tok::l_square,
777 tok::question, tok::colon, tok::kw_return,
778 // Should only be relevant to JavaScript:
779 tok::kw_default)) {
780 Left->setType(TT_ArrayInitializerLSquare);
781 } else {
782 BindingIncrease = 10;
783 Left->setType(TT_ArraySubscriptLSquare);
784 }
785 }
786
787 ScopedContextCreator ContextCreator(*this, tok::l_square, BindingIncrease);
788 Contexts.back().IsExpression = true;
789 if (Style.isJavaScript() && Parent && Parent->is(TT_JsTypeColon))
790 Contexts.back().IsExpression = false;
791
792 Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
793 Contexts.back().InCpp11AttributeSpecifier = IsCpp11AttributeSpecifier;
794 Contexts.back().InCSharpAttributeSpecifier = IsCSharpAttributeSpecifier;
795
796 while (CurrentToken) {
797 if (CurrentToken->is(tok::r_square)) {
798 if (IsCpp11AttributeSpecifier && !IsInnerSquare) {
799 CurrentToken->setType(TT_AttributeRSquare);
800 CurrentToken->EndsCppAttributeGroup = true;
801 }
802 if (IsCSharpAttributeSpecifier) {
803 CurrentToken->setType(TT_AttributeRSquare);
804 } else if (((CurrentToken->Next &&
805 CurrentToken->Next->is(tok::l_paren)) ||
806 (CurrentToken->Previous &&
807 CurrentToken->Previous->Previous == Left)) &&
808 Left->is(TT_ObjCMethodExpr)) {
809 // An ObjC method call is rarely followed by an open parenthesis. It
810 // also can't be composed of just one token, unless it's a macro that
811 // will be expanded to more tokens.
812 // FIXME: Do we incorrectly label ":" with this?
813 StartsObjCMethodExpr = false;
814 Left->setType(TT_Unknown);
815 }
816 if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
817 CurrentToken->setType(TT_ObjCMethodExpr);
818 // If we haven't seen a colon yet, make sure the last identifier
819 // before the r_square is tagged as a selector name component.
820 if (!ColonFound && CurrentToken->Previous &&
821 CurrentToken->Previous->is(TT_Unknown) &&
822 canBeObjCSelectorComponent(*CurrentToken->Previous)) {
823 CurrentToken->Previous->setType(TT_SelectorName);
824 }
825 // determineStarAmpUsage() thinks that '*' '[' is allocating an
826 // array of pointers, but if '[' starts a selector then '*' is a
827 // binary operator.
828 if (Parent && Parent->is(TT_PointerOrReference))
829 Parent->overwriteFixedType(TT_BinaryOperator);
830 }
831 Left->MatchingParen = CurrentToken;
832 CurrentToken->MatchingParen = Left;
833 // FirstObjCSelectorName is set when a colon is found. This does
834 // not work, however, when the method has no parameters.
835 // Here, we set FirstObjCSelectorName when the end of the method call is
836 // reached, in case it was not set already.
837 if (!Contexts.back().FirstObjCSelectorName) {
838 FormatToken *Previous = CurrentToken->getPreviousNonComment();
839 if (Previous && Previous->is(TT_SelectorName)) {
840 Previous->ObjCSelectorNameParts = 1;
841 Contexts.back().FirstObjCSelectorName = Previous;
842 }
843 } else {
844 Left->ParameterCount =
845 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
846 }
847 if (Contexts.back().FirstObjCSelectorName) {
848 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
849 Contexts.back().LongestObjCSelectorName;
850 if (Left->BlockParameterCount > 1)
851 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
852 }
853 if (Style.isTableGen() && Left->is(TT_TableGenListOpener))
854 CurrentToken->setType(TT_TableGenListCloser);
855 next();
856 return true;
857 }
858 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
859 return false;
860 if (CurrentToken->is(tok::colon)) {
861 if (IsCpp11AttributeSpecifier &&
862 CurrentToken->endsSequence(tok::colon, tok::identifier,
863 tok::kw_using)) {
864 // Remember that this is a [[using ns: foo]] C++ attribute, so we
865 // don't add a space before the colon (unlike other colons).
866 CurrentToken->setType(TT_AttributeColon);
867 } else if (!Style.isVerilog() && !Line.InPragmaDirective &&
868 Left->isOneOf(TT_ArraySubscriptLSquare,
869 TT_DesignatedInitializerLSquare)) {
870 Left->setType(TT_ObjCMethodExpr);
871 StartsObjCMethodExpr = true;
872 Contexts.back().ColonIsObjCMethodExpr = true;
873 if (Parent && Parent->is(tok::r_paren)) {
874 // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
875 Parent->setType(TT_CastRParen);
876 }
877 }
878 ColonFound = true;
879 }
880 if (CurrentToken->is(tok::comma) && Left->is(TT_ObjCMethodExpr) &&
881 !ColonFound) {
882 Left->setType(TT_ArrayInitializerLSquare);
883 }
884 FormatToken *Tok = CurrentToken;
885 if (Style.isTableGen()) {
886 if (CurrentToken->isOneOf(tok::comma, tok::minus, tok::ellipsis)) {
887 // '-' and '...' appears as a separator in slice.
888 next();
889 } else {
890 // In TableGen there must be a list of Values in square brackets.
891 // It must be ValueList or SliceElements.
892 if (!parseTableGenValue())
893 return false;
894 }
895 updateParameterCount(Left, Tok);
896 continue;
897 }
898 if (!consumeToken())
899 return false;
900 updateParameterCount(Left, Tok);
901 }
902 return false;
903 }
904
905 void skipToNextNonComment() {
906 next();
907 while (CurrentToken && CurrentToken->is(tok::comment))
908 next();
909 }
910
911 // Simplified parser for TableGen Value. Returns true on success.
912 // It consists of SimpleValues, SimpleValues with Suffixes, and Value followed
913 // by '#', paste operator.
914 // There also exists the case the Value is parsed as NameValue.
915 // In this case, the Value ends if '{' is found.
916 bool parseTableGenValue(bool ParseNameMode = false) {
917 if (!CurrentToken)
918 return false;
919 while (CurrentToken->is(tok::comment))
920 next();
921 if (!parseTableGenSimpleValue())
922 return false;
923 if (!CurrentToken)
924 return true;
925 // Value "#" [Value]
926 if (CurrentToken->is(tok::hash)) {
927 if (CurrentToken->Next &&
928 CurrentToken->Next->isOneOf(tok::colon, tok::semi, tok::l_brace)) {
929 // Trailing paste operator.
930 // These are only the allowed cases in TGParser::ParseValue().
931 CurrentToken->setType(TT_TableGenTrailingPasteOperator);
932 next();
933 return true;
934 }
935 FormatToken *HashTok = CurrentToken;
936 skipToNextNonComment();
937 HashTok->setType(TT_Unknown);
938 if (!parseTableGenValue(ParseNameMode))
939 return false;
940 if (!CurrentToken)
941 return true;
942 }
943 // In name mode, '{' is regarded as the end of the value.
944 // See TGParser::ParseValue in TGParser.cpp
945 if (ParseNameMode && CurrentToken->is(tok::l_brace))
946 return true;
947 // These tokens indicates this is a value with suffixes.
948 if (CurrentToken->isOneOf(tok::l_brace, tok::l_square, tok::period)) {
949 CurrentToken->setType(TT_TableGenValueSuffix);
950 FormatToken *Suffix = CurrentToken;
951 skipToNextNonComment();
952 if (Suffix->is(tok::l_square))
953 return parseSquare();
954 if (Suffix->is(tok::l_brace)) {
955 Scopes.push_back(getScopeType(*Suffix));
956 return parseBrace();
957 }
958 }
959 return true;
960 }
961
962 // TokVarName ::= "$" ualpha (ualpha | "0"..."9")*
963 // Appears as a part of DagArg.
964 // This does not change the current token on fail.
965 bool tryToParseTableGenTokVar() {
966 if (!CurrentToken)
967 return false;
968 if (CurrentToken->is(tok::identifier) &&
969 CurrentToken->TokenText.front() == '$') {
970 skipToNextNonComment();
971 return true;
972 }
973 return false;
974 }
975
976 // DagArg ::= Value [":" TokVarName] | TokVarName
977 // Appears as a part of SimpleValue6.
978 bool parseTableGenDAGArg(bool AlignColon = false) {
979 if (tryToParseTableGenTokVar())
980 return true;
981 if (parseTableGenValue()) {
982 if (CurrentToken && CurrentToken->is(tok::colon)) {
983 if (AlignColon)
984 CurrentToken->setType(TT_TableGenDAGArgListColonToAlign);
985 else
986 CurrentToken->setType(TT_TableGenDAGArgListColon);
987 skipToNextNonComment();
988 return tryToParseTableGenTokVar();
989 }
990 return true;
991 }
992 return false;
993 }
994
995 // Judge if the token is a operator ID to insert line break in DAGArg.
996 // That is, TableGenBreakingDAGArgOperators is empty (by the definition of the
997 // option) or the token is in the list.
998 bool isTableGenDAGArgBreakingOperator(const FormatToken &Tok) {
999 auto &Opes = Style.TableGenBreakingDAGArgOperators;
1000 // If the list is empty, all operators are breaking operators.
1001 if (Opes.empty())
1002 return true;
1003 // Otherwise, the operator is limited to normal identifiers.
1004 if (Tok.isNot(tok::identifier) ||
1005 Tok.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
1006 return false;
1007 }
1008 // The case next is colon, it is not a operator of identifier.
1009 if (!Tok.Next || Tok.Next->is(tok::colon))
1010 return false;
1011 return llvm::is_contained(Opes, Tok.TokenText.str());
1012 }
1013
1014 // SimpleValue6 ::= "(" DagArg [DagArgList] ")"
1015 // This parses SimpleValue 6's inside part of "(" ")"
1016 bool parseTableGenDAGArgAndList(FormatToken *Opener) {
1017 FormatToken *FirstTok = CurrentToken;
1018 if (!parseTableGenDAGArg())
1019 return false;
1020 bool BreakInside = false;
1021 if (Style.TableGenBreakInsideDAGArg != FormatStyle::DAS_DontBreak) {
1022 // Specialized detection for DAGArgOperator, that determines the way of
1023 // line break for this DAGArg elements.
1024 if (isTableGenDAGArgBreakingOperator(*FirstTok)) {
1025 // Special case for identifier DAGArg operator.
1026 BreakInside = true;
1027 Opener->setType(TT_TableGenDAGArgOpenerToBreak);
1028 if (FirstTok->isOneOf(TT_TableGenBangOperator,
1029 TT_TableGenCondOperator)) {
1030 // Special case for bang/cond operators. Set the whole operator as
1031 // the DAGArg operator. Always break after it.
1032 CurrentToken->Previous->setType(TT_TableGenDAGArgOperatorToBreak);
1033 } else if (FirstTok->is(tok::identifier)) {
1034 if (Style.TableGenBreakInsideDAGArg == FormatStyle::DAS_BreakAll)
1035 FirstTok->setType(TT_TableGenDAGArgOperatorToBreak);
1036 else
1037 FirstTok->setType(TT_TableGenDAGArgOperatorID);
1038 }
1039 }
1040 }
1041 // Parse the [DagArgList] part
1042 return parseTableGenDAGArgList(Opener, BreakInside);
1043 }
1044
1045 // DagArgList ::= "," DagArg [DagArgList]
1046 // This parses SimpleValue 6's [DagArgList] part.
1047 bool parseTableGenDAGArgList(FormatToken *Opener, bool BreakInside) {
1048 ScopedContextCreator ContextCreator(*this, tok::l_paren, 0);
1049 Contexts.back().IsTableGenDAGArgList = true;
1050 bool FirstDAGArgListElm = true;
1051 while (CurrentToken) {
1052 if (!FirstDAGArgListElm && CurrentToken->is(tok::comma)) {
1053 CurrentToken->setType(BreakInside ? TT_TableGenDAGArgListCommaToBreak
1054 : TT_TableGenDAGArgListComma);
1055 skipToNextNonComment();
1056 }
1057 if (CurrentToken && CurrentToken->is(tok::r_paren)) {
1058 CurrentToken->setType(TT_TableGenDAGArgCloser);
1059 Opener->MatchingParen = CurrentToken;
1060 CurrentToken->MatchingParen = Opener;
1061 skipToNextNonComment();
1062 return true;
1063 }
1064 if (!parseTableGenDAGArg(
1065 BreakInside &&
1066 Style.AlignConsecutiveTableGenBreakingDAGArgColons.Enabled)) {
1067 return false;
1068 }
1069 FirstDAGArgListElm = false;
1070 }
1071 return false;
1072 }
1073
1074 bool parseTableGenSimpleValue() {
1075 assert(Style.isTableGen());
1076 if (!CurrentToken)
1077 return false;
1078 FormatToken *Tok = CurrentToken;
1079 skipToNextNonComment();
1080 // SimpleValue 1, 2, 3: Literals
1081 if (Tok->isOneOf(tok::numeric_constant, tok::string_literal,
1082 TT_TableGenMultiLineString, tok::kw_true, tok::kw_false,
1083 tok::question, tok::kw_int)) {
1084 return true;
1085 }
1086 // SimpleValue 4: ValueList, Type
1087 if (Tok->is(tok::l_brace)) {
1088 Scopes.push_back(getScopeType(*Tok));
1089 return parseBrace();
1090 }
1091 // SimpleValue 5: List initializer
1092 if (Tok->is(tok::l_square)) {
1093 Tok->setType(TT_TableGenListOpener);
1094 if (!parseSquare())
1095 return false;
1096 if (Tok->is(tok::less)) {
1097 CurrentToken->setType(TT_TemplateOpener);
1098 return parseAngle();
1099 }
1100 return true;
1101 }
1102 // SimpleValue 6: DAGArg [DAGArgList]
1103 // SimpleValue6 ::= "(" DagArg [DagArgList] ")"
1104 if (Tok->is(tok::l_paren)) {
1105 Tok->setType(TT_TableGenDAGArgOpener);
1106 // Nested DAGArg requires space before '(' as separator.
1107 if (Contexts.back().IsTableGenDAGArgList)
1108 Tok->SpacesRequiredBefore = 1;
1109 return parseTableGenDAGArgAndList(Tok);
1110 }
1111 // SimpleValue 9: Bang operator
1112 if (Tok->is(TT_TableGenBangOperator)) {
1113 if (CurrentToken && CurrentToken->is(tok::less)) {
1114 CurrentToken->setType(TT_TemplateOpener);
1115 skipToNextNonComment();
1116 if (!parseAngle())
1117 return false;
1118 }
1119 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1120 return false;
1121 next();
1122 // FIXME: Hack using inheritance to child context
1123 Contexts.back().IsTableGenBangOpe = true;
1124 bool Result = parseParens();
1125 Contexts.back().IsTableGenBangOpe = false;
1126 return Result;
1127 }
1128 // SimpleValue 9: Cond operator
1129 if (Tok->is(TT_TableGenCondOperator)) {
1130 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1131 return false;
1132 next();
1133 return parseParens();
1134 }
1135 // We have to check identifier at the last because the kind of bang/cond
1136 // operators are also identifier.
1137 // SimpleValue 7: Identifiers
1138 if (Tok->is(tok::identifier)) {
1139 // SimpleValue 8: Anonymous record
1140 if (CurrentToken && CurrentToken->is(tok::less)) {
1141 CurrentToken->setType(TT_TemplateOpener);
1142 skipToNextNonComment();
1143 return parseAngle();
1144 }
1145 return true;
1146 }
1147
1148 return false;
1149 }
1150
1151 bool couldBeInStructArrayInitializer() const {
1152 if (Contexts.size() < 2)
1153 return false;
1154 // We want to back up no more then 2 context levels i.e.
1155 // . { { <-
1156 const auto End = std::next(Contexts.rbegin(), 2);
1157 auto Last = Contexts.rbegin();
1158 unsigned Depth = 0;
1159 for (; Last != End; ++Last)
1160 if (Last->ContextKind == tok::l_brace)
1161 ++Depth;
1162 return Depth == 2 && Last->ContextKind != tok::l_brace;
1163 }
1164
1165 bool parseBrace() {
1166 if (!CurrentToken)
1167 return true;
1168
1169 assert(CurrentToken->Previous);
1170 FormatToken &OpeningBrace = *CurrentToken->Previous;
1171 assert(OpeningBrace.is(tok::l_brace));
1172 OpeningBrace.ParentBracket = Contexts.back().ContextKind;
1173
1174 if (Contexts.back().CaretFound)
1175 OpeningBrace.overwriteFixedType(TT_ObjCBlockLBrace);
1176 Contexts.back().CaretFound = false;
1177
1178 ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
1179 Contexts.back().ColonIsDictLiteral = true;
1180 if (OpeningBrace.is(BK_BracedInit))
1181 Contexts.back().IsExpression = true;
1182 if (Style.isJavaScript() && OpeningBrace.Previous &&
1183 OpeningBrace.Previous->is(TT_JsTypeColon)) {
1184 Contexts.back().IsExpression = false;
1185 }
1186 if (Style.isVerilog() &&
1187 (!OpeningBrace.getPreviousNonComment() ||
1188 OpeningBrace.getPreviousNonComment()->isNot(Keywords.kw_apostrophe))) {
1189 Contexts.back().VerilogMayBeConcatenation = true;
1190 }
1191 if (Style.isTableGen())
1192 Contexts.back().ColonIsDictLiteral = false;
1193
1194 unsigned CommaCount = 0;
1195 while (CurrentToken) {
1196 if (CurrentToken->is(tok::r_brace)) {
1197 assert(!Scopes.empty());
1198 assert(Scopes.back() == getScopeType(OpeningBrace));
1199 Scopes.pop_back();
1200 assert(OpeningBrace.Optional == CurrentToken->Optional);
1201 OpeningBrace.MatchingParen = CurrentToken;
1202 CurrentToken->MatchingParen = &OpeningBrace;
1203 if (Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
1204 if (OpeningBrace.ParentBracket == tok::l_brace &&
1205 couldBeInStructArrayInitializer() && CommaCount > 0) {
1206 Contexts.back().ContextType = Context::StructArrayInitializer;
1207 }
1208 }
1209 next();
1210 return true;
1211 }
1212 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
1213 return false;
1214 updateParameterCount(&OpeningBrace, CurrentToken);
1215 if (CurrentToken->isOneOf(tok::colon, tok::l_brace, tok::less)) {
1216 FormatToken *Previous = CurrentToken->getPreviousNonComment();
1217 if (Previous->is(TT_JsTypeOptionalQuestion))
1218 Previous = Previous->getPreviousNonComment();
1219 if ((CurrentToken->is(tok::colon) && !Style.isTableGen() &&
1220 (!Contexts.back().ColonIsDictLiteral || !IsCpp)) ||
1221 Style.isProto()) {
1222 OpeningBrace.setType(TT_DictLiteral);
1223 if (Previous->Tok.getIdentifierInfo() ||
1224 Previous->is(tok::string_literal)) {
1225 Previous->setType(TT_SelectorName);
1226 }
1227 }
1228 if (CurrentToken->is(tok::colon) && OpeningBrace.is(TT_Unknown) &&
1229 !Style.isTableGen()) {
1230 OpeningBrace.setType(TT_DictLiteral);
1231 } else if (Style.isJavaScript()) {
1232 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1233 }
1234 }
1235 if (CurrentToken->is(tok::comma)) {
1236 if (Style.isJavaScript())
1237 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1238 ++CommaCount;
1239 }
1240 if (!consumeToken())
1241 return false;
1242 }
1243 return true;
1244 }
1245
1246 void updateParameterCount(FormatToken *Left, FormatToken *Current) {
1247 // For ObjC methods, the number of parameters is calculated differently as
1248 // method declarations have a different structure (the parameters are not
1249 // inside a bracket scope).
1250 if (Current->is(tok::l_brace) && Current->is(BK_Block))
1251 ++Left->BlockParameterCount;
1252 if (Current->is(tok::comma)) {
1253 ++Left->ParameterCount;
1254 if (!Left->Role)
1255 Left->Role.reset(new CommaSeparatedList(Style));
1256 Left->Role->CommaFound(Current);
1257 } else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
1258 Left->ParameterCount = 1;
1259 }
1260 }
1261
1262 bool parseConditional() {
1263 while (CurrentToken) {
1264 if (CurrentToken->is(tok::colon) && CurrentToken->is(TT_Unknown)) {
1265 CurrentToken->setType(TT_ConditionalExpr);
1266 next();
1267 return true;
1268 }
1269 if (!consumeToken())
1270 return false;
1271 }
1272 return false;
1273 }
1274
1275 bool parseTemplateDeclaration() {
1276 if (!CurrentToken || CurrentToken->isNot(tok::less))
1277 return false;
1278
1279 CurrentToken->setType(TT_TemplateOpener);
1280 next();
1281
1282 TemplateDeclarationDepth++;
1283 const bool WellFormed = parseAngle();
1284 TemplateDeclarationDepth--;
1285 if (!WellFormed)
1286 return false;
1287
1288 if (CurrentToken && TemplateDeclarationDepth == 0)
1289 CurrentToken->Previous->ClosesTemplateDeclaration = true;
1290
1291 return true;
1292 }
1293
1294 bool consumeToken() {
1295 if (IsCpp) {
1296 const auto *Prev = CurrentToken->getPreviousNonComment();
1297 if (Prev && Prev->is(TT_AttributeRSquare) &&
1298 CurrentToken->isOneOf(tok::kw_if, tok::kw_switch, tok::kw_case,
1299 tok::kw_default, tok::kw_for, tok::kw_while) &&
1300 mustBreakAfterAttributes(*CurrentToken, Style)) {
1301 CurrentToken->MustBreakBefore = true;
1302 }
1303 }
1304 FormatToken *Tok = CurrentToken;
1305 next();
1306 // In Verilog primitives' state tables, `:`, `?`, and `-` aren't normal
1307 // operators.
1308 if (Tok->is(TT_VerilogTableItem))
1309 return true;
1310 // Multi-line string itself is a single annotated token.
1311 if (Tok->is(TT_TableGenMultiLineString))
1312 return true;
1313 auto *Prev = Tok->getPreviousNonComment();
1314 auto *Next = Tok->getNextNonComment();
1315 switch (bool IsIf = false; Tok->Tok.getKind()) {
1316 case tok::plus:
1317 case tok::minus:
1318 if (!Prev && Line.MustBeDeclaration)
1319 Tok->setType(TT_ObjCMethodSpecifier);
1320 break;
1321 case tok::colon:
1322 if (!Prev)
1323 return false;
1324 // Goto labels and case labels are already identified in
1325 // UnwrappedLineParser.
1326 if (Tok->isTypeFinalized())
1327 break;
1328 // Colons from ?: are handled in parseConditional().
1329 if (Style.isJavaScript()) {
1330 if (Contexts.back().ColonIsForRangeExpr || // colon in for loop
1331 (Contexts.size() == 1 && // switch/case labels
1332 Line.First->isNoneOf(tok::kw_enum, tok::kw_case)) ||
1333 Contexts.back().ContextKind == tok::l_paren || // function params
1334 Contexts.back().ContextKind == tok::l_square || // array type
1335 (!Contexts.back().IsExpression &&
1336 Contexts.back().ContextKind == tok::l_brace) || // object type
1337 (Contexts.size() == 1 &&
1338 Line.MustBeDeclaration)) { // method/property declaration
1339 Contexts.back().IsExpression = false;
1340 Tok->setType(TT_JsTypeColon);
1341 break;
1342 }
1343 } else if (Style.isCSharp()) {
1344 if (Contexts.back().InCSharpAttributeSpecifier) {
1345 Tok->setType(TT_AttributeColon);
1346 break;
1347 }
1348 if (Contexts.back().ContextKind == tok::l_paren) {
1349 Tok->setType(TT_CSharpNamedArgumentColon);
1350 break;
1351 }
1352 } else if (Style.isVerilog() && Tok->isNot(TT_BinaryOperator)) {
1353 // The distribution weight operators are labeled
1354 // TT_BinaryOperator by the lexer.
1355 if (Keywords.isVerilogEnd(*Prev) || Keywords.isVerilogBegin(*Prev)) {
1356 Tok->setType(TT_VerilogBlockLabelColon);
1357 } else if (Contexts.back().ContextKind == tok::l_square) {
1358 Tok->setType(TT_BitFieldColon);
1359 } else if (Contexts.back().ColonIsDictLiteral) {
1360 Tok->setType(TT_DictLiteral);
1361 } else if (Contexts.size() == 1) {
1362 // In Verilog a case label doesn't have the case keyword. We
1363 // assume a colon following an expression is a case label.
1364 // Colons from ?: are annotated in parseConditional().
1365 Tok->setType(TT_CaseLabelColon);
1366 if (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))
1367 --Line.Level;
1368 }
1369 break;
1370 }
1371 if (Line.First->isOneOf(Keywords.kw_module, Keywords.kw_import) ||
1372 Line.First->startsSequence(tok::kw_export, Keywords.kw_module) ||
1373 Line.First->startsSequence(tok::kw_export, Keywords.kw_import)) {
1374 Tok->setType(TT_ModulePartitionColon);
1375 } else if (Line.First->is(tok::kw_asm)) {
1376 Tok->setType(TT_InlineASMColon);
1377 } else if (Contexts.back().ColonIsDictLiteral || Style.isProto()) {
1378 Tok->setType(TT_DictLiteral);
1379 if (Style.isTextProto())
1380 Prev->setType(TT_SelectorName);
1381 } else if (Contexts.back().ColonIsObjCMethodExpr ||
1382 Line.startsWith(TT_ObjCMethodSpecifier)) {
1383 Tok->setType(TT_ObjCMethodExpr);
1384 const auto *PrevPrev = Prev->Previous;
1385 // Ensure we tag all identifiers in method declarations as
1386 // TT_SelectorName.
1387 bool UnknownIdentifierInMethodDeclaration =
1388 Line.startsWith(TT_ObjCMethodSpecifier) &&
1389 Prev->is(tok::identifier) && Prev->is(TT_Unknown);
1390 if (!PrevPrev ||
1391 // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
1392 !(PrevPrev->is(TT_CastRParen) ||
1393 (PrevPrev->is(TT_ObjCMethodExpr) && PrevPrev->is(tok::colon))) ||
1394 PrevPrev->is(tok::r_square) ||
1395 Contexts.back().LongestObjCSelectorName == 0 ||
1396 UnknownIdentifierInMethodDeclaration) {
1397 Prev->setType(TT_SelectorName);
1398 if (!Contexts.back().FirstObjCSelectorName)
1399 Contexts.back().FirstObjCSelectorName = Prev;
1400 else if (Prev->ColumnWidth > Contexts.back().LongestObjCSelectorName)
1401 Contexts.back().LongestObjCSelectorName = Prev->ColumnWidth;
1402 Prev->ParameterIndex =
1403 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1404 ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1405 }
1406 } else if (Contexts.back().ColonIsForRangeExpr) {
1407 Tok->setType(TT_RangeBasedForLoopColon);
1408 for (auto *Token = Prev;
1409 Token && Token->isNoneOf(tok::semi, tok::l_paren);
1410 Token = Token->Previous) {
1411 if (Token->isPointerOrReference())
1412 Token->setFinalizedType(TT_PointerOrReference);
1413 }
1414 } else if (Contexts.back().ContextType == Context::C11GenericSelection) {
1415 Tok->setType(TT_GenericSelectionColon);
1416 if (Prev->isPointerOrReference())
1417 Prev->setFinalizedType(TT_PointerOrReference);
1418 } else if ((CurrentToken && CurrentToken->is(tok::numeric_constant)) ||
1419 (Prev->is(TT_StartOfName) && !Scopes.empty() &&
1420 Scopes.back() == ST_Class)) {
1421 Tok->setType(TT_BitFieldColon);
1422 } else if (Contexts.size() == 1 &&
1423 Line.getFirstNonComment()->isNoneOf(tok::kw_enum, tok::kw_case,
1424 tok::kw_default) &&
1425 !Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
1426 if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept) ||
1427 Prev->ClosesRequiresClause) {
1428 Tok->setType(TT_CtorInitializerColon);
1429 } else if (Prev->is(tok::kw_try)) {
1430 // Member initializer list within function try block.
1431 FormatToken *PrevPrev = Prev->getPreviousNonComment();
1432 if (!PrevPrev)
1433 break;
1434 if (PrevPrev && PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept))
1435 Tok->setType(TT_CtorInitializerColon);
1436 } else {
1437 Tok->setType(TT_InheritanceColon);
1438 if (Prev->isAccessSpecifierKeyword())
1439 Line.Type = LT_AccessModifier;
1440 }
1441 } else if (canBeObjCSelectorComponent(*Prev) && Next &&
1442 (Next->isOneOf(tok::r_paren, tok::comma) ||
1443 (canBeObjCSelectorComponent(*Next) && Next->Next &&
1444 Next->Next->is(tok::colon)))) {
1445 // This handles a special macro in ObjC code where selectors including
1446 // the colon are passed as macro arguments.
1447 Tok->setType(TT_ObjCSelector);
1448 }
1449 break;
1450 case tok::pipe:
1451 case tok::amp:
1452 // | and & in declarations/type expressions represent union and
1453 // intersection types, respectively.
1454 if (Style.isJavaScript() && !Contexts.back().IsExpression)
1455 Tok->setType(TT_JsTypeOperator);
1456 break;
1457 case tok::kw_if:
1458 if (Style.isTableGen()) {
1459 // In TableGen it has the form 'if' <value> 'then'.
1460 if (!parseTableGenValue())
1461 return false;
1462 if (CurrentToken && CurrentToken->is(Keywords.kw_then))
1463 next(); // skip then
1464 break;
1465 }
1466 if (CurrentToken &&
1467 CurrentToken->isOneOf(tok::kw_constexpr, tok::identifier)) {
1468 next();
1469 }
1470 IsIf = true;
1471 [[fallthrough]];
1472 case tok::kw_while:
1473 if (CurrentToken && CurrentToken->is(tok::l_paren)) {
1474 next();
1475 if (!parseParens(IsIf))
1476 return false;
1477 }
1478 break;
1479 case tok::kw_for:
1480 if (Style.isJavaScript()) {
1481 // x.for and {for: ...}
1482 if ((Prev && Prev->is(tok::period)) || (Next && Next->is(tok::colon)))
1483 break;
1484 // JS' for await ( ...
1485 if (CurrentToken && CurrentToken->is(Keywords.kw_await))
1486 next();
1487 }
1488 if (IsCpp && CurrentToken && CurrentToken->is(tok::kw_co_await))
1489 next();
1490 Contexts.back().ColonIsForRangeExpr = true;
1491 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1492 return false;
1493 next();
1494 if (!parseParens())
1495 return false;
1496 break;
1497 case tok::l_paren:
1498 // When faced with 'operator()()', the kw_operator handler incorrectly
1499 // marks the first l_paren as a OverloadedOperatorLParen. Here, we make
1500 // the first two parens OverloadedOperators and the second l_paren an
1501 // OverloadedOperatorLParen.
1502 if (Prev && Prev->is(tok::r_paren) && Prev->MatchingParen &&
1503 Prev->MatchingParen->is(TT_OverloadedOperatorLParen)) {
1504 Prev->setType(TT_OverloadedOperator);
1505 Prev->MatchingParen->setType(TT_OverloadedOperator);
1506 Tok->setType(TT_OverloadedOperatorLParen);
1507 }
1508
1509 if (Style.isVerilog()) {
1510 // Identify the parameter list and port list in a module instantiation.
1511 // This is still needed when we already have
1512 // UnwrappedLineParser::parseVerilogHierarchyHeader because that
1513 // function is only responsible for the definition, not the
1514 // instantiation.
1515 auto IsInstancePort = [&]() {
1516 const FormatToken *PrevPrev;
1517 // In the following example all 4 left parentheses will be treated as
1518 // 'TT_VerilogInstancePortLParen'.
1519 //
1520 // module_x instance_1(port_1); // Case A.
1521 // module_x #(parameter_1) // Case B.
1522 // instance_2(port_1), // Case C.
1523 // instance_3(port_1); // Case D.
1524 if (!Prev || !(PrevPrev = Prev->getPreviousNonComment()))
1525 return false;
1526 // Case A.
1527 if (Keywords.isVerilogIdentifier(*Prev) &&
1528 Keywords.isVerilogIdentifier(*PrevPrev)) {
1529 return true;
1530 }
1531 // Case B.
1532 if (Prev->is(Keywords.kw_verilogHash) &&
1533 Keywords.isVerilogIdentifier(*PrevPrev)) {
1534 return true;
1535 }
1536 // Case C.
1537 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::r_paren))
1538 return true;
1539 // Case D.
1540 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::comma)) {
1541 const FormatToken *PrevParen = PrevPrev->getPreviousNonComment();
1542 if (PrevParen && PrevParen->is(tok::r_paren) &&
1543 PrevParen->MatchingParen &&
1544 PrevParen->MatchingParen->is(TT_VerilogInstancePortLParen)) {
1545 return true;
1546 }
1547 }
1548 return false;
1549 };
1550
1551 if (IsInstancePort())
1552 Tok->setType(TT_VerilogInstancePortLParen);
1553 }
1554
1555 if (!parseParens())
1556 return false;
1557 if (Line.MustBeDeclaration && Contexts.size() == 1 &&
1558 !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
1559 !Line.startsWith(tok::l_paren) &&
1560 Tok->isNoneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen)) {
1561 if (!Prev ||
1562 (!Prev->isAttribute() &&
1563 Prev->isNoneOf(TT_RequiresClause, TT_LeadingJavaAnnotation,
1564 TT_BinaryOperator))) {
1565 Line.MightBeFunctionDecl = true;
1566 Tok->MightBeFunctionDeclParen = true;
1567 }
1568 }
1569 break;
1570 case tok::l_square:
1571 if (Style.isTableGen())
1572 Tok->setType(TT_TableGenListOpener);
1573 if (!parseSquare())
1574 return false;
1575 break;
1576 case tok::l_brace:
1577 if (IsCpp) {
1578 if (Tok->is(TT_RequiresExpressionLBrace))
1579 Line.Type = LT_RequiresExpression;
1580 } else if (Style.isTextProto()) {
1581 if (Prev && Prev->isNot(TT_DictLiteral))
1582 Prev->setType(TT_SelectorName);
1583 }
1584 Scopes.push_back(getScopeType(*Tok));
1585 if (!parseBrace())
1586 return false;
1587 break;
1588 case tok::less:
1589 if (parseAngle()) {
1590 Tok->setType(TT_TemplateOpener);
1591 // In TT_Proto, we must distignuish between:
1592 // map<key, value>
1593 // msg < item: data >
1594 // msg: < item: data >
1595 // In TT_TextProto, map<key, value> does not occur.
1596 if (Style.isTextProto() ||
1597 (Style.Language == FormatStyle::LK_Proto && Prev &&
1598 Prev->isOneOf(TT_SelectorName, TT_DictLiteral))) {
1599 Tok->setType(TT_DictLiteral);
1600 if (Prev && Prev->isNot(TT_DictLiteral))
1601 Prev->setType(TT_SelectorName);
1602 }
1603 if (Style.isTableGen())
1604 Tok->setType(TT_TemplateOpener);
1605 } else {
1606 Tok->setType(TT_BinaryOperator);
1607 NonTemplateLess.insert(Tok);
1608 CurrentToken = Tok;
1609 next();
1610 }
1611 break;
1612 case tok::r_paren:
1613 case tok::r_square:
1614 return false;
1615 case tok::r_brace:
1616 // Don't pop scope when encountering unbalanced r_brace.
1617 if (!Scopes.empty())
1618 Scopes.pop_back();
1619 // Lines can start with '}'.
1620 if (Prev)
1621 return false;
1622 break;
1623 case tok::greater:
1624 if (!Style.isTextProto() && Tok->is(TT_Unknown))
1625 Tok->setType(TT_BinaryOperator);
1626 if (Prev && Prev->is(TT_TemplateCloser))
1627 Tok->SpacesRequiredBefore = 1;
1628 break;
1629 case tok::kw_operator:
1630 if (Style.isProto())
1631 break;
1632 // Handle C++ user-defined conversion function.
1633 if (IsCpp && CurrentToken) {
1634 const auto *Info = CurrentToken->Tok.getIdentifierInfo();
1635 // What follows Tok is an identifier or a non-operator keyword.
1636 if (Info && !(CurrentToken->isPlacementOperator() ||
1637 CurrentToken->is(tok::kw_co_await) ||
1638 Info->isCPlusPlusOperatorKeyword())) {
1639 FormatToken *LParen;
1640 if (CurrentToken->startsSequence(tok::kw_decltype, tok::l_paren,
1641 tok::kw_auto, tok::r_paren)) {
1642 // Skip `decltype(auto)`.
1643 LParen = CurrentToken->Next->Next->Next->Next;
1644 } else {
1645 // Skip to l_paren.
1646 for (LParen = CurrentToken->Next;
1647 LParen && LParen->isNot(tok::l_paren); LParen = LParen->Next) {
1648 if (LParen->isPointerOrReference())
1649 LParen->setFinalizedType(TT_PointerOrReference);
1650 }
1651 }
1652 if (LParen && LParen->is(tok::l_paren)) {
1653 if (!Contexts.back().IsExpression) {
1654 Tok->setFinalizedType(TT_FunctionDeclarationName);
1655 LParen->setFinalizedType(TT_FunctionDeclarationLParen);
1656 }
1657 break;
1658 }
1659 }
1660 }
1661 while (CurrentToken &&
1662 CurrentToken->isNoneOf(tok::l_paren, tok::semi, tok::r_paren)) {
1663 if (CurrentToken->isOneOf(tok::star, tok::amp))
1664 CurrentToken->setType(TT_PointerOrReference);
1665 auto Next = CurrentToken->getNextNonComment();
1666 if (!Next)
1667 break;
1668 if (Next->is(tok::less))
1669 next();
1670 else
1671 consumeToken();
1672 if (!CurrentToken)
1673 break;
1674 auto Previous = CurrentToken->getPreviousNonComment();
1675 assert(Previous);
1676 if (CurrentToken->is(tok::comma) && Previous->isNot(tok::kw_operator))
1677 break;
1678 if (Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator, tok::comma,
1679 tok::arrow) ||
1680 Previous->isPointerOrReference() ||
1681 // User defined literal.
1682 Previous->TokenText.starts_with("\"\"")) {
1683 Previous->setType(TT_OverloadedOperator);
1684 if (CurrentToken->isOneOf(tok::less, tok::greater))
1685 break;
1686 }
1687 }
1688 if (CurrentToken && CurrentToken->is(tok::l_paren))
1689 CurrentToken->setType(TT_OverloadedOperatorLParen);
1690 if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator))
1691 CurrentToken->Previous->setType(TT_OverloadedOperator);
1692 break;
1693 case tok::question:
1694 if (Style.isJavaScript() && Next &&
1695 Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
1696 tok::r_brace, tok::r_square)) {
1697 // Question marks before semicolons, colons, etc. indicate optional
1698 // types (fields, parameters), e.g.
1699 // function(x?: string, y?) {...}
1700 // class X { y?; }
1701 Tok->setType(TT_JsTypeOptionalQuestion);
1702 break;
1703 }
1704 // Declarations cannot be conditional expressions, this can only be part
1705 // of a type declaration.
1706 if (Line.MustBeDeclaration && !Contexts.back().IsExpression &&
1707 Style.isJavaScript()) {
1708 break;
1709 }
1710 if (Style.isCSharp()) {
1711 // `Type?)`, `Type?>`, `Type? name;`, and `Type? name =` can only be
1712 // nullable types.
1713 if (Next && (Next->isOneOf(tok::r_paren, tok::greater) ||
1714 Next->startsSequence(tok::identifier, tok::semi) ||
1715 Next->startsSequence(tok::identifier, tok::equal))) {
1716 Tok->setType(TT_CSharpNullable);
1717 break;
1718 }
1719
1720 // Line.MustBeDeclaration will be true for `Type? name;`.
1721 // But not
1722 // cond ? "A" : "B";
1723 // cond ? id : "B";
1724 // cond ? cond2 ? "A" : "B" : "C";
1725 if (!Contexts.back().IsExpression && Line.MustBeDeclaration &&
1726 (!Next || Next->isNoneOf(tok::identifier, tok::string_literal) ||
1727 !Next->Next || Next->Next->isNoneOf(tok::colon, tok::question))) {
1728 Tok->setType(TT_CSharpNullable);
1729 break;
1730 }
1731 }
1732 parseConditional();
1733 break;
1734 case tok::kw_template:
1735 parseTemplateDeclaration();
1736 break;
1737 case tok::comma:
1738 switch (Contexts.back().ContextType) {
1739 case Context::CtorInitializer:
1740 Tok->setType(TT_CtorInitializerComma);
1741 break;
1742 case Context::InheritanceList:
1743 Tok->setType(TT_InheritanceComma);
1744 break;
1745 case Context::VerilogInstancePortList:
1746 Tok->setType(TT_VerilogInstancePortComma);
1747 break;
1748 default:
1749 if (Style.isVerilog() && Contexts.size() == 1 &&
1750 Line.startsWith(Keywords.kw_assign)) {
1751 Tok->setFinalizedType(TT_VerilogAssignComma);
1752 } else if (Contexts.back().FirstStartOfName &&
1753 (Contexts.size() == 1 || startsWithInitStatement(Line))) {
1754 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true;
1755 Line.IsMultiVariableDeclStmt = true;
1756 }
1757 break;
1758 }
1759 if (Contexts.back().ContextType == Context::ForEachMacro)
1760 Contexts.back().IsExpression = true;
1761 break;
1762 case tok::kw_default:
1763 // Unindent case labels.
1764 if (Style.isVerilog() && Keywords.isVerilogEndOfLabel(*Tok) &&
1765 (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))) {
1766 --Line.Level;
1767 }
1768 break;
1769 case tok::identifier:
1770 if (Tok->isOneOf(Keywords.kw___has_include,
1771 Keywords.kw___has_include_next)) {
1772 parseHasInclude();
1773 }
1774 if (IsCpp) {
1775 if (Next && Next->is(tok::l_paren) && Prev &&
1776 Prev->isOneOf(tok::kw___cdecl, tok::kw___stdcall,
1777 tok::kw___fastcall, tok::kw___thiscall,
1778 tok::kw___regcall, tok::kw___vectorcall)) {
1779 Tok->setFinalizedType(TT_FunctionDeclarationName);
1780 Next->setFinalizedType(TT_FunctionDeclarationLParen);
1781 }
1782 } else if (Style.isCSharp()) {
1783 if (Tok->is(Keywords.kw_where) && Next && Next->isNot(tok::l_paren)) {
1784 Tok->setType(TT_CSharpGenericTypeConstraint);
1785 parseCSharpGenericTypeConstraint();
1786 if (!Prev)
1787 Line.IsContinuation = true;
1788 }
1789 } else if (Style.isTableGen()) {
1790 if (Tok->is(Keywords.kw_assert)) {
1791 if (!parseTableGenValue())
1792 return false;
1793 } else if (Tok->isOneOf(Keywords.kw_def, Keywords.kw_defm) &&
1794 (!Next || Next->isNoneOf(tok::colon, tok::l_brace))) {
1795 // The case NameValue appears.
1796 if (!parseTableGenValue(true))
1797 return false;
1798 }
1799 }
1800 if (Style.AllowBreakBeforeQtProperty &&
1801 Contexts.back().ContextType == Context::QtProperty &&
1802 Tok->isQtProperty()) {
1803 Tok->setFinalizedType(TT_QtProperty);
1804 }
1805 break;
1806 case tok::arrow:
1807 if (Tok->isNot(TT_LambdaArrow) && Prev && Prev->is(tok::kw_noexcept))
1808 Tok->setType(TT_TrailingReturnArrow);
1809 break;
1810 case tok::equal:
1811 // In TableGen, there must be a value after "=";
1812 if (Style.isTableGen() && !parseTableGenValue())
1813 return false;
1814 break;
1815 default:
1816 break;
1817 }
1818 return true;
1819 }
1820
1821 void parseCSharpGenericTypeConstraint() {
1822 int OpenAngleBracketsCount = 0;
1823 while (CurrentToken) {
1824 if (CurrentToken->is(tok::less)) {
1825 // parseAngle is too greedy and will consume the whole line.
1826 CurrentToken->setType(TT_TemplateOpener);
1827 ++OpenAngleBracketsCount;
1828 next();
1829 } else if (CurrentToken->is(tok::greater)) {
1830 CurrentToken->setType(TT_TemplateCloser);
1831 --OpenAngleBracketsCount;
1832 next();
1833 } else if (CurrentToken->is(tok::comma) && OpenAngleBracketsCount == 0) {
1834 // We allow line breaks after GenericTypeConstraintComma's
1835 // so do not flag commas in Generics as GenericTypeConstraintComma's.
1836 CurrentToken->setType(TT_CSharpGenericTypeConstraintComma);
1837 next();
1838 } else if (CurrentToken->is(Keywords.kw_where)) {
1839 CurrentToken->setType(TT_CSharpGenericTypeConstraint);
1840 next();
1841 } else if (CurrentToken->is(tok::colon)) {
1842 CurrentToken->setType(TT_CSharpGenericTypeConstraintColon);
1843 next();
1844 } else {
1845 next();
1846 }
1847 }
1848 }
1849
1850 void parseIncludeDirective() {
1851 if (CurrentToken && CurrentToken->is(tok::less)) {
1852 next();
1853 while (CurrentToken) {
1854 // Mark tokens up to the trailing line comments as implicit string
1855 // literals.
1856 if (CurrentToken->isNot(tok::comment) &&
1857 !CurrentToken->TokenText.starts_with("//")) {
1858 CurrentToken->setType(TT_ImplicitStringLiteral);
1859 }
1860 next();
1861 }
1862 }
1863 }
1864
1865 void parseWarningOrError() {
1866 next();
1867 // We still want to format the whitespace left of the first token of the
1868 // warning or error.
1869 next();
1870 while (CurrentToken) {
1871 CurrentToken->setType(TT_ImplicitStringLiteral);
1872 next();
1873 }
1874 }
1875
1876 void parsePragma() {
1877 next(); // Consume "pragma".
1878 if (CurrentToken &&
1879 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option,
1880 Keywords.kw_region)) {
1881 bool IsMarkOrRegion =
1882 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_region);
1883 next();
1884 next(); // Consume first token (so we fix leading whitespace).
1885 while (CurrentToken) {
1886 if (IsMarkOrRegion || CurrentToken->Previous->is(TT_BinaryOperator))
1887 CurrentToken->setType(TT_ImplicitStringLiteral);
1888 next();
1889 }
1890 }
1891 }
1892
1893 void parseHasInclude() {
1894 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1895 return;
1896 next(); // '('
1897 parseIncludeDirective();
1898 next(); // ')'
1899 }
1900
1901 LineType parsePreprocessorDirective() {
1902 bool IsFirstToken = CurrentToken->IsFirst;
1904 next();
1905 if (!CurrentToken)
1906 return Type;
1907
1908 if (Style.isJavaScript() && IsFirstToken) {
1909 // JavaScript files can contain shebang lines of the form:
1910 // #!/usr/bin/env node
1911 // Treat these like C++ #include directives.
1912 while (CurrentToken) {
1913 // Tokens cannot be comments here.
1914 CurrentToken->setType(TT_ImplicitStringLiteral);
1915 next();
1916 }
1917 return LT_ImportStatement;
1918 }
1919
1920 if (CurrentToken->is(tok::numeric_constant)) {
1921 CurrentToken->SpacesRequiredBefore = 1;
1922 return Type;
1923 }
1924 // Hashes in the middle of a line can lead to any strange token
1925 // sequence.
1926 if (!CurrentToken->Tok.getIdentifierInfo())
1927 return Type;
1928 // In Verilog macro expansions start with a backtick just like preprocessor
1929 // directives. Thus we stop if the word is not a preprocessor directive.
1930 if (Style.isVerilog() && !Keywords.isVerilogPPDirective(*CurrentToken))
1931 return LT_Invalid;
1932 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1933 case tok::pp_include:
1934 case tok::pp_include_next:
1935 case tok::pp_import:
1936 next();
1937 parseIncludeDirective();
1939 break;
1940 case tok::pp_error:
1941 case tok::pp_warning:
1942 parseWarningOrError();
1943 break;
1944 case tok::pp_pragma:
1945 parsePragma();
1946 break;
1947 case tok::pp_if:
1948 case tok::pp_elif:
1949 Contexts.back().IsExpression = true;
1950 next();
1951 if (CurrentToken)
1952 CurrentToken->SpacesRequiredBefore = 1;
1953 parseLine();
1954 break;
1955 default:
1956 break;
1957 }
1958 while (CurrentToken) {
1959 FormatToken *Tok = CurrentToken;
1960 next();
1961 if (Tok->is(tok::l_paren)) {
1962 parseParens();
1963 } else if (Tok->isOneOf(Keywords.kw___has_include,
1964 Keywords.kw___has_include_next)) {
1965 parseHasInclude();
1966 }
1967 }
1968 return Type;
1969 }
1970
1971public:
1972 LineType parseLine() {
1973 if (!CurrentToken)
1974 return LT_Invalid;
1975 NonTemplateLess.clear();
1976 if (!Line.InMacroBody && CurrentToken->is(tok::hash)) {
1977 // We were not yet allowed to use C++17 optional when this was being
1978 // written. So we used LT_Invalid to mark that the line is not a
1979 // preprocessor directive.
1980 auto Type = parsePreprocessorDirective();
1981 if (Type != LT_Invalid)
1982 return Type;
1983 }
1984
1985 // Directly allow to 'import <string-literal>' to support protocol buffer
1986 // definitions (github.com/google/protobuf) or missing "#" (either way we
1987 // should not break the line).
1988 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
1989 if ((Style.isJava() && CurrentToken->is(Keywords.kw_package)) ||
1990 (!Style.isVerilog() && Info &&
1991 Info->getPPKeywordID() == tok::pp_import && CurrentToken->Next &&
1992 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
1993 tok::kw_static))) {
1994 next();
1995 parseIncludeDirective();
1996 return LT_ImportStatement;
1997 }
1998
1999 // If this line starts and ends in '<' and '>', respectively, it is likely
2000 // part of "#define <a/b.h>".
2001 if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
2002 parseIncludeDirective();
2003 return LT_ImportStatement;
2004 }
2005
2006 // In .proto files, top-level options and package statements are very
2007 // similar to import statements and should not be line-wrapped.
2008 if (Style.Language == FormatStyle::LK_Proto && Line.Level == 0 &&
2009 CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) {
2010 next();
2011 if (CurrentToken && CurrentToken->is(tok::identifier)) {
2012 while (CurrentToken)
2013 next();
2014 return LT_ImportStatement;
2015 }
2016 }
2017
2018 bool KeywordVirtualFound = false;
2019 bool ImportStatement = false;
2020
2021 // import {...} from '...';
2022 if (Style.isJavaScript() && CurrentToken->is(Keywords.kw_import))
2023 ImportStatement = true;
2024
2025 while (CurrentToken) {
2026 if (CurrentToken->is(tok::kw_virtual))
2027 KeywordVirtualFound = true;
2028 if (Style.isJavaScript()) {
2029 // export {...} from '...';
2030 // An export followed by "from 'some string';" is a re-export from
2031 // another module identified by a URI and is treated as a
2032 // LT_ImportStatement (i.e. prevent wraps on it for long URIs).
2033 // Just "export {...};" or "export class ..." should not be treated as
2034 // an import in this sense.
2035 if (Line.First->is(tok::kw_export) &&
2036 CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
2037 CurrentToken->Next->isStringLiteral()) {
2038 ImportStatement = true;
2039 }
2040 if (isClosureImportStatement(*CurrentToken))
2041 ImportStatement = true;
2042 }
2043 if (!consumeToken())
2044 return LT_Invalid;
2045 }
2046 if (const auto Type = Line.Type; Type == LT_AccessModifier ||
2049 return Type;
2050 }
2051 if (KeywordVirtualFound)
2053 if (ImportStatement)
2054 return LT_ImportStatement;
2055
2056 if (Line.startsWith(TT_ObjCMethodSpecifier)) {
2057 if (Contexts.back().FirstObjCSelectorName) {
2058 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
2059 Contexts.back().LongestObjCSelectorName;
2060 }
2061 return LT_ObjCMethodDecl;
2062 }
2063
2064 for (const auto &ctx : Contexts)
2065 if (ctx.ContextType == Context::StructArrayInitializer)
2067
2068 return LT_Other;
2069 }
2070
2071private:
2072 bool isClosureImportStatement(const FormatToken &Tok) {
2073 // FIXME: Closure-library specific stuff should not be hard-coded but be
2074 // configurable.
2075 return Tok.TokenText == "goog" && Tok.Next && Tok.Next->is(tok::period) &&
2076 Tok.Next->Next &&
2077 (Tok.Next->Next->TokenText == "module" ||
2078 Tok.Next->Next->TokenText == "provide" ||
2079 Tok.Next->Next->TokenText == "require" ||
2080 Tok.Next->Next->TokenText == "requireType" ||
2081 Tok.Next->Next->TokenText == "forwardDeclare") &&
2082 Tok.Next->Next->Next && Tok.Next->Next->Next->is(tok::l_paren);
2083 }
2084
2085 void resetTokenMetadata() {
2086 if (!CurrentToken)
2087 return;
2088
2089 // Reset token type in case we have already looked at it and then
2090 // recovered from an error (e.g. failure to find the matching >).
2091 if (!CurrentToken->isTypeFinalized() &&
2092 CurrentToken->isNoneOf(
2093 TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
2094 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
2095 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
2096 TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator,
2097 TT_RegexLiteral, TT_TemplateString, TT_ObjCStringLiteral,
2098 TT_UntouchableMacroFunc, TT_StatementAttributeLikeMacro,
2099 TT_FunctionLikeOrFreestandingMacro, TT_ClassLBrace, TT_EnumLBrace,
2100 TT_RecordLBrace, TT_StructLBrace, TT_UnionLBrace, TT_RequiresClause,
2101 TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
2102 TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
2103 TT_CompoundRequirementLBrace, TT_BracedListLBrace,
2104 TT_FunctionLikeMacro)) {
2105 CurrentToken->setType(TT_Unknown);
2106 }
2107 CurrentToken->Role.reset();
2108 CurrentToken->MatchingParen = nullptr;
2109 CurrentToken->FakeLParens.clear();
2110 CurrentToken->FakeRParens = 0;
2111 }
2112
2113 void next() {
2114 if (!CurrentToken)
2115 return;
2116
2117 CurrentToken->NestingLevel = Contexts.size() - 1;
2118 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
2119 modifyContext(*CurrentToken);
2120 determineTokenType(*CurrentToken);
2121 CurrentToken = CurrentToken->Next;
2122
2123 resetTokenMetadata();
2124 }
2125
2126 /// A struct to hold information valid in a specific context, e.g.
2127 /// a pair of parenthesis.
2128 struct Context {
2129 Context(tok::TokenKind ContextKind, unsigned BindingStrength,
2130 bool IsExpression)
2131 : ContextKind(ContextKind), BindingStrength(BindingStrength),
2132 IsExpression(IsExpression) {}
2133
2134 tok::TokenKind ContextKind;
2135 unsigned BindingStrength;
2136 bool IsExpression;
2137 unsigned LongestObjCSelectorName = 0;
2138 bool ColonIsForRangeExpr = false;
2139 bool ColonIsDictLiteral = false;
2140 bool ColonIsObjCMethodExpr = false;
2141 FormatToken *FirstObjCSelectorName = nullptr;
2142 FormatToken *FirstStartOfName = nullptr;
2143 bool CanBeExpression = true;
2144 bool CaretFound = false;
2145 bool InCpp11AttributeSpecifier = false;
2146 bool InCSharpAttributeSpecifier = false;
2147 bool VerilogAssignmentFound = false;
2148 // Whether the braces may mean concatenation instead of structure or array
2149 // literal.
2150 bool VerilogMayBeConcatenation = false;
2151 bool IsTableGenDAGArgList = false;
2152 bool IsTableGenBangOpe = false;
2153 bool IsTableGenCondOpe = false;
2154 enum {
2155 Unknown,
2156 // Like the part after `:` in a constructor.
2157 // Context(...) : IsExpression(IsExpression)
2158 CtorInitializer,
2159 // Like in the parentheses in a foreach.
2160 ForEachMacro,
2161 // Like the inheritance list in a class declaration.
2162 // class Input : public IO
2163 InheritanceList,
2164 // Like in the braced list.
2165 // int x[] = {};
2166 StructArrayInitializer,
2167 // Like in `static_cast<int>`.
2168 TemplateArgument,
2169 // C11 _Generic selection.
2170 C11GenericSelection,
2171 QtProperty,
2172 // Like in the outer parentheses in `ffnand ff1(.q());`.
2173 VerilogInstancePortList,
2174 } ContextType = Unknown;
2175 };
2176
2177 /// Puts a new \c Context onto the stack \c Contexts for the lifetime
2178 /// of each instance.
2179 struct ScopedContextCreator {
2180 AnnotatingParser &P;
2181
2182 ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind,
2183 unsigned Increase)
2184 : P(P) {
2185 P.Contexts.push_back(Context(ContextKind,
2186 P.Contexts.back().BindingStrength + Increase,
2187 P.Contexts.back().IsExpression));
2188 }
2189
2190 ~ScopedContextCreator() {
2191 if (P.Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
2192 if (P.Contexts.back().ContextType == Context::StructArrayInitializer) {
2193 P.Contexts.pop_back();
2194 P.Contexts.back().ContextType = Context::StructArrayInitializer;
2195 return;
2196 }
2197 }
2198 P.Contexts.pop_back();
2199 }
2200 };
2201
2202 void modifyContext(const FormatToken &Current) {
2203 auto AssignmentStartsExpression = [&]() {
2204 if (Current.getPrecedence() != prec::Assignment)
2205 return false;
2206
2207 if (Line.First->isOneOf(tok::kw_using, tok::kw_return))
2208 return false;
2209 if (Line.First->is(tok::kw_template)) {
2210 assert(Current.Previous);
2211 if (Current.Previous->is(tok::kw_operator)) {
2212 // `template ... operator=` cannot be an expression.
2213 return false;
2214 }
2215
2216 // `template` keyword can start a variable template.
2217 const FormatToken *Tok = Line.First->getNextNonComment();
2218 assert(Tok); // Current token is on the same line.
2219 if (Tok->isNot(TT_TemplateOpener)) {
2220 // Explicit template instantiations do not have `<>`.
2221 return false;
2222 }
2223
2224 // This is the default value of a template parameter, determine if it's
2225 // type or non-type.
2226 if (Contexts.back().ContextKind == tok::less) {
2227 assert(Current.Previous->Previous);
2228 return Current.Previous->Previous->isNoneOf(tok::kw_typename,
2229 tok::kw_class);
2230 }
2231
2232 Tok = Tok->MatchingParen;
2233 if (!Tok)
2234 return false;
2235 Tok = Tok->getNextNonComment();
2236 if (!Tok)
2237 return false;
2238
2239 if (Tok->isOneOf(tok::kw_class, tok::kw_enum, tok::kw_struct,
2240 tok::kw_using)) {
2241 return false;
2242 }
2243
2244 return true;
2245 }
2246
2247 // Type aliases use `type X = ...;` in TypeScript and can be exported
2248 // using `export type ...`.
2249 if (Style.isJavaScript() &&
2250 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
2251 Line.startsWith(tok::kw_export, Keywords.kw_type,
2252 tok::identifier))) {
2253 return false;
2254 }
2255
2256 return !Current.Previous || Current.Previous->isNot(tok::kw_operator);
2257 };
2258
2259 if (AssignmentStartsExpression()) {
2260 Contexts.back().IsExpression = true;
2261 if (!Line.startsWith(TT_UnaryOperator)) {
2262 for (FormatToken *Previous = Current.Previous;
2263 Previous && Previous->Previous &&
2264 Previous->Previous->isNoneOf(tok::comma, tok::semi);
2265 Previous = Previous->Previous) {
2266 if (Previous->isOneOf(tok::r_square, tok::r_paren, tok::greater)) {
2267 Previous = Previous->MatchingParen;
2268 if (!Previous)
2269 break;
2270 }
2271 if (Previous->opensScope())
2272 break;
2273 if (Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
2274 Previous->isPointerOrReference() && Previous->Previous &&
2275 Previous->Previous->isNot(tok::equal)) {
2276 Previous->setType(TT_PointerOrReference);
2277 }
2278 }
2279 }
2280 } else if (Current.is(tok::lessless) &&
2281 (!Current.Previous ||
2282 Current.Previous->isNot(tok::kw_operator))) {
2283 Contexts.back().IsExpression = true;
2284 } else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
2285 Contexts.back().IsExpression = true;
2286 } else if (Current.is(TT_TrailingReturnArrow)) {
2287 Contexts.back().IsExpression = false;
2288 } else if (Current.isOneOf(TT_LambdaArrow, Keywords.kw_assert)) {
2289 Contexts.back().IsExpression = Style.isJava();
2290 } else if (Current.Previous &&
2291 Current.Previous->is(TT_CtorInitializerColon)) {
2292 Contexts.back().IsExpression = true;
2293 Contexts.back().ContextType = Context::CtorInitializer;
2294 } else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
2295 Contexts.back().ContextType = Context::InheritanceList;
2296 } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
2297 for (FormatToken *Previous = Current.Previous;
2298 Previous && Previous->isOneOf(tok::star, tok::amp);
2299 Previous = Previous->Previous) {
2300 Previous->setType(TT_PointerOrReference);
2301 }
2302 if (Line.MustBeDeclaration &&
2303 Contexts.front().ContextType != Context::CtorInitializer) {
2304 Contexts.back().IsExpression = false;
2305 }
2306 } else if (Current.is(tok::kw_new)) {
2307 Contexts.back().CanBeExpression = false;
2308 } else if (Current.is(tok::semi) ||
2309 (Current.is(tok::exclaim) && Current.Previous &&
2310 Current.Previous->isNot(tok::kw_operator))) {
2311 // This should be the condition or increment in a for-loop.
2312 // But not operator !() (can't use TT_OverloadedOperator here as its not
2313 // been annotated yet).
2314 Contexts.back().IsExpression = true;
2315 }
2316 }
2317
2318 static FormatToken *untilMatchingParen(FormatToken *Current) {
2319 // Used when `MatchingParen` is not yet established.
2320 int ParenLevel = 0;
2321 while (Current) {
2322 if (Current->is(tok::l_paren))
2323 ++ParenLevel;
2324 if (Current->is(tok::r_paren))
2325 --ParenLevel;
2326 if (ParenLevel < 1)
2327 break;
2328 Current = Current->Next;
2329 }
2330 return Current;
2331 }
2332
2333 static bool isDeductionGuide(FormatToken &Current) {
2334 // Look for a deduction guide template<T> A(...) -> A<...>;
2335 if (Current.Previous && Current.Previous->is(tok::r_paren) &&
2336 Current.startsSequence(tok::arrow, tok::identifier, tok::less)) {
2337 // Find the TemplateCloser.
2338 FormatToken *TemplateCloser = Current.Next->Next;
2339 int NestingLevel = 0;
2340 while (TemplateCloser) {
2341 // Skip over an expressions in parens A<(3 < 2)>;
2342 if (TemplateCloser->is(tok::l_paren)) {
2343 // No Matching Paren yet so skip to matching paren
2344 TemplateCloser = untilMatchingParen(TemplateCloser);
2345 if (!TemplateCloser)
2346 break;
2347 }
2348 if (TemplateCloser->is(tok::less))
2349 ++NestingLevel;
2350 if (TemplateCloser->is(tok::greater))
2351 --NestingLevel;
2352 if (NestingLevel < 1)
2353 break;
2354 TemplateCloser = TemplateCloser->Next;
2355 }
2356 // Assuming we have found the end of the template ensure its followed
2357 // with a semi-colon.
2358 if (TemplateCloser && TemplateCloser->Next &&
2359 TemplateCloser->Next->is(tok::semi) &&
2360 Current.Previous->MatchingParen) {
2361 // Determine if the identifier `A` prior to the A<..>; is the same as
2362 // prior to the A(..)
2363 FormatToken *LeadingIdentifier =
2364 Current.Previous->MatchingParen->Previous;
2365
2366 return LeadingIdentifier &&
2367 LeadingIdentifier->TokenText == Current.Next->TokenText;
2368 }
2369 }
2370 return false;
2371 }
2372
2373 void determineTokenType(FormatToken &Current) {
2374 if (Current.isNot(TT_Unknown)) {
2375 // The token type is already known.
2376 return;
2377 }
2378
2379 if ((Style.isJavaScript() || Style.isCSharp()) &&
2380 Current.is(tok::exclaim)) {
2381 if (Current.Previous) {
2382 bool IsIdentifier =
2383 Style.isJavaScript()
2384 ? Keywords.isJavaScriptIdentifier(
2385 *Current.Previous, /* AcceptIdentifierName= */ true)
2386 : Current.Previous->is(tok::identifier);
2387 if (IsIdentifier ||
2388 Current.Previous->isOneOf(
2389 tok::kw_default, tok::kw_namespace, tok::r_paren, tok::r_square,
2390 tok::r_brace, tok::kw_false, tok::kw_true, Keywords.kw_type,
2391 Keywords.kw_get, Keywords.kw_init, Keywords.kw_set) ||
2392 Current.Previous->Tok.isLiteral()) {
2393 Current.setType(TT_NonNullAssertion);
2394 return;
2395 }
2396 }
2397 if (Current.Next &&
2398 Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
2399 Current.setType(TT_NonNullAssertion);
2400 return;
2401 }
2402 }
2403
2404 // Line.MightBeFunctionDecl can only be true after the parentheses of a
2405 // function declaration have been found. In this case, 'Current' is a
2406 // trailing token of this declaration and thus cannot be a name.
2407 if ((Style.isJavaScript() || Style.isJava()) &&
2408 Current.is(Keywords.kw_instanceof)) {
2409 Current.setType(TT_BinaryOperator);
2410 } else if (isStartOfName(Current) &&
2411 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
2412 Contexts.back().FirstStartOfName = &Current;
2413 Current.setType(TT_StartOfName);
2414 } else if (Current.is(tok::semi)) {
2415 // Reset FirstStartOfName after finding a semicolon so that a for loop
2416 // with multiple increment statements is not confused with a for loop
2417 // having multiple variable declarations.
2418 Contexts.back().FirstStartOfName = nullptr;
2419 } else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
2420 AutoFound = true;
2421 } else if (Current.is(tok::arrow) && Style.isJava()) {
2422 Current.setType(TT_LambdaArrow);
2423 } else if (Current.is(tok::arrow) && Style.isVerilog()) {
2424 // The implication operator.
2425 Current.setType(TT_BinaryOperator);
2426 } else if (Current.is(tok::arrow) && AutoFound &&
2427 Line.MightBeFunctionDecl && Current.NestingLevel == 0 &&
2428 Current.Previous->isNoneOf(tok::kw_operator, tok::identifier)) {
2429 // not auto operator->() -> xxx;
2430 Current.setType(TT_TrailingReturnArrow);
2431 } else if (Current.is(tok::arrow) && Current.Previous &&
2432 Current.Previous->is(tok::r_brace) &&
2433 Current.Previous->is(BK_Block)) {
2434 // Concept implicit conversion constraint needs to be treated like
2435 // a trailing return type ... } -> <type>.
2436 Current.setType(TT_TrailingReturnArrow);
2437 } else if (isDeductionGuide(Current)) {
2438 // Deduction guides trailing arrow " A(...) -> A<T>;".
2439 Current.setType(TT_TrailingReturnArrow);
2440 } else if (Current.isPointerOrReference()) {
2441 Current.setType(determineStarAmpUsage(
2442 Current,
2443 Contexts.back().CanBeExpression && Contexts.back().IsExpression,
2444 Contexts.back().ContextType == Context::TemplateArgument));
2445 } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret) ||
2446 (Style.isVerilog() && Current.is(tok::pipe))) {
2447 Current.setType(determinePlusMinusCaretUsage(Current));
2448 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
2449 Contexts.back().CaretFound = true;
2450 } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
2451 Current.setType(determineIncrementUsage(Current));
2452 } else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
2453 Current.setType(TT_UnaryOperator);
2454 } else if (Current.is(tok::question)) {
2455 if (Style.isJavaScript() && Line.MustBeDeclaration &&
2456 !Contexts.back().IsExpression) {
2457 // In JavaScript, `interface X { foo?(): bar; }` is an optional method
2458 // on the interface, not a ternary expression.
2459 Current.setType(TT_JsTypeOptionalQuestion);
2460 } else if (Style.isTableGen()) {
2461 // In TableGen, '?' is just an identifier like token.
2462 Current.setType(TT_Unknown);
2463 } else {
2464 Current.setType(TT_ConditionalExpr);
2465 }
2466 } else if (Current.isBinaryOperator() &&
2467 (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
2468 (Current.isNot(tok::greater) && !Style.isTextProto())) {
2469 if (Style.isVerilog()) {
2470 if (Current.is(tok::lessequal) && Contexts.size() == 1 &&
2471 !Contexts.back().VerilogAssignmentFound) {
2472 // In Verilog `<=` is assignment if in its own statement. It is a
2473 // statement instead of an expression, that is it can not be chained.
2474 Current.ForcedPrecedence = prec::Assignment;
2475 Current.setFinalizedType(TT_BinaryOperator);
2476 }
2477 if (Current.getPrecedence() == prec::Assignment)
2478 Contexts.back().VerilogAssignmentFound = true;
2479 }
2480 Current.setType(TT_BinaryOperator);
2481 } else if (Current.is(tok::comment)) {
2482 if (Current.TokenText.starts_with("/*")) {
2483 if (Current.TokenText.ends_with("*/")) {
2484 Current.setType(TT_BlockComment);
2485 } else {
2486 // The lexer has for some reason determined a comment here. But we
2487 // cannot really handle it, if it isn't properly terminated.
2488 Current.Tok.setKind(tok::unknown);
2489 }
2490 } else {
2491 Current.setType(TT_LineComment);
2492 }
2493 } else if (Current.is(tok::string_literal)) {
2494 if (Style.isVerilog() && Contexts.back().VerilogMayBeConcatenation &&
2495 Current.getPreviousNonComment() &&
2496 Current.getPreviousNonComment()->isOneOf(tok::comma, tok::l_brace) &&
2497 Current.getNextNonComment() &&
2498 Current.getNextNonComment()->isOneOf(tok::comma, tok::r_brace)) {
2499 Current.setType(TT_StringInConcatenation);
2500 }
2501 } else if (Current.is(tok::l_paren)) {
2502 if (lParenStartsCppCast(Current))
2503 Current.setType(TT_CppCastLParen);
2504 } else if (Current.is(tok::r_paren)) {
2505 if (rParenEndsCast(Current))
2506 Current.setType(TT_CastRParen);
2507 if (Current.MatchingParen && Current.Next &&
2508 !Current.Next->isBinaryOperator() &&
2509 Current.Next->isNoneOf(
2510 tok::semi, tok::colon, tok::l_brace, tok::l_paren, tok::comma,
2511 tok::period, tok::arrow, tok::coloncolon, tok::kw_noexcept)) {
2512 if (FormatToken *AfterParen = Current.MatchingParen->Next;
2513 AfterParen && AfterParen->isNot(tok::caret)) {
2514 // Make sure this isn't the return type of an Obj-C block declaration.
2515 if (FormatToken *BeforeParen = Current.MatchingParen->Previous;
2516 BeforeParen && BeforeParen->is(tok::identifier) &&
2517 BeforeParen->isNot(TT_TypenameMacro) &&
2518 BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
2519 (!BeforeParen->Previous ||
2520 BeforeParen->Previous->ClosesTemplateDeclaration ||
2521 BeforeParen->Previous->ClosesRequiresClause)) {
2522 Current.setType(TT_FunctionAnnotationRParen);
2523 }
2524 }
2525 }
2526 } else if (Current.is(tok::at) && Current.Next && !Style.isJavaScript() &&
2527 !Style.isJava()) {
2528 // In Java & JavaScript, "@..." is a decorator or annotation. In ObjC, it
2529 // marks declarations and properties that need special formatting.
2530 switch (Current.Next->Tok.getObjCKeywordID()) {
2531 case tok::objc_interface:
2532 case tok::objc_implementation:
2533 case tok::objc_protocol:
2534 Current.setType(TT_ObjCDecl);
2535 break;
2536 case tok::objc_property:
2537 Current.setType(TT_ObjCProperty);
2538 break;
2539 default:
2540 break;
2541 }
2542 } else if (Current.is(tok::period)) {
2543 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
2544 if (PreviousNoComment &&
2545 PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) {
2546 Current.setType(TT_DesignatedInitializerPeriod);
2547 } else if (Style.isJava() && Current.Previous &&
2548 Current.Previous->isOneOf(TT_JavaAnnotation,
2549 TT_LeadingJavaAnnotation)) {
2550 Current.setType(Current.Previous->getType());
2551 }
2552 } else if (canBeObjCSelectorComponent(Current) &&
2553 // FIXME(bug 36976): ObjC return types shouldn't use
2554 // TT_CastRParen.
2555 Current.Previous && Current.Previous->is(TT_CastRParen) &&
2556 Current.Previous->MatchingParen &&
2557 Current.Previous->MatchingParen->Previous &&
2558 Current.Previous->MatchingParen->Previous->is(
2559 TT_ObjCMethodSpecifier)) {
2560 // This is the first part of an Objective-C selector name. (If there's no
2561 // colon after this, this is the only place which annotates the identifier
2562 // as a selector.)
2563 Current.setType(TT_SelectorName);
2564 } else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
2565 tok::kw_requires) &&
2566 Current.Previous &&
2567 Current.Previous->isNoneOf(tok::equal, tok::at,
2568 TT_CtorInitializerComma,
2569 TT_CtorInitializerColon) &&
2570 Line.MightBeFunctionDecl && Contexts.size() == 1) {
2571 // Line.MightBeFunctionDecl can only be true after the parentheses of a
2572 // function declaration have been found.
2573 Current.setType(TT_TrailingAnnotation);
2574 } else if ((Style.isJava() || Style.isJavaScript()) && Current.Previous) {
2575 if (Current.Previous->is(tok::at) &&
2576 Current.isNot(Keywords.kw_interface)) {
2577 const FormatToken &AtToken = *Current.Previous;
2578 const FormatToken *Previous = AtToken.getPreviousNonComment();
2579 if (!Previous || Previous->is(TT_LeadingJavaAnnotation))
2580 Current.setType(TT_LeadingJavaAnnotation);
2581 else
2582 Current.setType(TT_JavaAnnotation);
2583 } else if (Current.Previous->is(tok::period) &&
2584 Current.Previous->isOneOf(TT_JavaAnnotation,
2585 TT_LeadingJavaAnnotation)) {
2586 Current.setType(Current.Previous->getType());
2587 }
2588 }
2589 }
2590
2591 /// Take a guess at whether \p Tok starts a name of a function or
2592 /// variable declaration.
2593 ///
2594 /// This is a heuristic based on whether \p Tok is an identifier following
2595 /// something that is likely a type.
2596 bool isStartOfName(const FormatToken &Tok) {
2597 // Handled in ExpressionParser for Verilog.
2598 if (Style.isVerilog())
2599 return false;
2600
2601 if (!Tok.Previous || Tok.isNot(tok::identifier) || Tok.is(TT_ClassHeadName))
2602 return false;
2603
2604 if (Tok.endsSequence(Keywords.kw_final, TT_ClassHeadName))
2605 return false;
2606
2607 if ((Style.isJavaScript() || Style.isJava()) && Tok.is(Keywords.kw_extends))
2608 return false;
2609
2610 if (const auto *NextNonComment = Tok.getNextNonComment();
2611 (!NextNonComment && !Line.InMacroBody) ||
2612 (NextNonComment &&
2613 (NextNonComment->isPointerOrReference() ||
2614 NextNonComment->isOneOf(TT_ClassHeadName, tok::string_literal) ||
2615 (Line.InPragmaDirective && NextNonComment->is(tok::identifier))))) {
2616 return false;
2617 }
2618
2619 if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
2620 Keywords.kw_as)) {
2621 return false;
2622 }
2623 if (Style.isJavaScript() && Tok.Previous->is(Keywords.kw_in))
2624 return false;
2625
2626 // Skip "const" as it does not have an influence on whether this is a name.
2627 FormatToken *PreviousNotConst = Tok.getPreviousNonComment();
2628
2629 // For javascript const can be like "let" or "var"
2630 if (!Style.isJavaScript())
2631 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
2632 PreviousNotConst = PreviousNotConst->getPreviousNonComment();
2633
2634 if (!PreviousNotConst)
2635 return false;
2636
2637 if (PreviousNotConst->ClosesRequiresClause)
2638 return false;
2639
2640 if (Style.isTableGen()) {
2641 // keywords such as let and def* defines names.
2642 if (Keywords.isTableGenDefinition(*PreviousNotConst))
2643 return true;
2644 // Otherwise C++ style declarations is available only inside the brace.
2645 if (Contexts.back().ContextKind != tok::l_brace)
2646 return false;
2647 }
2648
2649 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
2650 PreviousNotConst->Previous &&
2651 PreviousNotConst->Previous->is(tok::hash);
2652
2653 if (PreviousNotConst->is(TT_TemplateCloser)) {
2654 return PreviousNotConst && PreviousNotConst->MatchingParen &&
2655 PreviousNotConst->MatchingParen->Previous &&
2656 PreviousNotConst->MatchingParen->Previous->isNoneOf(
2657 tok::period, tok::kw_template);
2658 }
2659
2660 if ((PreviousNotConst->is(tok::r_paren) &&
2661 PreviousNotConst->is(TT_TypeDeclarationParen)) ||
2662 PreviousNotConst->is(TT_AttributeRParen)) {
2663 return true;
2664 }
2665
2666 // If is a preprocess keyword like #define.
2667 if (IsPPKeyword)
2668 return false;
2669
2670 // int a or auto a.
2671 if (PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto) &&
2672 PreviousNotConst->isNot(TT_StatementAttributeLikeMacro)) {
2673 return true;
2674 }
2675
2676 // *a or &a or &&a.
2677 if (PreviousNotConst->is(TT_PointerOrReference) ||
2678 PreviousNotConst->endsSequence(tok::coloncolon,
2679 TT_PointerOrReference)) {
2680 return true;
2681 }
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_AttributeRSquare)) {
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(TT_AttributeLSquare)) {
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.isNoneOf(tok::identifier, tok::kw_operator))
3798 return false;
3799
3800 const auto *Prev = Current.getPreviousNonComment();
3801 assert(Prev);
3802
3803 const auto &Previous = *Prev;
3804
3805 if (const auto *PrevPrev = Previous.getPreviousNonComment();
3806 PrevPrev && PrevPrev->is(TT_ObjCDecl)) {
3807 return false;
3808 }
3809
3810 auto skipOperatorName =
3811 [&LangOpts](const FormatToken *Next) -> const FormatToken * {
3812 for (; Next; Next = Next->Next) {
3813 if (Next->is(TT_OverloadedOperatorLParen))
3814 return Next;
3815 if (Next->is(TT_OverloadedOperator))
3816 continue;
3817 if (Next->isPlacementOperator() || Next->is(tok::kw_co_await)) {
3818 // For 'new[]' and 'delete[]'.
3819 if (Next->Next &&
3820 Next->Next->startsSequence(tok::l_square, tok::r_square)) {
3821 Next = Next->Next->Next;
3822 }
3823 continue;
3824 }
3825 if (Next->startsSequence(tok::l_square, tok::r_square)) {
3826 // For operator[]().
3827 Next = Next->Next;
3828 continue;
3829 }
3830 if ((Next->isTypeName(LangOpts) || Next->is(tok::identifier)) &&
3831 Next->Next && Next->Next->isPointerOrReference()) {
3832 // For operator void*(), operator char*(), operator Foo*().
3833 Next = Next->Next;
3834 continue;
3835 }
3836 if (Next->is(TT_TemplateOpener) && Next->MatchingParen) {
3837 Next = Next->MatchingParen;
3838 continue;
3839 }
3840
3841 break;
3842 }
3843 return nullptr;
3844 };
3845
3846 const auto *Next = Current.Next;
3847 const bool IsCpp = LangOpts.CXXOperatorNames || LangOpts.C11;
3848
3849 // Find parentheses of parameter list.
3850 if (Current.is(tok::kw_operator)) {
3851 if (Line.startsWith(tok::kw_friend))
3852 return true;
3853 if (Previous.Tok.getIdentifierInfo() &&
3854 Previous.isNoneOf(tok::kw_return, tok::kw_co_return)) {
3855 return true;
3856 }
3857 if (Previous.is(tok::r_paren) && Previous.is(TT_TypeDeclarationParen)) {
3858 assert(Previous.MatchingParen);
3859 assert(Previous.MatchingParen->is(tok::l_paren));
3860 assert(Previous.MatchingParen->is(TT_TypeDeclarationParen));
3861 return true;
3862 }
3863 if (!Previous.isPointerOrReference() && Previous.isNot(TT_TemplateCloser))
3864 return false;
3865 Next = skipOperatorName(Next);
3866 } else {
3867 if (Current.isNot(TT_StartOfName) || Current.NestingLevel != 0)
3868 return false;
3869 while (Next && Next->startsSequence(tok::hashhash, tok::identifier))
3870 Next = Next->Next->Next;
3871 for (; Next; Next = Next->Next) {
3872 if (Next->is(TT_TemplateOpener) && Next->MatchingParen) {
3873 Next = Next->MatchingParen;
3874 } else if (Next->is(tok::coloncolon)) {
3875 Next = Next->Next;
3876 if (!Next)
3877 return false;
3878 if (Next->is(tok::kw_operator)) {
3879 Next = skipOperatorName(Next->Next);
3880 break;
3881 }
3882 if (Next->isNot(tok::identifier))
3883 return false;
3884 } else if (isCppAttribute(IsCpp, *Next)) {
3885 Next = Next->MatchingParen;
3886 if (!Next)
3887 return false;
3888 } else if (Next->is(tok::l_paren)) {
3889 break;
3890 } else {
3891 return false;
3892 }
3893 }
3894 }
3895
3896 // Check whether parameter list can belong to a function declaration.
3897 if (!Next || Next->isNot(tok::l_paren) || !Next->MatchingParen)
3898 return false;
3899 ClosingParen = Next->MatchingParen;
3900 assert(ClosingParen->is(tok::r_paren));
3901 // If the lines ends with "{", this is likely a function definition.
3902 if (Line.Last->is(tok::l_brace))
3903 return true;
3904 if (Next->Next == ClosingParen)
3905 return true; // Empty parentheses.
3906 // If there is an &/&& after the r_paren, this is likely a function.
3907 if (ClosingParen->Next && ClosingParen->Next->is(TT_PointerOrReference))
3908 return true;
3909
3910 // Check for K&R C function definitions (and C++ function definitions with
3911 // unnamed parameters), e.g.:
3912 // int f(i)
3913 // {
3914 // return i + 1;
3915 // }
3916 // bool g(size_t = 0, bool b = false)
3917 // {
3918 // return !b;
3919 // }
3920 if (IsCpp && Next->Next && Next->Next->is(tok::identifier) &&
3921 !Line.endsWith(tok::semi)) {
3922 return true;
3923 }
3924
3925 for (const FormatToken *Tok = Next->Next; Tok && Tok != ClosingParen;
3926 Tok = Tok->Next) {
3927 if (Tok->is(TT_TypeDeclarationParen))
3928 return true;
3929 if (Tok->isOneOf(tok::l_paren, TT_TemplateOpener) && Tok->MatchingParen) {
3930 Tok = Tok->MatchingParen;
3931 continue;
3932 }
3933 if (Tok->is(tok::kw_const) || Tok->isTypeName(LangOpts) ||
3934 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) {
3935 return true;
3936 }
3937 if (Tok->isOneOf(tok::l_brace, TT_ObjCMethodExpr) || Tok->Tok.isLiteral())
3938 return false;
3939 }
3940 return false;
3941}
3942
3943bool TokenAnnotator::mustBreakForReturnType(const AnnotatedLine &Line) const {
3944 assert(Line.MightBeFunctionDecl);
3945
3946 if ((Style.BreakAfterReturnType == FormatStyle::RTBS_TopLevel ||
3947 Style.BreakAfterReturnType == FormatStyle::RTBS_TopLevelDefinitions) &&
3948 Line.Level > 0) {
3949 return false;
3950 }
3951
3952 switch (Style.BreakAfterReturnType) {
3953 case FormatStyle::RTBS_None:
3954 case FormatStyle::RTBS_Automatic:
3955 case FormatStyle::RTBS_ExceptShortType:
3956 return false;
3957 case FormatStyle::RTBS_All:
3958 case FormatStyle::RTBS_TopLevel:
3959 return true;
3960 case FormatStyle::RTBS_AllDefinitions:
3961 case FormatStyle::RTBS_TopLevelDefinitions:
3962 return Line.mightBeFunctionDefinition();
3963 }
3964
3965 return false;
3966}
3967
3969 if (Line.Computed)
3970 return;
3971
3972 Line.Computed = true;
3973
3974 for (AnnotatedLine *ChildLine : Line.Children)
3976
3977 auto *First = Line.First;
3978 First->TotalLength = First->IsMultiline
3979 ? Style.ColumnLimit
3980 : Line.FirstStartColumn + First->ColumnWidth;
3981 bool AlignArrayOfStructures =
3982 (Style.AlignArrayOfStructures != FormatStyle::AIAS_None &&
3984 if (AlignArrayOfStructures)
3985 calculateArrayInitializerColumnList(Line);
3986
3987 const auto *FirstNonComment = Line.getFirstNonComment();
3988 bool SeenName = false;
3989 bool LineIsFunctionDeclaration = false;
3990 FormatToken *AfterLastAttribute = nullptr;
3991 FormatToken *ClosingParen = nullptr;
3992
3993 for (auto *Tok = FirstNonComment && FirstNonComment->isNot(tok::kw_using)
3994 ? FirstNonComment->Next
3995 : nullptr;
3996 Tok && Tok->isNot(BK_BracedInit); Tok = Tok->Next) {
3997 if (Tok->is(TT_StartOfName))
3998 SeenName = true;
3999 if (Tok->Previous->EndsCppAttributeGroup)
4000 AfterLastAttribute = Tok;
4001 if (const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
4002 IsCtorOrDtor ||
4003 isFunctionDeclarationName(LangOpts, *Tok, Line, ClosingParen)) {
4004 if (!IsCtorOrDtor)
4005 Tok->setFinalizedType(TT_FunctionDeclarationName);
4006 LineIsFunctionDeclaration = true;
4007 SeenName = true;
4008 if (ClosingParen) {
4009 auto *OpeningParen = ClosingParen->MatchingParen;
4010 assert(OpeningParen);
4011 if (OpeningParen->is(TT_Unknown))
4012 OpeningParen->setType(TT_FunctionDeclarationLParen);
4013 }
4014 break;
4015 }
4016 }
4017
4018 if (IsCpp) {
4019 if ((LineIsFunctionDeclaration ||
4020 (FirstNonComment && FirstNonComment->is(TT_CtorDtorDeclName))) &&
4021 Line.endsWith(tok::semi, tok::r_brace)) {
4022 auto *Tok = Line.Last->Previous;
4023 while (Tok->isNot(tok::r_brace))
4024 Tok = Tok->Previous;
4025 if (auto *LBrace = Tok->MatchingParen; LBrace && LBrace->is(TT_Unknown)) {
4026 assert(LBrace->is(tok::l_brace));
4027 Tok->setBlockKind(BK_Block);
4028 LBrace->setBlockKind(BK_Block);
4029 LBrace->setFinalizedType(TT_FunctionLBrace);
4030 }
4031 }
4032
4033 if (SeenName && AfterLastAttribute &&
4034 mustBreakAfterAttributes(*AfterLastAttribute, Style)) {
4035 AfterLastAttribute->MustBreakBefore = true;
4036 if (LineIsFunctionDeclaration)
4037 Line.ReturnTypeWrapped = true;
4038 }
4039
4040 if (!LineIsFunctionDeclaration) {
4041 // Annotate */&/&& in `operator` function calls as binary operators.
4042 for (const auto *Tok = FirstNonComment; Tok; Tok = Tok->Next) {
4043 if (Tok->isNot(tok::kw_operator))
4044 continue;
4045 do {
4046 Tok = Tok->Next;
4047 } while (Tok && Tok->isNot(TT_OverloadedOperatorLParen));
4048 if (!Tok || !Tok->MatchingParen)
4049 break;
4050 const auto *LeftParen = Tok;
4051 for (Tok = Tok->Next; Tok && Tok != LeftParen->MatchingParen;
4052 Tok = Tok->Next) {
4053 if (Tok->isNot(tok::identifier))
4054 continue;
4055 auto *Next = Tok->Next;
4056 const bool NextIsBinaryOperator =
4057 Next && Next->isPointerOrReference() && Next->Next &&
4058 Next->Next->is(tok::identifier);
4059 if (!NextIsBinaryOperator)
4060 continue;
4061 Next->setType(TT_BinaryOperator);
4062 Tok = Next;
4063 }
4064 }
4065 } else if (ClosingParen) {
4066 for (auto *Tok = ClosingParen->Next; Tok; Tok = Tok->Next) {
4067 if (Tok->is(TT_CtorInitializerColon))
4068 break;
4069 if (Tok->is(tok::arrow)) {
4070 Tok->setType(TT_TrailingReturnArrow);
4071 break;
4072 }
4073 if (Tok->isNot(TT_TrailingAnnotation))
4074 continue;
4075 const auto *Next = Tok->Next;
4076 if (!Next || Next->isNot(tok::l_paren))
4077 continue;
4078 Tok = Next->MatchingParen;
4079 if (!Tok)
4080 break;
4081 }
4082 }
4083 }
4084
4085 if (First->is(TT_ElseLBrace)) {
4086 First->CanBreakBefore = true;
4087 First->MustBreakBefore = true;
4088 }
4089
4090 bool InFunctionDecl = Line.MightBeFunctionDecl;
4091 bool InParameterList = false;
4092 for (auto *Current = First->Next; Current; Current = Current->Next) {
4093 const FormatToken *Prev = Current->Previous;
4094 if (Current->is(TT_LineComment)) {
4095 if (Prev->is(BK_BracedInit) && Prev->opensScope()) {
4096 Current->SpacesRequiredBefore =
4097 (Style.Cpp11BracedListStyle == FormatStyle::BLS_AlignFirstComment &&
4098 !Style.SpacesInParensOptions.Other)
4099 ? 0
4100 : 1;
4101 } else if (Prev->is(TT_VerilogMultiLineListLParen)) {
4102 Current->SpacesRequiredBefore = 0;
4103 } else {
4104 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
4105 }
4106
4107 // If we find a trailing comment, iterate backwards to determine whether
4108 // it seems to relate to a specific parameter. If so, break before that
4109 // parameter to avoid changing the comment's meaning. E.g. don't move 'b'
4110 // to the previous line in:
4111 // SomeFunction(a,
4112 // b, // comment
4113 // c);
4114 if (!Current->HasUnescapedNewline) {
4115 for (FormatToken *Parameter = Current->Previous; Parameter;
4116 Parameter = Parameter->Previous) {
4117 if (Parameter->isOneOf(tok::comment, tok::r_brace))
4118 break;
4119 if (Parameter->Previous && Parameter->Previous->is(tok::comma)) {
4120 if (Parameter->Previous->isNot(TT_CtorInitializerComma) &&
4121 Parameter->HasUnescapedNewline) {
4122 Parameter->MustBreakBefore = true;
4123 }
4124 break;
4125 }
4126 }
4127 }
4128 } else if (!Current->Finalized && Current->SpacesRequiredBefore == 0 &&
4129 spaceRequiredBefore(Line, *Current)) {
4130 Current->SpacesRequiredBefore = 1;
4131 }
4132
4133 const auto &Children = Prev->Children;
4134 if (!Children.empty() && Children.back()->Last->is(TT_LineComment)) {
4135 Current->MustBreakBefore = true;
4136 } else {
4137 Current->MustBreakBefore =
4138 Current->MustBreakBefore || mustBreakBefore(Line, *Current);
4139 if (!Current->MustBreakBefore && InFunctionDecl &&
4140 Current->is(TT_FunctionDeclarationName)) {
4141 Current->MustBreakBefore = mustBreakForReturnType(Line);
4142 }
4143 }
4144
4145 Current->CanBreakBefore =
4146 Current->MustBreakBefore || canBreakBefore(Line, *Current);
4147
4148 if (Current->is(TT_FunctionDeclarationLParen)) {
4149 InParameterList = true;
4150 } else if (Current->is(tok::r_paren)) {
4151 const auto *LParen = Current->MatchingParen;
4152 if (LParen && LParen->is(TT_FunctionDeclarationLParen))
4153 InParameterList = false;
4154 } else if (InParameterList &&
4155 Current->endsSequence(TT_AttributeMacro,
4156 TT_PointerOrReference)) {
4157 Current->CanBreakBefore = false;
4158 }
4159
4160 unsigned ChildSize = 0;
4161 if (Prev->Children.size() == 1) {
4162 FormatToken &LastOfChild = *Prev->Children[0]->Last;
4163 ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit
4164 : LastOfChild.TotalLength + 1;
4165 }
4166 if (Current->MustBreakBefore || Prev->Children.size() > 1 ||
4167 (Prev->Children.size() == 1 &&
4168 Prev->Children[0]->First->MustBreakBefore) ||
4169 Current->IsMultiline) {
4170 Current->TotalLength = Prev->TotalLength + Style.ColumnLimit;
4171 } else {
4172 Current->TotalLength = Prev->TotalLength + Current->ColumnWidth +
4173 ChildSize + Current->SpacesRequiredBefore;
4174 }
4175
4176 if (Current->is(TT_ControlStatementLBrace)) {
4177 if (Style.ColumnLimit > 0 &&
4178 Style.BraceWrapping.AfterControlStatement ==
4179 FormatStyle::BWACS_MultiLine &&
4180 Line.Level * Style.IndentWidth + Line.Last->TotalLength >
4181 Style.ColumnLimit) {
4182 Current->CanBreakBefore = true;
4183 Current->MustBreakBefore = true;
4184 }
4185 } else if (Current->is(TT_CtorInitializerColon)) {
4186 InFunctionDecl = false;
4187 }
4188
4189 // FIXME: Only calculate this if CanBreakBefore is true once static
4190 // initializers etc. are sorted out.
4191 // FIXME: Move magic numbers to a better place.
4192
4193 // Reduce penalty for aligning ObjC method arguments using the colon
4194 // alignment as this is the canonical way (still prefer fitting everything
4195 // into one line if possible). Trying to fit a whole expression into one
4196 // line should not force other line breaks (e.g. when ObjC method
4197 // expression is a part of other expression).
4198 Current->SplitPenalty = splitPenalty(Line, *Current, InFunctionDecl);
4199 if (Style.Language == FormatStyle::LK_ObjC &&
4200 Current->is(TT_SelectorName) && Current->ParameterIndex > 0) {
4201 if (Current->ParameterIndex == 1)
4202 Current->SplitPenalty += 5 * Current->BindingStrength;
4203 } else {
4204 Current->SplitPenalty += 20 * Current->BindingStrength;
4205 }
4206 }
4207
4208 calculateUnbreakableTailLengths(Line);
4209 unsigned IndentLevel = Line.Level;
4210 for (auto *Current = First; Current; Current = Current->Next) {
4211 if (Current->Role)
4212 Current->Role->precomputeFormattingInfos(Current);
4213 if (Current->MatchingParen &&
4214 Current->MatchingParen->opensBlockOrBlockTypeList(Style) &&
4215 IndentLevel > 0) {
4216 --IndentLevel;
4217 }
4218 Current->IndentLevel = IndentLevel;
4219 if (Current->opensBlockOrBlockTypeList(Style))
4220 ++IndentLevel;
4221 }
4222
4223 LLVM_DEBUG({ printDebugInfo(Line); });
4224}
4225
4226void TokenAnnotator::calculateUnbreakableTailLengths(
4227 AnnotatedLine &Line) const {
4228 unsigned UnbreakableTailLength = 0;
4229 FormatToken *Current = Line.Last;
4230 while (Current) {
4232 if (Current->CanBreakBefore ||
4233 Current->isOneOf(tok::comment, tok::string_literal)) {
4235 } else {
4237 Current->ColumnWidth + Current->SpacesRequiredBefore;
4238 }
4239 Current = Current->Previous;
4240 }
4241}
4242
4243void TokenAnnotator::calculateArrayInitializerColumnList(
4244 AnnotatedLine &Line) const {
4245 if (Line.First == Line.Last)
4246 return;
4247 auto *CurrentToken = Line.First;
4248 CurrentToken->ArrayInitializerLineStart = true;
4249 unsigned Depth = 0;
4250 while (CurrentToken && CurrentToken != Line.Last) {
4251 if (CurrentToken->is(tok::l_brace)) {
4252 CurrentToken->IsArrayInitializer = true;
4253 if (CurrentToken->Next)
4254 CurrentToken->Next->MustBreakBefore = true;
4255 CurrentToken =
4256 calculateInitializerColumnList(Line, CurrentToken->Next, Depth + 1);
4257 } else {
4258 CurrentToken = CurrentToken->Next;
4259 }
4260 }
4261}
4262
4263FormatToken *TokenAnnotator::calculateInitializerColumnList(
4264 AnnotatedLine &Line, FormatToken *CurrentToken, unsigned Depth) const {
4265 while (CurrentToken && CurrentToken != Line.Last) {
4266 if (CurrentToken->is(tok::l_brace))
4267 ++Depth;
4268 else if (CurrentToken->is(tok::r_brace))
4269 --Depth;
4270 if (Depth == 2 && CurrentToken->isOneOf(tok::l_brace, tok::comma)) {
4271 CurrentToken = CurrentToken->Next;
4272 if (!CurrentToken)
4273 break;
4274 CurrentToken->StartsColumn = true;
4275 CurrentToken = CurrentToken->Previous;
4276 }
4277 CurrentToken = CurrentToken->Next;
4278 }
4279 return CurrentToken;
4280}
4281
4282unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
4283 const FormatToken &Tok,
4284 bool InFunctionDecl) const {
4285 const FormatToken &Left = *Tok.Previous;
4286 const FormatToken &Right = Tok;
4287
4288 if (Left.is(tok::semi))
4289 return 0;
4290
4291 // Language specific handling.
4292 if (Style.isJava()) {
4293 if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
4294 return 1;
4295 if (Right.is(Keywords.kw_implements))
4296 return 2;
4297 if (Left.is(tok::comma) && Left.NestingLevel == 0)
4298 return 3;
4299 } else if (Style.isJavaScript()) {
4300 if (Right.is(Keywords.kw_function) && Left.isNot(tok::comma))
4301 return 100;
4302 if (Left.is(TT_JsTypeColon))
4303 return 35;
4304 if ((Left.is(TT_TemplateString) && Left.TokenText.ends_with("${")) ||
4305 (Right.is(TT_TemplateString) && Right.TokenText.starts_with("}"))) {
4306 return 100;
4307 }
4308 // Prefer breaking call chains (".foo") over empty "{}", "[]" or "()".
4309 if (Left.opensScope() && Right.closesScope())
4310 return 200;
4311 } else if (Style.Language == FormatStyle::LK_Proto) {
4312 if (Right.is(tok::l_square))
4313 return 1;
4314 if (Right.is(tok::period))
4315 return 500;
4316 }
4317
4318 if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
4319 return 1;
4320 if (Right.is(tok::l_square)) {
4321 if (Left.is(tok::r_square))
4322 return 200;
4323 // Slightly prefer formatting local lambda definitions like functions.
4324 if (Right.is(TT_LambdaLSquare) && Left.is(tok::equal))
4325 return 35;
4326 if (Right.isNoneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4327 TT_ArrayInitializerLSquare,
4328 TT_DesignatedInitializerLSquare, TT_AttributeLSquare)) {
4329 return 500;
4330 }
4331 }
4332
4333 if (Left.is(tok::coloncolon))
4334 return Style.PenaltyBreakScopeResolution;
4335 if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
4336 tok::kw_operator)) {
4337 if (Line.startsWith(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
4338 return 3;
4339 if (Left.is(TT_StartOfName))
4340 return 110;
4341 if (InFunctionDecl && Right.NestingLevel == 0)
4342 return Style.PenaltyReturnTypeOnItsOwnLine;
4343 return 200;
4344 }
4345 if (Right.is(TT_PointerOrReference))
4346 return 190;
4347 if (Right.is(TT_LambdaArrow))
4348 return 110;
4349 if (Left.is(tok::equal) && Right.is(tok::l_brace))
4350 return 160;
4351 if (Left.is(TT_CastRParen))
4352 return 100;
4353 if (Left.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union))
4354 return 5000;
4355 if (Left.is(tok::comment))
4356 return 1000;
4357
4358 if (Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
4359 TT_CtorInitializerColon)) {
4360 return 2;
4361 }
4362
4363 if (Right.isMemberAccess()) {
4364 // Breaking before the "./->" of a chained call/member access is reasonably
4365 // cheap, as formatting those with one call per line is generally
4366 // desirable. In particular, it should be cheaper to break before the call
4367 // than it is to break inside a call's parameters, which could lead to weird
4368 // "hanging" indents. The exception is the very last "./->" to support this
4369 // frequent pattern:
4370 //
4371 // aaaaaaaa.aaaaaaaa.bbbbbbb().ccccccccccccccccccccc(
4372 // dddddddd);
4373 //
4374 // which might otherwise be blown up onto many lines. Here, clang-format
4375 // won't produce "hanging" indents anyway as there is no other trailing
4376 // call.
4377 //
4378 // Also apply higher penalty is not a call as that might lead to a wrapping
4379 // like:
4380 //
4381 // aaaaaaa
4382 // .aaaaaaaaa.bbbbbbbb(cccccccc);
4383 const auto *NextOperator = Right.NextOperator;
4384 const auto Penalty = Style.PenaltyBreakBeforeMemberAccess;
4385 return NextOperator && NextOperator->Previous->closesScope()
4386 ? std::min(Penalty, 35u)
4387 : Penalty;
4388 }
4389
4390 if (Right.is(TT_TrailingAnnotation) &&
4391 (!Right.Next || Right.Next->isNot(tok::l_paren))) {
4392 // Moving trailing annotations to the next line is fine for ObjC method
4393 // declarations.
4394 if (Line.startsWith(TT_ObjCMethodSpecifier))
4395 return 10;
4396 // Generally, breaking before a trailing annotation is bad unless it is
4397 // function-like. It seems to be especially preferable to keep standard
4398 // annotations (i.e. "const", "final" and "override") on the same line.
4399 // Use a slightly higher penalty after ")" so that annotations like
4400 // "const override" are kept together.
4401 bool is_short_annotation = Right.TokenText.size() < 10;
4402 return (Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
4403 }
4404
4405 // In for-loops, prefer breaking at ',' and ';'.
4406 if (Line.startsWith(tok::kw_for) && Left.is(tok::equal))
4407 return 4;
4408
4409 // In Objective-C method expressions, prefer breaking before "param:" over
4410 // breaking after it.
4411 if (Right.is(TT_SelectorName))
4412 return 0;
4413 if (Left.is(tok::colon)) {
4414 if (Left.is(TT_ObjCMethodExpr))
4415 return Line.MightBeFunctionDecl ? 50 : 500;
4416 if (Left.is(TT_ObjCSelector))
4417 return 500;
4418 }
4419
4420 // In Objective-C type declarations, avoid breaking after the category's
4421 // open paren (we'll prefer breaking after the protocol list's opening
4422 // angle bracket, if present).
4423 if (Line.Type == LT_ObjCDecl && Left.is(tok::l_paren) && Left.Previous &&
4424 Left.Previous->isOneOf(tok::identifier, tok::greater)) {
4425 return 500;
4426 }
4427
4428 if (Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
4429 return Style.PenaltyBreakOpenParenthesis;
4430 if (Left.is(tok::l_paren) && InFunctionDecl && Style.AlignAfterOpenBracket)
4431 return 100;
4432 if (Left.is(tok::l_paren) && Left.Previous &&
4433 (Left.Previous->isOneOf(tok::kw_for, tok::kw__Generic) ||
4434 Left.Previous->isIf())) {
4435 return 1000;
4436 }
4437 if (Left.is(tok::equal) && InFunctionDecl)
4438 return 110;
4439 if (Right.is(tok::r_brace))
4440 return 1;
4441 if (Left.is(TT_TemplateOpener))
4442 return 100;
4443 if (Left.opensScope()) {
4444 // If we aren't aligning after opening parens/braces we can always break
4445 // here unless the style does not want us to place all arguments on the
4446 // next line.
4447 if (!Style.AlignAfterOpenBracket &&
4448 (Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
4449 return 0;
4450 }
4451 if (Left.is(tok::l_brace) &&
4452 Style.Cpp11BracedListStyle == FormatStyle::BLS_Block) {
4453 return 19;
4454 }
4455 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
4456 : 19;
4457 }
4458 if (Left.is(TT_JavaAnnotation))
4459 return 50;
4460
4461 if (Left.is(TT_UnaryOperator))
4462 return 60;
4463 if (Left.isOneOf(tok::plus, tok::comma) && Left.Previous &&
4464 Left.Previous->isLabelString() &&
4465 (Left.NextOperator || Left.OperatorIndex != 0)) {
4466 return 50;
4467 }
4468 if (Right.is(tok::plus) && Left.isLabelString() &&
4469 (Right.NextOperator || Right.OperatorIndex != 0)) {
4470 return 25;
4471 }
4472 if (Left.is(tok::comma))
4473 return 1;
4474 if (Right.is(tok::lessless) && Left.isLabelString() &&
4475 (Right.NextOperator || Right.OperatorIndex != 1)) {
4476 return 25;
4477 }
4478 if (Right.is(tok::lessless)) {
4479 // Breaking at a << is really cheap.
4480 if (Left.isNot(tok::r_paren) || Right.OperatorIndex > 0) {
4481 // Slightly prefer to break before the first one in log-like statements.
4482 return 2;
4483 }
4484 return 1;
4485 }
4486 if (Left.ClosesTemplateDeclaration)
4487 return Style.PenaltyBreakTemplateDeclaration;
4488 if (Left.ClosesRequiresClause)
4489 return 0;
4490 if (Left.is(TT_ConditionalExpr))
4491 return prec::Conditional;
4492 prec::Level Level = Left.getPrecedence();
4493 if (Level == prec::Unknown)
4494 Level = Right.getPrecedence();
4495 if (Level == prec::Assignment)
4496 return Style.PenaltyBreakAssignment;
4497 if (Level != prec::Unknown)
4498 return Level;
4499
4500 return 3;
4501}
4502
4503bool TokenAnnotator::spaceRequiredBeforeParens(const FormatToken &Right) const {
4504 if (Style.SpaceBeforeParens == FormatStyle::SBPO_Always)
4505 return true;
4506 if (Right.is(TT_OverloadedOperatorLParen) &&
4507 Style.SpaceBeforeParensOptions.AfterOverloadedOperator) {
4508 return true;
4509 }
4510 if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
4511 Right.ParameterCount > 0) {
4512 return true;
4513 }
4514 return false;
4515}
4516
4517bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
4518 const FormatToken &Left,
4519 const FormatToken &Right) const {
4520 if (Left.is(tok::kw_return) &&
4521 Right.isNoneOf(tok::semi, tok::r_paren, tok::hashhash)) {
4522 return true;
4523 }
4524 if (Left.is(tok::kw_throw) && Right.is(tok::l_paren) && Right.MatchingParen &&
4525 Right.MatchingParen->is(TT_CastRParen)) {
4526 return true;
4527 }
4528 if (Left.is(Keywords.kw_assert) && Style.isJava())
4529 return true;
4530 if (Style.ObjCSpaceAfterProperty && Line.Type == LT_ObjCProperty &&
4531 Left.is(tok::objc_property)) {
4532 return true;
4533 }
4534 if (Right.is(tok::hashhash))
4535 return Left.is(tok::hash);
4536 if (Left.isOneOf(tok::hashhash, tok::hash))
4537 return Right.is(tok::hash);
4538 if (Style.SpacesInParens == FormatStyle::SIPO_Custom) {
4539 if (Left.is(tok::l_paren) && Right.is(tok::r_paren))
4540 return Style.SpacesInParensOptions.InEmptyParentheses;
4541 if (Style.SpacesInParensOptions.ExceptDoubleParentheses &&
4542 Left.is(tok::r_paren) && Right.is(tok::r_paren)) {
4543 auto *InnerLParen = Left.MatchingParen;
4544 if (InnerLParen && InnerLParen->Previous == Right.MatchingParen) {
4545 InnerLParen->SpacesRequiredBefore = 0;
4546 return false;
4547 }
4548 }
4549 const FormatToken *LeftParen = nullptr;
4550 if (Left.is(tok::l_paren))
4551 LeftParen = &Left;
4552 else if (Right.is(tok::r_paren) && Right.MatchingParen)
4553 LeftParen = Right.MatchingParen;
4554 if (LeftParen && (LeftParen->is(TT_ConditionLParen) ||
4555 (LeftParen->Previous &&
4556 isKeywordWithCondition(*LeftParen->Previous)))) {
4557 return Style.SpacesInParensOptions.InConditionalStatements;
4558 }
4559 }
4560
4561 // trailing return type 'auto': []() -> auto {}, auto foo() -> auto {}
4562 if (Left.is(tok::kw_auto) && Right.isOneOf(TT_LambdaLBrace, TT_FunctionLBrace,
4563 // function return type 'auto'
4564 TT_FunctionTypeLParen)) {
4565 return true;
4566 }
4567
4568 // auto{x} auto(x)
4569 if (Left.is(tok::kw_auto) && Right.isOneOf(tok::l_paren, tok::l_brace))
4570 return false;
4571
4572 const auto *BeforeLeft = Left.Previous;
4573
4574 // operator co_await(x)
4575 if (Right.is(tok::l_paren) && Left.is(tok::kw_co_await) && BeforeLeft &&
4576 BeforeLeft->is(tok::kw_operator)) {
4577 return false;
4578 }
4579 // co_await (x), co_yield (x), co_return (x)
4580 if (Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
4581 Right.isNoneOf(tok::semi, tok::r_paren)) {
4582 return true;
4583 }
4584
4585 if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) {
4586 return (Right.is(TT_CastRParen) ||
4587 (Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
4588 ? Style.SpacesInParensOptions.InCStyleCasts
4589 : Style.SpacesInParensOptions.Other;
4590 }
4591 if (Right.isOneOf(tok::semi, tok::comma))
4592 return false;
4593 if (Right.is(tok::less) && Line.Type == LT_ObjCDecl) {
4594 bool IsLightweightGeneric = Right.MatchingParen &&
4595 Right.MatchingParen->Next &&
4596 Right.MatchingParen->Next->is(tok::colon);
4597 return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList;
4598 }
4599 if (Right.is(tok::less) && Left.is(tok::kw_template))
4600 return Style.SpaceAfterTemplateKeyword;
4601 if (Left.isOneOf(tok::exclaim, tok::tilde))
4602 return false;
4603 if (Left.is(tok::at) &&
4604 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
4605 tok::numeric_constant, tok::l_paren, tok::l_brace,
4606 tok::kw_true, tok::kw_false)) {
4607 return false;
4608 }
4609 if (Left.is(tok::colon))
4610 return Left.isNoneOf(TT_ObjCSelector, TT_ObjCMethodExpr);
4611 if (Left.is(tok::coloncolon))
4612 return false;
4613 if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less)) {
4614 if (Style.isTextProto() ||
4615 (Style.Language == FormatStyle::LK_Proto &&
4616 (Left.is(TT_DictLiteral) || Right.is(TT_DictLiteral)))) {
4617 // Format empty list as `<>`.
4618 if (Left.is(tok::less) && Right.is(tok::greater))
4619 return false;
4620 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block;
4621 }
4622 // Don't attempt to format operator<(), as it is handled later.
4623 if (Right.isNot(TT_OverloadedOperatorLParen))
4624 return false;
4625 }
4626 if (Right.is(tok::ellipsis)) {
4627 return Left.Tok.isLiteral() || (Left.is(tok::identifier) && BeforeLeft &&
4628 BeforeLeft->is(tok::kw_case));
4629 }
4630 if (Left.is(tok::l_square) && Right.is(tok::amp))
4631 return Style.SpacesInSquareBrackets;
4632 if (Right.is(TT_PointerOrReference)) {
4633 if (Left.is(tok::r_paren) && Line.MightBeFunctionDecl) {
4634 if (!Left.MatchingParen)
4635 return true;
4636 FormatToken *TokenBeforeMatchingParen =
4637 Left.MatchingParen->getPreviousNonComment();
4638 if (!TokenBeforeMatchingParen || Left.isNot(TT_TypeDeclarationParen))
4639 return true;
4640 }
4641 // Add a space if the previous token is a pointer qualifier or the closing
4642 // parenthesis of __attribute__(()) expression and the style requires spaces
4643 // after pointer qualifiers.
4644 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_After ||
4645 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
4646 (Left.is(TT_AttributeRParen) ||
4647 Left.canBePointerOrReferenceQualifier())) {
4648 return true;
4649 }
4650 if (Left.Tok.isLiteral())
4651 return true;
4652 // for (auto a = 0, b = 0; const auto & c : {1, 2, 3})
4653 if (Left.isTypeOrIdentifier(LangOpts) && Right.Next && Right.Next->Next &&
4654 Right.Next->Next->is(TT_RangeBasedForLoopColon)) {
4655 return getTokenPointerOrReferenceAlignment(Right) !=
4656 FormatStyle::PAS_Left;
4657 }
4658 return Left.isNoneOf(TT_PointerOrReference, tok::l_paren) &&
4659 (getTokenPointerOrReferenceAlignment(Right) !=
4660 FormatStyle::PAS_Left ||
4661 (Line.IsMultiVariableDeclStmt &&
4662 (Left.NestingLevel == 0 ||
4663 (Left.NestingLevel == 1 && startsWithInitStatement(Line)))));
4664 }
4665 if (Right.is(TT_FunctionTypeLParen) && Left.isNot(tok::l_paren) &&
4666 (Left.isNot(TT_PointerOrReference) ||
4667 (getTokenPointerOrReferenceAlignment(Left) != FormatStyle::PAS_Right &&
4668 !Line.IsMultiVariableDeclStmt))) {
4669 return true;
4670 }
4671 if (Left.is(TT_PointerOrReference)) {
4672 // Add a space if the next token is a pointer qualifier and the style
4673 // requires spaces before pointer qualifiers.
4674 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Before ||
4675 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
4676 Right.canBePointerOrReferenceQualifier()) {
4677 return true;
4678 }
4679 // & 1
4680 if (Right.Tok.isLiteral())
4681 return true;
4682 // & /* comment
4683 if (Right.is(TT_BlockComment))
4684 return true;
4685 // foo() -> const Bar * override/final
4686 // S::foo() & noexcept/requires
4687 if (Right.isOneOf(Keywords.kw_override, Keywords.kw_final, tok::kw_noexcept,
4688 TT_RequiresClause) &&
4689 Right.isNot(TT_StartOfName)) {
4690 return true;
4691 }
4692 // & {
4693 if (Right.is(tok::l_brace) && Right.is(BK_Block))
4694 return true;
4695 // for (auto a = 0, b = 0; const auto& c : {1, 2, 3})
4696 if (BeforeLeft && BeforeLeft->isTypeOrIdentifier(LangOpts) && Right.Next &&
4697 Right.Next->is(TT_RangeBasedForLoopColon)) {
4698 return getTokenPointerOrReferenceAlignment(Left) !=
4699 FormatStyle::PAS_Right;
4700 }
4701 if (Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
4702 tok::l_paren)) {
4703 return false;
4704 }
4705 if (getTokenPointerOrReferenceAlignment(Left) == FormatStyle::PAS_Right)
4706 return false;
4707 // FIXME: Setting IsMultiVariableDeclStmt for the whole line is error-prone,
4708 // because it does not take into account nested scopes like lambdas.
4709 // In multi-variable declaration statements, attach */& to the variable
4710 // independently of the style. However, avoid doing it if we are in a nested
4711 // scope, e.g. lambda. We still need to special-case statements with
4712 // initializers.
4713 if (Line.IsMultiVariableDeclStmt &&
4714 (Left.NestingLevel == Line.First->NestingLevel ||
4715 ((Left.NestingLevel == Line.First->NestingLevel + 1) &&
4716 startsWithInitStatement(Line)))) {
4717 return false;
4718 }
4719 if (!BeforeLeft)
4720 return false;
4721 if (BeforeLeft->is(tok::coloncolon)) {
4722 if (Left.isNot(tok::star))
4723 return false;
4724 assert(Style.PointerAlignment != FormatStyle::PAS_Right);
4725 if (!Right.startsSequence(tok::identifier, tok::r_paren))
4726 return true;
4727 assert(Right.Next);
4728 const auto *LParen = Right.Next->MatchingParen;
4729 return !LParen || LParen->isNot(TT_FunctionTypeLParen);
4730 }
4731 return BeforeLeft->isNoneOf(tok::l_paren, tok::l_square);
4732 }
4733 // Ensure right pointer alignment with ellipsis e.g. int *...P
4734 if (Left.is(tok::ellipsis) && BeforeLeft &&
4735 BeforeLeft->isPointerOrReference()) {
4736 return Style.PointerAlignment != FormatStyle::PAS_Right;
4737 }
4738
4739 if (Right.is(tok::star) && Left.is(tok::l_paren))
4740 return false;
4741 if (Left.is(tok::star) && Right.isPointerOrReference())
4742 return false;
4743 if (Right.isPointerOrReference()) {
4744 const FormatToken *Previous = &Left;
4745 while (Previous && Previous->isNot(tok::kw_operator)) {
4746 if (Previous->is(tok::identifier) || Previous->isTypeName(LangOpts)) {
4747 Previous = Previous->getPreviousNonComment();
4748 continue;
4749 }
4750 if (Previous->is(TT_TemplateCloser) && Previous->MatchingParen) {
4751 Previous = Previous->MatchingParen->getPreviousNonComment();
4752 continue;
4753 }
4754 if (Previous->is(tok::coloncolon)) {
4755 Previous = Previous->getPreviousNonComment();
4756 continue;
4757 }
4758 break;
4759 }
4760 // Space between the type and the * in:
4761 // operator void*()
4762 // operator char*()
4763 // operator void const*()
4764 // operator void volatile*()
4765 // operator /*comment*/ const char*()
4766 // operator volatile /*comment*/ char*()
4767 // operator Foo*()
4768 // operator C<T>*()
4769 // operator std::Foo*()
4770 // operator C<T>::D<U>*()
4771 // dependent on PointerAlignment style.
4772 if (Previous) {
4773 if (Previous->endsSequence(tok::kw_operator))
4774 return Style.PointerAlignment != FormatStyle::PAS_Left;
4775 if (Previous->isOneOf(tok::kw_const, tok::kw_volatile)) {
4776 return (Style.PointerAlignment != FormatStyle::PAS_Left) ||
4777 (Style.SpaceAroundPointerQualifiers ==
4778 FormatStyle::SAPQ_After) ||
4779 (Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both);
4780 }
4781 }
4782 }
4783 if (Style.isCSharp() && Left.is(Keywords.kw_is) && Right.is(tok::l_square))
4784 return true;
4785 const auto SpaceRequiredForArrayInitializerLSquare =
4786 [](const FormatToken &LSquareTok, const FormatStyle &Style) {
4787 return Style.SpacesInContainerLiterals ||
4788 (Style.isProto() &&
4789 Style.Cpp11BracedListStyle == FormatStyle::BLS_Block &&
4790 LSquareTok.endsSequence(tok::l_square, tok::colon,
4791 TT_SelectorName));
4792 };
4793 if (Left.is(tok::l_square)) {
4794 return (Left.is(TT_ArrayInitializerLSquare) && Right.isNot(tok::r_square) &&
4795 SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
4796 (Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare,
4797 TT_LambdaLSquare) &&
4798 Style.SpacesInSquareBrackets && Right.isNot(tok::r_square));
4799 }
4800 if (Right.is(tok::r_square)) {
4801 return Right.MatchingParen &&
4802 ((Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
4803 SpaceRequiredForArrayInitializerLSquare(*Right.MatchingParen,
4804 Style)) ||
4805 (Style.SpacesInSquareBrackets &&
4806 Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
4807 TT_StructuredBindingLSquare,
4808 TT_LambdaLSquare)));
4809 }
4810 if (Right.is(tok::l_square) &&
4811 Right.isNoneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4812 TT_DesignatedInitializerLSquare,
4813 TT_StructuredBindingLSquare, TT_AttributeLSquare) &&
4814 Left.isNoneOf(tok::numeric_constant, TT_DictLiteral) &&
4815 !(Left.isNot(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
4816 Right.is(TT_ArraySubscriptLSquare))) {
4817 return false;
4818 }
4819 if ((Left.is(tok::l_brace) && Left.isNot(BK_Block)) ||
4820 (Right.is(tok::r_brace) && Right.MatchingParen &&
4821 Right.MatchingParen->isNot(BK_Block))) {
4822 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block ||
4823 Style.SpacesInParensOptions.Other;
4824 }
4825 if (Left.is(TT_BlockComment)) {
4826 // No whitespace in x(/*foo=*/1), except for JavaScript.
4827 return Style.isJavaScript() || !Left.TokenText.ends_with("=*/");
4828 }
4829
4830 // Space between template and attribute.
4831 // e.g. template <typename T> [[nodiscard]] ...
4832 if (Left.is(TT_TemplateCloser) && Right.is(TT_AttributeLSquare))
4833 return true;
4834 // Space before parentheses common for all languages
4835 if (Right.is(tok::l_paren)) {
4836 if (Left.is(TT_TemplateCloser) && Right.isNot(TT_FunctionTypeLParen))
4837 return spaceRequiredBeforeParens(Right);
4838 if (Left.isOneOf(TT_RequiresClause,
4839 TT_RequiresClauseInARequiresExpression)) {
4840 return Style.SpaceBeforeParensOptions.AfterRequiresInClause ||
4841 spaceRequiredBeforeParens(Right);
4842 }
4843 if (Left.is(TT_RequiresExpression)) {
4844 return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
4845 spaceRequiredBeforeParens(Right);
4846 }
4847 if (Left.isOneOf(TT_AttributeRParen, TT_AttributeRSquare))
4848 return true;
4849 if (Left.is(TT_ForEachMacro)) {
4850 return Style.SpaceBeforeParensOptions.AfterForeachMacros ||
4851 spaceRequiredBeforeParens(Right);
4852 }
4853 if (Left.is(TT_IfMacro)) {
4854 return Style.SpaceBeforeParensOptions.AfterIfMacros ||
4855 spaceRequiredBeforeParens(Right);
4856 }
4857 if (Style.SpaceBeforeParens == FormatStyle::SBPO_Custom &&
4858 Left.isPlacementOperator() &&
4859 Right.isNot(TT_OverloadedOperatorLParen) &&
4860 !(Line.MightBeFunctionDecl && Left.is(TT_FunctionDeclarationName))) {
4861 const auto *RParen = Right.MatchingParen;
4862 return Style.SpaceBeforeParensOptions.AfterPlacementOperator ||
4863 (RParen && RParen->is(TT_CastRParen));
4864 }
4865 if (Line.Type == LT_ObjCDecl)
4866 return true;
4867 if (Left.is(tok::semi))
4868 return true;
4869 if (Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while, tok::kw_switch,
4870 tok::kw_case, TT_ForEachMacro, TT_ObjCForIn) ||
4871 Left.isIf(Line.Type != LT_PreprocessorDirective) ||
4872 Right.is(TT_ConditionLParen)) {
4873 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4874 spaceRequiredBeforeParens(Right);
4875 }
4876
4877 // TODO add Operator overloading specific Options to
4878 // SpaceBeforeParensOptions
4879 if (Right.is(TT_OverloadedOperatorLParen))
4880 return spaceRequiredBeforeParens(Right);
4881 // Function declaration or definition
4882 if (Line.MightBeFunctionDecl && Right.is(TT_FunctionDeclarationLParen)) {
4883 if (spaceRequiredBeforeParens(Right))
4884 return true;
4885 const auto &Options = Style.SpaceBeforeParensOptions;
4886 return Line.mightBeFunctionDefinition()
4887 ? Options.AfterFunctionDefinitionName
4888 : Options.AfterFunctionDeclarationName;
4889 }
4890 // Lambda
4891 if (Line.Type != LT_PreprocessorDirective && Left.is(tok::r_square) &&
4892 Left.MatchingParen && Left.MatchingParen->is(TT_LambdaLSquare)) {
4893 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
4894 spaceRequiredBeforeParens(Right);
4895 }
4896 if (!BeforeLeft || BeforeLeft->isNoneOf(tok::period, tok::arrow)) {
4897 if (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) {
4898 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4899 spaceRequiredBeforeParens(Right);
4900 }
4901 if (Left.isPlacementOperator() ||
4902 (Left.is(tok::r_square) && Left.MatchingParen &&
4903 Left.MatchingParen->Previous &&
4904 Left.MatchingParen->Previous->is(tok::kw_delete))) {
4905 return Style.SpaceBeforeParens != FormatStyle::SBPO_Never ||
4906 spaceRequiredBeforeParens(Right);
4907 }
4908 }
4909 // Handle builtins like identifiers.
4910 if (Line.Type != LT_PreprocessorDirective &&
4911 (Left.Tok.getIdentifierInfo() || Left.is(tok::r_paren))) {
4912 return spaceRequiredBeforeParens(Right);
4913 }
4914 return false;
4915 }
4916 if (Left.is(tok::at) && Right.isNot(tok::objc_not_keyword))
4917 return false;
4918 if (Right.is(TT_UnaryOperator)) {
4919 return Left.isNoneOf(tok::l_paren, tok::l_square, tok::at) &&
4920 (Left.isNot(tok::colon) || Left.isNot(TT_ObjCMethodExpr));
4921 }
4922 // No space between the variable name and the initializer list.
4923 // A a1{1};
4924 // Verilog doesn't have such syntax, but it has word operators that are C++
4925 // identifiers like `a inside {b, c}`. So the rule is not applicable.
4926 if (!Style.isVerilog() &&
4927 (Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
4928 tok::r_paren) ||
4929 Left.isTypeName(LangOpts)) &&
4930 Right.is(tok::l_brace) && Right.getNextNonComment() &&
4931 Right.isNot(BK_Block)) {
4932 return false;
4933 }
4934 if (Left.is(tok::period) || Right.is(tok::period))
4935 return false;
4936 // u#str, U#str, L#str, u8#str
4937 // uR#str, UR#str, LR#str, u8R#str
4938 if (Right.is(tok::hash) && Left.is(tok::identifier) &&
4939 (Left.TokenText == "L" || Left.TokenText == "u" ||
4940 Left.TokenText == "U" || Left.TokenText == "u8" ||
4941 Left.TokenText == "LR" || Left.TokenText == "uR" ||
4942 Left.TokenText == "UR" || Left.TokenText == "u8R")) {
4943 return false;
4944 }
4945 if (Left.is(TT_TemplateCloser) && Left.MatchingParen &&
4946 Left.MatchingParen->Previous &&
4947 Left.MatchingParen->Previous->isOneOf(tok::period, tok::coloncolon)) {
4948 // Java call to generic function with explicit type:
4949 // A.<B<C<...>>>DoSomething();
4950 // A::<B<C<...>>>DoSomething(); // With a Java 8 method reference.
4951 return false;
4952 }
4953 if (Left.is(TT_TemplateCloser) && Right.is(tok::l_square))
4954 return false;
4955 if (Left.is(tok::l_brace) && Left.endsSequence(TT_DictLiteral, tok::at)) {
4956 // Objective-C dictionary literal -> no space after opening brace.
4957 return false;
4958 }
4959 if (Right.is(tok::r_brace) && Right.MatchingParen &&
4960 Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at)) {
4961 // Objective-C dictionary literal -> no space before closing brace.
4962 return false;
4963 }
4964 if (Right.is(TT_TrailingAnnotation) && Right.isOneOf(tok::amp, tok::ampamp) &&
4965 Left.isOneOf(tok::kw_const, tok::kw_volatile) &&
4966 (!Right.Next || Right.Next->is(tok::semi))) {
4967 // Match const and volatile ref-qualifiers without any additional
4968 // qualifiers such as
4969 // void Fn() const &;
4970 return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
4971 }
4972
4973 return true;
4974}
4975
4976bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
4977 const FormatToken &Right) const {
4978 const FormatToken &Left = *Right.Previous;
4979
4980 // If the token is finalized don't touch it (as it could be in a
4981 // clang-format-off section).
4982 if (Left.Finalized)
4983 return Right.hasWhitespaceBefore();
4984
4985 const bool IsVerilog = Style.isVerilog();
4986 assert(!IsVerilog || !IsCpp);
4987
4988 // Never ever merge two words.
4989 if (Keywords.isWordLike(Right, IsVerilog) &&
4990 Keywords.isWordLike(Left, IsVerilog)) {
4991 return true;
4992 }
4993
4994 // Leave a space between * and /* to avoid C4138 `comment end` found outside
4995 // of comment.
4996 if (Left.is(tok::star) && Right.is(tok::comment))
4997 return true;
4998
4999 if (Left.is(tok::l_brace) && Right.is(tok::r_brace) &&
5000 Left.Children.empty()) {
5001 if (Left.is(BK_Block))
5002 return Style.SpaceInEmptyBraces != FormatStyle::SIEB_Never;
5003 if (Style.Cpp11BracedListStyle != FormatStyle::BLS_Block) {
5004 return Style.SpacesInParens == FormatStyle::SIPO_Custom &&
5005 Style.SpacesInParensOptions.InEmptyParentheses;
5006 }
5007 return Style.SpaceInEmptyBraces == FormatStyle::SIEB_Always;
5008 }
5009
5010 const auto *BeforeLeft = Left.Previous;
5011
5012 if (IsCpp) {
5013 if (Left.is(TT_OverloadedOperator) &&
5014 Right.isOneOf(TT_TemplateOpener, TT_TemplateCloser)) {
5015 return true;
5016 }
5017 // Space between UDL and dot: auto b = 4s .count();
5018 if (Right.is(tok::period) && Left.is(tok::numeric_constant))
5019 return true;
5020 // Space between import <iostream>.
5021 // or import .....;
5022 if (Left.is(Keywords.kw_import) && Right.isOneOf(tok::less, tok::ellipsis))
5023 return true;
5024 // Space between `module :` and `import :`.
5025 if (Left.isOneOf(Keywords.kw_module, Keywords.kw_import) &&
5026 Right.is(TT_ModulePartitionColon)) {
5027 return true;
5028 }
5029
5030 if (Right.is(TT_AfterPPDirective))
5031 return true;
5032
5033 // No space between import foo:bar but keep a space between import :bar;
5034 if (Left.is(tok::identifier) && Right.is(TT_ModulePartitionColon))
5035 return false;
5036 // No space between :bar;
5037 if (Left.is(TT_ModulePartitionColon) &&
5038 Right.isOneOf(tok::identifier, tok::kw_private)) {
5039 return false;
5040 }
5041 if (Left.is(tok::ellipsis) && Right.is(tok::identifier) &&
5042 Line.First->is(Keywords.kw_import)) {
5043 return false;
5044 }
5045 // Space in __attribute__((attr)) ::type.
5046 if (Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
5047 Right.is(tok::coloncolon)) {
5048 return true;
5049 }
5050
5051 if (Left.is(tok::kw_operator))
5052 return Right.is(tok::coloncolon) || Style.SpaceAfterOperatorKeyword;
5053 if (Right.is(tok::l_brace) && Right.is(BK_BracedInit) &&
5054 !Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
5055 return true;
5056 }
5057 if (Left.is(tok::less) && Left.is(TT_OverloadedOperator) &&
5058 Right.is(TT_TemplateOpener)) {
5059 return true;
5060 }
5061 // C++ Core Guidelines suppression tag, e.g. `[[suppress(type.5)]]`.
5062 if (Left.is(tok::identifier) && Right.is(tok::numeric_constant))
5063 return Right.TokenText[0] != '.';
5064 // `Left` is a keyword (including C++ alternative operator) or identifier.
5065 if (Left.Tok.getIdentifierInfo() && Right.Tok.isLiteral())
5066 return true;
5067 } else if (Style.isProto()) {
5068 if (Right.is(tok::period) && !(BeforeLeft && BeforeLeft->is(tok::period)) &&
5069 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
5070 Keywords.kw_repeated, Keywords.kw_extend)) {
5071 return true;
5072 }
5073 if (Right.is(tok::l_paren) &&
5074 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)) {
5075 return true;
5076 }
5077 if (Right.isOneOf(tok::l_brace, tok::less) && Left.is(TT_SelectorName))
5078 return true;
5079 // Slashes occur in text protocol extension syntax: [type/type] { ... }.
5080 if (Left.is(tok::slash) || Right.is(tok::slash))
5081 return false;
5082 if (Left.MatchingParen &&
5083 Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
5084 Right.isOneOf(tok::l_brace, tok::less)) {
5085 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block;
5086 }
5087 // A percent is probably part of a formatting specification, such as %lld.
5088 if (Left.is(tok::percent))
5089 return false;
5090 // Preserve the existence of a space before a percent for cases like 0x%04x
5091 // and "%d %d"
5092 if (Left.is(tok::numeric_constant) && Right.is(tok::percent))
5093 return Right.hasWhitespaceBefore();
5094 } else if (Style.isJson()) {
5095 if (Right.is(tok::colon) && Left.is(tok::string_literal))
5096 return Style.SpaceBeforeJsonColon;
5097 } else if (Style.isCSharp()) {
5098 // Require spaces around '{' and before '}' unless they appear in
5099 // interpolated strings. Interpolated strings are merged into a single token
5100 // so cannot have spaces inserted by this function.
5101
5102 // No space between 'this' and '['
5103 if (Left.is(tok::kw_this) && Right.is(tok::l_square))
5104 return false;
5105
5106 // No space between 'new' and '('
5107 if (Left.is(tok::kw_new) && Right.is(tok::l_paren))
5108 return false;
5109
5110 // Space before { (including space within '{ {').
5111 if (Right.is(tok::l_brace))
5112 return true;
5113
5114 // Spaces inside braces.
5115 if (Left.is(tok::l_brace) && Right.isNot(tok::r_brace))
5116 return true;
5117
5118 if (Left.isNot(tok::l_brace) && Right.is(tok::r_brace))
5119 return true;
5120
5121 // Spaces around '=>'.
5122 if (Left.is(TT_FatArrow) || Right.is(TT_FatArrow))
5123 return true;
5124
5125 // No spaces around attribute target colons
5126 if (Left.is(TT_AttributeColon) || Right.is(TT_AttributeColon))
5127 return false;
5128
5129 // space between type and variable e.g. Dictionary<string,string> foo;
5130 if (Left.is(TT_TemplateCloser) && Right.is(TT_StartOfName))
5131 return true;
5132
5133 // spaces inside square brackets.
5134 if (Left.is(tok::l_square) || Right.is(tok::r_square))
5135 return Style.SpacesInSquareBrackets;
5136
5137 // No space before ? in nullable types.
5138 if (Right.is(TT_CSharpNullable))
5139 return false;
5140
5141 // No space before null forgiving '!'.
5142 if (Right.is(TT_NonNullAssertion))
5143 return false;
5144
5145 // No space between consecutive commas '[,,]'.
5146 if (Left.is(tok::comma) && Right.is(tok::comma))
5147 return false;
5148
5149 // space after var in `var (key, value)`
5150 if (Left.is(Keywords.kw_var) && Right.is(tok::l_paren))
5151 return true;
5152
5153 // space between keywords and paren e.g. "using ("
5154 if (Right.is(tok::l_paren)) {
5155 if (Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when,
5156 Keywords.kw_lock)) {
5157 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5158 spaceRequiredBeforeParens(Right);
5159 }
5160 }
5161
5162 // space between method modifier and opening parenthesis of a tuple return
5163 // type
5164 if ((Left.isAccessSpecifierKeyword() ||
5165 Left.isOneOf(tok::kw_virtual, tok::kw_extern, tok::kw_static,
5166 Keywords.kw_internal, Keywords.kw_abstract,
5167 Keywords.kw_sealed, Keywords.kw_override,
5168 Keywords.kw_async, Keywords.kw_unsafe)) &&
5169 Right.is(tok::l_paren)) {
5170 return true;
5171 }
5172 } else if (Style.isJavaScript()) {
5173 if (Left.is(TT_FatArrow))
5174 return true;
5175 // for await ( ...
5176 if (Right.is(tok::l_paren) && Left.is(Keywords.kw_await) && BeforeLeft &&
5177 BeforeLeft->is(tok::kw_for)) {
5178 return true;
5179 }
5180 if (Left.is(Keywords.kw_async) && Right.is(tok::l_paren) &&
5181 Right.MatchingParen) {
5182 const FormatToken *Next = Right.MatchingParen->getNextNonComment();
5183 // An async arrow function, for example: `x = async () => foo();`,
5184 // as opposed to calling a function called async: `x = async();`
5185 if (Next && Next->is(TT_FatArrow))
5186 return true;
5187 }
5188 if ((Left.is(TT_TemplateString) && Left.TokenText.ends_with("${")) ||
5189 (Right.is(TT_TemplateString) && Right.TokenText.starts_with("}"))) {
5190 return false;
5191 }
5192 // In tagged template literals ("html`bar baz`"), there is no space between
5193 // the tag identifier and the template string.
5194 if (Keywords.isJavaScriptIdentifier(Left,
5195 /* AcceptIdentifierName= */ false) &&
5196 Right.is(TT_TemplateString)) {
5197 return false;
5198 }
5199 if (Right.is(tok::star) &&
5200 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)) {
5201 return false;
5202 }
5203 if (Right.isOneOf(tok::l_brace, tok::l_square) &&
5204 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
5205 Keywords.kw_extends, Keywords.kw_implements)) {
5206 return true;
5207 }
5208 if (Right.is(tok::l_paren)) {
5209 // JS methods can use some keywords as names (e.g. `delete()`).
5210 if (Line.MustBeDeclaration && Left.Tok.getIdentifierInfo())
5211 return false;
5212 // Valid JS method names can include keywords, e.g. `foo.delete()` or
5213 // `bar.instanceof()`. Recognize call positions by preceding period.
5214 if (BeforeLeft && BeforeLeft->is(tok::period) &&
5215 Left.Tok.getIdentifierInfo()) {
5216 return false;
5217 }
5218 // Additional unary JavaScript operators that need a space after.
5219 if (Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
5220 tok::kw_void)) {
5221 return true;
5222 }
5223 }
5224 // `foo as const;` casts into a const type.
5225 if (Left.endsSequence(tok::kw_const, Keywords.kw_as))
5226 return false;
5227 if ((Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
5228 tok::kw_const) ||
5229 // "of" is only a keyword if it appears after another identifier
5230 // (e.g. as "const x of y" in a for loop), or after a destructuring
5231 // operation (const [x, y] of z, const {a, b} of c).
5232 (Left.is(Keywords.kw_of) && BeforeLeft &&
5233 BeforeLeft->isOneOf(tok::identifier, tok::r_square, tok::r_brace))) &&
5234 (!BeforeLeft || BeforeLeft->isNot(tok::period))) {
5235 return true;
5236 }
5237 if (Left.isOneOf(tok::kw_for, Keywords.kw_as) && BeforeLeft &&
5238 BeforeLeft->is(tok::period) && Right.is(tok::l_paren)) {
5239 return false;
5240 }
5241 if (Left.is(Keywords.kw_as) &&
5242 Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)) {
5243 return true;
5244 }
5245 if (Left.is(tok::kw_default) && BeforeLeft &&
5246 BeforeLeft->is(tok::kw_export)) {
5247 return true;
5248 }
5249 if (Left.is(Keywords.kw_is) && Right.is(tok::l_brace))
5250 return true;
5251 if (Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
5252 return false;
5253 if (Left.is(TT_JsTypeOperator) || Right.is(TT_JsTypeOperator))
5254 return false;
5255 if ((Left.is(tok::l_brace) || Right.is(tok::r_brace)) &&
5256 Line.First->isOneOf(Keywords.kw_import, tok::kw_export)) {
5257 return false;
5258 }
5259 if (Left.is(tok::ellipsis))
5260 return false;
5261 if (Left.is(TT_TemplateCloser) &&
5262 Right.isNoneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
5263 Keywords.kw_implements, Keywords.kw_extends)) {
5264 // Type assertions ('<type>expr') are not followed by whitespace. Other
5265 // locations that should have whitespace following are identified by the
5266 // above set of follower tokens.
5267 return false;
5268 }
5269 if (Right.is(TT_NonNullAssertion))
5270 return false;
5271 if (Left.is(TT_NonNullAssertion) &&
5272 Right.isOneOf(Keywords.kw_as, Keywords.kw_in)) {
5273 return true; // "x! as string", "x! in y"
5274 }
5275 } else if (Style.isJava()) {
5276 if (Left.is(TT_CaseLabelArrow) || Right.is(TT_CaseLabelArrow))
5277 return true;
5278 if (Left.is(tok::r_square) && Right.is(tok::l_brace))
5279 return true;
5280 // spaces inside square brackets.
5281 if (Left.is(tok::l_square) || Right.is(tok::r_square))
5282 return Style.SpacesInSquareBrackets;
5283
5284 if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren)) {
5285 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5286 spaceRequiredBeforeParens(Right);
5287 }
5288 if ((Left.isAccessSpecifierKeyword() ||
5289 Left.isOneOf(tok::kw_static, Keywords.kw_final, Keywords.kw_abstract,
5290 Keywords.kw_native)) &&
5291 Right.is(TT_TemplateOpener)) {
5292 return true;
5293 }
5294 } else if (IsVerilog) {
5295 // An escaped identifier ends with whitespace.
5296 if (Left.is(tok::identifier) && Left.TokenText[0] == '\\')
5297 return true;
5298 // Add space between things in a primitive's state table unless in a
5299 // transition like `(0?)`.
5300 if ((Left.is(TT_VerilogTableItem) &&
5301 Right.isNoneOf(tok::r_paren, tok::semi)) ||
5302 (Right.is(TT_VerilogTableItem) && Left.isNot(tok::l_paren))) {
5303 const FormatToken *Next = Right.getNextNonComment();
5304 return !(Next && Next->is(tok::r_paren));
5305 }
5306 // Don't add space within a delay like `#0`.
5307 if (Left.isNot(TT_BinaryOperator) &&
5308 Left.isOneOf(Keywords.kw_verilogHash, Keywords.kw_verilogHashHash)) {
5309 return false;
5310 }
5311 // Add space after a delay.
5312 if (Right.isNot(tok::semi) &&
5313 (Left.endsSequence(tok::numeric_constant, Keywords.kw_verilogHash) ||
5314 Left.endsSequence(tok::numeric_constant,
5315 Keywords.kw_verilogHashHash) ||
5316 (Left.is(tok::r_paren) && Left.MatchingParen &&
5317 Left.MatchingParen->endsSequence(tok::l_paren, tok::at)))) {
5318 return true;
5319 }
5320 // Don't add embedded spaces in a number literal like `16'h1?ax` or an array
5321 // literal like `'{}`.
5322 if (Left.is(Keywords.kw_apostrophe) ||
5323 (Left.is(TT_VerilogNumberBase) && Right.is(tok::numeric_constant))) {
5324 return false;
5325 }
5326 // Add spaces around the implication operator `->`.
5327 if (Left.is(tok::arrow) || Right.is(tok::arrow))
5328 return true;
5329 // Don't add spaces between two at signs. Like in a coverage event.
5330 // Don't add spaces between at and a sensitivity list like
5331 // `@(posedge clk)`.
5332 if (Left.is(tok::at) && Right.isOneOf(tok::l_paren, tok::star, tok::at))
5333 return false;
5334 // Add space between the type name and dimension like `logic [1:0]`.
5335 if (Right.is(tok::l_square) &&
5336 Left.isOneOf(TT_VerilogDimensionedTypeName, Keywords.kw_function)) {
5337 return true;
5338 }
5339 // In a tagged union expression, there should be a space after the tag.
5340 if (Right.isOneOf(tok::period, Keywords.kw_apostrophe) &&
5341 Keywords.isVerilogIdentifier(Left) && Left.getPreviousNonComment() &&
5342 Left.getPreviousNonComment()->is(Keywords.kw_tagged)) {
5343 return true;
5344 }
5345 // Don't add spaces between a casting type and the quote or repetition count
5346 // and the brace. The case of tagged union expressions is handled by the
5347 // previous rule.
5348 if ((Right.is(Keywords.kw_apostrophe) ||
5349 (Right.is(BK_BracedInit) && Right.is(tok::l_brace))) &&
5350 Left.isNoneOf(Keywords.kw_assign, Keywords.kw_unique) &&
5351 !Keywords.isVerilogWordOperator(Left) &&
5352 (Left.isOneOf(tok::r_square, tok::r_paren, tok::r_brace,
5353 tok::numeric_constant) ||
5354 Keywords.isWordLike(Left))) {
5355 return false;
5356 }
5357 // Don't add spaces in imports like `import foo::*;`.
5358 if ((Right.is(tok::star) && Left.is(tok::coloncolon)) ||
5359 (Left.is(tok::star) && Right.is(tok::semi))) {
5360 return false;
5361 }
5362 // Add space in attribute like `(* ASYNC_REG = "TRUE" *)`.
5363 if (Left.endsSequence(tok::star, tok::l_paren) && Right.is(tok::identifier))
5364 return true;
5365 // Add space before drive strength like in `wire (strong1, pull0)`.
5366 if (Right.is(tok::l_paren) && Right.is(TT_VerilogStrength))
5367 return true;
5368 // Don't add space in a streaming concatenation like `{>>{j}}`.
5369 if ((Left.is(tok::l_brace) &&
5370 Right.isOneOf(tok::lessless, tok::greatergreater)) ||
5371 (Left.endsSequence(tok::lessless, tok::l_brace) ||
5372 Left.endsSequence(tok::greatergreater, tok::l_brace))) {
5373 return false;
5374 }
5375 } else if (Style.isTableGen()) {
5376 // Avoid to connect [ and {. [{ is start token of multiline string.
5377 if (Left.is(tok::l_square) && Right.is(tok::l_brace))
5378 return true;
5379 if (Left.is(tok::r_brace) && Right.is(tok::r_square))
5380 return true;
5381 // Do not insert around colon in DAGArg and cond operator.
5382 if (Right.isOneOf(TT_TableGenDAGArgListColon,
5383 TT_TableGenDAGArgListColonToAlign) ||
5384 Left.isOneOf(TT_TableGenDAGArgListColon,
5385 TT_TableGenDAGArgListColonToAlign)) {
5386 return false;
5387 }
5388 if (Right.is(TT_TableGenCondOperatorColon))
5389 return false;
5390 if (Left.isOneOf(TT_TableGenDAGArgOperatorID,
5391 TT_TableGenDAGArgOperatorToBreak) &&
5392 Right.isNot(TT_TableGenDAGArgCloser)) {
5393 return true;
5394 }
5395 // Do not insert bang operators and consequent openers.
5396 if (Right.isOneOf(tok::l_paren, tok::less) &&
5397 Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
5398 return false;
5399 }
5400 // Trailing paste requires space before '{' or ':', the case in name values.
5401 // Not before ';', the case in normal values.
5402 if (Left.is(TT_TableGenTrailingPasteOperator) &&
5403 Right.isOneOf(tok::l_brace, tok::colon)) {
5404 return true;
5405 }
5406 // Otherwise paste operator does not prefer space around.
5407 if (Left.is(tok::hash) || Right.is(tok::hash))
5408 return false;
5409 // Sure not to connect after defining keywords.
5410 if (Keywords.isTableGenDefinition(Left))
5411 return true;
5412 }
5413
5414 if (Left.is(TT_ImplicitStringLiteral))
5415 return Right.hasWhitespaceBefore();
5416 if (Line.Type == LT_ObjCMethodDecl) {
5417 if (Left.is(TT_ObjCMethodSpecifier))
5418 return true;
5419 if (Left.is(tok::r_paren) && Left.isNot(TT_AttributeRParen) &&
5420 canBeObjCSelectorComponent(Right)) {
5421 // Don't space between ')' and <id> or ')' and 'new'. 'new' is not a
5422 // keyword in Objective-C, and '+ (instancetype)new;' is a standard class
5423 // method declaration.
5424 return false;
5425 }
5426 }
5427 if (Line.Type == LT_ObjCProperty &&
5428 (Right.is(tok::equal) || Left.is(tok::equal))) {
5429 return false;
5430 }
5431
5432 if (Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) ||
5433 Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow)) {
5434 return true;
5435 }
5436 if (Left.is(tok::comma) && Right.isNot(TT_OverloadedOperatorLParen) &&
5437 // In an unexpanded macro call we only find the parentheses and commas
5438 // in a line; the commas and closing parenthesis do not require a space.
5439 (Left.Children.empty() || !Left.MacroParent)) {
5440 return true;
5441 }
5442 if (Right.is(tok::comma))
5443 return false;
5444 if (Right.is(TT_ObjCBlockLParen))
5445 return true;
5446 if (Right.is(TT_CtorInitializerColon))
5447 return Style.SpaceBeforeCtorInitializerColon;
5448 if (Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
5449 return false;
5450 if (Right.is(TT_RangeBasedForLoopColon) &&
5451 !Style.SpaceBeforeRangeBasedForLoopColon) {
5452 return false;
5453 }
5454 if (Left.is(TT_BitFieldColon)) {
5455 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
5456 Style.BitFieldColonSpacing == FormatStyle::BFCS_After;
5457 }
5458 if (Right.is(tok::colon)) {
5459 if (Right.is(TT_CaseLabelColon))
5460 return Style.SpaceBeforeCaseColon;
5461 if (Right.is(TT_GotoLabelColon))
5462 return false;
5463 // `private:` and `public:`.
5464 if (!Right.getNextNonComment())
5465 return false;
5466 if (Right.isOneOf(TT_ObjCSelector, TT_ObjCMethodExpr))
5467 return false;
5468 if (Left.is(tok::question))
5469 return false;
5470 if (Right.is(TT_InlineASMColon) && Left.is(tok::coloncolon))
5471 return false;
5472 if (Right.is(TT_DictLiteral))
5473 return Style.SpacesInContainerLiterals;
5474 if (Right.is(TT_AttributeColon))
5475 return false;
5476 if (Right.is(TT_CSharpNamedArgumentColon))
5477 return false;
5478 if (Right.is(TT_GenericSelectionColon))
5479 return false;
5480 if (Right.is(TT_BitFieldColon)) {
5481 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
5482 Style.BitFieldColonSpacing == FormatStyle::BFCS_Before;
5483 }
5484 return true;
5485 }
5486 // Do not merge "- -" into "--".
5487 if ((Left.isOneOf(tok::minus, tok::minusminus) &&
5488 Right.isOneOf(tok::minus, tok::minusminus)) ||
5489 (Left.isOneOf(tok::plus, tok::plusplus) &&
5490 Right.isOneOf(tok::plus, tok::plusplus))) {
5491 return true;
5492 }
5493 if (Left.is(TT_UnaryOperator)) {
5494 // Lambda captures allow for a lone &, so "&]" needs to be properly
5495 // handled.
5496 if (Left.is(tok::amp) && Right.is(tok::r_square))
5497 return Style.SpacesInSquareBrackets;
5498 if (Left.isNot(tok::exclaim))
5499 return false;
5500 if (Left.TokenText == "!")
5501 return Style.SpaceAfterLogicalNot;
5502 assert(Left.TokenText == "not");
5503 return Right.isOneOf(tok::coloncolon, TT_UnaryOperator) ||
5504 (Right.is(tok::l_paren) && Style.SpaceBeforeParensOptions.AfterNot);
5505 }
5506
5507 // If the next token is a binary operator or a selector name, we have
5508 // incorrectly classified the parenthesis as a cast. FIXME: Detect correctly.
5509 if (Left.is(TT_CastRParen)) {
5510 return Style.SpaceAfterCStyleCast ||
5511 Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
5512 }
5513
5514 auto ShouldAddSpacesInAngles = [this, &Right]() {
5515 if (this->Style.SpacesInAngles == FormatStyle::SIAS_Always)
5516 return true;
5517 if (this->Style.SpacesInAngles == FormatStyle::SIAS_Leave)
5518 return Right.hasWhitespaceBefore();
5519 return false;
5520 };
5521
5522 if (Left.is(tok::greater) && Right.is(tok::greater)) {
5523 if (Style.isTextProto() ||
5524 (Style.Language == FormatStyle::LK_Proto && Left.is(TT_DictLiteral))) {
5525 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block;
5526 }
5527 return Right.is(TT_TemplateCloser) && Left.is(TT_TemplateCloser) &&
5528 ((Style.Standard < FormatStyle::LS_Cpp11) ||
5529 ShouldAddSpacesInAngles());
5530 }
5531 if (Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
5532 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
5533 (Right.is(tok::period) && Right.isNot(TT_DesignatedInitializerPeriod))) {
5534 return false;
5535 }
5536 if (!Style.SpaceBeforeAssignmentOperators && Left.isNot(TT_TemplateCloser) &&
5537 Right.getPrecedence() == prec::Assignment) {
5538 return false;
5539 }
5540 if (Style.isJava() && Right.is(tok::coloncolon) &&
5541 Left.isOneOf(tok::identifier, tok::kw_this)) {
5542 return false;
5543 }
5544 if (Right.is(tok::coloncolon) && Left.is(tok::identifier)) {
5545 // Generally don't remove existing spaces between an identifier and "::".
5546 // The identifier might actually be a macro name such as ALWAYS_INLINE. If
5547 // this turns out to be too lenient, add analysis of the identifier itself.
5548 return Right.hasWhitespaceBefore();
5549 }
5550 if (Right.is(tok::coloncolon) &&
5551 Left.isNoneOf(tok::l_brace, tok::comment, tok::l_paren)) {
5552 // Put a space between < and :: in vector< ::std::string >
5553 return (Left.is(TT_TemplateOpener) &&
5554 ((Style.Standard < FormatStyle::LS_Cpp11) ||
5555 ShouldAddSpacesInAngles())) ||
5556 Left.isNoneOf(tok::l_paren, tok::r_paren, tok::l_square,
5557 tok::kw___super, TT_TemplateOpener,
5558 TT_TemplateCloser) ||
5559 (Left.is(tok::l_paren) && Style.SpacesInParensOptions.Other);
5560 }
5561 if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser)))
5562 return ShouldAddSpacesInAngles();
5563 if (Left.is(tok::r_paren) && Left.isNot(TT_TypeDeclarationParen) &&
5564 Right.is(TT_PointerOrReference) && Right.isOneOf(tok::amp, tok::ampamp)) {
5565 return true;
5566 }
5567 // Space before TT_StructuredBindingLSquare.
5568 if (Right.is(TT_StructuredBindingLSquare)) {
5569 return Left.isNoneOf(tok::amp, tok::ampamp) ||
5570 getTokenReferenceAlignment(Left) != FormatStyle::PAS_Right;
5571 }
5572 // Space before & or && following a TT_StructuredBindingLSquare.
5573 if (Right.Next && Right.Next->is(TT_StructuredBindingLSquare) &&
5574 Right.isOneOf(tok::amp, tok::ampamp)) {
5575 return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
5576 }
5577 if ((Right.is(TT_BinaryOperator) && Left.isNot(tok::l_paren)) ||
5578 (Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) &&
5579 Right.isNot(tok::r_paren))) {
5580 return true;
5581 }
5582 if (Right.is(TT_TemplateOpener) && Left.is(tok::r_paren) &&
5583 Left.MatchingParen &&
5584 Left.MatchingParen->is(TT_OverloadedOperatorLParen)) {
5585 return false;
5586 }
5587 if (Right.is(tok::less) && Left.isNot(tok::l_paren) &&
5588 Line.Type == LT_ImportStatement) {
5589 return true;
5590 }
5591 if (Right.is(TT_TrailingUnaryOperator))
5592 return false;
5593 if (Left.is(TT_RegexLiteral))
5594 return false;
5595 return spaceRequiredBetween(Line, Left, Right);
5596}
5597
5598// Returns 'true' if 'Tok' is a brace we'd want to break before in Allman style.
5599static bool isAllmanBrace(const FormatToken &Tok) {
5600 return Tok.is(tok::l_brace) && Tok.is(BK_Block) &&
5601 Tok.isNoneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral);
5602}
5603
5604// Returns 'true' if 'Tok' is a function argument.
5606 return Tok.MatchingParen && Tok.MatchingParen->Next &&
5607 Tok.MatchingParen->Next->isOneOf(tok::comma, tok::r_paren,
5608 tok::r_brace);
5609}
5610
5611static bool
5613 FormatStyle::ShortLambdaStyle ShortLambdaOption) {
5614 return Tok.Children.empty() && ShortLambdaOption != FormatStyle::SLS_None;
5615}
5616
5618 return Tok.is(tok::l_brace) && Tok.is(BK_Block) &&
5619 Tok.isNoneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
5620}
5621
5622bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
5623 const FormatToken &Right) const {
5624 if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0 &&
5625 (!Style.RemoveEmptyLinesInUnwrappedLines || &Right == Line.First)) {
5626 return true;
5627 }
5628
5629 const FormatToken &Left = *Right.Previous;
5630
5631 if (Style.BreakFunctionDefinitionParameters && Line.MightBeFunctionDecl &&
5632 Line.mightBeFunctionDefinition() && Left.MightBeFunctionDeclParen &&
5633 Left.ParameterCount > 0) {
5634 return true;
5635 }
5636
5637 // Ignores the first parameter as this will be handled separately by
5638 // BreakFunctionDefinitionParameters or AlignAfterOpenBracket.
5639 if (Style.BinPackParameters == FormatStyle::BPPS_AlwaysOnePerLine &&
5640 Line.MightBeFunctionDecl && !Left.opensScope() &&
5641 startsNextParameter(Right, Style)) {
5642 return true;
5643 }
5644
5645 const auto *BeforeLeft = Left.Previous;
5646 const auto *AfterRight = Right.Next;
5647
5648 if (Style.isCSharp()) {
5649 if (Left.is(TT_FatArrow) && Right.is(tok::l_brace) &&
5650 Style.BraceWrapping.AfterFunction) {
5651 return true;
5652 }
5653 if (Right.is(TT_CSharpNamedArgumentColon) ||
5654 Left.is(TT_CSharpNamedArgumentColon)) {
5655 return false;
5656 }
5657 if (Right.is(TT_CSharpGenericTypeConstraint))
5658 return true;
5659 if (AfterRight && AfterRight->is(TT_FatArrow) &&
5660 (Right.is(tok::numeric_constant) ||
5661 (Right.is(tok::identifier) && Right.TokenText == "_"))) {
5662 return true;
5663 }
5664
5665 // Break after C# [...] and before public/protected/private/internal.
5666 if (Left.is(TT_AttributeRSquare) &&
5667 (Right.isAccessSpecifier(/*ColonRequired=*/false) ||
5668 Right.is(Keywords.kw_internal))) {
5669 return true;
5670 }
5671 // Break between ] and [ but only when there are really 2 attributes.
5672 if (Left.is(TT_AttributeRSquare) && Right.is(TT_AttributeLSquare))
5673 return true;
5674 } else if (Style.isJavaScript()) {
5675 // FIXME: This might apply to other languages and token kinds.
5676 if (Right.is(tok::string_literal) && Left.is(tok::plus) && BeforeLeft &&
5677 BeforeLeft->is(tok::string_literal)) {
5678 return true;
5679 }
5680 if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) && Line.Level == 0 &&
5681 BeforeLeft && BeforeLeft->is(tok::equal) &&
5682 Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
5683 tok::kw_const) &&
5684 // kw_var/kw_let are pseudo-tokens that are tok::identifier, so match
5685 // above.
5686 Line.First->isNoneOf(Keywords.kw_var, Keywords.kw_let)) {
5687 // Object literals on the top level of a file are treated as "enum-style".
5688 // Each key/value pair is put on a separate line, instead of bin-packing.
5689 return true;
5690 }
5691 if (Left.is(tok::l_brace) && Line.Level == 0 &&
5692 (Line.startsWith(tok::kw_enum) ||
5693 Line.startsWith(tok::kw_const, tok::kw_enum) ||
5694 Line.startsWith(tok::kw_export, tok::kw_enum) ||
5695 Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum))) {
5696 // JavaScript top-level enum key/value pairs are put on separate lines
5697 // instead of bin-packing.
5698 return true;
5699 }
5700 if (Right.is(tok::r_brace) && Left.is(tok::l_brace) && BeforeLeft &&
5701 BeforeLeft->is(TT_FatArrow)) {
5702 // JS arrow function (=> {...}).
5703 switch (Style.AllowShortLambdasOnASingleLine) {
5704 case FormatStyle::SLS_All:
5705 return false;
5706 case FormatStyle::SLS_None:
5707 return true;
5708 case FormatStyle::SLS_Empty:
5709 return !Left.Children.empty();
5710 case FormatStyle::SLS_Inline:
5711 // allow one-lining inline (e.g. in function call args) and empty arrow
5712 // functions.
5713 return (Left.NestingLevel == 0 && Line.Level == 0) &&
5714 !Left.Children.empty();
5715 }
5716 llvm_unreachable("Unknown FormatStyle::ShortLambdaStyle enum");
5717 }
5718
5719 if (Right.is(tok::r_brace) && Left.is(tok::l_brace) &&
5720 !Left.Children.empty()) {
5721 // Support AllowShortFunctionsOnASingleLine for JavaScript.
5722 return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None ||
5723 Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Empty ||
5724 (Left.NestingLevel == 0 && Line.Level == 0 &&
5725 Style.AllowShortFunctionsOnASingleLine &
5726 FormatStyle::SFS_InlineOnly);
5727 }
5728 } else if (Style.isJava()) {
5729 if (Right.is(tok::plus) && Left.is(tok::string_literal) && AfterRight &&
5730 AfterRight->is(tok::string_literal)) {
5731 return true;
5732 }
5733 } else if (Style.isVerilog()) {
5734 // Break between assignments.
5735 if (Left.is(TT_VerilogAssignComma))
5736 return true;
5737 // Break between ports of different types.
5738 if (Left.is(TT_VerilogTypeComma))
5739 return true;
5740 // Break between ports in a module instantiation and after the parameter
5741 // list.
5742 if (Style.VerilogBreakBetweenInstancePorts &&
5743 (Left.is(TT_VerilogInstancePortComma) ||
5744 (Left.is(tok::r_paren) && Keywords.isVerilogIdentifier(Right) &&
5745 Left.MatchingParen &&
5746 Left.MatchingParen->is(TT_VerilogInstancePortLParen)))) {
5747 return true;
5748 }
5749 // Break after labels. In Verilog labels don't have the 'case' keyword, so
5750 // it is hard to identify them in UnwrappedLineParser.
5751 if (!Keywords.isVerilogBegin(Right) && Keywords.isVerilogEndOfLabel(Left))
5752 return true;
5753 } else if (Style.BreakAdjacentStringLiterals &&
5754 (IsCpp || Style.isProto() || Style.isTableGen())) {
5755 if (Left.isStringLiteral() && Right.isStringLiteral())
5756 return true;
5757 }
5758
5759 // Basic JSON newline processing.
5760 if (Style.isJson()) {
5761 // Always break after a JSON record opener.
5762 // {
5763 // }
5764 if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace))
5765 return true;
5766 // Always break after a JSON array opener based on BreakArrays.
5767 if ((Left.is(TT_ArrayInitializerLSquare) && Left.is(tok::l_square) &&
5768 Right.isNot(tok::r_square)) ||
5769 Left.is(tok::comma)) {
5770 if (Right.is(tok::l_brace))
5771 return true;
5772 // scan to the right if an we see an object or an array inside
5773 // then break.
5774 for (const auto *Tok = &Right; Tok; Tok = Tok->Next) {
5775 if (Tok->isOneOf(tok::l_brace, tok::l_square))
5776 return true;
5777 if (Tok->isOneOf(tok::r_brace, tok::r_square))
5778 break;
5779 }
5780 return Style.BreakArrays;
5781 }
5782 } else if (Style.isTableGen()) {
5783 // Break the comma in side cond operators.
5784 // !cond(case1:1,
5785 // case2:0);
5786 if (Left.is(TT_TableGenCondOperatorComma))
5787 return true;
5788 if (Left.is(TT_TableGenDAGArgOperatorToBreak) &&
5789 Right.isNot(TT_TableGenDAGArgCloser)) {
5790 return true;
5791 }
5792 if (Left.is(TT_TableGenDAGArgListCommaToBreak))
5793 return true;
5794 if (Right.is(TT_TableGenDAGArgCloser) && Right.MatchingParen &&
5795 Right.MatchingParen->is(TT_TableGenDAGArgOpenerToBreak) &&
5796 &Left != Right.MatchingParen->Next) {
5797 // Check to avoid empty DAGArg such as (ins).
5798 return Style.TableGenBreakInsideDAGArg == FormatStyle::DAS_BreakAll;
5799 }
5800 }
5801
5802 if (Line.startsWith(tok::kw_asm) && Right.is(TT_InlineASMColon) &&
5803 Style.BreakBeforeInlineASMColon == FormatStyle::BBIAS_Always) {
5804 return true;
5805 }
5806
5807 // If the last token before a '}', ']', or ')' is a comma or a trailing
5808 // comment, the intention is to insert a line break after it in order to make
5809 // shuffling around entries easier. Import statements, especially in
5810 // JavaScript, can be an exception to this rule.
5811 if (Style.JavaScriptWrapImports || Line.Type != LT_ImportStatement) {
5812 const FormatToken *BeforeClosingBrace = nullptr;
5813 if ((Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
5814 (Style.isJavaScript() && Left.is(tok::l_paren))) &&
5815 Left.isNot(BK_Block) && Left.MatchingParen) {
5816 BeforeClosingBrace = Left.MatchingParen->Previous;
5817 } else if (Right.MatchingParen &&
5818 (Right.MatchingParen->isOneOf(tok::l_brace,
5819 TT_ArrayInitializerLSquare) ||
5820 (Style.isJavaScript() &&
5821 Right.MatchingParen->is(tok::l_paren)))) {
5822 BeforeClosingBrace = &Left;
5823 }
5824 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
5825 BeforeClosingBrace->isTrailingComment())) {
5826 return true;
5827 }
5828 }
5829
5830 if (Right.is(tok::comment)) {
5831 return Left.isNoneOf(BK_BracedInit, TT_CtorInitializerColon) &&
5832 Right.NewlinesBefore > 0 && Right.HasUnescapedNewline;
5833 }
5834 if (Left.isTrailingComment())
5835 return true;
5836 if (Left.IsUnterminatedLiteral)
5837 return true;
5838
5839 if (BeforeLeft && BeforeLeft->is(tok::lessless) &&
5840 Left.is(tok::string_literal) && Right.is(tok::lessless) && AfterRight &&
5841 AfterRight->is(tok::string_literal)) {
5842 return Right.NewlinesBefore > 0;
5843 }
5844
5845 if (Right.is(TT_RequiresClause)) {
5846 switch (Style.RequiresClausePosition) {
5847 case FormatStyle::RCPS_OwnLine:
5848 case FormatStyle::RCPS_OwnLineWithBrace:
5849 case FormatStyle::RCPS_WithFollowing:
5850 return true;
5851 default:
5852 break;
5853 }
5854 }
5855 // Can break after template<> declaration
5856 if (Left.ClosesTemplateDeclaration && Left.MatchingParen &&
5857 Left.MatchingParen->NestingLevel == 0) {
5858 // Put concepts on the next line e.g.
5859 // template<typename T>
5860 // concept ...
5861 if (Right.is(tok::kw_concept))
5862 return Style.BreakBeforeConceptDeclarations == FormatStyle::BBCDS_Always;
5863 return Style.BreakTemplateDeclarations == FormatStyle::BTDS_Yes ||
5864 (Style.BreakTemplateDeclarations == FormatStyle::BTDS_Leave &&
5865 Right.NewlinesBefore > 0);
5866 }
5867 if (Left.ClosesRequiresClause) {
5868 switch (Style.RequiresClausePosition) {
5869 case FormatStyle::RCPS_OwnLine:
5870 case FormatStyle::RCPS_WithPreceding:
5871 return Right.isNot(tok::semi);
5872 case FormatStyle::RCPS_OwnLineWithBrace:
5873 return Right.isNoneOf(tok::semi, tok::l_brace);
5874 default:
5875 break;
5876 }
5877 }
5878 if (Style.PackConstructorInitializers == FormatStyle::PCIS_Never) {
5879 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon &&
5880 (Left.is(TT_CtorInitializerComma) ||
5881 Right.is(TT_CtorInitializerColon))) {
5882 return true;
5883 }
5884
5885 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
5886 Left.isOneOf(TT_CtorInitializerColon, TT_CtorInitializerComma)) {
5887 return true;
5888 }
5889 }
5890 if (Style.PackConstructorInitializers < FormatStyle::PCIS_CurrentLine &&
5891 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma &&
5892 Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) {
5893 return true;
5894 }
5895 if (Style.PackConstructorInitializers == FormatStyle::PCIS_NextLineOnly) {
5896 if ((Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon ||
5897 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) &&
5898 Right.is(TT_CtorInitializerColon)) {
5899 return true;
5900 }
5901
5902 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
5903 Left.is(TT_CtorInitializerColon)) {
5904 return true;
5905 }
5906 }
5907 // Break only if we have multiple inheritance.
5908 if (Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma &&
5909 Right.is(TT_InheritanceComma)) {
5910 return true;
5911 }
5912 if (Style.BreakInheritanceList == FormatStyle::BILS_AfterComma &&
5913 Left.is(TT_InheritanceComma)) {
5914 return true;
5915 }
5916 if (Right.is(tok::string_literal) && Right.TokenText.starts_with("R\"")) {
5917 // Multiline raw string literals are special wrt. line breaks. The author
5918 // has made a deliberate choice and might have aligned the contents of the
5919 // string literal accordingly. Thus, we try keep existing line breaks.
5920 return Right.IsMultiline && Right.NewlinesBefore > 0;
5921 }
5922 if ((Left.is(tok::l_brace) ||
5923 (Left.is(tok::less) && BeforeLeft && BeforeLeft->is(tok::equal))) &&
5924 Right.NestingLevel == 1 && Style.Language == FormatStyle::LK_Proto) {
5925 // Don't put enums or option definitions onto single lines in protocol
5926 // buffers.
5927 return true;
5928 }
5929 if (Right.is(TT_InlineASMBrace))
5930 return Right.HasUnescapedNewline;
5931
5932 if (isAllmanBrace(Left) || isAllmanBrace(Right)) {
5933 auto *FirstNonComment = Line.getFirstNonComment();
5934 bool AccessSpecifier =
5935 FirstNonComment && (FirstNonComment->is(Keywords.kw_internal) ||
5936 FirstNonComment->isAccessSpecifierKeyword());
5937
5938 if (Style.BraceWrapping.AfterEnum) {
5939 if (Line.startsWith(tok::kw_enum) ||
5940 Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
5941 return true;
5942 }
5943 // Ensure BraceWrapping for `public enum A {`.
5944 if (AccessSpecifier && FirstNonComment->Next &&
5945 FirstNonComment->Next->is(tok::kw_enum)) {
5946 return true;
5947 }
5948 }
5949
5950 // Ensure BraceWrapping for `public interface A {`.
5951 if (Style.BraceWrapping.AfterClass &&
5952 ((AccessSpecifier && FirstNonComment->Next &&
5953 FirstNonComment->Next->is(Keywords.kw_interface)) ||
5954 Line.startsWith(Keywords.kw_interface))) {
5955 return true;
5956 }
5957
5958 // Don't attempt to interpret struct return types as structs.
5959 if (Right.isNot(TT_FunctionLBrace)) {
5960 return (Line.startsWith(tok::kw_class) &&
5961 Style.BraceWrapping.AfterClass) ||
5962 (Line.startsWith(tok::kw_struct) &&
5963 Style.BraceWrapping.AfterStruct);
5964 }
5965 }
5966
5967 if (Left.is(TT_ObjCBlockLBrace) &&
5968 Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never) {
5969 return true;
5970 }
5971
5972 // Ensure wrapping after __attribute__((XX)) and @interface etc.
5973 if (Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
5974 Right.is(TT_ObjCDecl)) {
5975 return true;
5976 }
5977
5978 if (Left.is(TT_LambdaLBrace)) {
5979 if (IsFunctionArgument(Left) &&
5980 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline) {
5981 return false;
5982 }
5983
5984 if (Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_None ||
5985 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline ||
5986 (!Left.Children.empty() &&
5987 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Empty)) {
5988 return true;
5989 }
5990 }
5991
5992 if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace) &&
5993 (Left.isPointerOrReference() || Left.is(TT_TemplateCloser))) {
5994 return true;
5995 }
5996
5997 // Put multiple Java annotation on a new line.
5998 if ((Style.isJava() || Style.isJavaScript()) &&
5999 Left.is(TT_LeadingJavaAnnotation) &&
6000 Right.isNoneOf(TT_LeadingJavaAnnotation, tok::l_paren) &&
6001 (Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) {
6002 return true;
6003 }
6004
6005 if (Right.is(TT_ProtoExtensionLSquare))
6006 return true;
6007
6008 // In text proto instances if a submessage contains at least 2 entries and at
6009 // least one of them is a submessage, like A { ... B { ... } ... },
6010 // put all of the entries of A on separate lines by forcing the selector of
6011 // the submessage B to be put on a newline.
6012 //
6013 // Example: these can stay on one line:
6014 // a { scalar_1: 1 scalar_2: 2 }
6015 // a { b { key: value } }
6016 //
6017 // and these entries need to be on a new line even if putting them all in one
6018 // line is under the column limit:
6019 // a {
6020 // scalar: 1
6021 // b { key: value }
6022 // }
6023 //
6024 // We enforce this by breaking before a submessage field that has previous
6025 // siblings, *and* breaking before a field that follows a submessage field.
6026 //
6027 // Be careful to exclude the case [proto.ext] { ... } since the `]` is
6028 // the TT_SelectorName there, but we don't want to break inside the brackets.
6029 //
6030 // Another edge case is @submessage { key: value }, which is a common
6031 // substitution placeholder. In this case we want to keep `@` and `submessage`
6032 // together.
6033 //
6034 // We ensure elsewhere that extensions are always on their own line.
6035 if (Style.isProto() && Right.is(TT_SelectorName) &&
6036 Right.isNot(tok::r_square) && AfterRight) {
6037 // Keep `@submessage` together in:
6038 // @submessage { key: value }
6039 if (Left.is(tok::at))
6040 return false;
6041 // Look for the scope opener after selector in cases like:
6042 // selector { ...
6043 // selector: { ...
6044 // selector: @base { ...
6045 const auto *LBrace = AfterRight;
6046 if (LBrace && LBrace->is(tok::colon)) {
6047 LBrace = LBrace->Next;
6048 if (LBrace && LBrace->is(tok::at)) {
6049 LBrace = LBrace->Next;
6050 if (LBrace)
6051 LBrace = LBrace->Next;
6052 }
6053 }
6054 if (LBrace &&
6055 // The scope opener is one of {, [, <:
6056 // selector { ... }
6057 // selector [ ... ]
6058 // selector < ... >
6059 //
6060 // In case of selector { ... }, the l_brace is TT_DictLiteral.
6061 // In case of an empty selector {}, the l_brace is not TT_DictLiteral,
6062 // so we check for immediately following r_brace.
6063 ((LBrace->is(tok::l_brace) &&
6064 (LBrace->is(TT_DictLiteral) ||
6065 (LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
6066 LBrace->isOneOf(TT_ArrayInitializerLSquare, tok::less))) {
6067 // If Left.ParameterCount is 0, then this submessage entry is not the
6068 // first in its parent submessage, and we want to break before this entry.
6069 // If Left.ParameterCount is greater than 0, then its parent submessage
6070 // might contain 1 or more entries and we want to break before this entry
6071 // if it contains at least 2 entries. We deal with this case later by
6072 // detecting and breaking before the next entry in the parent submessage.
6073 if (Left.ParameterCount == 0)
6074 return true;
6075 // However, if this submessage is the first entry in its parent
6076 // submessage, Left.ParameterCount might be 1 in some cases.
6077 // We deal with this case later by detecting an entry
6078 // following a closing paren of this submessage.
6079 }
6080
6081 // If this is an entry immediately following a submessage, it will be
6082 // preceded by a closing paren of that submessage, like in:
6083 // left---. .---right
6084 // v v
6085 // sub: { ... } key: value
6086 // If there was a comment between `}` an `key` above, then `key` would be
6087 // put on a new line anyways.
6088 if (Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
6089 return true;
6090 }
6091
6092 return false;
6093}
6094
6095bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
6096 const FormatToken &Right) const {
6097 const FormatToken &Left = *Right.Previous;
6098 // Language-specific stuff.
6099 if (Style.isCSharp()) {
6100 if (Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) ||
6101 Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon)) {
6102 return false;
6103 }
6104 // Only break after commas for generic type constraints.
6105 if (Line.First->is(TT_CSharpGenericTypeConstraint))
6106 return Left.is(TT_CSharpGenericTypeConstraintComma);
6107 // Keep nullable operators attached to their identifiers.
6108 if (Right.is(TT_CSharpNullable))
6109 return false;
6110 } else if (Style.isJava()) {
6111 if (Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6112 Keywords.kw_implements)) {
6113 return false;
6114 }
6115 if (Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
6116 Keywords.kw_implements)) {
6117 return true;
6118 }
6119 } else if (Style.isJavaScript()) {
6120 const FormatToken *NonComment = Right.getPreviousNonComment();
6121 if (NonComment &&
6122 (NonComment->isAccessSpecifierKeyword() ||
6123 NonComment->isOneOf(
6124 tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
6125 tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
6126 tok::kw_static, Keywords.kw_readonly, Keywords.kw_override,
6127 Keywords.kw_abstract, Keywords.kw_get, Keywords.kw_set,
6128 Keywords.kw_async, Keywords.kw_await))) {
6129 return false; // Otherwise automatic semicolon insertion would trigger.
6130 }
6131 if (Right.NestingLevel == 0 &&
6132 (Left.Tok.getIdentifierInfo() ||
6133 Left.isOneOf(tok::r_square, tok::r_paren)) &&
6134 Right.isOneOf(tok::l_square, tok::l_paren)) {
6135 return false; // Otherwise automatic semicolon insertion would trigger.
6136 }
6137 if (NonComment && NonComment->is(tok::identifier) &&
6138 NonComment->TokenText == "asserts") {
6139 return false;
6140 }
6141 if (Left.is(TT_FatArrow) && Right.is(tok::l_brace))
6142 return false;
6143 if (Left.is(TT_JsTypeColon))
6144 return true;
6145 // Don't wrap between ":" and "!" of a strict prop init ("field!: type;").
6146 if (Left.is(tok::exclaim) && Right.is(tok::colon))
6147 return false;
6148 // Look for is type annotations like:
6149 // function f(): a is B { ... }
6150 // Do not break before is in these cases.
6151 if (Right.is(Keywords.kw_is)) {
6152 const FormatToken *Next = Right.getNextNonComment();
6153 // If `is` is followed by a colon, it's likely that it's a dict key, so
6154 // ignore it for this check.
6155 // For example this is common in Polymer:
6156 // Polymer({
6157 // is: 'name',
6158 // ...
6159 // });
6160 if (!Next || Next->isNot(tok::colon))
6161 return false;
6162 }
6163 if (Left.is(Keywords.kw_in))
6164 return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None;
6165 if (Right.is(Keywords.kw_in))
6166 return Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None;
6167 if (Right.is(Keywords.kw_as))
6168 return false; // must not break before as in 'x as type' casts
6169 if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
6170 // extends and infer can appear as keywords in conditional types:
6171 // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#conditional-types
6172 // do not break before them, as the expressions are subject to ASI.
6173 return false;
6174 }
6175 if (Left.is(Keywords.kw_as))
6176 return true;
6177 if (Left.is(TT_NonNullAssertion))
6178 return true;
6179 if (Left.is(Keywords.kw_declare) &&
6180 Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
6181 Keywords.kw_function, tok::kw_class, tok::kw_enum,
6182 Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
6183 Keywords.kw_let, tok::kw_const)) {
6184 // See grammar for 'declare' statements at:
6185 // https://github.com/Microsoft/TypeScript/blob/main/doc/spec-ARCHIVED.md#A.10
6186 return false;
6187 }
6188 if (Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
6189 Right.isOneOf(tok::identifier, tok::string_literal)) {
6190 return false; // must not break in "module foo { ...}"
6191 }
6192 if (Right.is(TT_TemplateString) && Right.closesScope())
6193 return false;
6194 // Don't split tagged template literal so there is a break between the tag
6195 // identifier and template string.
6196 if (Left.is(tok::identifier) && Right.is(TT_TemplateString))
6197 return false;
6198 if (Left.is(TT_TemplateString) && Left.opensScope())
6199 return true;
6200 } else if (Style.isTableGen()) {
6201 // Avoid to break after "def", "class", "let" and so on.
6202 if (Keywords.isTableGenDefinition(Left))
6203 return false;
6204 // Avoid to break after '(' in the cases that is in bang operators.
6205 if (Right.is(tok::l_paren)) {
6206 return Left.isNoneOf(TT_TableGenBangOperator, TT_TableGenCondOperator,
6207 TT_TemplateCloser);
6208 }
6209 // Avoid to break between the value and its suffix part.
6210 if (Left.is(TT_TableGenValueSuffix))
6211 return false;
6212 // Avoid to break around paste operator.
6213 if (Left.is(tok::hash) || Right.is(tok::hash))
6214 return false;
6215 if (Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator))
6216 return false;
6217 }
6218
6219 // We can break before an r_brace if there was a break after the matching
6220 // l_brace, which is tracked by BreakBeforeClosingBrace, or if we are in a
6221 // block-indented initialization list.
6222 if (Right.is(tok::r_brace)) {
6223 return Right.MatchingParen && (Right.MatchingParen->is(BK_Block) ||
6224 (Right.isBlockIndentedInitRBrace(Style)));
6225 }
6226
6227 // We can break before r_paren if we're in a block indented context or
6228 // a control statement with an explicit style option.
6229 if (Right.is(tok::r_paren)) {
6230 if (!Right.MatchingParen)
6231 return false;
6232 auto Next = Right.Next;
6233 if (Next && Next->is(tok::r_paren))
6234 Next = Next->Next;
6235 if (Next && Next->is(tok::l_paren))
6236 return false;
6237 const FormatToken *Previous = Right.MatchingParen->Previous;
6238 if (!Previous)
6239 return false;
6240 if (Previous->isIf())
6241 return Style.BreakBeforeCloseBracketIf;
6242 if (Previous->isLoop(Style))
6243 return Style.BreakBeforeCloseBracketLoop;
6244 if (Previous->is(tok::kw_switch))
6245 return Style.BreakBeforeCloseBracketSwitch;
6246 return Style.BreakBeforeCloseBracketFunction;
6247 }
6248
6249 if (Left.isOneOf(tok::r_paren, TT_TrailingAnnotation) &&
6250 Right.is(TT_TrailingAnnotation) &&
6251 Style.BreakBeforeCloseBracketFunction) {
6252 return false;
6253 }
6254
6255 if (Right.is(TT_TemplateCloser))
6256 return Style.BreakBeforeTemplateCloser;
6257
6258 if (Left.isOneOf(tok::at, tok::objc_interface))
6259 return false;
6260 if (Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
6261 return Right.isNot(tok::l_paren);
6262 if (Right.is(TT_PointerOrReference)) {
6263 return Line.IsMultiVariableDeclStmt ||
6264 (getTokenPointerOrReferenceAlignment(Right) ==
6265 FormatStyle::PAS_Right &&
6266 !(Right.Next &&
6267 Right.Next->isOneOf(TT_FunctionDeclarationName, tok::kw_const)));
6268 }
6269 if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
6270 TT_ClassHeadName, TT_QtProperty, tok::kw_operator)) {
6271 return true;
6272 }
6273 if (Left.is(TT_PointerOrReference))
6274 return false;
6275 if (Right.isTrailingComment()) {
6276 // We rely on MustBreakBefore being set correctly here as we should not
6277 // change the "binding" behavior of a comment.
6278 // The first comment in a braced lists is always interpreted as belonging to
6279 // the first list element. Otherwise, it should be placed outside of the
6280 // list.
6281 return Left.is(BK_BracedInit) ||
6282 (Left.is(TT_CtorInitializerColon) && Right.NewlinesBefore > 0 &&
6283 Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon);
6284 }
6285 if (Left.is(tok::question) && Right.is(tok::colon))
6286 return false;
6287 if (Right.isOneOf(TT_ConditionalExpr, tok::question))
6288 return Style.BreakBeforeTernaryOperators;
6289 if (Left.isOneOf(TT_ConditionalExpr, tok::question))
6290 return !Style.BreakBeforeTernaryOperators;
6291 if (Left.is(TT_InheritanceColon))
6292 return Style.BreakInheritanceList == FormatStyle::BILS_AfterColon;
6293 if (Right.is(TT_InheritanceColon))
6294 return Style.BreakInheritanceList != FormatStyle::BILS_AfterColon;
6295 // When the method parameter has no name, allow breaking before the colon.
6296 if (Right.is(TT_ObjCMethodExpr) && Right.isNot(tok::r_square) &&
6297 Left.isNot(TT_SelectorName)) {
6298 return true;
6299 }
6300
6301 if (Right.is(tok::colon) &&
6302 Right.isNoneOf(TT_CtorInitializerColon, TT_InlineASMColon,
6303 TT_BitFieldColon)) {
6304 return false;
6305 }
6306 if (Left.is(tok::colon) && Left.isOneOf(TT_ObjCSelector, TT_ObjCMethodExpr))
6307 return true;
6308 if (Left.is(tok::colon) && Left.is(TT_DictLiteral)) {
6309 if (Style.isProto()) {
6310 if (!Style.AlwaysBreakBeforeMultilineStrings && Right.isStringLiteral())
6311 return false;
6312 // Prevent cases like:
6313 //
6314 // submessage:
6315 // { key: valueeeeeeeeeeee }
6316 //
6317 // when the snippet does not fit into one line.
6318 // Prefer:
6319 //
6320 // submessage: {
6321 // key: valueeeeeeeeeeee
6322 // }
6323 //
6324 // instead, even if it is longer by one line.
6325 //
6326 // Note that this allows the "{" to go over the column limit
6327 // when the column limit is just between ":" and "{", but that does
6328 // not happen too often and alternative formattings in this case are
6329 // not much better.
6330 //
6331 // The code covers the cases:
6332 //
6333 // submessage: { ... }
6334 // submessage: < ... >
6335 // repeated: [ ... ]
6336 if ((Right.isOneOf(tok::l_brace, tok::less) &&
6337 Right.is(TT_DictLiteral)) ||
6338 Right.is(TT_ArrayInitializerLSquare)) {
6339 return false;
6340 }
6341 }
6342 return true;
6343 }
6344 if (Right.is(tok::r_square) && Right.MatchingParen &&
6345 Right.MatchingParen->is(TT_ProtoExtensionLSquare)) {
6346 return false;
6347 }
6348 if (Right.is(TT_SelectorName) || (Right.is(tok::identifier) && Right.Next &&
6349 Right.Next->is(TT_ObjCMethodExpr))) {
6350 return Left.isNot(tok::period); // FIXME: Properly parse ObjC calls.
6351 }
6352 if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty)
6353 return true;
6354 if (Right.is(tok::kw_concept))
6355 return Style.BreakBeforeConceptDeclarations != FormatStyle::BBCDS_Never;
6356 if (Right.is(TT_RequiresClause))
6357 return true;
6358 if (Left.ClosesTemplateDeclaration) {
6359 return Style.BreakTemplateDeclarations != FormatStyle::BTDS_Leave ||
6360 Right.NewlinesBefore > 0;
6361 }
6362 if (Left.is(TT_FunctionAnnotationRParen))
6363 return true;
6364 if (Left.ClosesRequiresClause)
6365 return true;
6366 if (Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
6367 TT_OverloadedOperator)) {
6368 return false;
6369 }
6370 if (Left.is(TT_RangeBasedForLoopColon))
6371 return true;
6372 if (Right.is(TT_RangeBasedForLoopColon))
6373 return false;
6374 if (Left.is(TT_TemplateCloser) && Right.is(TT_TemplateOpener))
6375 return true;
6376 if ((Left.is(tok::greater) && Right.is(tok::greater)) ||
6377 (Left.is(tok::less) && Right.is(tok::less))) {
6378 return false;
6379 }
6380 if (Right.is(TT_BinaryOperator) &&
6381 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None &&
6382 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All ||
6383 Right.getPrecedence() != prec::Assignment)) {
6384 return true;
6385 }
6386 if (Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator, tok::kw_operator))
6387 return false;
6388 if (Left.is(tok::equal) && Right.isNoneOf(tok::kw_default, tok::kw_delete) &&
6389 Line.Type == LT_VirtualFunctionDecl && Left.NestingLevel == 0) {
6390 return false;
6391 }
6392 if (Left.is(tok::equal) && Right.is(tok::l_brace) &&
6393 Style.Cpp11BracedListStyle == FormatStyle::BLS_Block) {
6394 return false;
6395 }
6396 if (Left.is(TT_AttributeLParen) ||
6397 (Left.is(tok::l_paren) && Left.is(TT_TypeDeclarationParen))) {
6398 return false;
6399 }
6400 if (Left.is(tok::l_paren) && Left.Previous &&
6401 (Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen))) {
6402 return false;
6403 }
6404 if (Right.is(TT_ImplicitStringLiteral))
6405 return false;
6406
6407 if (Right.is(tok::r_square) && Right.MatchingParen &&
6408 Right.MatchingParen->is(TT_LambdaLSquare)) {
6409 return false;
6410 }
6411
6412 // Allow breaking after a trailing annotation, e.g. after a method
6413 // declaration.
6414 if (Left.is(TT_TrailingAnnotation)) {
6415 return Right.isNoneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
6416 tok::less, tok::coloncolon);
6417 }
6418
6419 if (Right.isAttribute())
6420 return true;
6421
6422 if (Right.is(TT_AttributeLSquare)) {
6423 assert(Left.isNot(tok::l_square));
6424 return true;
6425 }
6426
6427 if (Left.is(tok::identifier) && Right.is(tok::string_literal))
6428 return true;
6429
6430 if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
6431 return true;
6432
6433 if (Left.is(TT_CtorInitializerColon)) {
6434 return Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
6435 (!Right.isTrailingComment() || Right.NewlinesBefore > 0);
6436 }
6437 if (Right.is(TT_CtorInitializerColon))
6438 return Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon;
6439 if (Left.is(TT_CtorInitializerComma) &&
6440 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
6441 return false;
6442 }
6443 if (Right.is(TT_CtorInitializerComma) &&
6444 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
6445 return true;
6446 }
6447 if (Left.is(TT_InheritanceComma) &&
6448 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) {
6449 return false;
6450 }
6451 if (Right.is(TT_InheritanceComma) &&
6452 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) {
6453 return true;
6454 }
6455 if (Left.is(TT_ArrayInitializerLSquare))
6456 return true;
6457 if (Right.is(tok::kw_typename) && Left.isNot(tok::kw_const))
6458 return true;
6459 if ((Left.isBinaryOperator() || Left.is(TT_BinaryOperator)) &&
6460 Left.isNoneOf(tok::arrowstar, tok::lessless) &&
6461 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All &&
6462 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None ||
6463 Left.getPrecedence() == prec::Assignment)) {
6464 return true;
6465 }
6466 if (Left.is(TT_AttributeLSquare) && Right.is(tok::l_square)) {
6467 assert(Right.isNot(TT_AttributeLSquare));
6468 return false;
6469 }
6470 if (Left.is(tok::r_square) && Right.is(TT_AttributeRSquare)) {
6471 assert(Left.isNot(TT_AttributeRSquare));
6472 return false;
6473 }
6474
6475 auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
6476 if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace)) {
6477 if (isAllmanLambdaBrace(Left))
6478 return !isEmptyLambdaAllowed(Left, ShortLambdaOption);
6479 if (isAllmanLambdaBrace(Right))
6480 return !isEmptyLambdaAllowed(Right, ShortLambdaOption);
6481 }
6482
6483 if (Right.is(tok::kw_noexcept) && Right.is(TT_TrailingAnnotation)) {
6484 switch (Style.AllowBreakBeforeNoexceptSpecifier) {
6485 case FormatStyle::BBNSS_Never:
6486 return false;
6487 case FormatStyle::BBNSS_Always:
6488 return true;
6489 case FormatStyle::BBNSS_OnlyWithParen:
6490 return Right.Next && Right.Next->is(tok::l_paren);
6491 }
6492 }
6493
6494 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
6495 tok::kw_class, tok::kw_struct, tok::comment) ||
6496 Right.isMemberAccess() ||
6497 Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
6498 tok::colon, tok::l_square, tok::at) ||
6499 (Left.is(tok::r_paren) &&
6500 Right.isOneOf(tok::identifier, tok::kw_const)) ||
6501 (Left.is(tok::l_paren) && Right.isNot(tok::r_paren)) ||
6502 (Left.is(TT_TemplateOpener) && Right.isNot(TT_TemplateCloser));
6503}
6504
6505void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) const {
6506 llvm::errs() << "AnnotatedTokens(L=" << Line.Level << ", P=" << Line.PPLevel
6507 << ", T=" << Line.Type << ", C=" << Line.IsContinuation
6508 << "):\n";
6509 const FormatToken *Tok = Line.First;
6510 while (Tok) {
6511 llvm::errs() << " I=" << Tok->IndentLevel << " M=" << Tok->MustBreakBefore
6512 << " C=" << Tok->CanBreakBefore
6513 << " T=" << getTokenTypeName(Tok->getType())
6514 << " S=" << Tok->SpacesRequiredBefore
6515 << " F=" << Tok->Finalized << " B=" << Tok->BlockParameterCount
6516 << " BK=" << Tok->getBlockKind() << " P=" << Tok->SplitPenalty
6517 << " Name=" << Tok->Tok.getName() << " N=" << Tok->NestingLevel
6518 << " L=" << Tok->TotalLength
6519 << " PPK=" << Tok->getPackingKind() << " FakeLParens=";
6520 for (prec::Level LParen : Tok->FakeLParens)
6521 llvm::errs() << LParen << "/";
6522 llvm::errs() << " FakeRParens=" << Tok->FakeRParens;
6523 llvm::errs() << " II=" << Tok->Tok.getIdentifierInfo();
6524 llvm::errs() << " Text='" << Tok->TokenText << "'\n";
6525 if (!Tok->Next)
6526 assert(Tok == Line.Last);
6527 Tok = Tok->Next;
6528 }
6529 llvm::errs() << "----\n";
6530}
6531
6532FormatStyle::PointerAlignmentStyle
6533TokenAnnotator::getTokenReferenceAlignment(const FormatToken &Reference) const {
6534 assert(Reference.isOneOf(tok::amp, tok::ampamp));
6535 switch (Style.ReferenceAlignment) {
6536 case FormatStyle::RAS_Pointer:
6537 return Style.PointerAlignment;
6538 case FormatStyle::RAS_Left:
6539 return FormatStyle::PAS_Left;
6540 case FormatStyle::RAS_Right:
6541 return FormatStyle::PAS_Right;
6542 case FormatStyle::RAS_Middle:
6543 return FormatStyle::PAS_Middle;
6544 }
6545 assert(0); //"Unhandled value of ReferenceAlignment"
6546 return Style.PointerAlignment;
6547}
6548
6549FormatStyle::PointerAlignmentStyle
6550TokenAnnotator::getTokenPointerOrReferenceAlignment(
6551 const FormatToken &PointerOrReference) const {
6552 if (PointerOrReference.isOneOf(tok::amp, tok::ampamp))
6553 return getTokenReferenceAlignment(PointerOrReference);
6554 assert(PointerOrReference.is(tok::star));
6555 return Style.PointerAlignment;
6556}
6557
6558} // namespace format
6559} // 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
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:4584
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:4224
bool startsNextParameter(const FormatToken &Current, const FormatStyle &Style)
bool Ret(InterpState &S, CodePtr &PC)
Definition Interp.h:223
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:3530
@ 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
bool isNoneOf(Ts... Ks) 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.