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