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