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