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