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 if (AfterRParen->is(Kind: tok::l_brace) &&
2925 AfterRParen->getBlockKind() == BK_BracedInit) {
2926 return true;
2927 }
2928
2929 // If the next token after the parenthesis is a unary operator, assume
2930 // that this is cast, unless there are unexpected tokens inside the
2931 // parenthesis.
2932 const bool NextIsAmpOrStar = AfterRParen->isOneOf(K1: tok::amp, K2: tok::star);
2933 if (!(AfterRParen->isUnaryOperator() || NextIsAmpOrStar) ||
2934 AfterRParen->is(Kind: tok::plus) ||
2935 AfterRParen->Next->isNoneOf(Ks: tok::identifier, Ks: tok::numeric_constant)) {
2936 return false;
2937 }
2938
2939 if (NextIsAmpOrStar &&
2940 (AfterRParen->Next->is(Kind: tok::numeric_constant) || Line.InPPDirective)) {
2941 return false;
2942 }
2943
2944 if (Line.InPPDirective && AfterRParen->is(Kind: tok::minus))
2945 return false;
2946
2947 const auto *Prev = BeforeRParen;
2948
2949 // Look for a function pointer type, e.g. `(*)()`.
2950 if (Prev->is(Kind: tok::r_paren)) {
2951 if (Prev->is(TT: TT_CastRParen))
2952 return false;
2953 Prev = Prev->MatchingParen;
2954 if (!Prev)
2955 return false;
2956 Prev = Prev->Previous;
2957 if (!Prev || Prev->isNot(Kind: tok::r_paren))
2958 return false;
2959 Prev = Prev->MatchingParen;
2960 return Prev && Prev->is(TT: TT_FunctionTypeLParen);
2961 }
2962
2963 // Search for unexpected tokens.
2964 for (Prev = BeforeRParen; Prev != LParen; Prev = Prev->Previous)
2965 if (Prev->isNoneOf(Ks: tok::kw_const, Ks: tok::identifier, Ks: tok::coloncolon))
2966 return false;
2967
2968 return true;
2969 }
2970
2971 /// Returns true if the token is used as a unary operator.
2972 bool determineUnaryOperatorByUsage(const FormatToken &Tok) {
2973 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2974 if (!PrevToken)
2975 return true;
2976
2977 // These keywords are deliberately not included here because they may
2978 // precede only one of unary star/amp and plus/minus but not both. They are
2979 // either included in determineStarAmpUsage or determinePlusMinusCaretUsage.
2980 //
2981 // @ - It may be followed by a unary `-` in Objective-C literals. We don't
2982 // know how they can be followed by a star or amp.
2983 if (PrevToken->isOneOf(
2984 K1: TT_ConditionalExpr, K2: tok::l_paren, Ks: tok::comma, Ks: tok::colon, Ks: tok::semi,
2985 Ks: tok::equal, Ks: tok::question, Ks: tok::l_square, Ks: tok::l_brace,
2986 Ks: tok::kw_case, Ks: tok::kw_co_await, Ks: tok::kw_co_return, Ks: tok::kw_co_yield,
2987 Ks: tok::kw_delete, Ks: tok::kw_return, Ks: tok::kw_throw)) {
2988 return true;
2989 }
2990
2991 // We put sizeof here instead of only in determineStarAmpUsage. In the cases
2992 // where the unary `+` operator is overloaded, it is reasonable to write
2993 // things like `sizeof +x`. Like commit 446d6ec996c6c3.
2994 if (PrevToken->is(Kind: tok::kw_sizeof))
2995 return true;
2996
2997 // A sequence of leading unary operators.
2998 if (PrevToken->isOneOf(K1: TT_CastRParen, K2: TT_UnaryOperator))
2999 return true;
3000
3001 // There can't be two consecutive binary operators.
3002 if (PrevToken->is(TT: TT_BinaryOperator))
3003 return true;
3004
3005 return false;
3006 }
3007
3008 /// Return the type of the given token assuming it is * or &.
3009 TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression,
3010 bool InTemplateArgument) {
3011 if (Style.isJavaScript())
3012 return TT_BinaryOperator;
3013
3014 // && in C# must be a binary operator.
3015 if (Style.isCSharp() && Tok.is(Kind: tok::ampamp))
3016 return TT_BinaryOperator;
3017
3018 if (Style.isVerilog()) {
3019 // In Verilog, `*` can only be a binary operator. `&` can be either unary
3020 // or binary. `*` also includes `*>` in module path declarations in
3021 // specify blocks because merged tokens take the type of the first one by
3022 // default.
3023 if (Tok.is(Kind: tok::star))
3024 return TT_BinaryOperator;
3025 return determineUnaryOperatorByUsage(Tok) ? TT_UnaryOperator
3026 : TT_BinaryOperator;
3027 }
3028
3029 const FormatToken *PrevToken = Tok.getPreviousNonComment();
3030 if (!PrevToken)
3031 return TT_UnaryOperator;
3032 if (PrevToken->isTypeName(LangOpts))
3033 return TT_PointerOrReference;
3034 if (PrevToken->isPlacementOperator() && Tok.is(Kind: tok::ampamp))
3035 return TT_BinaryOperator;
3036
3037 auto *NextToken = Tok.getNextNonComment();
3038 if (!NextToken)
3039 return TT_PointerOrReference;
3040 if (NextToken->is(Kind: tok::greater))
3041 return TT_PointerOrReference;
3042
3043 if (InTemplateArgument && NextToken->is(Kind: tok::kw_noexcept))
3044 return TT_BinaryOperator;
3045
3046 if (NextToken->isOneOf(K1: tok::arrow, K2: tok::equal, Ks: tok::comma, Ks: tok::r_paren,
3047 Ks: TT_RequiresClause) ||
3048 (NextToken->is(Kind: tok::kw_noexcept) && !IsExpression) ||
3049 NextToken->canBePointerOrReferenceQualifier() ||
3050 (NextToken->is(Kind: tok::l_brace) && !NextToken->getNextNonComment())) {
3051 return TT_PointerOrReference;
3052 }
3053
3054 if (PrevToken->is(Kind: tok::coloncolon))
3055 return TT_PointerOrReference;
3056
3057 if (PrevToken->is(Kind: tok::r_paren) && PrevToken->is(TT: TT_TypeDeclarationParen))
3058 return TT_PointerOrReference;
3059
3060 if (determineUnaryOperatorByUsage(Tok))
3061 return TT_UnaryOperator;
3062
3063 if (NextToken->is(Kind: tok::l_square) && NextToken->isNot(Kind: TT_LambdaLSquare))
3064 return TT_PointerOrReference;
3065 if (NextToken->is(Kind: tok::kw_operator) && !IsExpression)
3066 return TT_PointerOrReference;
3067 if (NextToken->isOneOf(K1: tok::comma, K2: tok::semi))
3068 return TT_PointerOrReference;
3069
3070 // After right braces, star tokens are likely to be pointers to struct,
3071 // union, or class.
3072 // struct {} *ptr;
3073 // This by itself is not sufficient to distinguish from multiplication
3074 // following a brace-initialized expression, as in:
3075 // int i = int{42} * 2;
3076 // In the struct case, the part of the struct declaration until the `{` and
3077 // the `}` are put on separate unwrapped lines; in the brace-initialized
3078 // case, the matching `{` is on the same unwrapped line, so check for the
3079 // presence of the matching brace to distinguish between those.
3080 if (PrevToken->is(Kind: tok::r_brace) && Tok.is(Kind: tok::star) &&
3081 !PrevToken->MatchingParen) {
3082 return TT_PointerOrReference;
3083 }
3084
3085 if (PrevToken->endsSequence(K1: tok::r_square, Tokens: tok::l_square, Tokens: tok::kw_delete))
3086 return TT_UnaryOperator;
3087
3088 if (PrevToken->Tok.isLiteral() ||
3089 PrevToken->isOneOf(K1: tok::r_paren, K2: tok::r_square, Ks: tok::kw_true,
3090 Ks: tok::kw_false, Ks: tok::r_brace)) {
3091 return TT_BinaryOperator;
3092 }
3093
3094 const FormatToken *NextNonParen = NextToken;
3095 while (NextNonParen && NextNonParen->is(Kind: tok::l_paren))
3096 NextNonParen = NextNonParen->getNextNonComment();
3097 if (NextNonParen && (NextNonParen->Tok.isLiteral() ||
3098 NextNonParen->isOneOf(K1: tok::kw_true, K2: tok::kw_false) ||
3099 NextNonParen->isUnaryOperator())) {
3100 return TT_BinaryOperator;
3101 }
3102
3103 // If we know we're in a template argument, there are no named declarations.
3104 // Thus, having an identifier on the right-hand side indicates a binary
3105 // operator.
3106 if (InTemplateArgument && NextToken->Tok.isAnyIdentifier())
3107 return TT_BinaryOperator;
3108
3109 // "&&" followed by "(", "*", or "&" is quite unlikely to be two successive
3110 // unary "&".
3111 if (Tok.is(Kind: tok::ampamp) &&
3112 NextToken->isOneOf(K1: tok::l_paren, K2: tok::star, Ks: tok::amp)) {
3113 return TT_BinaryOperator;
3114 }
3115
3116 // This catches some cases where evaluation order is used as control flow:
3117 // aaa && aaa->f();
3118 // Or expressions like:
3119 // width * height * length
3120 if (NextToken->Tok.isAnyIdentifier()) {
3121 auto *NextNextToken = NextToken->getNextNonComment();
3122 if (NextNextToken) {
3123 if (NextNextToken->is(Kind: tok::arrow))
3124 return TT_BinaryOperator;
3125 if (NextNextToken->isPointerOrReference() &&
3126 !NextToken->isObjCLifetimeQualifier(Style)) {
3127 NextNextToken->setFinalizedType(TT_BinaryOperator);
3128 return TT_BinaryOperator;
3129 }
3130 }
3131 }
3132
3133 // It is very unlikely that we are going to find a pointer or reference type
3134 // definition on the RHS of an assignment.
3135 if (IsExpression && !Contexts.back().CaretFound &&
3136 Line.getFirstNonComment()->isNot(
3137 Kind: TT_RequiresClauseInARequiresExpression)) {
3138 return TT_BinaryOperator;
3139 }
3140
3141 // Opeartors at class scope are likely pointer or reference members.
3142 if (!Scopes.empty() && Scopes.back() == ST_Class)
3143 return TT_PointerOrReference;
3144
3145 // Tokens that indicate member access or chained operator& use.
3146 auto IsChainedOperatorAmpOrMember = [](const FormatToken *token) {
3147 return !token || token->isOneOf(K1: tok::amp, K2: tok::period, Ks: tok::arrow,
3148 Ks: tok::arrowstar, Ks: tok::periodstar);
3149 };
3150
3151 // It's more likely that & represents operator& than an uninitialized
3152 // reference.
3153 if (Tok.is(Kind: tok::amp) && PrevToken->Tok.isAnyIdentifier() &&
3154 IsChainedOperatorAmpOrMember(PrevToken->getPreviousNonComment()) &&
3155 NextToken && NextToken->Tok.isAnyIdentifier()) {
3156 if (auto NextNext = NextToken->getNextNonComment();
3157 NextNext &&
3158 (IsChainedOperatorAmpOrMember(NextNext) || NextNext->is(Kind: tok::semi))) {
3159 return TT_BinaryOperator;
3160 }
3161 }
3162
3163 if (Line.Type == LT_SimpleRequirement ||
3164 (!Scopes.empty() && Scopes.back() == ST_CompoundRequirement)) {
3165 return TT_BinaryOperator;
3166 }
3167
3168 return TT_PointerOrReference;
3169 }
3170
3171 TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) {
3172 if (determineUnaryOperatorByUsage(Tok))
3173 return TT_UnaryOperator;
3174
3175 const FormatToken *PrevToken = Tok.getPreviousNonComment();
3176 if (!PrevToken)
3177 return TT_UnaryOperator;
3178
3179 if (PrevToken->is(Kind: tok::at))
3180 return TT_UnaryOperator;
3181
3182 // Fall back to marking the token as binary operator.
3183 return TT_BinaryOperator;
3184 }
3185
3186 /// Determine whether ++/-- are pre- or post-increments/-decrements.
3187 TokenType determineIncrementUsage(const FormatToken &Tok) {
3188 const FormatToken *PrevToken = Tok.getPreviousNonComment();
3189 if (!PrevToken || PrevToken->is(TT: TT_CastRParen))
3190 return TT_UnaryOperator;
3191 if (PrevToken->isOneOf(K1: tok::r_paren, K2: tok::r_square, Ks: tok::identifier))
3192 return TT_TrailingUnaryOperator;
3193
3194 return TT_UnaryOperator;
3195 }
3196
3197 SmallVector<Context, 8> Contexts;
3198
3199 const FormatStyle &Style;
3200 AnnotatedLine &Line;
3201 FormatToken *CurrentToken;
3202 bool AutoFound;
3203 bool IsCpp;
3204 LangOptions LangOpts;
3205 const AdditionalKeywords &Keywords;
3206
3207 SmallVector<ScopeType> &Scopes;
3208
3209 // Set of "<" tokens that do not open a template parameter list. If parseAngle
3210 // determines that a specific token can't be a template opener, it will make
3211 // same decision irrespective of the decisions for tokens leading up to it.
3212 // Store this information to prevent this from causing exponential runtime.
3213 llvm::SmallPtrSet<FormatToken *, 16> NonTemplateLess;
3214
3215 int TemplateDeclarationDepth;
3216};
3217
3218static const int PrecedenceUnaryOperator = prec::PointerToMember + 1;
3219static const int PrecedenceArrowAndPeriod = prec::PointerToMember + 2;
3220
3221/// Parses binary expressions by inserting fake parenthesis based on
3222/// operator precedence.
3223class ExpressionParser {
3224public:
3225 ExpressionParser(const FormatStyle &Style, const AdditionalKeywords &Keywords,
3226 AnnotatedLine &Line)
3227 : Style(Style), Keywords(Keywords), Line(Line), Current(Line.First) {}
3228
3229 /// Parse expressions with the given operator precedence.
3230 void parse(int Precedence = 0) {
3231 // Skip 'return' and ObjC selector colons as they are not part of a binary
3232 // expression.
3233 while (Current && (Current->is(Kind: tok::kw_return) ||
3234 (Current->is(Kind: tok::colon) &&
3235 Current->isOneOf(K1: TT_ObjCMethodExpr, K2: TT_DictLiteral)))) {
3236 next();
3237 }
3238
3239 if (!Current || Precedence > PrecedenceArrowAndPeriod)
3240 return;
3241
3242 // Conditional expressions need to be parsed separately for proper nesting.
3243 if (Precedence == prec::Conditional) {
3244 parseConditionalExpr();
3245 return;
3246 }
3247
3248 // Parse unary operators, which all have a higher precedence than binary
3249 // operators.
3250 if (Precedence == PrecedenceUnaryOperator) {
3251 parseUnaryOperator();
3252 return;
3253 }
3254
3255 FormatToken *Start = Current;
3256 FormatToken *LatestOperator = nullptr;
3257 unsigned OperatorIndex = 0;
3258 // The first name of the current type in a port list.
3259 FormatToken *VerilogFirstOfType = nullptr;
3260
3261 while (Current) {
3262 // In Verilog ports in a module header that don't have a type take the
3263 // type of the previous one. For example,
3264 // module a(output b,
3265 // c,
3266 // output d);
3267 // In this case there need to be fake parentheses around b and c.
3268 if (Style.isVerilog() && Precedence == prec::Comma) {
3269 VerilogFirstOfType =
3270 verilogGroupDecl(FirstOfType: VerilogFirstOfType, PreviousComma: LatestOperator);
3271 }
3272
3273 // Consume operators with higher precedence.
3274 parse(Precedence: Precedence + 1);
3275
3276 int CurrentPrecedence = getCurrentPrecedence();
3277 if (Style.BreakBinaryOperations == FormatStyle::BBO_OnePerLine &&
3278 CurrentPrecedence > prec::Conditional &&
3279 CurrentPrecedence < prec::PointerToMember) {
3280 // When BreakBinaryOperations is set to BreakAll,
3281 // all operations will be on the same line or on individual lines.
3282 // Override precedence to avoid adding fake parenthesis which could
3283 // group operations of a different precedence level on the same line
3284 CurrentPrecedence = prec::Additive;
3285 }
3286
3287 if (Precedence == CurrentPrecedence && Current &&
3288 Current->is(TT: TT_SelectorName)) {
3289 if (LatestOperator)
3290 addFakeParenthesis(Start, Precedence: prec::Level(Precedence));
3291 Start = Current;
3292 }
3293
3294 if ((Style.isCSharp() || Style.isJavaScript() || Style.isJava()) &&
3295 Precedence == prec::Additive && Current) {
3296 // A string can be broken without parentheses around it when it is
3297 // already in a sequence of strings joined by `+` signs.
3298 FormatToken *Prev = Current->getPreviousNonComment();
3299 if (Prev && Prev->is(Kind: tok::string_literal) &&
3300 (Prev == Start || Prev->endsSequence(K1: tok::string_literal, Tokens: tok::plus,
3301 Tokens: TT_StringInConcatenation))) {
3302 Prev->setType(TT_StringInConcatenation);
3303 }
3304 }
3305
3306 // At the end of the line or when an operator with lower precedence is
3307 // found, insert fake parenthesis and return.
3308 if (!Current ||
3309 (Current->closesScope() &&
3310 (Current->MatchingParen || Current->is(TT: TT_TemplateString))) ||
3311 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
3312 (CurrentPrecedence == prec::Conditional &&
3313 Precedence == prec::Assignment && Current->is(Kind: tok::colon))) {
3314 break;
3315 }
3316
3317 // Consume scopes: (), [], <> and {}
3318 // In addition to that we handle require clauses as scope, so that the
3319 // constraints in that are correctly indented.
3320 if (Current->opensScope() ||
3321 Current->isOneOf(K1: TT_RequiresClause,
3322 K2: TT_RequiresClauseInARequiresExpression)) {
3323 // In fragment of a JavaScript template string can look like '}..${' and
3324 // thus close a scope and open a new one at the same time.
3325 while (Current && (!Current->closesScope() || Current->opensScope())) {
3326 next();
3327 parse();
3328 }
3329 next();
3330 } else {
3331 // Operator found.
3332 if (CurrentPrecedence == Precedence) {
3333 if (LatestOperator)
3334 LatestOperator->NextOperator = Current;
3335 LatestOperator = Current;
3336 Current->OperatorIndex = OperatorIndex;
3337 ++OperatorIndex;
3338 }
3339 next(/*SkipPastLeadingComments=*/Precedence > 0);
3340 }
3341 }
3342
3343 // Group variables of the same type.
3344 if (Style.isVerilog() && Precedence == prec::Comma && VerilogFirstOfType)
3345 addFakeParenthesis(Start: VerilogFirstOfType, Precedence: prec::Comma);
3346
3347 if (LatestOperator && (Current || Precedence > 0)) {
3348 // The requires clauses do not neccessarily end in a semicolon or a brace,
3349 // but just go over to struct/class or a function declaration, we need to
3350 // intervene so that the fake right paren is inserted correctly.
3351 auto End =
3352 (Start->Previous &&
3353 Start->Previous->isOneOf(K1: TT_RequiresClause,
3354 K2: TT_RequiresClauseInARequiresExpression))
3355 ? [this]() {
3356 auto Ret = Current ? Current : Line.Last;
3357 while (!Ret->ClosesRequiresClause && Ret->Previous)
3358 Ret = Ret->Previous;
3359 return Ret;
3360 }()
3361 : nullptr;
3362
3363 if (Precedence == PrecedenceArrowAndPeriod) {
3364 // Call expressions don't have a binary operator precedence.
3365 addFakeParenthesis(Start, Precedence: prec::Unknown, End);
3366 } else {
3367 addFakeParenthesis(Start, Precedence: prec::Level(Precedence), End);
3368 }
3369 }
3370 }
3371
3372private:
3373 /// Gets the precedence (+1) of the given token for binary operators
3374 /// and other tokens that we treat like binary operators.
3375 int getCurrentPrecedence() {
3376 if (Current) {
3377 const FormatToken *NextNonComment = Current->getNextNonComment();
3378 if (Current->is(TT: TT_ConditionalExpr))
3379 return prec::Conditional;
3380 if (NextNonComment && Current->is(TT: TT_SelectorName) &&
3381 (NextNonComment->isOneOf(K1: TT_DictLiteral, K2: TT_JsTypeColon) ||
3382 (Style.isProto() && NextNonComment->is(Kind: tok::less)))) {
3383 return prec::Assignment;
3384 }
3385 if (Current->is(TT: TT_JsComputedPropertyName))
3386 return prec::Assignment;
3387 if (Current->is(TT: TT_LambdaArrow))
3388 return prec::Comma;
3389 if (Current->is(TT: TT_FatArrow))
3390 return prec::Assignment;
3391 if (Current->isOneOf(K1: tok::semi, K2: TT_InlineASMColon, Ks: TT_SelectorName) ||
3392 (Current->is(Kind: tok::comment) && NextNonComment &&
3393 NextNonComment->is(TT: TT_SelectorName))) {
3394 return 0;
3395 }
3396 if (Current->is(TT: TT_RangeBasedForLoopColon))
3397 return prec::Comma;
3398 if ((Style.isJava() || Style.isJavaScript()) &&
3399 Current->is(II: Keywords.kw_instanceof)) {
3400 return prec::Relational;
3401 }
3402 if (Style.isJavaScript() &&
3403 Current->isOneOf(K1: Keywords.kw_in, K2: Keywords.kw_as)) {
3404 return prec::Relational;
3405 }
3406 if (Current->isOneOf(K1: TT_BinaryOperator, K2: tok::comma))
3407 return Current->getPrecedence();
3408 if (Current->isOneOf(K1: tok::period, K2: tok::arrow) &&
3409 Current->isNot(Kind: TT_TrailingReturnArrow)) {
3410 return PrecedenceArrowAndPeriod;
3411 }
3412 if ((Style.isJava() || Style.isJavaScript()) &&
3413 Current->isOneOf(K1: Keywords.kw_extends, K2: Keywords.kw_implements,
3414 Ks: Keywords.kw_throws)) {
3415 return 0;
3416 }
3417 // In Verilog case labels are not on separate lines straight out of
3418 // UnwrappedLineParser. The colon is not part of an expression.
3419 if (Style.isVerilog() && Current->is(Kind: tok::colon))
3420 return 0;
3421 }
3422 return -1;
3423 }
3424
3425 void addFakeParenthesis(FormatToken *Start, prec::Level Precedence,
3426 FormatToken *End = nullptr) {
3427 // Do not assign fake parenthesis to tokens that are part of an
3428 // unexpanded macro call. The line within the macro call contains
3429 // the parenthesis and commas, and we will not find operators within
3430 // that structure.
3431 if (Start->MacroParent)
3432 return;
3433
3434 Start->FakeLParens.push_back(Elt: Precedence);
3435 if (Precedence > prec::Unknown)
3436 Start->StartsBinaryExpression = true;
3437 if (!End && Current)
3438 End = Current->getPreviousNonComment();
3439 if (End) {
3440 ++End->FakeRParens;
3441 if (Precedence > prec::Unknown)
3442 End->EndsBinaryExpression = true;
3443 }
3444 }
3445
3446 /// Parse unary operator expressions and surround them with fake
3447 /// parentheses if appropriate.
3448 void parseUnaryOperator() {
3449 SmallVector<FormatToken *, 2> Tokens;
3450 while (Current && Current->is(TT: TT_UnaryOperator)) {
3451 Tokens.push_back(Elt: Current);
3452 next();
3453 }
3454 parse(Precedence: PrecedenceArrowAndPeriod);
3455 for (FormatToken *Token : reverse(C&: Tokens)) {
3456 // The actual precedence doesn't matter.
3457 addFakeParenthesis(Start: Token, Precedence: prec::Unknown);
3458 }
3459 }
3460
3461 void parseConditionalExpr() {
3462 while (Current && Current->isTrailingComment())
3463 next();
3464 FormatToken *Start = Current;
3465 parse(Precedence: prec::LogicalOr);
3466 if (!Current || Current->isNot(Kind: tok::question))
3467 return;
3468 next();
3469 parse(Precedence: prec::Assignment);
3470 if (!Current || Current->isNot(Kind: TT_ConditionalExpr))
3471 return;
3472 next();
3473 parse(Precedence: prec::Assignment);
3474 addFakeParenthesis(Start, Precedence: prec::Conditional);
3475 }
3476
3477 void next(bool SkipPastLeadingComments = true) {
3478 if (Current)
3479 Current = Current->Next;
3480 while (Current &&
3481 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
3482 Current->isTrailingComment()) {
3483 Current = Current->Next;
3484 }
3485 }
3486
3487 // Add fake parenthesis around declarations of the same type for example in a
3488 // module prototype. Return the first port / variable of the current type.
3489 FormatToken *verilogGroupDecl(FormatToken *FirstOfType,
3490 FormatToken *PreviousComma) {
3491 if (!Current)
3492 return nullptr;
3493
3494 FormatToken *Start = Current;
3495
3496 // Skip attributes.
3497 while (Start->startsSequence(K1: tok::l_paren, Tokens: tok::star)) {
3498 if (!(Start = Start->MatchingParen) ||
3499 !(Start = Start->getNextNonComment())) {
3500 return nullptr;
3501 }
3502 }
3503
3504 FormatToken *Tok = Start;
3505
3506 if (Tok->is(II: Keywords.kw_assign))
3507 Tok = Tok->getNextNonComment();
3508
3509 // Skip any type qualifiers to find the first identifier. It may be either a
3510 // new type name or a variable name. There can be several type qualifiers
3511 // preceding a variable name, and we can not tell them apart by looking at
3512 // the word alone since a macro can be defined as either a type qualifier or
3513 // a variable name. Thus we use the last word before the dimensions instead
3514 // of the first word as the candidate for the variable or type name.
3515 FormatToken *First = nullptr;
3516 while (Tok) {
3517 FormatToken *Next = Tok->getNextNonComment();
3518
3519 if (Tok->is(Kind: tok::hash)) {
3520 // Start of a macro expansion.
3521 First = Tok;
3522 Tok = Next;
3523 if (Tok)
3524 Tok = Tok->getNextNonComment();
3525 } else if (Tok->is(Kind: tok::hashhash)) {
3526 // Concatenation. Skip.
3527 Tok = Next;
3528 if (Tok)
3529 Tok = Tok->getNextNonComment();
3530 } else if (Keywords.isVerilogQualifier(Tok: *Tok) ||
3531 Keywords.isVerilogIdentifier(Tok: *Tok)) {
3532 First = Tok;
3533 Tok = Next;
3534 // The name may have dots like `interface_foo.modport_foo`.
3535 while (Tok && Tok->isOneOf(K1: tok::period, K2: tok::coloncolon) &&
3536 (Tok = Tok->getNextNonComment())) {
3537 if (Keywords.isVerilogIdentifier(Tok: *Tok))
3538 Tok = Tok->getNextNonComment();
3539 }
3540 } else if (!Next) {
3541 Tok = nullptr;
3542 } else if (Tok->is(Kind: tok::l_paren)) {
3543 // Make sure the parenthesized list is a drive strength. Otherwise the
3544 // statement may be a module instantiation in which case we have already
3545 // found the instance name.
3546 if (Next->isOneOf(
3547 K1: Keywords.kw_highz0, K2: Keywords.kw_highz1, Ks: Keywords.kw_large,
3548 Ks: Keywords.kw_medium, Ks: Keywords.kw_pull0, Ks: Keywords.kw_pull1,
3549 Ks: Keywords.kw_small, Ks: Keywords.kw_strong0, Ks: Keywords.kw_strong1,
3550 Ks: Keywords.kw_supply0, Ks: Keywords.kw_supply1, Ks: Keywords.kw_weak0,
3551 Ks: Keywords.kw_weak1)) {
3552 Tok->setType(TT_VerilogStrength);
3553 Tok = Tok->MatchingParen;
3554 if (Tok) {
3555 Tok->setType(TT_VerilogStrength);
3556 Tok = Tok->getNextNonComment();
3557 }
3558 } else {
3559 break;
3560 }
3561 } else if (Tok->is(II: Keywords.kw_verilogHash)) {
3562 // Delay control.
3563 if (Next->is(Kind: tok::l_paren))
3564 Next = Next->MatchingParen;
3565 if (Next)
3566 Tok = Next->getNextNonComment();
3567 } else {
3568 break;
3569 }
3570 }
3571
3572 // Find the second identifier. If it exists it will be the name.
3573 FormatToken *Second = nullptr;
3574 // Dimensions.
3575 while (Tok && Tok->is(Kind: tok::l_square) && (Tok = Tok->MatchingParen))
3576 Tok = Tok->getNextNonComment();
3577 if (Tok && (Tok->is(Kind: tok::hash) || Keywords.isVerilogIdentifier(Tok: *Tok)))
3578 Second = Tok;
3579
3580 // If the second identifier doesn't exist and there are qualifiers, the type
3581 // is implied.
3582 FormatToken *TypedName = nullptr;
3583 if (Second) {
3584 TypedName = Second;
3585 if (First && First->is(TT: TT_Unknown))
3586 First->setType(TT_VerilogDimensionedTypeName);
3587 } else if (First != Start) {
3588 // If 'First' is null, then this isn't a declaration, 'TypedName' gets set
3589 // to null as intended.
3590 TypedName = First;
3591 }
3592
3593 if (TypedName) {
3594 // This is a declaration with a new type.
3595 if (TypedName->is(TT: TT_Unknown))
3596 TypedName->setType(TT_StartOfName);
3597 // Group variables of the previous type.
3598 if (FirstOfType && PreviousComma) {
3599 PreviousComma->setType(TT_VerilogTypeComma);
3600 addFakeParenthesis(Start: FirstOfType, Precedence: prec::Comma, End: PreviousComma->Previous);
3601 }
3602
3603 FirstOfType = TypedName;
3604
3605 // Don't let higher precedence handle the qualifiers. For example if we
3606 // have:
3607 // parameter x = 0
3608 // We skip `parameter` here. This way the fake parentheses for the
3609 // assignment will be around `x = 0`.
3610 while (Current && Current != FirstOfType) {
3611 if (Current->opensScope()) {
3612 next();
3613 parse();
3614 }
3615 next();
3616 }
3617 }
3618
3619 return FirstOfType;
3620 }
3621
3622 const FormatStyle &Style;
3623 const AdditionalKeywords &Keywords;
3624 const AnnotatedLine &Line;
3625 FormatToken *Current;
3626};
3627
3628} // end anonymous namespace
3629
3630void TokenAnnotator::setCommentLineLevels(
3631 SmallVectorImpl<AnnotatedLine *> &Lines) const {
3632 const AnnotatedLine *NextNonCommentLine = nullptr;
3633 for (AnnotatedLine *Line : reverse(C&: Lines)) {
3634 assert(Line->First);
3635
3636 // If the comment is currently aligned with the line immediately following
3637 // it, that's probably intentional and we should keep it.
3638 if (NextNonCommentLine && NextNonCommentLine->First->NewlinesBefore < 2 &&
3639 Line->isComment() && !isClangFormatOff(Comment: Line->First->TokenText) &&
3640 NextNonCommentLine->First->OriginalColumn ==
3641 Line->First->OriginalColumn) {
3642 const bool PPDirectiveOrImportStmt =
3643 NextNonCommentLine->Type == LT_PreprocessorDirective ||
3644 NextNonCommentLine->Type == LT_ImportStatement;
3645 if (PPDirectiveOrImportStmt)
3646 Line->Type = LT_CommentAbovePPDirective;
3647 // Align comments for preprocessor lines with the # in column 0 if
3648 // preprocessor lines are not indented. Otherwise, align with the next
3649 // line.
3650 Line->Level = Style.IndentPPDirectives < FormatStyle::PPDIS_BeforeHash &&
3651 PPDirectiveOrImportStmt
3652 ? 0
3653 : NextNonCommentLine->Level;
3654 } else {
3655 NextNonCommentLine = Line->First->isNot(Kind: tok::r_brace) ? Line : nullptr;
3656 }
3657
3658 setCommentLineLevels(Line->Children);
3659 }
3660}
3661
3662static unsigned maxNestingDepth(const AnnotatedLine &Line) {
3663 unsigned Result = 0;
3664 for (const auto *Tok = Line.First; Tok; Tok = Tok->Next)
3665 Result = std::max(a: Result, b: Tok->NestingLevel);
3666 return Result;
3667}
3668
3669// Returns the token after the first qualifier of the name, or nullptr if there
3670// is no qualifier.
3671static FormatToken *skipNameQualifier(const FormatToken *Tok) {
3672 assert(Tok);
3673
3674 // Qualified names must start with an identifier.
3675 if (Tok->isNot(Kind: tok::identifier))
3676 return nullptr;
3677
3678 Tok = Tok->getNextNonComment();
3679 if (!Tok)
3680 return nullptr;
3681
3682 // Consider: A::B::B()
3683 // Tok --^
3684 if (Tok->is(Kind: tok::coloncolon))
3685 return Tok->getNextNonComment();
3686
3687 // Consider: A<float>::B<int>::B()
3688 // Tok --^
3689 if (Tok->is(TT: TT_TemplateOpener)) {
3690 Tok = Tok->MatchingParen;
3691 if (!Tok)
3692 return nullptr;
3693
3694 Tok = Tok->getNextNonComment();
3695 if (!Tok)
3696 return nullptr;
3697 }
3698
3699 return Tok->is(Kind: tok::coloncolon) ? Tok->getNextNonComment() : nullptr;
3700}
3701
3702// Returns the name of a function with no return type, e.g. a constructor or
3703// destructor.
3704static FormatToken *getFunctionName(const AnnotatedLine &Line,
3705 FormatToken *&OpeningParen) {
3706 for (FormatToken *Tok = Line.getFirstNonComment(), *Name = nullptr; Tok;
3707 Tok = Tok->getNextNonComment()) {
3708 // Skip C++11 attributes both before and after the function name.
3709 if (Tok->is(TT: TT_AttributeLSquare)) {
3710 Tok = Tok->MatchingParen;
3711 if (!Tok)
3712 return nullptr;
3713 continue;
3714 }
3715
3716 // Make sure the name is followed by a pair of parentheses.
3717 if (Name) {
3718 if (Tok->is(Kind: tok::l_paren) && Tok->is(TT: TT_Unknown) && Tok->MatchingParen) {
3719 OpeningParen = Tok;
3720 return Name;
3721 }
3722 return nullptr;
3723 }
3724
3725 // Skip keywords that may precede the constructor/destructor name.
3726 if (Tok->isOneOf(K1: tok::kw_friend, K2: tok::kw_inline, Ks: tok::kw_virtual,
3727 Ks: tok::kw_constexpr, Ks: tok::kw_consteval, Ks: tok::kw_explicit)) {
3728 continue;
3729 }
3730
3731 // Skip past template typename declarations that may precede the
3732 // constructor/destructor name.
3733 if (Tok->is(Kind: tok::kw_template)) {
3734 Tok = Tok->getNextNonComment();
3735 if (!Tok)
3736 return nullptr;
3737
3738 // If the next token after the template keyword is not an opening bracket,
3739 // it is a template instantiation, and not a function.
3740 if (Tok->isNot(Kind: TT_TemplateOpener))
3741 return nullptr;
3742
3743 Tok = Tok->MatchingParen;
3744 if (!Tok)
3745 return nullptr;
3746
3747 continue;
3748 }
3749
3750 // A qualified name may start from the global namespace.
3751 if (Tok->is(Kind: tok::coloncolon)) {
3752 Tok = Tok->Next;
3753 if (!Tok)
3754 return nullptr;
3755 }
3756
3757 // Skip to the unqualified part of the name.
3758 while (auto *Next = skipNameQualifier(Tok))
3759 Tok = Next;
3760
3761 // Skip the `~` if a destructor name.
3762 if (Tok->is(Kind: tok::tilde)) {
3763 Tok = Tok->Next;
3764 if (!Tok)
3765 return nullptr;
3766 }
3767
3768 // Make sure the name is not already annotated, e.g. as NamespaceMacro.
3769 if (Tok->isNot(Kind: tok::identifier) || Tok->isNot(Kind: TT_Unknown))
3770 return nullptr;
3771
3772 Name = Tok;
3773 }
3774
3775 return nullptr;
3776}
3777
3778// Checks if Tok is a constructor/destructor name qualified by its class name.
3779static bool isCtorOrDtorName(const FormatToken *Tok) {
3780 assert(Tok && Tok->is(tok::identifier));
3781 const auto *Prev = Tok->Previous;
3782
3783 if (Prev && Prev->is(Kind: tok::tilde))
3784 Prev = Prev->Previous;
3785
3786 // Consider: A::A() and A<int>::A()
3787 if (!Prev || (!Prev->endsSequence(K1: tok::coloncolon, Tokens: tok::identifier) &&
3788 !Prev->endsSequence(K1: tok::coloncolon, Tokens: TT_TemplateCloser))) {
3789 return false;
3790 }
3791
3792 assert(Prev->Previous);
3793 if (Prev->Previous->is(TT: TT_TemplateCloser) && Prev->Previous->MatchingParen) {
3794 Prev = Prev->Previous->MatchingParen;
3795 assert(Prev->Previous);
3796 }
3797
3798 return Prev->Previous->TokenText == Tok->TokenText;
3799}
3800
3801void TokenAnnotator::annotate(AnnotatedLine &Line) {
3802 if (!Line.InMacroBody)
3803 MacroBodyScopes.clear();
3804
3805 auto &ScopeStack = Line.InMacroBody ? MacroBodyScopes : Scopes;
3806 AnnotatingParser Parser(Style, Line, Keywords, ScopeStack);
3807 Line.Type = Parser.parseLine();
3808
3809 if (!Line.Children.empty()) {
3810 ScopeStack.push_back(Elt: ST_Other);
3811 const bool InRequiresExpression = Line.Type == LT_RequiresExpression;
3812 for (auto &Child : Line.Children) {
3813 if (InRequiresExpression &&
3814 Child->First->isNoneOf(Ks: tok::kw_typename, Ks: tok::kw_requires,
3815 Ks: TT_CompoundRequirementLBrace)) {
3816 Child->Type = LT_SimpleRequirement;
3817 }
3818 annotate(Line&: *Child);
3819 }
3820 // ScopeStack can become empty if Child has an unmatched `}`.
3821 if (!ScopeStack.empty())
3822 ScopeStack.pop_back();
3823 }
3824
3825 // With very deep nesting, ExpressionParser uses lots of stack and the
3826 // formatting algorithm is very slow. We're not going to do a good job here
3827 // anyway - it's probably generated code being formatted by mistake.
3828 // Just skip the whole line.
3829 if (maxNestingDepth(Line) > 50)
3830 Line.Type = LT_Invalid;
3831
3832 if (Line.Type == LT_Invalid)
3833 return;
3834
3835 ExpressionParser ExprParser(Style, Keywords, Line);
3836 ExprParser.parse();
3837
3838 if (IsCpp) {
3839 FormatToken *OpeningParen = nullptr;
3840 auto *Tok = getFunctionName(Line, OpeningParen);
3841 if (Tok && ((!ScopeStack.empty() && ScopeStack.back() == ST_Class) ||
3842 Line.endsWith(Tokens: TT_FunctionLBrace) || isCtorOrDtorName(Tok))) {
3843 Tok->setFinalizedType(TT_CtorDtorDeclName);
3844 assert(OpeningParen);
3845 OpeningParen->setFinalizedType(TT_FunctionDeclarationLParen);
3846 }
3847 }
3848
3849 if (Line.startsWith(Tokens: TT_ObjCMethodSpecifier))
3850 Line.Type = LT_ObjCMethodDecl;
3851 else if (Line.startsWith(Tokens: TT_ObjCDecl))
3852 Line.Type = LT_ObjCDecl;
3853 else if (Line.startsWith(Tokens: TT_ObjCProperty))
3854 Line.Type = LT_ObjCProperty;
3855
3856 auto *First = Line.First;
3857 First->SpacesRequiredBefore = 1;
3858 First->CanBreakBefore = First->MustBreakBefore;
3859}
3860
3861// This function heuristically determines whether 'Current' starts the name of a
3862// function declaration.
3863static bool isFunctionDeclarationName(const LangOptions &LangOpts,
3864 const FormatToken &Current,
3865 const AnnotatedLine &Line,
3866 FormatToken *&ClosingParen) {
3867 if (Current.is(TT: TT_FunctionDeclarationName))
3868 return true;
3869
3870 if (Current.isNoneOf(Ks: tok::identifier, Ks: tok::kw_operator))
3871 return false;
3872
3873 const auto *Prev = Current.getPreviousNonComment();
3874 assert(Prev);
3875
3876 const auto &Previous = *Prev;
3877
3878 if (const auto *PrevPrev = Previous.getPreviousNonComment();
3879 PrevPrev && PrevPrev->is(TT: TT_ObjCDecl)) {
3880 return false;
3881 }
3882
3883 auto skipOperatorName =
3884 [&LangOpts](const FormatToken *Next) -> const FormatToken * {
3885 for (; Next; Next = Next->Next) {
3886 if (Next->is(TT: TT_OverloadedOperatorLParen))
3887 return Next;
3888 if (Next->is(TT: TT_OverloadedOperator))
3889 continue;
3890 if (Next->isPlacementOperator() || Next->is(Kind: tok::kw_co_await)) {
3891 // For 'new[]' and 'delete[]'.
3892 if (Next->Next &&
3893 Next->Next->startsSequence(K1: tok::l_square, Tokens: tok::r_square)) {
3894 Next = Next->Next->Next;
3895 }
3896 continue;
3897 }
3898 if (Next->startsSequence(K1: tok::l_square, Tokens: tok::r_square)) {
3899 // For operator[]().
3900 Next = Next->Next;
3901 continue;
3902 }
3903 if ((Next->isTypeName(LangOpts) || Next->is(Kind: tok::identifier)) &&
3904 Next->Next && Next->Next->isPointerOrReference()) {
3905 // For operator void*(), operator char*(), operator Foo*().
3906 Next = Next->Next;
3907 continue;
3908 }
3909 if (Next->is(TT: TT_TemplateOpener) && Next->MatchingParen) {
3910 Next = Next->MatchingParen;
3911 continue;
3912 }
3913
3914 break;
3915 }
3916 return nullptr;
3917 };
3918
3919 const auto *Next = Current.Next;
3920 const bool IsCpp = LangOpts.CXXOperatorNames || LangOpts.C11;
3921
3922 // Find parentheses of parameter list.
3923 if (Current.is(Kind: tok::kw_operator)) {
3924 if (Line.startsWith(Tokens: tok::kw_friend))
3925 return true;
3926 if (Previous.Tok.getIdentifierInfo() &&
3927 Previous.isNoneOf(Ks: tok::kw_return, Ks: tok::kw_co_return)) {
3928 return true;
3929 }
3930 if (Previous.is(Kind: tok::r_paren) && Previous.is(TT: TT_TypeDeclarationParen)) {
3931 assert(Previous.MatchingParen);
3932 assert(Previous.MatchingParen->is(tok::l_paren));
3933 assert(Previous.MatchingParen->is(TT_TypeDeclarationParen));
3934 return true;
3935 }
3936 if (!Previous.isPointerOrReference() && Previous.isNot(Kind: TT_TemplateCloser))
3937 return false;
3938 Next = skipOperatorName(Next);
3939 } else {
3940 if (Current.isNot(Kind: TT_StartOfName) || Current.NestingLevel != 0)
3941 return false;
3942 while (Next && Next->startsSequence(K1: tok::hashhash, Tokens: tok::identifier))
3943 Next = Next->Next->Next;
3944 for (; Next; Next = Next->Next) {
3945 if (Next->is(TT: TT_TemplateOpener) && Next->MatchingParen) {
3946 Next = Next->MatchingParen;
3947 } else if (Next->is(Kind: tok::coloncolon)) {
3948 Next = Next->Next;
3949 if (!Next)
3950 return false;
3951 if (Next->is(Kind: tok::kw_operator)) {
3952 Next = skipOperatorName(Next->Next);
3953 break;
3954 }
3955 if (Next->isNot(Kind: tok::identifier))
3956 return false;
3957 } else if (isCppAttribute(IsCpp, Tok: *Next)) {
3958 Next = Next->MatchingParen;
3959 if (!Next)
3960 return false;
3961 } else if (Next->is(Kind: tok::l_paren)) {
3962 break;
3963 } else {
3964 return false;
3965 }
3966 }
3967 }
3968
3969 // Check whether parameter list can belong to a function declaration.
3970 if (!Next || Next->isNot(Kind: tok::l_paren) || !Next->MatchingParen)
3971 return false;
3972 ClosingParen = Next->MatchingParen;
3973 assert(ClosingParen->is(tok::r_paren));
3974 // If the lines ends with "{", this is likely a function definition.
3975 if (Line.Last->is(Kind: tok::l_brace))
3976 return true;
3977 if (Next->Next == ClosingParen)
3978 return true; // Empty parentheses.
3979 // If there is an &/&& after the r_paren, this is likely a function.
3980 if (ClosingParen->Next && ClosingParen->Next->is(TT: TT_PointerOrReference))
3981 return true;
3982
3983 // Check for K&R C function definitions (and C++ function definitions with
3984 // unnamed parameters), e.g.:
3985 // int f(i)
3986 // {
3987 // return i + 1;
3988 // }
3989 // bool g(size_t = 0, bool b = false)
3990 // {
3991 // return !b;
3992 // }
3993 if (IsCpp && Next->Next && Next->Next->is(Kind: tok::identifier) &&
3994 !Line.endsWith(Tokens: tok::semi)) {
3995 return true;
3996 }
3997
3998 for (const FormatToken *Tok = Next->Next; Tok && Tok != ClosingParen;
3999 Tok = Tok->Next) {
4000 if (Tok->is(TT: TT_TypeDeclarationParen))
4001 return true;
4002 if (Tok->isOneOf(K1: tok::l_paren, K2: TT_TemplateOpener) && Tok->MatchingParen) {
4003 Tok = Tok->MatchingParen;
4004 continue;
4005 }
4006 if (Tok->is(Kind: tok::kw_const) || Tok->isTypeName(LangOpts) ||
4007 Tok->isOneOf(K1: TT_PointerOrReference, K2: TT_StartOfName, Ks: tok::ellipsis)) {
4008 return true;
4009 }
4010 if (Tok->isOneOf(K1: tok::l_brace, K2: TT_ObjCMethodExpr) || Tok->Tok.isLiteral())
4011 return false;
4012 }
4013 return false;
4014}
4015
4016bool TokenAnnotator::mustBreakForReturnType(const AnnotatedLine &Line) const {
4017 assert(Line.MightBeFunctionDecl);
4018
4019 if ((Style.BreakAfterReturnType == FormatStyle::RTBS_TopLevel ||
4020 Style.BreakAfterReturnType == FormatStyle::RTBS_TopLevelDefinitions) &&
4021 Line.Level > 0) {
4022 return false;
4023 }
4024
4025 switch (Style.BreakAfterReturnType) {
4026 case FormatStyle::RTBS_None:
4027 case FormatStyle::RTBS_Automatic:
4028 case FormatStyle::RTBS_ExceptShortType:
4029 return false;
4030 case FormatStyle::RTBS_All:
4031 case FormatStyle::RTBS_TopLevel:
4032 return true;
4033 case FormatStyle::RTBS_AllDefinitions:
4034 case FormatStyle::RTBS_TopLevelDefinitions:
4035 return Line.mightBeFunctionDefinition();
4036 }
4037
4038 return false;
4039}
4040
4041void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
4042 if (Line.Computed)
4043 return;
4044
4045 Line.Computed = true;
4046
4047 for (AnnotatedLine *ChildLine : Line.Children)
4048 calculateFormattingInformation(Line&: *ChildLine);
4049
4050 auto *First = Line.First;
4051 First->TotalLength = First->IsMultiline
4052 ? Style.ColumnLimit
4053 : Line.FirstStartColumn + First->ColumnWidth;
4054 bool AlignArrayOfStructures =
4055 (Style.AlignArrayOfStructures != FormatStyle::AIAS_None &&
4056 Line.Type == LT_ArrayOfStructInitializer);
4057 if (AlignArrayOfStructures)
4058 calculateArrayInitializerColumnList(Line);
4059
4060 const auto *FirstNonComment = Line.getFirstNonComment();
4061 bool SeenName = false;
4062 bool LineIsFunctionDeclaration = false;
4063 FormatToken *AfterLastAttribute = nullptr;
4064 FormatToken *ClosingParen = nullptr;
4065
4066 for (auto *Tok = FirstNonComment && FirstNonComment->isNot(Kind: tok::kw_using)
4067 ? FirstNonComment->Next
4068 : nullptr;
4069 Tok && Tok->isNot(Kind: BK_BracedInit); Tok = Tok->Next) {
4070 if (Tok->is(TT: TT_StartOfName))
4071 SeenName = true;
4072 if (Tok->Previous->EndsCppAttributeGroup)
4073 AfterLastAttribute = Tok;
4074 if (const bool IsCtorOrDtor = Tok->is(TT: TT_CtorDtorDeclName);
4075 IsCtorOrDtor ||
4076 isFunctionDeclarationName(LangOpts, Current: *Tok, Line, ClosingParen)) {
4077 if (!IsCtorOrDtor)
4078 Tok->setFinalizedType(TT_FunctionDeclarationName);
4079 LineIsFunctionDeclaration = true;
4080 SeenName = true;
4081 if (ClosingParen) {
4082 auto *OpeningParen = ClosingParen->MatchingParen;
4083 assert(OpeningParen);
4084 if (OpeningParen->is(TT: TT_Unknown))
4085 OpeningParen->setType(TT_FunctionDeclarationLParen);
4086 }
4087 break;
4088 }
4089 }
4090
4091 if (IsCpp) {
4092 if ((LineIsFunctionDeclaration ||
4093 (FirstNonComment && FirstNonComment->is(TT: TT_CtorDtorDeclName))) &&
4094 Line.endsWith(Tokens: tok::semi, Tokens: tok::r_brace)) {
4095 auto *Tok = Line.Last->Previous;
4096 while (Tok->isNot(Kind: tok::r_brace))
4097 Tok = Tok->Previous;
4098 if (auto *LBrace = Tok->MatchingParen; LBrace && LBrace->is(TT: TT_Unknown)) {
4099 assert(LBrace->is(tok::l_brace));
4100 Tok->setBlockKind(BK_Block);
4101 LBrace->setBlockKind(BK_Block);
4102 LBrace->setFinalizedType(TT_FunctionLBrace);
4103 }
4104 }
4105
4106 if (SeenName && AfterLastAttribute &&
4107 mustBreakAfterAttributes(Tok: *AfterLastAttribute, Style)) {
4108 AfterLastAttribute->MustBreakBefore = true;
4109 if (LineIsFunctionDeclaration)
4110 Line.ReturnTypeWrapped = true;
4111 }
4112
4113 if (!LineIsFunctionDeclaration) {
4114 // Annotate */&/&& in `operator` function calls as binary operators.
4115 for (const auto *Tok = FirstNonComment; Tok; Tok = Tok->Next) {
4116 if (Tok->isNot(Kind: tok::kw_operator))
4117 continue;
4118 do {
4119 Tok = Tok->Next;
4120 } while (Tok && Tok->isNot(Kind: TT_OverloadedOperatorLParen));
4121 if (!Tok || !Tok->MatchingParen)
4122 break;
4123 const auto *LeftParen = Tok;
4124 for (Tok = Tok->Next; Tok && Tok != LeftParen->MatchingParen;
4125 Tok = Tok->Next) {
4126 if (Tok->isNot(Kind: tok::identifier))
4127 continue;
4128 auto *Next = Tok->Next;
4129 const bool NextIsBinaryOperator =
4130 Next && Next->isPointerOrReference() && Next->Next &&
4131 Next->Next->is(Kind: tok::identifier);
4132 if (!NextIsBinaryOperator)
4133 continue;
4134 Next->setType(TT_BinaryOperator);
4135 Tok = Next;
4136 }
4137 }
4138 } else if (ClosingParen) {
4139 for (auto *Tok = ClosingParen->Next; Tok; Tok = Tok->Next) {
4140 if (Tok->is(TT: TT_CtorInitializerColon))
4141 break;
4142 if (Tok->is(Kind: tok::arrow)) {
4143 Tok->setType(TT_TrailingReturnArrow);
4144 break;
4145 }
4146 if (Tok->isNot(Kind: TT_TrailingAnnotation))
4147 continue;
4148 const auto *Next = Tok->Next;
4149 if (!Next || Next->isNot(Kind: tok::l_paren))
4150 continue;
4151 Tok = Next->MatchingParen;
4152 if (!Tok)
4153 break;
4154 }
4155 }
4156 }
4157
4158 if (First->is(TT: TT_ElseLBrace)) {
4159 First->CanBreakBefore = true;
4160 First->MustBreakBefore = true;
4161 }
4162
4163 bool InFunctionDecl = Line.MightBeFunctionDecl;
4164 bool InParameterList = false;
4165 for (auto *Current = First->Next; Current; Current = Current->Next) {
4166 const FormatToken *Prev = Current->Previous;
4167 if (Current->is(TT: TT_LineComment)) {
4168 if (Prev->is(BBK: BK_BracedInit) && Prev->opensScope()) {
4169 Current->SpacesRequiredBefore =
4170 (Style.Cpp11BracedListStyle == FormatStyle::BLS_AlignFirstComment &&
4171 !Style.SpacesInParensOptions.Other)
4172 ? 0
4173 : 1;
4174 } else if (Prev->is(TT: TT_VerilogMultiLineListLParen)) {
4175 Current->SpacesRequiredBefore = 0;
4176 } else {
4177 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
4178 }
4179
4180 // If we find a trailing comment, iterate backwards to determine whether
4181 // it seems to relate to a specific parameter. If so, break before that
4182 // parameter to avoid changing the comment's meaning. E.g. don't move 'b'
4183 // to the previous line in:
4184 // SomeFunction(a,
4185 // b, // comment
4186 // c);
4187 if (!Current->HasUnescapedNewline) {
4188 for (FormatToken *Parameter = Current->Previous; Parameter;
4189 Parameter = Parameter->Previous) {
4190 if (Parameter->isOneOf(K1: tok::comment, K2: tok::r_brace))
4191 break;
4192 if (Parameter->Previous && Parameter->Previous->is(Kind: tok::comma)) {
4193 if (Parameter->Previous->isNot(Kind: TT_CtorInitializerComma) &&
4194 Parameter->HasUnescapedNewline) {
4195 Parameter->MustBreakBefore = true;
4196 }
4197 break;
4198 }
4199 }
4200 }
4201 } else if (!Current->Finalized && Current->SpacesRequiredBefore == 0 &&
4202 spaceRequiredBefore(Line, Right: *Current)) {
4203 Current->SpacesRequiredBefore = 1;
4204 }
4205
4206 const auto &Children = Prev->Children;
4207 if (!Children.empty() && Children.back()->Last->is(TT: TT_LineComment)) {
4208 Current->MustBreakBefore = true;
4209 } else {
4210 Current->MustBreakBefore =
4211 Current->MustBreakBefore || mustBreakBefore(Line, Right: *Current);
4212 if (!Current->MustBreakBefore && InFunctionDecl &&
4213 Current->is(TT: TT_FunctionDeclarationName)) {
4214 Current->MustBreakBefore = mustBreakForReturnType(Line);
4215 }
4216 }
4217
4218 Current->CanBreakBefore =
4219 Current->MustBreakBefore || canBreakBefore(Line, Right: *Current);
4220
4221 if (Current->is(TT: TT_FunctionDeclarationLParen)) {
4222 InParameterList = true;
4223 } else if (Current->is(Kind: tok::r_paren)) {
4224 const auto *LParen = Current->MatchingParen;
4225 if (LParen && LParen->is(TT: TT_FunctionDeclarationLParen))
4226 InParameterList = false;
4227 } else if (InParameterList &&
4228 Current->endsSequence(K1: TT_AttributeMacro,
4229 Tokens: TT_PointerOrReference)) {
4230 Current->CanBreakBefore = false;
4231 }
4232
4233 unsigned ChildSize = 0;
4234 if (Prev->Children.size() == 1) {
4235 FormatToken &LastOfChild = *Prev->Children[0]->Last;
4236 ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit
4237 : LastOfChild.TotalLength + 1;
4238 }
4239 if (Current->MustBreakBefore || Prev->Children.size() > 1 ||
4240 (Prev->Children.size() == 1 &&
4241 Prev->Children[0]->First->MustBreakBefore) ||
4242 Current->IsMultiline) {
4243 Current->TotalLength = Prev->TotalLength + Style.ColumnLimit;
4244 } else {
4245 Current->TotalLength = Prev->TotalLength + Current->ColumnWidth +
4246 ChildSize + Current->SpacesRequiredBefore;
4247 }
4248
4249 if (Current->is(TT: TT_ControlStatementLBrace)) {
4250 if (Style.ColumnLimit > 0 &&
4251 Style.BraceWrapping.AfterControlStatement ==
4252 FormatStyle::BWACS_MultiLine &&
4253 Line.Level * Style.IndentWidth + Line.Last->TotalLength >
4254 Style.ColumnLimit) {
4255 Current->CanBreakBefore = true;
4256 Current->MustBreakBefore = true;
4257 }
4258 } else if (Current->is(TT: TT_CtorInitializerColon)) {
4259 InFunctionDecl = false;
4260 }
4261
4262 // FIXME: Only calculate this if CanBreakBefore is true once static
4263 // initializers etc. are sorted out.
4264 // FIXME: Move magic numbers to a better place.
4265
4266 // Reduce penalty for aligning ObjC method arguments using the colon
4267 // alignment as this is the canonical way (still prefer fitting everything
4268 // into one line if possible). Trying to fit a whole expression into one
4269 // line should not force other line breaks (e.g. when ObjC method
4270 // expression is a part of other expression).
4271 Current->SplitPenalty = splitPenalty(Line, Tok: *Current, InFunctionDecl);
4272 if (Style.Language == FormatStyle::LK_ObjC &&
4273 Current->is(TT: TT_SelectorName) && Current->ParameterIndex > 0) {
4274 if (Current->ParameterIndex == 1)
4275 Current->SplitPenalty += 5 * Current->BindingStrength;
4276 } else {
4277 Current->SplitPenalty += 20 * Current->BindingStrength;
4278 }
4279 }
4280
4281 calculateUnbreakableTailLengths(Line);
4282 unsigned IndentLevel = Line.Level;
4283 for (auto *Current = First; Current; Current = Current->Next) {
4284 if (Current->Role)
4285 Current->Role->precomputeFormattingInfos(Token: Current);
4286 if (Current->MatchingParen &&
4287 Current->MatchingParen->opensBlockOrBlockTypeList(Style) &&
4288 IndentLevel > 0) {
4289 --IndentLevel;
4290 }
4291 Current->IndentLevel = IndentLevel;
4292 if (Current->opensBlockOrBlockTypeList(Style))
4293 ++IndentLevel;
4294 }
4295
4296 LLVM_DEBUG({ printDebugInfo(Line); });
4297}
4298
4299void TokenAnnotator::calculateUnbreakableTailLengths(
4300 AnnotatedLine &Line) const {
4301 unsigned UnbreakableTailLength = 0;
4302 FormatToken *Current = Line.Last;
4303 while (Current) {
4304 Current->UnbreakableTailLength = UnbreakableTailLength;
4305 if (Current->CanBreakBefore ||
4306 Current->isOneOf(K1: tok::comment, K2: tok::string_literal)) {
4307 UnbreakableTailLength = 0;
4308 } else {
4309 UnbreakableTailLength +=
4310 Current->ColumnWidth + Current->SpacesRequiredBefore;
4311 }
4312 Current = Current->Previous;
4313 }
4314}
4315
4316void TokenAnnotator::calculateArrayInitializerColumnList(
4317 AnnotatedLine &Line) const {
4318 if (Line.First == Line.Last)
4319 return;
4320 auto *CurrentToken = Line.First;
4321 CurrentToken->ArrayInitializerLineStart = true;
4322 unsigned Depth = 0;
4323 while (CurrentToken && CurrentToken != Line.Last) {
4324 if (CurrentToken->is(Kind: tok::l_brace)) {
4325 CurrentToken->IsArrayInitializer = true;
4326 if (CurrentToken->Next)
4327 CurrentToken->Next->MustBreakBefore = true;
4328 CurrentToken =
4329 calculateInitializerColumnList(Line, CurrentToken: CurrentToken->Next, Depth: Depth + 1);
4330 } else {
4331 CurrentToken = CurrentToken->Next;
4332 }
4333 }
4334}
4335
4336FormatToken *TokenAnnotator::calculateInitializerColumnList(
4337 AnnotatedLine &Line, FormatToken *CurrentToken, unsigned Depth) const {
4338 while (CurrentToken && CurrentToken != Line.Last) {
4339 if (CurrentToken->is(Kind: tok::l_brace))
4340 ++Depth;
4341 else if (CurrentToken->is(Kind: tok::r_brace))
4342 --Depth;
4343 if (Depth == 2 && CurrentToken->isOneOf(K1: tok::l_brace, K2: tok::comma)) {
4344 CurrentToken = CurrentToken->Next;
4345 if (!CurrentToken)
4346 break;
4347 CurrentToken->StartsColumn = true;
4348 CurrentToken = CurrentToken->Previous;
4349 }
4350 CurrentToken = CurrentToken->Next;
4351 }
4352 return CurrentToken;
4353}
4354
4355unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
4356 const FormatToken &Tok,
4357 bool InFunctionDecl) const {
4358 const FormatToken &Left = *Tok.Previous;
4359 const FormatToken &Right = Tok;
4360
4361 if (Left.is(Kind: tok::semi))
4362 return 0;
4363
4364 // Language specific handling.
4365 if (Style.isJava()) {
4366 if (Right.isOneOf(K1: Keywords.kw_extends, K2: Keywords.kw_throws))
4367 return 1;
4368 if (Right.is(II: Keywords.kw_implements))
4369 return 2;
4370 if (Left.is(Kind: tok::comma) && Left.NestingLevel == 0)
4371 return 3;
4372 } else if (Style.isJavaScript()) {
4373 if (Right.is(II: Keywords.kw_function) && Left.isNot(Kind: tok::comma))
4374 return 100;
4375 if (Left.is(TT: TT_JsTypeColon))
4376 return 35;
4377 if ((Left.is(TT: TT_TemplateString) && Left.TokenText.ends_with(Suffix: "${")) ||
4378 (Right.is(TT: TT_TemplateString) && Right.TokenText.starts_with(Prefix: "}"))) {
4379 return 100;
4380 }
4381 // Prefer breaking call chains (".foo") over empty "{}", "[]" or "()".
4382 if (Left.opensScope() && Right.closesScope())
4383 return 200;
4384 } else if (Style.Language == FormatStyle::LK_Proto) {
4385 if (Right.is(Kind: tok::l_square))
4386 return 1;
4387 if (Right.is(Kind: tok::period))
4388 return 500;
4389 }
4390
4391 if (Right.is(Kind: tok::identifier) && Right.Next && Right.Next->is(TT: TT_DictLiteral))
4392 return 1;
4393 if (Right.is(Kind: tok::l_square)) {
4394 if (Left.is(Kind: tok::r_square))
4395 return 200;
4396 // Slightly prefer formatting local lambda definitions like functions.
4397 if (Right.is(TT: TT_LambdaLSquare) && Left.is(Kind: tok::equal))
4398 return 35;
4399 if (Right.isNoneOf(Ks: TT_ObjCMethodExpr, Ks: TT_LambdaLSquare,
4400 Ks: TT_ArrayInitializerLSquare,
4401 Ks: TT_DesignatedInitializerLSquare, Ks: TT_AttributeLSquare)) {
4402 return 500;
4403 }
4404 }
4405
4406 if (Left.is(Kind: tok::coloncolon))
4407 return Style.PenaltyBreakScopeResolution;
4408 if (Right.isOneOf(K1: TT_StartOfName, K2: TT_FunctionDeclarationName,
4409 Ks: tok::kw_operator)) {
4410 if (Line.startsWith(Tokens: tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
4411 return 3;
4412 if (Left.is(TT: TT_StartOfName))
4413 return 110;
4414 if (InFunctionDecl && Right.NestingLevel == 0)
4415 return Style.PenaltyReturnTypeOnItsOwnLine;
4416 return 200;
4417 }
4418 if (Right.is(TT: TT_PointerOrReference))
4419 return 190;
4420 if (Right.is(TT: TT_LambdaArrow))
4421 return 110;
4422 if (Left.is(Kind: tok::equal) && Right.is(Kind: tok::l_brace))
4423 return 160;
4424 if (Left.is(TT: TT_CastRParen))
4425 return 100;
4426 if (Left.isOneOf(K1: tok::kw_class, K2: tok::kw_struct, Ks: tok::kw_union))
4427 return 5000;
4428 if (Left.is(Kind: tok::comment))
4429 return 1000;
4430
4431 if (Left.isOneOf(K1: TT_RangeBasedForLoopColon, K2: TT_InheritanceColon,
4432 Ks: TT_CtorInitializerColon)) {
4433 return 2;
4434 }
4435
4436 if (Right.isMemberAccess()) {
4437 // Breaking before the "./->" of a chained call/member access is reasonably
4438 // cheap, as formatting those with one call per line is generally
4439 // desirable. In particular, it should be cheaper to break before the call
4440 // than it is to break inside a call's parameters, which could lead to weird
4441 // "hanging" indents. The exception is the very last "./->" to support this
4442 // frequent pattern:
4443 //
4444 // aaaaaaaa.aaaaaaaa.bbbbbbb().ccccccccccccccccccccc(
4445 // dddddddd);
4446 //
4447 // which might otherwise be blown up onto many lines. Here, clang-format
4448 // won't produce "hanging" indents anyway as there is no other trailing
4449 // call.
4450 //
4451 // Also apply higher penalty is not a call as that might lead to a wrapping
4452 // like:
4453 //
4454 // aaaaaaa
4455 // .aaaaaaaaa.bbbbbbbb(cccccccc);
4456 const auto *NextOperator = Right.NextOperator;
4457 const auto Penalty = Style.PenaltyBreakBeforeMemberAccess;
4458 return NextOperator && NextOperator->Previous->closesScope()
4459 ? std::min(a: Penalty, b: 35u)
4460 : Penalty;
4461 }
4462
4463 if (Right.is(TT: TT_TrailingAnnotation) &&
4464 (!Right.Next || Right.Next->isNot(Kind: tok::l_paren))) {
4465 // Moving trailing annotations to the next line is fine for ObjC method
4466 // declarations.
4467 if (Line.startsWith(Tokens: TT_ObjCMethodSpecifier))
4468 return 10;
4469 // Generally, breaking before a trailing annotation is bad unless it is
4470 // function-like. It seems to be especially preferable to keep standard
4471 // annotations (i.e. "const", "final" and "override") on the same line.
4472 // Use a slightly higher penalty after ")" so that annotations like
4473 // "const override" are kept together.
4474 bool is_short_annotation = Right.TokenText.size() < 10;
4475 return (Left.is(Kind: tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
4476 }
4477
4478 // In for-loops, prefer breaking at ',' and ';'.
4479 if (Line.startsWith(Tokens: tok::kw_for) && Left.is(Kind: tok::equal))
4480 return 4;
4481
4482 // In Objective-C method expressions, prefer breaking before "param:" over
4483 // breaking after it.
4484 if (Right.is(TT: TT_SelectorName))
4485 return 0;
4486 if (Left.is(Kind: tok::colon)) {
4487 if (Left.is(TT: TT_ObjCMethodExpr))
4488 return Line.MightBeFunctionDecl ? 50 : 500;
4489 if (Left.is(TT: TT_ObjCSelector))
4490 return 500;
4491 }
4492
4493 // In Objective-C type declarations, avoid breaking after the category's
4494 // open paren (we'll prefer breaking after the protocol list's opening
4495 // angle bracket, if present).
4496 if (Line.Type == LT_ObjCDecl && Left.is(Kind: tok::l_paren) && Left.Previous &&
4497 Left.Previous->isOneOf(K1: tok::identifier, K2: tok::greater)) {
4498 return 500;
4499 }
4500
4501 if (Left.is(Kind: tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
4502 return Style.PenaltyBreakOpenParenthesis;
4503 if (Left.is(Kind: tok::l_paren) && InFunctionDecl && Style.AlignAfterOpenBracket)
4504 return 100;
4505 if (Left.is(Kind: tok::l_paren) && Left.Previous &&
4506 (Left.Previous->isOneOf(K1: tok::kw_for, K2: tok::kw__Generic) ||
4507 Left.Previous->isIf())) {
4508 return 1000;
4509 }
4510 if (Left.is(Kind: tok::equal) && InFunctionDecl)
4511 return 110;
4512 if (Right.is(Kind: tok::r_brace))
4513 return 1;
4514 if (Left.is(TT: TT_TemplateOpener))
4515 return 100;
4516 if (Left.opensScope()) {
4517 // If we aren't aligning after opening parens/braces we can always break
4518 // here unless the style does not want us to place all arguments on the
4519 // next line.
4520 if (!Style.AlignAfterOpenBracket &&
4521 (Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
4522 return 0;
4523 }
4524 if (Left.is(Kind: tok::l_brace) &&
4525 Style.Cpp11BracedListStyle == FormatStyle::BLS_Block) {
4526 return 19;
4527 }
4528 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
4529 : 19;
4530 }
4531 if (Left.is(TT: TT_JavaAnnotation))
4532 return 50;
4533
4534 if (Left.is(TT: TT_UnaryOperator))
4535 return 60;
4536 if (Left.isOneOf(K1: tok::plus, K2: tok::comma) && Left.Previous &&
4537 Left.Previous->isLabelString() &&
4538 (Left.NextOperator || Left.OperatorIndex != 0)) {
4539 return 50;
4540 }
4541 if (Right.is(Kind: tok::plus) && Left.isLabelString() &&
4542 (Right.NextOperator || Right.OperatorIndex != 0)) {
4543 return 25;
4544 }
4545 if (Left.is(Kind: tok::comma))
4546 return 1;
4547 if (Right.is(Kind: tok::lessless) && Left.isLabelString() &&
4548 (Right.NextOperator || Right.OperatorIndex != 1)) {
4549 return 25;
4550 }
4551 if (Right.is(Kind: tok::lessless)) {
4552 // Breaking at a << is really cheap.
4553 if (Left.isNot(Kind: tok::r_paren) || Right.OperatorIndex > 0) {
4554 // Slightly prefer to break before the first one in log-like statements.
4555 return 2;
4556 }
4557 return 1;
4558 }
4559 if (Left.ClosesTemplateDeclaration)
4560 return Style.PenaltyBreakTemplateDeclaration;
4561 if (Left.ClosesRequiresClause)
4562 return 0;
4563 if (Left.is(TT: TT_ConditionalExpr))
4564 return prec::Conditional;
4565 prec::Level Level = Left.getPrecedence();
4566 if (Level == prec::Unknown)
4567 Level = Right.getPrecedence();
4568 if (Level == prec::Assignment)
4569 return Style.PenaltyBreakAssignment;
4570 if (Level != prec::Unknown)
4571 return Level;
4572
4573 return 3;
4574}
4575
4576bool TokenAnnotator::spaceRequiredBeforeParens(const FormatToken &Right) const {
4577 if (Style.SpaceBeforeParens == FormatStyle::SBPO_Always)
4578 return true;
4579 if (Right.is(TT: TT_OverloadedOperatorLParen) &&
4580 Style.SpaceBeforeParensOptions.AfterOverloadedOperator) {
4581 return true;
4582 }
4583 if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
4584 Right.ParameterCount > 0) {
4585 return true;
4586 }
4587 return false;
4588}
4589
4590bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
4591 const FormatToken &Left,
4592 const FormatToken &Right) const {
4593 if (Left.is(Kind: tok::kw_return) &&
4594 Right.isNoneOf(Ks: tok::semi, Ks: tok::r_paren, Ks: tok::hashhash)) {
4595 return true;
4596 }
4597 if (Left.is(Kind: tok::kw_throw) && Right.is(Kind: tok::l_paren) && Right.MatchingParen &&
4598 Right.MatchingParen->is(TT: TT_CastRParen)) {
4599 return true;
4600 }
4601 if (Left.is(II: Keywords.kw_assert) && Style.isJava())
4602 return true;
4603 if (Style.ObjCSpaceAfterProperty && Line.Type == LT_ObjCProperty &&
4604 Left.is(Kind: tok::objc_property)) {
4605 return true;
4606 }
4607 if (Right.is(Kind: tok::hashhash))
4608 return Left.is(Kind: tok::hash);
4609 if (Left.isOneOf(K1: tok::hashhash, K2: tok::hash))
4610 return Right.is(Kind: tok::hash);
4611 if (Style.SpacesInParens == FormatStyle::SIPO_Custom) {
4612 if (Left.is(Kind: tok::l_paren) && Right.is(Kind: tok::r_paren))
4613 return Style.SpacesInParensOptions.InEmptyParentheses;
4614 if (Style.SpacesInParensOptions.ExceptDoubleParentheses &&
4615 Left.is(Kind: tok::r_paren) && Right.is(Kind: tok::r_paren)) {
4616 auto *InnerLParen = Left.MatchingParen;
4617 if (InnerLParen && InnerLParen->Previous == Right.MatchingParen) {
4618 InnerLParen->SpacesRequiredBefore = 0;
4619 return false;
4620 }
4621 }
4622 const FormatToken *LeftParen = nullptr;
4623 if (Left.is(Kind: tok::l_paren))
4624 LeftParen = &Left;
4625 else if (Right.is(Kind: tok::r_paren) && Right.MatchingParen)
4626 LeftParen = Right.MatchingParen;
4627 if (LeftParen && (LeftParen->is(TT: TT_ConditionLParen) ||
4628 (LeftParen->Previous &&
4629 isKeywordWithCondition(Tok: *LeftParen->Previous)))) {
4630 return Style.SpacesInParensOptions.InConditionalStatements;
4631 }
4632 }
4633
4634 // trailing return type 'auto': []() -> auto {}, auto foo() -> auto {}
4635 if (Left.is(Kind: tok::kw_auto) && Right.isOneOf(K1: TT_LambdaLBrace, K2: TT_FunctionLBrace,
4636 // function return type 'auto'
4637 Ks: TT_FunctionTypeLParen)) {
4638 return true;
4639 }
4640
4641 // auto{x} auto(x)
4642 if (Left.is(Kind: tok::kw_auto) && Right.isOneOf(K1: tok::l_paren, K2: tok::l_brace))
4643 return false;
4644
4645 const auto *BeforeLeft = Left.Previous;
4646
4647 // operator co_await(x)
4648 if (Right.is(Kind: tok::l_paren) && Left.is(Kind: tok::kw_co_await) && BeforeLeft &&
4649 BeforeLeft->is(Kind: tok::kw_operator)) {
4650 return false;
4651 }
4652 // co_await (x), co_yield (x), co_return (x)
4653 if (Left.isOneOf(K1: tok::kw_co_await, K2: tok::kw_co_yield, Ks: tok::kw_co_return) &&
4654 Right.isNoneOf(Ks: tok::semi, Ks: tok::r_paren)) {
4655 return true;
4656 }
4657
4658 if (Left.is(Kind: tok::l_paren) || Right.is(Kind: tok::r_paren)) {
4659 return (Right.is(TT: TT_CastRParen) ||
4660 (Left.MatchingParen && Left.MatchingParen->is(TT: TT_CastRParen)))
4661 ? Style.SpacesInParensOptions.InCStyleCasts
4662 : Style.SpacesInParensOptions.Other;
4663 }
4664 if (Right.isOneOf(K1: tok::semi, K2: tok::comma))
4665 return false;
4666 if (Right.is(Kind: tok::less) && Line.Type == LT_ObjCDecl) {
4667 bool IsLightweightGeneric = Right.MatchingParen &&
4668 Right.MatchingParen->Next &&
4669 Right.MatchingParen->Next->is(Kind: tok::colon);
4670 return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList;
4671 }
4672 if (Right.is(Kind: tok::less) && Left.is(Kind: tok::kw_template))
4673 return Style.SpaceAfterTemplateKeyword;
4674 if (Left.isOneOf(K1: tok::exclaim, K2: tok::tilde))
4675 return false;
4676 if (Left.is(Kind: tok::at) &&
4677 Right.isOneOf(K1: tok::identifier, K2: tok::string_literal, Ks: tok::char_constant,
4678 Ks: tok::numeric_constant, Ks: tok::l_paren, Ks: tok::l_brace,
4679 Ks: tok::kw_true, Ks: tok::kw_false)) {
4680 return false;
4681 }
4682 if (Left.is(Kind: tok::colon))
4683 return Left.isNoneOf(Ks: TT_ObjCSelector, Ks: TT_ObjCMethodExpr);
4684 if (Left.is(Kind: tok::coloncolon))
4685 return false;
4686 if (Left.is(Kind: tok::less) || Right.isOneOf(K1: tok::greater, K2: tok::less)) {
4687 if (Style.isTextProto() ||
4688 (Style.Language == FormatStyle::LK_Proto &&
4689 (Left.is(TT: TT_DictLiteral) || Right.is(TT: TT_DictLiteral)))) {
4690 // Format empty list as `<>`.
4691 if (Left.is(Kind: tok::less) && Right.is(Kind: tok::greater))
4692 return false;
4693 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block;
4694 }
4695 // Don't attempt to format operator<(), as it is handled later.
4696 if (Right.isNot(Kind: TT_OverloadedOperatorLParen))
4697 return false;
4698 }
4699 if (Right.is(Kind: tok::ellipsis)) {
4700 return Left.Tok.isLiteral() || (Left.is(Kind: tok::identifier) && BeforeLeft &&
4701 BeforeLeft->is(Kind: tok::kw_case));
4702 }
4703 if (Left.is(Kind: tok::l_square) && Right.is(Kind: tok::amp))
4704 return Style.SpacesInSquareBrackets;
4705 if (Right.is(TT: TT_PointerOrReference)) {
4706 if (Left.is(Kind: tok::r_paren) && Line.MightBeFunctionDecl) {
4707 if (!Left.MatchingParen)
4708 return true;
4709 FormatToken *TokenBeforeMatchingParen =
4710 Left.MatchingParen->getPreviousNonComment();
4711 if (!TokenBeforeMatchingParen || Left.isNot(Kind: TT_TypeDeclarationParen))
4712 return true;
4713 }
4714 // Add a space if the previous token is a pointer qualifier or the closing
4715 // parenthesis of __attribute__(()) expression and the style requires spaces
4716 // after pointer qualifiers.
4717 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_After ||
4718 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
4719 (Left.is(TT: TT_AttributeRParen) ||
4720 Left.canBePointerOrReferenceQualifier())) {
4721 return true;
4722 }
4723 if (Left.Tok.isLiteral())
4724 return true;
4725 // for (auto a = 0, b = 0; const auto & c : {1, 2, 3})
4726 if (Left.isTypeOrIdentifier(LangOpts) && Right.Next && Right.Next->Next &&
4727 Right.Next->Next->is(TT: TT_RangeBasedForLoopColon)) {
4728 return getTokenPointerOrReferenceAlignment(PointerOrReference: Right) !=
4729 FormatStyle::PAS_Left;
4730 }
4731 return Left.isNoneOf(Ks: TT_PointerOrReference, Ks: tok::l_paren) &&
4732 (getTokenPointerOrReferenceAlignment(PointerOrReference: Right) !=
4733 FormatStyle::PAS_Left ||
4734 (Line.IsMultiVariableDeclStmt &&
4735 (Left.NestingLevel == 0 ||
4736 (Left.NestingLevel == 1 && startsWithInitStatement(Line)))));
4737 }
4738 if (Right.is(TT: TT_FunctionTypeLParen) && Left.isNot(Kind: tok::l_paren) &&
4739 (Left.isNot(Kind: TT_PointerOrReference) ||
4740 (getTokenPointerOrReferenceAlignment(PointerOrReference: Left) != FormatStyle::PAS_Right &&
4741 !Line.IsMultiVariableDeclStmt))) {
4742 return true;
4743 }
4744 if (Left.is(TT: TT_PointerOrReference)) {
4745 // Add a space if the next token is a pointer qualifier and the style
4746 // requires spaces before pointer qualifiers.
4747 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Before ||
4748 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
4749 Right.canBePointerOrReferenceQualifier()) {
4750 return true;
4751 }
4752 // & 1
4753 if (Right.Tok.isLiteral())
4754 return true;
4755 // & /* comment
4756 if (Right.is(TT: TT_BlockComment))
4757 return true;
4758 // foo() -> const Bar * override/final
4759 // S::foo() & noexcept/requires
4760 if (Right.isOneOf(K1: Keywords.kw_override, K2: Keywords.kw_final, Ks: tok::kw_noexcept,
4761 Ks: TT_RequiresClause) &&
4762 Right.isNot(Kind: TT_StartOfName)) {
4763 return true;
4764 }
4765 // & {
4766 if (Right.is(Kind: tok::l_brace) && Right.is(BBK: BK_Block))
4767 return true;
4768 // for (auto a = 0, b = 0; const auto& c : {1, 2, 3})
4769 if (BeforeLeft && BeforeLeft->isTypeOrIdentifier(LangOpts) && Right.Next &&
4770 Right.Next->is(TT: TT_RangeBasedForLoopColon)) {
4771 return getTokenPointerOrReferenceAlignment(PointerOrReference: Left) !=
4772 FormatStyle::PAS_Right;
4773 }
4774 if (Right.isOneOf(K1: TT_PointerOrReference, K2: TT_ArraySubscriptLSquare,
4775 Ks: tok::l_paren)) {
4776 return false;
4777 }
4778 if (getTokenPointerOrReferenceAlignment(PointerOrReference: Left) == FormatStyle::PAS_Right)
4779 return false;
4780 // FIXME: Setting IsMultiVariableDeclStmt for the whole line is error-prone,
4781 // because it does not take into account nested scopes like lambdas.
4782 // In multi-variable declaration statements, attach */& to the variable
4783 // independently of the style. However, avoid doing it if we are in a nested
4784 // scope, e.g. lambda. We still need to special-case statements with
4785 // initializers.
4786 if (Line.IsMultiVariableDeclStmt &&
4787 (Left.NestingLevel == Line.First->NestingLevel ||
4788 ((Left.NestingLevel == Line.First->NestingLevel + 1) &&
4789 startsWithInitStatement(Line)))) {
4790 return false;
4791 }
4792 if (!BeforeLeft)
4793 return false;
4794 if (BeforeLeft->is(Kind: tok::coloncolon)) {
4795 if (Left.isNot(Kind: tok::star))
4796 return false;
4797 assert(Style.PointerAlignment != FormatStyle::PAS_Right);
4798 if (!Right.startsSequence(K1: tok::identifier, Tokens: tok::r_paren))
4799 return true;
4800 assert(Right.Next);
4801 const auto *LParen = Right.Next->MatchingParen;
4802 return !LParen || LParen->isNot(Kind: TT_FunctionTypeLParen);
4803 }
4804 return BeforeLeft->isNoneOf(Ks: tok::l_paren, Ks: tok::l_square);
4805 }
4806 // Ensure right pointer alignment with ellipsis e.g. int *...P
4807 if (Left.is(Kind: tok::ellipsis) && BeforeLeft &&
4808 BeforeLeft->isPointerOrReference()) {
4809 return Style.PointerAlignment != FormatStyle::PAS_Right;
4810 }
4811
4812 if (Right.is(Kind: tok::star) && Left.is(Kind: tok::l_paren))
4813 return false;
4814 if (Left.is(Kind: tok::star) && Right.isPointerOrReference())
4815 return false;
4816 if (Right.isPointerOrReference()) {
4817 const FormatToken *Previous = &Left;
4818 while (Previous && Previous->isNot(Kind: tok::kw_operator)) {
4819 if (Previous->is(Kind: tok::identifier) || Previous->isTypeName(LangOpts)) {
4820 Previous = Previous->getPreviousNonComment();
4821 continue;
4822 }
4823 if (Previous->is(TT: TT_TemplateCloser) && Previous->MatchingParen) {
4824 Previous = Previous->MatchingParen->getPreviousNonComment();
4825 continue;
4826 }
4827 if (Previous->is(Kind: tok::coloncolon)) {
4828 Previous = Previous->getPreviousNonComment();
4829 continue;
4830 }
4831 break;
4832 }
4833 // Space between the type and the * in:
4834 // operator void*()
4835 // operator char*()
4836 // operator void const*()
4837 // operator void volatile*()
4838 // operator /*comment*/ const char*()
4839 // operator volatile /*comment*/ char*()
4840 // operator Foo*()
4841 // operator C<T>*()
4842 // operator std::Foo*()
4843 // operator C<T>::D<U>*()
4844 // dependent on PointerAlignment style.
4845 if (Previous) {
4846 if (Previous->endsSequence(K1: tok::kw_operator))
4847 return Style.PointerAlignment != FormatStyle::PAS_Left;
4848 if (Previous->isOneOf(K1: tok::kw_const, K2: tok::kw_volatile)) {
4849 return (Style.PointerAlignment != FormatStyle::PAS_Left) ||
4850 (Style.SpaceAroundPointerQualifiers ==
4851 FormatStyle::SAPQ_After) ||
4852 (Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both);
4853 }
4854 }
4855 }
4856 if (Style.isCSharp() && Left.is(II: Keywords.kw_is) && Right.is(Kind: tok::l_square))
4857 return true;
4858 const auto SpaceRequiredForArrayInitializerLSquare =
4859 [](const FormatToken &LSquareTok, const FormatStyle &Style) {
4860 return Style.SpacesInContainerLiterals ||
4861 (Style.isProto() &&
4862 Style.Cpp11BracedListStyle == FormatStyle::BLS_Block &&
4863 LSquareTok.endsSequence(K1: tok::l_square, Tokens: tok::colon,
4864 Tokens: TT_SelectorName));
4865 };
4866 if (Left.is(Kind: tok::l_square)) {
4867 return (Left.is(TT: TT_ArrayInitializerLSquare) && Right.isNot(Kind: tok::r_square) &&
4868 SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
4869 (Left.isOneOf(K1: TT_ArraySubscriptLSquare, K2: TT_StructuredBindingLSquare,
4870 Ks: TT_LambdaLSquare) &&
4871 Style.SpacesInSquareBrackets && Right.isNot(Kind: tok::r_square));
4872 }
4873 if (Right.is(Kind: tok::r_square)) {
4874 return Right.MatchingParen &&
4875 ((Right.MatchingParen->is(TT: TT_ArrayInitializerLSquare) &&
4876 SpaceRequiredForArrayInitializerLSquare(*Right.MatchingParen,
4877 Style)) ||
4878 (Style.SpacesInSquareBrackets &&
4879 Right.MatchingParen->isOneOf(K1: TT_ArraySubscriptLSquare,
4880 K2: TT_StructuredBindingLSquare,
4881 Ks: TT_LambdaLSquare)));
4882 }
4883 if (Right.is(Kind: tok::l_square) &&
4884 Right.isNoneOf(Ks: TT_ObjCMethodExpr, Ks: TT_LambdaLSquare,
4885 Ks: TT_DesignatedInitializerLSquare,
4886 Ks: TT_StructuredBindingLSquare, Ks: TT_AttributeLSquare) &&
4887 Left.isNoneOf(Ks: tok::numeric_constant, Ks: TT_DictLiteral) &&
4888 !(Left.isNot(Kind: tok::r_square) && Style.SpaceBeforeSquareBrackets &&
4889 Right.is(TT: TT_ArraySubscriptLSquare))) {
4890 return false;
4891 }
4892 if ((Left.is(Kind: tok::l_brace) && Left.isNot(Kind: BK_Block)) ||
4893 (Right.is(Kind: tok::r_brace) && Right.MatchingParen &&
4894 Right.MatchingParen->isNot(Kind: BK_Block))) {
4895 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block ||
4896 Style.SpacesInParensOptions.Other;
4897 }
4898 if (Left.is(TT: TT_BlockComment)) {
4899 // No whitespace in x(/*foo=*/1), except for JavaScript.
4900 return Style.isJavaScript() || !Left.TokenText.ends_with(Suffix: "=*/");
4901 }
4902
4903 // Space between template and attribute.
4904 // e.g. template <typename T> [[nodiscard]] ...
4905 if (Left.is(TT: TT_TemplateCloser) && Right.is(TT: TT_AttributeLSquare))
4906 return true;
4907 // Space before parentheses common for all languages
4908 if (Right.is(Kind: tok::l_paren)) {
4909 if (Left.is(TT: TT_TemplateCloser) && Right.isNot(Kind: TT_FunctionTypeLParen))
4910 return spaceRequiredBeforeParens(Right);
4911 if (Left.isOneOf(K1: TT_RequiresClause,
4912 K2: TT_RequiresClauseInARequiresExpression)) {
4913 return Style.SpaceBeforeParensOptions.AfterRequiresInClause ||
4914 spaceRequiredBeforeParens(Right);
4915 }
4916 if (Left.is(TT: TT_RequiresExpression)) {
4917 return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
4918 spaceRequiredBeforeParens(Right);
4919 }
4920 if (Left.isOneOf(K1: TT_AttributeRParen, K2: TT_AttributeRSquare))
4921 return true;
4922 if (Left.is(TT: TT_ForEachMacro)) {
4923 return Style.SpaceBeforeParensOptions.AfterForeachMacros ||
4924 spaceRequiredBeforeParens(Right);
4925 }
4926 if (Left.is(TT: TT_IfMacro)) {
4927 return Style.SpaceBeforeParensOptions.AfterIfMacros ||
4928 spaceRequiredBeforeParens(Right);
4929 }
4930 if (Style.SpaceBeforeParens == FormatStyle::SBPO_Custom &&
4931 Left.isPlacementOperator() &&
4932 Right.isNot(Kind: TT_OverloadedOperatorLParen) &&
4933 !(Line.MightBeFunctionDecl && Left.is(TT: TT_FunctionDeclarationName))) {
4934 const auto *RParen = Right.MatchingParen;
4935 return Style.SpaceBeforeParensOptions.AfterPlacementOperator ||
4936 (RParen && RParen->is(TT: TT_CastRParen));
4937 }
4938 if (Line.Type == LT_ObjCDecl)
4939 return true;
4940 if (Left.is(Kind: tok::semi))
4941 return true;
4942 if (Left.isOneOf(K1: tok::pp_elif, K2: tok::kw_for, Ks: tok::kw_while, Ks: tok::kw_switch,
4943 Ks: tok::kw_case, Ks: TT_ForEachMacro, Ks: TT_ObjCForIn) ||
4944 Left.isIf(AllowConstexprMacro: Line.Type != LT_PreprocessorDirective) ||
4945 Right.is(TT: TT_ConditionLParen)) {
4946 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4947 spaceRequiredBeforeParens(Right);
4948 }
4949
4950 // TODO add Operator overloading specific Options to
4951 // SpaceBeforeParensOptions
4952 if (Right.is(TT: TT_OverloadedOperatorLParen))
4953 return spaceRequiredBeforeParens(Right);
4954 // Function declaration or definition
4955 if (Line.MightBeFunctionDecl && Right.is(TT: TT_FunctionDeclarationLParen)) {
4956 if (spaceRequiredBeforeParens(Right))
4957 return true;
4958 const auto &Options = Style.SpaceBeforeParensOptions;
4959 return Line.mightBeFunctionDefinition()
4960 ? Options.AfterFunctionDefinitionName
4961 : Options.AfterFunctionDeclarationName;
4962 }
4963 // Lambda
4964 if (Line.Type != LT_PreprocessorDirective && Left.is(Kind: tok::r_square) &&
4965 Left.MatchingParen && Left.MatchingParen->is(TT: TT_LambdaLSquare)) {
4966 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
4967 spaceRequiredBeforeParens(Right);
4968 }
4969 if (!BeforeLeft || BeforeLeft->isNoneOf(Ks: tok::period, Ks: tok::arrow)) {
4970 if (Left.isOneOf(K1: tok::kw_try, K2: Keywords.kw___except, Ks: tok::kw_catch)) {
4971 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4972 spaceRequiredBeforeParens(Right);
4973 }
4974 if (Left.isPlacementOperator() ||
4975 (Left.is(Kind: tok::r_square) && Left.MatchingParen &&
4976 Left.MatchingParen->Previous &&
4977 Left.MatchingParen->Previous->is(Kind: tok::kw_delete))) {
4978 return Style.SpaceBeforeParens != FormatStyle::SBPO_Never ||
4979 spaceRequiredBeforeParens(Right);
4980 }
4981 }
4982 // Handle builtins like identifiers.
4983 if (Line.Type != LT_PreprocessorDirective &&
4984 (Left.Tok.getIdentifierInfo() || Left.is(Kind: tok::r_paren))) {
4985 return spaceRequiredBeforeParens(Right);
4986 }
4987 return false;
4988 }
4989 if (Left.is(Kind: tok::at) && Right.isNot(Kind: tok::objc_not_keyword))
4990 return false;
4991 if (Right.is(TT: TT_UnaryOperator)) {
4992 return Left.isNoneOf(Ks: tok::l_paren, Ks: tok::l_square, Ks: tok::at) &&
4993 (Left.isNot(Kind: tok::colon) || Left.isNot(Kind: TT_ObjCMethodExpr));
4994 }
4995 // No space between the variable name and the initializer list.
4996 // A a1{1};
4997 // Verilog doesn't have such syntax, but it has word operators that are C++
4998 // identifiers like `a inside {b, c}`. So the rule is not applicable.
4999 if (!Style.isVerilog() &&
5000 (Left.isOneOf(K1: tok::identifier, K2: tok::greater, Ks: tok::r_square,
5001 Ks: tok::r_paren) ||
5002 Left.isTypeName(LangOpts)) &&
5003 Right.is(Kind: tok::l_brace) && Right.getNextNonComment() &&
5004 Right.isNot(Kind: BK_Block)) {
5005 return false;
5006 }
5007 if (Left.is(Kind: tok::period) || Right.is(Kind: tok::period))
5008 return false;
5009 // u#str, U#str, L#str, u8#str
5010 // uR#str, UR#str, LR#str, u8R#str
5011 if (Right.is(Kind: tok::hash) && Left.is(Kind: tok::identifier) &&
5012 (Left.TokenText == "L" || Left.TokenText == "u" ||
5013 Left.TokenText == "U" || Left.TokenText == "u8" ||
5014 Left.TokenText == "LR" || Left.TokenText == "uR" ||
5015 Left.TokenText == "UR" || Left.TokenText == "u8R")) {
5016 return false;
5017 }
5018 if (Left.is(TT: TT_TemplateCloser) && Left.MatchingParen &&
5019 Left.MatchingParen->Previous &&
5020 Left.MatchingParen->Previous->isOneOf(K1: tok::period, K2: tok::coloncolon)) {
5021 // Java call to generic function with explicit type:
5022 // A.<B<C<...>>>DoSomething();
5023 // A::<B<C<...>>>DoSomething(); // With a Java 8 method reference.
5024 return false;
5025 }
5026 if (Left.is(TT: TT_TemplateCloser) && Right.is(Kind: tok::l_square))
5027 return false;
5028 if (Left.is(Kind: tok::l_brace) && Left.endsSequence(K1: TT_DictLiteral, Tokens: tok::at)) {
5029 // Objective-C dictionary literal -> no space after opening brace.
5030 return false;
5031 }
5032 if (Right.is(Kind: tok::r_brace) && Right.MatchingParen &&
5033 Right.MatchingParen->endsSequence(K1: TT_DictLiteral, Tokens: tok::at)) {
5034 // Objective-C dictionary literal -> no space before closing brace.
5035 return false;
5036 }
5037 if (Right.is(TT: TT_TrailingAnnotation) && Right.isOneOf(K1: tok::amp, K2: tok::ampamp) &&
5038 Left.isOneOf(K1: tok::kw_const, K2: tok::kw_volatile) &&
5039 (!Right.Next || Right.Next->is(Kind: tok::semi))) {
5040 // Match const and volatile ref-qualifiers without any additional
5041 // qualifiers such as
5042 // void Fn() const &;
5043 return getTokenReferenceAlignment(PointerOrReference: Right) != FormatStyle::PAS_Left;
5044 }
5045
5046 return true;
5047}
5048
5049bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
5050 const FormatToken &Right) const {
5051 const FormatToken &Left = *Right.Previous;
5052
5053 // If the token is finalized don't touch it (as it could be in a
5054 // clang-format-off section).
5055 if (Left.Finalized)
5056 return Right.hasWhitespaceBefore();
5057
5058 const bool IsVerilog = Style.isVerilog();
5059 assert(!IsVerilog || !IsCpp);
5060
5061 // Never ever merge two words.
5062 if (Keywords.isWordLike(Tok: Right, IsVerilog) &&
5063 Keywords.isWordLike(Tok: Left, IsVerilog)) {
5064 return true;
5065 }
5066
5067 // Leave a space between * and /* to avoid C4138 `comment end` found outside
5068 // of comment.
5069 if (Left.is(Kind: tok::star) && Right.is(Kind: tok::comment))
5070 return true;
5071
5072 if (Left.is(Kind: tok::l_brace) && Right.is(Kind: tok::r_brace) &&
5073 Left.Children.empty()) {
5074 if (Left.is(BBK: BK_Block))
5075 return Style.SpaceInEmptyBraces != FormatStyle::SIEB_Never;
5076 if (Style.Cpp11BracedListStyle != FormatStyle::BLS_Block) {
5077 return Style.SpacesInParens == FormatStyle::SIPO_Custom &&
5078 Style.SpacesInParensOptions.InEmptyParentheses;
5079 }
5080 return Style.SpaceInEmptyBraces == FormatStyle::SIEB_Always;
5081 }
5082
5083 const auto *BeforeLeft = Left.Previous;
5084
5085 if (IsCpp) {
5086 if (Left.is(TT: TT_OverloadedOperator) &&
5087 Right.isOneOf(K1: TT_TemplateOpener, K2: TT_TemplateCloser)) {
5088 return true;
5089 }
5090 // Space between UDL and dot: auto b = 4s .count();
5091 if (Right.is(Kind: tok::period) && Left.is(Kind: tok::numeric_constant))
5092 return true;
5093 // Space between import <iostream>.
5094 // or import .....;
5095 if (Left.is(II: Keywords.kw_import) &&
5096 Right.isOneOf(K1: tok::less, K2: tok::ellipsis) &&
5097 (!BeforeLeft || BeforeLeft->is(Kind: tok::kw_export))) {
5098 return true;
5099 }
5100 // Space between `module :` and `import :`.
5101 if (Left.isOneOf(K1: Keywords.kw_module, K2: Keywords.kw_import) &&
5102 Right.is(TT: TT_ModulePartitionColon)) {
5103 return true;
5104 }
5105
5106 if (Right.is(TT: TT_AfterPPDirective))
5107 return true;
5108
5109 // No space between import foo:bar but keep a space between import :bar;
5110 if (Left.is(Kind: tok::identifier) && Right.is(TT: TT_ModulePartitionColon))
5111 return false;
5112 // No space between :bar;
5113 if (Left.is(TT: TT_ModulePartitionColon) &&
5114 Right.isOneOf(K1: tok::identifier, K2: tok::kw_private)) {
5115 return false;
5116 }
5117 if (Left.is(Kind: tok::ellipsis) && Right.is(Kind: tok::identifier) &&
5118 Line.First->is(II: Keywords.kw_import)) {
5119 return false;
5120 }
5121 // Space in __attribute__((attr)) ::type.
5122 if (Left.isOneOf(K1: TT_AttributeRParen, K2: TT_AttributeMacro) &&
5123 Right.is(Kind: tok::coloncolon)) {
5124 return true;
5125 }
5126
5127 if (Left.is(Kind: tok::kw_operator))
5128 return Right.is(Kind: tok::coloncolon) || Style.SpaceAfterOperatorKeyword;
5129 if (Right.is(Kind: tok::l_brace) && Right.is(BBK: BK_BracedInit) &&
5130 !Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
5131 return true;
5132 }
5133 if (Left.is(Kind: tok::less) && Left.is(TT: TT_OverloadedOperator) &&
5134 Right.is(TT: TT_TemplateOpener)) {
5135 return true;
5136 }
5137 // C++ Core Guidelines suppression tag, e.g. `[[suppress(type.5)]]`.
5138 if (Left.is(Kind: tok::identifier) && Right.is(Kind: tok::numeric_constant))
5139 return Right.TokenText[0] != '.';
5140 // `Left` is a keyword (including C++ alternative operator) or identifier.
5141 if (Left.Tok.getIdentifierInfo() && Right.Tok.isLiteral())
5142 return true;
5143 } else if (Style.isProto()) {
5144 if (Right.is(Kind: tok::period) && !(BeforeLeft && BeforeLeft->is(Kind: tok::period)) &&
5145 Left.isOneOf(K1: Keywords.kw_optional, K2: Keywords.kw_required,
5146 Ks: Keywords.kw_repeated, Ks: Keywords.kw_extend)) {
5147 return true;
5148 }
5149 if (Right.is(Kind: tok::l_paren) &&
5150 Left.isOneOf(K1: Keywords.kw_returns, K2: Keywords.kw_option)) {
5151 return true;
5152 }
5153 if (Right.isOneOf(K1: tok::l_brace, K2: tok::less) && Left.is(TT: TT_SelectorName))
5154 return true;
5155 // Slashes occur in text protocol extension syntax: [type/type] { ... }.
5156 if (Left.is(Kind: tok::slash) || Right.is(Kind: tok::slash))
5157 return false;
5158 if (Left.MatchingParen &&
5159 Left.MatchingParen->is(TT: TT_ProtoExtensionLSquare) &&
5160 Right.isOneOf(K1: tok::l_brace, K2: tok::less)) {
5161 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block;
5162 }
5163 // A percent is probably part of a formatting specification, such as %lld.
5164 if (Left.is(Kind: tok::percent))
5165 return false;
5166 // Preserve the existence of a space before a percent for cases like 0x%04x
5167 // and "%d %d"
5168 if (Left.is(Kind: tok::numeric_constant) && Right.is(Kind: tok::percent))
5169 return Right.hasWhitespaceBefore();
5170 } else if (Style.isJson()) {
5171 if (Right.is(Kind: tok::colon) && Left.is(Kind: tok::string_literal))
5172 return Style.SpaceBeforeJsonColon;
5173 } else if (Style.isCSharp()) {
5174 // Require spaces around '{' and before '}' unless they appear in
5175 // interpolated strings. Interpolated strings are merged into a single token
5176 // so cannot have spaces inserted by this function.
5177
5178 // No space between 'this' and '['
5179 if (Left.is(Kind: tok::kw_this) && Right.is(Kind: tok::l_square))
5180 return false;
5181
5182 // No space between 'new' and '('
5183 if (Left.is(Kind: tok::kw_new) && Right.is(Kind: tok::l_paren))
5184 return false;
5185
5186 // Space before { (including space within '{ {').
5187 if (Right.is(Kind: tok::l_brace))
5188 return true;
5189
5190 // Spaces inside braces.
5191 if (Left.is(Kind: tok::l_brace) && Right.isNot(Kind: tok::r_brace))
5192 return true;
5193
5194 if (Left.isNot(Kind: tok::l_brace) && Right.is(Kind: tok::r_brace))
5195 return true;
5196
5197 // Spaces around '=>'.
5198 if (Left.is(TT: TT_FatArrow) || Right.is(TT: TT_FatArrow))
5199 return true;
5200
5201 // No spaces around attribute target colons
5202 if (Left.is(TT: TT_AttributeColon) || Right.is(TT: TT_AttributeColon))
5203 return false;
5204
5205 // space between type and variable e.g. Dictionary<string,string> foo;
5206 if (Left.is(TT: TT_TemplateCloser) && Right.is(TT: TT_StartOfName))
5207 return true;
5208
5209 // spaces inside square brackets.
5210 if (Left.is(Kind: tok::l_square) || Right.is(Kind: tok::r_square))
5211 return Style.SpacesInSquareBrackets;
5212
5213 // No space before ? in nullable types.
5214 if (Right.is(TT: TT_CSharpNullable))
5215 return false;
5216
5217 // No space before null forgiving '!'.
5218 if (Right.is(TT: TT_NonNullAssertion))
5219 return false;
5220
5221 // No space between consecutive commas '[,,]'.
5222 if (Left.is(Kind: tok::comma) && Right.is(Kind: tok::comma))
5223 return false;
5224
5225 // space after var in `var (key, value)`
5226 if (Left.is(II: Keywords.kw_var) && Right.is(Kind: tok::l_paren))
5227 return true;
5228
5229 // space between keywords and paren e.g. "using ("
5230 if (Right.is(Kind: tok::l_paren)) {
5231 if (Left.isOneOf(K1: tok::kw_using, K2: Keywords.kw_async, Ks: Keywords.kw_when,
5232 Ks: Keywords.kw_lock)) {
5233 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5234 spaceRequiredBeforeParens(Right);
5235 }
5236 }
5237
5238 // space between method modifier and opening parenthesis of a tuple return
5239 // type
5240 if ((Left.isAccessSpecifierKeyword() ||
5241 Left.isOneOf(K1: tok::kw_virtual, K2: tok::kw_extern, Ks: tok::kw_static,
5242 Ks: Keywords.kw_internal, Ks: Keywords.kw_abstract,
5243 Ks: Keywords.kw_sealed, Ks: Keywords.kw_override,
5244 Ks: Keywords.kw_async, Ks: Keywords.kw_unsafe)) &&
5245 Right.is(Kind: tok::l_paren)) {
5246 return true;
5247 }
5248 } else if (Style.isJavaScript()) {
5249 if (Left.is(TT: TT_FatArrow))
5250 return true;
5251 // for await ( ...
5252 if (Right.is(Kind: tok::l_paren) && Left.is(II: Keywords.kw_await) && BeforeLeft &&
5253 BeforeLeft->is(Kind: tok::kw_for)) {
5254 return true;
5255 }
5256 if (Left.is(II: Keywords.kw_async) && Right.is(Kind: tok::l_paren) &&
5257 Right.MatchingParen) {
5258 const FormatToken *Next = Right.MatchingParen->getNextNonComment();
5259 // An async arrow function, for example: `x = async () => foo();`,
5260 // as opposed to calling a function called async: `x = async();`
5261 if (Next && Next->is(TT: TT_FatArrow))
5262 return true;
5263 }
5264 if ((Left.is(TT: TT_TemplateString) && Left.TokenText.ends_with(Suffix: "${")) ||
5265 (Right.is(TT: TT_TemplateString) && Right.TokenText.starts_with(Prefix: "}"))) {
5266 return false;
5267 }
5268 // In tagged template literals ("html`bar baz`"), there is no space between
5269 // the tag identifier and the template string.
5270 if (Keywords.isJavaScriptIdentifier(Tok: Left,
5271 /* AcceptIdentifierName= */ false) &&
5272 Right.is(TT: TT_TemplateString)) {
5273 return false;
5274 }
5275 if (Right.is(Kind: tok::star) &&
5276 Left.isOneOf(K1: Keywords.kw_function, K2: Keywords.kw_yield)) {
5277 return false;
5278 }
5279 if (Right.isOneOf(K1: tok::l_brace, K2: tok::l_square) &&
5280 Left.isOneOf(K1: Keywords.kw_function, K2: Keywords.kw_yield,
5281 Ks: Keywords.kw_extends, Ks: Keywords.kw_implements)) {
5282 return true;
5283 }
5284 if (Right.is(Kind: tok::l_paren)) {
5285 // JS methods can use some keywords as names (e.g. `delete()`).
5286 if (Line.MustBeDeclaration && Left.Tok.getIdentifierInfo())
5287 return false;
5288 // Valid JS method names can include keywords, e.g. `foo.delete()` or
5289 // `bar.instanceof()`. Recognize call positions by preceding period.
5290 if (BeforeLeft && BeforeLeft->is(Kind: tok::period) &&
5291 Left.Tok.getIdentifierInfo()) {
5292 return false;
5293 }
5294 // Additional unary JavaScript operators that need a space after.
5295 if (Left.isOneOf(K1: tok::kw_throw, K2: Keywords.kw_await, Ks: Keywords.kw_typeof,
5296 Ks: tok::kw_void)) {
5297 return true;
5298 }
5299 }
5300 // `foo as const;` casts into a const type.
5301 if (Left.endsSequence(K1: tok::kw_const, Tokens: Keywords.kw_as))
5302 return false;
5303 if ((Left.isOneOf(K1: Keywords.kw_let, K2: Keywords.kw_var, Ks: Keywords.kw_in,
5304 Ks: tok::kw_const) ||
5305 // "of" is only a keyword if it appears after another identifier
5306 // (e.g. as "const x of y" in a for loop), or after a destructuring
5307 // operation (const [x, y] of z, const {a, b} of c).
5308 (Left.is(II: Keywords.kw_of) && BeforeLeft &&
5309 BeforeLeft->isOneOf(K1: tok::identifier, K2: tok::r_square, Ks: tok::r_brace))) &&
5310 (!BeforeLeft || BeforeLeft->isNot(Kind: tok::period))) {
5311 return true;
5312 }
5313 if (Left.isOneOf(K1: tok::kw_for, K2: Keywords.kw_as) && BeforeLeft &&
5314 BeforeLeft->is(Kind: tok::period) && Right.is(Kind: tok::l_paren)) {
5315 return false;
5316 }
5317 if (Left.is(II: Keywords.kw_as) &&
5318 Right.isOneOf(K1: tok::l_square, K2: tok::l_brace, Ks: tok::l_paren)) {
5319 return true;
5320 }
5321 if (Left.is(Kind: tok::kw_default) && BeforeLeft &&
5322 BeforeLeft->is(Kind: tok::kw_export)) {
5323 return true;
5324 }
5325 if (Left.is(II: Keywords.kw_is) && Right.is(Kind: tok::l_brace))
5326 return true;
5327 if (Right.isOneOf(K1: TT_JsTypeColon, K2: TT_JsTypeOptionalQuestion))
5328 return false;
5329 if (Left.is(TT: TT_JsTypeOperator) || Right.is(TT: TT_JsTypeOperator))
5330 return false;
5331 if ((Left.is(Kind: tok::l_brace) || Right.is(Kind: tok::r_brace)) &&
5332 Line.First->isOneOf(K1: Keywords.kw_import, K2: tok::kw_export)) {
5333 return false;
5334 }
5335 if (Left.is(Kind: tok::ellipsis))
5336 return false;
5337 if (Left.is(TT: TT_TemplateCloser) &&
5338 Right.isNoneOf(Ks: tok::equal, Ks: tok::l_brace, Ks: tok::comma, Ks: tok::l_square,
5339 Ks: Keywords.kw_implements, Ks: Keywords.kw_extends)) {
5340 // Type assertions ('<type>expr') are not followed by whitespace. Other
5341 // locations that should have whitespace following are identified by the
5342 // above set of follower tokens.
5343 return false;
5344 }
5345 if (Right.is(TT: TT_NonNullAssertion))
5346 return false;
5347 if (Left.is(TT: TT_NonNullAssertion) &&
5348 Right.isOneOf(K1: Keywords.kw_as, K2: Keywords.kw_in)) {
5349 return true; // "x! as string", "x! in y"
5350 }
5351 } else if (Style.isJava()) {
5352 if (Left.is(TT: TT_CaseLabelArrow) || Right.is(TT: TT_CaseLabelArrow))
5353 return true;
5354 if (Left.is(Kind: tok::r_square) && Right.is(Kind: tok::l_brace))
5355 return true;
5356 // spaces inside square brackets.
5357 if (Left.is(Kind: tok::l_square) || Right.is(Kind: tok::r_square))
5358 return Style.SpacesInSquareBrackets;
5359
5360 if (Left.is(II: Keywords.kw_synchronized) && Right.is(Kind: tok::l_paren)) {
5361 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5362 spaceRequiredBeforeParens(Right);
5363 }
5364 if ((Left.isAccessSpecifierKeyword() ||
5365 Left.isOneOf(K1: tok::kw_static, K2: Keywords.kw_final, Ks: Keywords.kw_abstract,
5366 Ks: Keywords.kw_native)) &&
5367 Right.is(TT: TT_TemplateOpener)) {
5368 return true;
5369 }
5370 } else if (IsVerilog) {
5371 // An escaped identifier ends with whitespace.
5372 if (Left.is(Kind: tok::identifier) && Left.TokenText[0] == '\\')
5373 return true;
5374 // Add space between things in a primitive's state table unless in a
5375 // transition like `(0?)`.
5376 if ((Left.is(TT: TT_VerilogTableItem) &&
5377 Right.isNoneOf(Ks: tok::r_paren, Ks: tok::semi)) ||
5378 (Right.is(TT: TT_VerilogTableItem) && Left.isNot(Kind: tok::l_paren))) {
5379 const FormatToken *Next = Right.getNextNonComment();
5380 return !(Next && Next->is(Kind: tok::r_paren));
5381 }
5382 // Don't add space within a delay like `#0`.
5383 if (Left.isNot(Kind: TT_BinaryOperator) &&
5384 Left.isOneOf(K1: Keywords.kw_verilogHash, K2: Keywords.kw_verilogHashHash)) {
5385 return false;
5386 }
5387 // Add space after a delay.
5388 if (Right.isNot(Kind: tok::semi) &&
5389 (Left.endsSequence(K1: tok::numeric_constant, Tokens: Keywords.kw_verilogHash) ||
5390 Left.endsSequence(K1: tok::numeric_constant,
5391 Tokens: Keywords.kw_verilogHashHash) ||
5392 (Left.is(Kind: tok::r_paren) && Left.MatchingParen &&
5393 Left.MatchingParen->endsSequence(K1: tok::l_paren, Tokens: tok::at)))) {
5394 return true;
5395 }
5396 // Don't add embedded spaces in a number literal like `16'h1?ax` or an array
5397 // literal like `'{}`.
5398 if (Left.is(II: Keywords.kw_apostrophe) ||
5399 (Left.is(TT: TT_VerilogNumberBase) && Right.is(Kind: tok::numeric_constant))) {
5400 return false;
5401 }
5402 // Add spaces around the implication operator `->`.
5403 if (Left.is(Kind: tok::arrow) || Right.is(Kind: tok::arrow))
5404 return true;
5405 // Don't add spaces between two at signs. Like in a coverage event.
5406 // Don't add spaces between at and a sensitivity list like
5407 // `@(posedge clk)`.
5408 if (Left.is(Kind: tok::at) && Right.isOneOf(K1: tok::l_paren, K2: tok::star, Ks: tok::at))
5409 return false;
5410 // Add space between the type name and dimension like `logic [1:0]`.
5411 if (Right.is(Kind: tok::l_square) &&
5412 Left.isOneOf(K1: TT_VerilogDimensionedTypeName, K2: Keywords.kw_function)) {
5413 return true;
5414 }
5415 // In a tagged union expression, there should be a space after the tag.
5416 if (Right.isOneOf(K1: tok::period, K2: Keywords.kw_apostrophe) &&
5417 Keywords.isVerilogIdentifier(Tok: Left) && Left.getPreviousNonComment() &&
5418 Left.getPreviousNonComment()->is(II: Keywords.kw_tagged)) {
5419 return true;
5420 }
5421 // Don't add spaces between a casting type and the quote or repetition count
5422 // and the brace. The case of tagged union expressions is handled by the
5423 // previous rule.
5424 if ((Right.is(II: Keywords.kw_apostrophe) ||
5425 (Right.is(BBK: BK_BracedInit) && Right.is(Kind: tok::l_brace))) &&
5426 Left.isNoneOf(Ks: Keywords.kw_assign, Ks: Keywords.kw_unique) &&
5427 !Keywords.isVerilogWordOperator(Tok: Left) &&
5428 (Left.isOneOf(K1: tok::r_square, K2: tok::r_paren, Ks: tok::r_brace,
5429 Ks: tok::numeric_constant) ||
5430 Keywords.isWordLike(Tok: Left))) {
5431 return false;
5432 }
5433 // Don't add spaces in imports like `import foo::*;`.
5434 if ((Right.is(Kind: tok::star) && Left.is(Kind: tok::coloncolon)) ||
5435 (Left.is(Kind: tok::star) && Right.is(Kind: tok::semi))) {
5436 return false;
5437 }
5438 // Add space in attribute like `(* ASYNC_REG = "TRUE" *)`.
5439 if (Left.endsSequence(K1: tok::star, Tokens: tok::l_paren) && Right.is(Kind: tok::identifier))
5440 return true;
5441 // Add space before drive strength like in `wire (strong1, pull0)`.
5442 if (Right.is(Kind: tok::l_paren) && Right.is(TT: TT_VerilogStrength))
5443 return true;
5444 // Don't add space in a streaming concatenation like `{>>{j}}`.
5445 if ((Left.is(Kind: tok::l_brace) &&
5446 Right.isOneOf(K1: tok::lessless, K2: tok::greatergreater)) ||
5447 (Left.endsSequence(K1: tok::lessless, Tokens: tok::l_brace) ||
5448 Left.endsSequence(K1: tok::greatergreater, Tokens: tok::l_brace))) {
5449 return false;
5450 }
5451 } else if (Style.isTableGen()) {
5452 // Avoid to connect [ and {. [{ is start token of multiline string.
5453 if (Left.is(Kind: tok::l_square) && Right.is(Kind: tok::l_brace))
5454 return true;
5455 if (Left.is(Kind: tok::r_brace) && Right.is(Kind: tok::r_square))
5456 return true;
5457 // Do not insert around colon in DAGArg and cond operator.
5458 if (Right.isOneOf(K1: TT_TableGenDAGArgListColon,
5459 K2: TT_TableGenDAGArgListColonToAlign) ||
5460 Left.isOneOf(K1: TT_TableGenDAGArgListColon,
5461 K2: TT_TableGenDAGArgListColonToAlign)) {
5462 return false;
5463 }
5464 if (Right.is(TT: TT_TableGenCondOperatorColon))
5465 return false;
5466 if (Left.isOneOf(K1: TT_TableGenDAGArgOperatorID,
5467 K2: TT_TableGenDAGArgOperatorToBreak) &&
5468 Right.isNot(Kind: TT_TableGenDAGArgCloser)) {
5469 return true;
5470 }
5471 // Do not insert bang operators and consequent openers.
5472 if (Right.isOneOf(K1: tok::l_paren, K2: tok::less) &&
5473 Left.isOneOf(K1: TT_TableGenBangOperator, K2: TT_TableGenCondOperator)) {
5474 return false;
5475 }
5476 // Trailing paste requires space before '{' or ':', the case in name values.
5477 // Not before ';', the case in normal values.
5478 if (Left.is(TT: TT_TableGenTrailingPasteOperator) &&
5479 Right.isOneOf(K1: tok::l_brace, K2: tok::colon)) {
5480 return true;
5481 }
5482 // Otherwise paste operator does not prefer space around.
5483 if (Left.is(Kind: tok::hash) || Right.is(Kind: tok::hash))
5484 return false;
5485 // Sure not to connect after defining keywords.
5486 if (Keywords.isTableGenDefinition(Tok: Left))
5487 return true;
5488 }
5489
5490 if (Left.is(TT: TT_ImplicitStringLiteral))
5491 return Right.hasWhitespaceBefore();
5492 if (Line.Type == LT_ObjCMethodDecl) {
5493 if (Left.is(TT: TT_ObjCMethodSpecifier))
5494 return Style.ObjCSpaceAfterMethodDeclarationPrefix;
5495 if (Left.is(Kind: tok::r_paren) && Left.isNot(Kind: TT_AttributeRParen) &&
5496 canBeObjCSelectorComponent(Tok: Right)) {
5497 // Don't space between ')' and <id> or ')' and 'new'. 'new' is not a
5498 // keyword in Objective-C, and '+ (instancetype)new;' is a standard class
5499 // method declaration.
5500 return false;
5501 }
5502 }
5503 if (Line.Type == LT_ObjCProperty &&
5504 (Right.is(Kind: tok::equal) || Left.is(Kind: tok::equal))) {
5505 return false;
5506 }
5507
5508 if (Right.isOneOf(K1: TT_TrailingReturnArrow, K2: TT_LambdaArrow) ||
5509 Left.isOneOf(K1: TT_TrailingReturnArrow, K2: TT_LambdaArrow)) {
5510 return true;
5511 }
5512 if (Left.is(Kind: tok::comma) && Right.isNot(Kind: TT_OverloadedOperatorLParen) &&
5513 // In an unexpanded macro call we only find the parentheses and commas
5514 // in a line; the commas and closing parenthesis do not require a space.
5515 (Left.Children.empty() || !Left.MacroParent)) {
5516 return true;
5517 }
5518 if (Right.is(Kind: tok::comma))
5519 return false;
5520 if (Right.is(TT: TT_ObjCBlockLParen))
5521 return true;
5522 if (Right.is(TT: TT_CtorInitializerColon))
5523 return Style.SpaceBeforeCtorInitializerColon;
5524 if (Right.is(TT: TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
5525 return false;
5526 if (Right.is(TT: TT_RangeBasedForLoopColon) &&
5527 !Style.SpaceBeforeRangeBasedForLoopColon) {
5528 return false;
5529 }
5530 if (Left.is(TT: TT_BitFieldColon)) {
5531 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
5532 Style.BitFieldColonSpacing == FormatStyle::BFCS_After;
5533 }
5534 if (Right.is(Kind: tok::colon)) {
5535 if (Right.is(TT: TT_CaseLabelColon))
5536 return Style.SpaceBeforeCaseColon;
5537 if (Right.is(TT: TT_GotoLabelColon))
5538 return false;
5539 // `private:` and `public:`.
5540 if (!Right.getNextNonComment())
5541 return false;
5542 if (Right.isOneOf(K1: TT_ObjCSelector, K2: TT_ObjCMethodExpr))
5543 return false;
5544 if (Left.is(Kind: tok::question))
5545 return false;
5546 if (Right.is(TT: TT_InlineASMColon) && Left.is(Kind: tok::coloncolon))
5547 return false;
5548 if (Right.is(TT: TT_DictLiteral))
5549 return Style.SpacesInContainerLiterals;
5550 if (Right.is(TT: TT_AttributeColon))
5551 return false;
5552 if (Right.is(TT: TT_CSharpNamedArgumentColon))
5553 return false;
5554 if (Right.is(TT: TT_GenericSelectionColon))
5555 return false;
5556 if (Right.is(TT: TT_BitFieldColon)) {
5557 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
5558 Style.BitFieldColonSpacing == FormatStyle::BFCS_Before;
5559 }
5560 return true;
5561 }
5562 // Do not merge "- -" into "--".
5563 if ((Left.isOneOf(K1: tok::minus, K2: tok::minusminus) &&
5564 Right.isOneOf(K1: tok::minus, K2: tok::minusminus)) ||
5565 (Left.isOneOf(K1: tok::plus, K2: tok::plusplus) &&
5566 Right.isOneOf(K1: tok::plus, K2: tok::plusplus))) {
5567 return true;
5568 }
5569 if (Left.is(TT: TT_UnaryOperator)) {
5570 // Lambda captures allow for a lone &, so "&]" needs to be properly
5571 // handled.
5572 if (Left.is(Kind: tok::amp) && Right.is(Kind: tok::r_square))
5573 return Style.SpacesInSquareBrackets;
5574 if (Left.isNot(Kind: tok::exclaim))
5575 return false;
5576 if (Left.TokenText == "!")
5577 return Style.SpaceAfterLogicalNot;
5578 assert(Left.TokenText == "not");
5579 return Right.isOneOf(K1: tok::coloncolon, K2: TT_UnaryOperator) ||
5580 (Right.is(Kind: tok::l_paren) && Style.SpaceBeforeParensOptions.AfterNot);
5581 }
5582
5583 // If the next token is a binary operator or a selector name, we have
5584 // incorrectly classified the parenthesis as a cast. FIXME: Detect correctly.
5585 if (Left.is(TT: TT_CastRParen)) {
5586 return Style.SpaceAfterCStyleCast ||
5587 Right.isOneOf(K1: TT_BinaryOperator, K2: TT_SelectorName);
5588 }
5589
5590 auto ShouldAddSpacesInAngles = [this, &Right]() {
5591 if (this->Style.SpacesInAngles == FormatStyle::SIAS_Always)
5592 return true;
5593 if (this->Style.SpacesInAngles == FormatStyle::SIAS_Leave)
5594 return Right.hasWhitespaceBefore();
5595 return false;
5596 };
5597
5598 if (Left.is(Kind: tok::greater) && Right.is(Kind: tok::greater)) {
5599 if (Style.isTextProto() ||
5600 (Style.Language == FormatStyle::LK_Proto && Left.is(TT: TT_DictLiteral))) {
5601 return Style.Cpp11BracedListStyle == FormatStyle::BLS_Block;
5602 }
5603 return Right.is(TT: TT_TemplateCloser) && Left.is(TT: TT_TemplateCloser) &&
5604 ((Style.Standard < FormatStyle::LS_Cpp11) ||
5605 ShouldAddSpacesInAngles());
5606 }
5607 if (Right.isOneOf(K1: tok::arrow, K2: tok::arrowstar, Ks: tok::periodstar) ||
5608 Left.isOneOf(K1: tok::arrow, K2: tok::period, Ks: tok::arrowstar, Ks: tok::periodstar) ||
5609 (Right.is(Kind: tok::period) && Right.isNot(Kind: TT_DesignatedInitializerPeriod))) {
5610 return false;
5611 }
5612 if (!Style.SpaceBeforeAssignmentOperators && Left.isNot(Kind: TT_TemplateCloser) &&
5613 Right.getPrecedence() == prec::Assignment) {
5614 return false;
5615 }
5616 if (Style.isJava() && Right.is(Kind: tok::coloncolon) &&
5617 Left.isOneOf(K1: tok::identifier, K2: tok::kw_this)) {
5618 return false;
5619 }
5620 if (Right.is(Kind: tok::coloncolon) && Left.is(Kind: tok::identifier)) {
5621 // Generally don't remove existing spaces between an identifier and "::".
5622 // The identifier might actually be a macro name such as ALWAYS_INLINE. If
5623 // this turns out to be too lenient, add analysis of the identifier itself.
5624 return Right.hasWhitespaceBefore();
5625 }
5626 if (Right.is(Kind: tok::coloncolon) &&
5627 Left.isNoneOf(Ks: tok::l_brace, Ks: tok::comment, Ks: tok::l_paren)) {
5628 // Put a space between < and :: in vector< ::std::string >
5629 return (Left.is(TT: TT_TemplateOpener) &&
5630 ((Style.Standard < FormatStyle::LS_Cpp11) ||
5631 ShouldAddSpacesInAngles())) ||
5632 Left.isNoneOf(Ks: tok::l_paren, Ks: tok::r_paren, Ks: tok::l_square,
5633 Ks: tok::kw___super, Ks: TT_TemplateOpener,
5634 Ks: TT_TemplateCloser) ||
5635 (Left.is(Kind: tok::l_paren) && Style.SpacesInParensOptions.Other);
5636 }
5637 if ((Left.is(TT: TT_TemplateOpener)) != (Right.is(TT: TT_TemplateCloser)))
5638 return ShouldAddSpacesInAngles();
5639 if (Left.is(Kind: tok::r_paren) && Left.isNot(Kind: TT_TypeDeclarationParen) &&
5640 Right.is(TT: TT_PointerOrReference) && Right.isOneOf(K1: tok::amp, K2: tok::ampamp)) {
5641 return true;
5642 }
5643 // Space before TT_StructuredBindingLSquare.
5644 if (Right.is(TT: TT_StructuredBindingLSquare)) {
5645 return Left.isNoneOf(Ks: tok::amp, Ks: tok::ampamp) ||
5646 getTokenReferenceAlignment(PointerOrReference: Left) != FormatStyle::PAS_Right;
5647 }
5648 // Space before & or && following a TT_StructuredBindingLSquare.
5649 if (Right.Next && Right.Next->is(TT: TT_StructuredBindingLSquare) &&
5650 Right.isOneOf(K1: tok::amp, K2: tok::ampamp)) {
5651 return getTokenReferenceAlignment(PointerOrReference: Right) != FormatStyle::PAS_Left;
5652 }
5653 if ((Right.is(TT: TT_BinaryOperator) && Left.isNot(Kind: tok::l_paren)) ||
5654 (Left.isOneOf(K1: TT_BinaryOperator, K2: TT_ConditionalExpr) &&
5655 Right.isNot(Kind: tok::r_paren))) {
5656 return true;
5657 }
5658 if (Right.is(TT: TT_TemplateOpener) && Left.is(Kind: tok::r_paren) &&
5659 Left.MatchingParen &&
5660 Left.MatchingParen->is(TT: TT_OverloadedOperatorLParen)) {
5661 return false;
5662 }
5663 if (Right.is(Kind: tok::less) && Left.isNot(Kind: tok::l_paren) &&
5664 Line.Type == LT_ImportStatement) {
5665 return true;
5666 }
5667 if (Right.is(TT: TT_TrailingUnaryOperator))
5668 return false;
5669 if (Left.is(TT: TT_RegexLiteral))
5670 return false;
5671 return spaceRequiredBetween(Line, Left, Right);
5672}
5673
5674// Returns 'true' if 'Tok' is a brace we'd want to break before in Allman style.
5675static bool isAllmanBrace(const FormatToken &Tok) {
5676 return Tok.is(Kind: tok::l_brace) && Tok.is(BBK: BK_Block) &&
5677 Tok.isNoneOf(Ks: TT_ObjCBlockLBrace, Ks: TT_LambdaLBrace, Ks: TT_DictLiteral);
5678}
5679
5680// Returns 'true' if 'Tok' is a function argument.
5681static bool IsFunctionArgument(const FormatToken &Tok) {
5682 return Tok.MatchingParen && Tok.MatchingParen->Next &&
5683 Tok.MatchingParen->Next->isOneOf(K1: tok::comma, K2: tok::r_paren,
5684 Ks: tok::r_brace);
5685}
5686
5687static bool
5688isEmptyLambdaAllowed(const FormatToken &Tok,
5689 FormatStyle::ShortLambdaStyle ShortLambdaOption) {
5690 return Tok.Children.empty() && ShortLambdaOption != FormatStyle::SLS_None;
5691}
5692
5693static bool isAllmanLambdaBrace(const FormatToken &Tok) {
5694 return Tok.is(Kind: tok::l_brace) && Tok.is(BBK: BK_Block) &&
5695 Tok.isNoneOf(Ks: TT_ObjCBlockLBrace, Ks: TT_DictLiteral);
5696}
5697
5698bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
5699 const FormatToken &Right) const {
5700 if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0 &&
5701 (!Style.RemoveEmptyLinesInUnwrappedLines || &Right == Line.First)) {
5702 return true;
5703 }
5704
5705 const FormatToken &Left = *Right.Previous;
5706
5707 if (Style.BreakFunctionDefinitionParameters && Line.MightBeFunctionDecl &&
5708 Line.mightBeFunctionDefinition() && Left.MightBeFunctionDeclParen &&
5709 Left.ParameterCount > 0) {
5710 return true;
5711 }
5712
5713 // Ignores the first parameter as this will be handled separately by
5714 // BreakFunctionDefinitionParameters or AlignAfterOpenBracket.
5715 if (Style.BinPackParameters == FormatStyle::BPPS_AlwaysOnePerLine &&
5716 Line.MightBeFunctionDecl && !Left.opensScope() &&
5717 startsNextParameter(Current: Right, Style)) {
5718 return true;
5719 }
5720
5721 const auto *BeforeLeft = Left.Previous;
5722 const auto *AfterRight = Right.Next;
5723
5724 if (Style.isCSharp()) {
5725 if (Left.is(TT: TT_FatArrow) && Right.is(Kind: tok::l_brace) &&
5726 Style.BraceWrapping.AfterFunction) {
5727 return true;
5728 }
5729 if (Right.is(TT: TT_CSharpNamedArgumentColon) ||
5730 Left.is(TT: TT_CSharpNamedArgumentColon)) {
5731 return false;
5732 }
5733 if (Right.is(TT: TT_CSharpGenericTypeConstraint))
5734 return true;
5735 if (AfterRight && AfterRight->is(TT: TT_FatArrow) &&
5736 (Right.is(Kind: tok::numeric_constant) ||
5737 (Right.is(Kind: tok::identifier) && Right.TokenText == "_"))) {
5738 return true;
5739 }
5740
5741 // Break after C# [...] and before public/protected/private/internal.
5742 if (Left.is(TT: TT_AttributeRSquare) &&
5743 (Right.isAccessSpecifier(/*ColonRequired=*/false) ||
5744 Right.is(II: Keywords.kw_internal))) {
5745 return true;
5746 }
5747 // Break between ] and [ but only when there are really 2 attributes.
5748 if (Left.is(TT: TT_AttributeRSquare) && Right.is(TT: TT_AttributeLSquare))
5749 return true;
5750 } else if (Style.isJavaScript()) {
5751 // FIXME: This might apply to other languages and token kinds.
5752 if (Right.is(Kind: tok::string_literal) && Left.is(Kind: tok::plus) && BeforeLeft &&
5753 BeforeLeft->is(Kind: tok::string_literal)) {
5754 return true;
5755 }
5756 if (Left.is(TT: TT_DictLiteral) && Left.is(Kind: tok::l_brace) && Line.Level == 0 &&
5757 BeforeLeft && BeforeLeft->is(Kind: tok::equal) &&
5758 Line.First->isOneOf(K1: tok::identifier, K2: Keywords.kw_import, Ks: tok::kw_export,
5759 Ks: tok::kw_const) &&
5760 // kw_var/kw_let are pseudo-tokens that are tok::identifier, so match
5761 // above.
5762 Line.First->isNoneOf(Ks: Keywords.kw_var, Ks: Keywords.kw_let)) {
5763 // Object literals on the top level of a file are treated as "enum-style".
5764 // Each key/value pair is put on a separate line, instead of bin-packing.
5765 return true;
5766 }
5767 if (Left.is(Kind: tok::l_brace) && Line.Level == 0 &&
5768 (Line.startsWith(Tokens: tok::kw_enum) ||
5769 Line.startsWith(Tokens: tok::kw_const, Tokens: tok::kw_enum) ||
5770 Line.startsWith(Tokens: tok::kw_export, Tokens: tok::kw_enum) ||
5771 Line.startsWith(Tokens: tok::kw_export, Tokens: tok::kw_const, Tokens: tok::kw_enum))) {
5772 // JavaScript top-level enum key/value pairs are put on separate lines
5773 // instead of bin-packing.
5774 return true;
5775 }
5776 if (Right.is(Kind: tok::r_brace) && Left.is(Kind: tok::l_brace) && BeforeLeft &&
5777 BeforeLeft->is(TT: TT_FatArrow)) {
5778 // JS arrow function (=> {...}).
5779 switch (Style.AllowShortLambdasOnASingleLine) {
5780 case FormatStyle::SLS_All:
5781 return false;
5782 case FormatStyle::SLS_None:
5783 return true;
5784 case FormatStyle::SLS_Empty:
5785 return !Left.Children.empty();
5786 case FormatStyle::SLS_Inline:
5787 // allow one-lining inline (e.g. in function call args) and empty arrow
5788 // functions.
5789 return (Left.NestingLevel == 0 && Line.Level == 0) &&
5790 !Left.Children.empty();
5791 }
5792 llvm_unreachable("Unknown FormatStyle::ShortLambdaStyle enum");
5793 }
5794
5795 if (Right.is(Kind: tok::r_brace) && Left.is(Kind: tok::l_brace) &&
5796 !Left.Children.empty()) {
5797 // Support AllowShortFunctionsOnASingleLine for JavaScript.
5798 return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None ||
5799 Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Empty ||
5800 (Left.NestingLevel == 0 && Line.Level == 0 &&
5801 Style.AllowShortFunctionsOnASingleLine &
5802 FormatStyle::SFS_InlineOnly);
5803 }
5804 } else if (Style.isJava()) {
5805 if (Right.is(Kind: tok::plus) && Left.is(Kind: tok::string_literal) && AfterRight &&
5806 AfterRight->is(Kind: tok::string_literal)) {
5807 return true;
5808 }
5809 } else if (Style.isVerilog()) {
5810 // Break between assignments.
5811 if (Left.is(TT: TT_VerilogAssignComma))
5812 return true;
5813 // Break between ports of different types.
5814 if (Left.is(TT: TT_VerilogTypeComma))
5815 return true;
5816 // Break between ports in a module instantiation and after the parameter
5817 // list.
5818 if (Style.VerilogBreakBetweenInstancePorts &&
5819 (Left.is(TT: TT_VerilogInstancePortComma) ||
5820 (Left.is(Kind: tok::r_paren) && Keywords.isVerilogIdentifier(Tok: Right) &&
5821 Left.MatchingParen &&
5822 Left.MatchingParen->is(TT: TT_VerilogInstancePortLParen)))) {
5823 return true;
5824 }
5825 // Break after labels. In Verilog labels don't have the 'case' keyword, so
5826 // it is hard to identify them in UnwrappedLineParser.
5827 if (!Keywords.isVerilogBegin(Tok: Right) && Keywords.isVerilogEndOfLabel(Tok: Left))
5828 return true;
5829 } else if (Style.BreakAdjacentStringLiterals &&
5830 (IsCpp || Style.isProto() || Style.isTableGen())) {
5831 if (Left.isStringLiteral() && Right.isStringLiteral())
5832 return true;
5833 }
5834
5835 // Basic JSON newline processing.
5836 if (Style.isJson()) {
5837 // Always break after a JSON record opener.
5838 // {
5839 // }
5840 if (Left.is(TT: TT_DictLiteral) && Left.is(Kind: tok::l_brace))
5841 return true;
5842 // Always break after a JSON array opener based on BreakArrays.
5843 if ((Left.is(TT: TT_ArrayInitializerLSquare) && Left.is(Kind: tok::l_square) &&
5844 Right.isNot(Kind: tok::r_square)) ||
5845 Left.is(Kind: tok::comma)) {
5846 if (Right.is(Kind: tok::l_brace))
5847 return true;
5848 // scan to the right if an we see an object or an array inside
5849 // then break.
5850 for (const auto *Tok = &Right; Tok; Tok = Tok->Next) {
5851 if (Tok->isOneOf(K1: tok::l_brace, K2: tok::l_square))
5852 return true;
5853 if (Tok->isOneOf(K1: tok::r_brace, K2: tok::r_square))
5854 break;
5855 }
5856 return Style.BreakArrays;
5857 }
5858 } else if (Style.isTableGen()) {
5859 // Break the comma in side cond operators.
5860 // !cond(case1:1,
5861 // case2:0);
5862 if (Left.is(TT: TT_TableGenCondOperatorComma))
5863 return true;
5864 if (Left.is(TT: TT_TableGenDAGArgOperatorToBreak) &&
5865 Right.isNot(Kind: TT_TableGenDAGArgCloser)) {
5866 return true;
5867 }
5868 if (Left.is(TT: TT_TableGenDAGArgListCommaToBreak))
5869 return true;
5870 if (Right.is(TT: TT_TableGenDAGArgCloser) && Right.MatchingParen &&
5871 Right.MatchingParen->is(TT: TT_TableGenDAGArgOpenerToBreak) &&
5872 &Left != Right.MatchingParen->Next) {
5873 // Check to avoid empty DAGArg such as (ins).
5874 return Style.TableGenBreakInsideDAGArg == FormatStyle::DAS_BreakAll;
5875 }
5876 }
5877
5878 if (Line.startsWith(Tokens: tok::kw_asm) && Right.is(TT: TT_InlineASMColon) &&
5879 Style.BreakBeforeInlineASMColon == FormatStyle::BBIAS_Always) {
5880 return true;
5881 }
5882
5883 // If the last token before a '}', ']', or ')' is a comma or a trailing
5884 // comment, the intention is to insert a line break after it in order to make
5885 // shuffling around entries easier. Import statements, especially in
5886 // JavaScript, can be an exception to this rule.
5887 if (Style.JavaScriptWrapImports || Line.Type != LT_ImportStatement) {
5888 const FormatToken *BeforeClosingBrace = nullptr;
5889 if ((Left.isOneOf(K1: tok::l_brace, K2: TT_ArrayInitializerLSquare) ||
5890 (Style.isJavaScript() && Left.is(Kind: tok::l_paren))) &&
5891 Left.isNot(Kind: BK_Block) && Left.MatchingParen) {
5892 BeforeClosingBrace = Left.MatchingParen->Previous;
5893 } else if (Right.MatchingParen &&
5894 (Right.MatchingParen->isOneOf(K1: tok::l_brace,
5895 K2: TT_ArrayInitializerLSquare) ||
5896 (Style.isJavaScript() &&
5897 Right.MatchingParen->is(Kind: tok::l_paren)))) {
5898 BeforeClosingBrace = &Left;
5899 }
5900 if (BeforeClosingBrace && (BeforeClosingBrace->is(Kind: tok::comma) ||
5901 BeforeClosingBrace->isTrailingComment())) {
5902 return true;
5903 }
5904 }
5905
5906 if (Right.is(Kind: tok::comment)) {
5907 return Left.isNoneOf(Ks: BK_BracedInit, Ks: TT_CtorInitializerColon) &&
5908 Right.NewlinesBefore > 0 && Right.HasUnescapedNewline;
5909 }
5910 if (Left.isTrailingComment())
5911 return true;
5912 if (Left.IsUnterminatedLiteral)
5913 return true;
5914
5915 if (BeforeLeft && BeforeLeft->is(Kind: tok::lessless) &&
5916 Left.is(Kind: tok::string_literal) && Right.is(Kind: tok::lessless) && AfterRight &&
5917 AfterRight->is(Kind: tok::string_literal)) {
5918 return Right.NewlinesBefore > 0;
5919 }
5920
5921 if (Right.is(TT: TT_RequiresClause)) {
5922 switch (Style.RequiresClausePosition) {
5923 case FormatStyle::RCPS_OwnLine:
5924 case FormatStyle::RCPS_OwnLineWithBrace:
5925 case FormatStyle::RCPS_WithFollowing:
5926 return true;
5927 default:
5928 break;
5929 }
5930 }
5931 // Can break after template<> declaration
5932 if (Left.ClosesTemplateDeclaration && Left.MatchingParen &&
5933 Left.MatchingParen->NestingLevel == 0) {
5934 // Put concepts on the next line e.g.
5935 // template<typename T>
5936 // concept ...
5937 if (Right.is(Kind: tok::kw_concept))
5938 return Style.BreakBeforeConceptDeclarations == FormatStyle::BBCDS_Always;
5939 return Style.BreakTemplateDeclarations == FormatStyle::BTDS_Yes ||
5940 (Style.BreakTemplateDeclarations == FormatStyle::BTDS_Leave &&
5941 Right.NewlinesBefore > 0);
5942 }
5943 if (Left.ClosesRequiresClause) {
5944 switch (Style.RequiresClausePosition) {
5945 case FormatStyle::RCPS_OwnLine:
5946 case FormatStyle::RCPS_WithPreceding:
5947 return Right.isNot(Kind: tok::semi);
5948 case FormatStyle::RCPS_OwnLineWithBrace:
5949 return Right.isNoneOf(Ks: tok::semi, Ks: tok::l_brace);
5950 default:
5951 break;
5952 }
5953 }
5954 if (Style.PackConstructorInitializers == FormatStyle::PCIS_Never) {
5955 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon &&
5956 (Left.is(TT: TT_CtorInitializerComma) ||
5957 Right.is(TT: TT_CtorInitializerColon))) {
5958 return true;
5959 }
5960
5961 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
5962 Left.isOneOf(K1: TT_CtorInitializerColon, K2: TT_CtorInitializerComma)) {
5963 return true;
5964 }
5965 }
5966 if (Style.PackConstructorInitializers < FormatStyle::PCIS_CurrentLine &&
5967 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma &&
5968 Right.isOneOf(K1: TT_CtorInitializerComma, K2: TT_CtorInitializerColon)) {
5969 return true;
5970 }
5971 if (Style.PackConstructorInitializers == FormatStyle::PCIS_NextLineOnly) {
5972 if ((Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon ||
5973 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) &&
5974 Right.is(TT: TT_CtorInitializerColon)) {
5975 return true;
5976 }
5977
5978 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
5979 Left.is(TT: TT_CtorInitializerColon)) {
5980 return true;
5981 }
5982 }
5983 // Break only if we have multiple inheritance.
5984 if (Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma &&
5985 Right.is(TT: TT_InheritanceComma)) {
5986 return true;
5987 }
5988 if (Style.BreakInheritanceList == FormatStyle::BILS_AfterComma &&
5989 Left.is(TT: TT_InheritanceComma)) {
5990 return true;
5991 }
5992 if (Right.is(Kind: tok::string_literal) && Right.TokenText.starts_with(Prefix: "R\"")) {
5993 // Multiline raw string literals are special wrt. line breaks. The author
5994 // has made a deliberate choice and might have aligned the contents of the
5995 // string literal accordingly. Thus, we try keep existing line breaks.
5996 return Right.IsMultiline && Right.NewlinesBefore > 0;
5997 }
5998 if ((Left.is(Kind: tok::l_brace) ||
5999 (Left.is(Kind: tok::less) && BeforeLeft && BeforeLeft->is(Kind: tok::equal))) &&
6000 Right.NestingLevel == 1 && Style.Language == FormatStyle::LK_Proto) {
6001 // Don't put enums or option definitions onto single lines in protocol
6002 // buffers.
6003 return true;
6004 }
6005 if (Right.is(TT: TT_InlineASMBrace))
6006 return Right.HasUnescapedNewline;
6007
6008 if (isAllmanBrace(Tok: Left) || isAllmanBrace(Tok: Right)) {
6009 auto *FirstNonComment = Line.getFirstNonComment();
6010 bool AccessSpecifier =
6011 FirstNonComment && (FirstNonComment->is(II: Keywords.kw_internal) ||
6012 FirstNonComment->isAccessSpecifierKeyword());
6013
6014 if (Style.BraceWrapping.AfterEnum) {
6015 if (Line.startsWith(Tokens: tok::kw_enum) ||
6016 Line.startsWith(Tokens: tok::kw_typedef, Tokens: tok::kw_enum)) {
6017 return true;
6018 }
6019 // Ensure BraceWrapping for `public enum A {`.
6020 if (AccessSpecifier && FirstNonComment->Next &&
6021 FirstNonComment->Next->is(Kind: tok::kw_enum)) {
6022 return true;
6023 }
6024 }
6025
6026 // Ensure BraceWrapping for `public interface A {`.
6027 if (Style.BraceWrapping.AfterClass &&
6028 ((AccessSpecifier && FirstNonComment->Next &&
6029 FirstNonComment->Next->is(II: Keywords.kw_interface)) ||
6030 Line.startsWith(Tokens: Keywords.kw_interface))) {
6031 return true;
6032 }
6033
6034 // Don't attempt to interpret struct return types as structs.
6035 if (Right.isNot(Kind: TT_FunctionLBrace)) {
6036 return (Line.startsWith(Tokens: tok::kw_class) &&
6037 Style.BraceWrapping.AfterClass) ||
6038 (Line.startsWith(Tokens: tok::kw_struct) &&
6039 Style.BraceWrapping.AfterStruct);
6040 }
6041 }
6042
6043 if (Left.is(TT: TT_ObjCBlockLBrace) &&
6044 Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never) {
6045 return true;
6046 }
6047
6048 // Ensure wrapping after __attribute__((XX)) and @interface etc.
6049 if (Left.isOneOf(K1: TT_AttributeRParen, K2: TT_AttributeMacro) &&
6050 Right.is(TT: TT_ObjCDecl)) {
6051 return true;
6052 }
6053
6054 if (Left.is(TT: TT_LambdaLBrace)) {
6055 if (IsFunctionArgument(Tok: Left) &&
6056 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline) {
6057 return false;
6058 }
6059
6060 if (Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_None ||
6061 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline ||
6062 (!Left.Children.empty() &&
6063 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Empty)) {
6064 return true;
6065 }
6066 }
6067
6068 if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT: TT_LambdaLBrace) &&
6069 (Left.isPointerOrReference() || Left.is(TT: TT_TemplateCloser))) {
6070 return true;
6071 }
6072
6073 // Put multiple Java annotation on a new line.
6074 if ((Style.isJava() || Style.isJavaScript()) &&
6075 Left.is(TT: TT_LeadingJavaAnnotation) &&
6076 Right.isNoneOf(Ks: TT_LeadingJavaAnnotation, Ks: tok::l_paren) &&
6077 (Line.Last->is(Kind: tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) {
6078 return true;
6079 }
6080
6081 if (Right.is(TT: TT_ProtoExtensionLSquare))
6082 return true;
6083
6084 // In text proto instances if a submessage contains at least 2 entries and at
6085 // least one of them is a submessage, like A { ... B { ... } ... },
6086 // put all of the entries of A on separate lines by forcing the selector of
6087 // the submessage B to be put on a newline.
6088 //
6089 // Example: these can stay on one line:
6090 // a { scalar_1: 1 scalar_2: 2 }
6091 // a { b { key: value } }
6092 //
6093 // and these entries need to be on a new line even if putting them all in one
6094 // line is under the column limit:
6095 // a {
6096 // scalar: 1
6097 // b { key: value }
6098 // }
6099 //
6100 // We enforce this by breaking before a submessage field that has previous
6101 // siblings, *and* breaking before a field that follows a submessage field.
6102 //
6103 // Be careful to exclude the case [proto.ext] { ... } since the `]` is
6104 // the TT_SelectorName there, but we don't want to break inside the brackets.
6105 //
6106 // Another edge case is @submessage { key: value }, which is a common
6107 // substitution placeholder. In this case we want to keep `@` and `submessage`
6108 // together.
6109 //
6110 // We ensure elsewhere that extensions are always on their own line.
6111 if (Style.isProto() && Right.is(TT: TT_SelectorName) &&
6112 Right.isNot(Kind: tok::r_square) && AfterRight) {
6113 // Keep `@submessage` together in:
6114 // @submessage { key: value }
6115 if (Left.is(Kind: tok::at))
6116 return false;
6117 // Look for the scope opener after selector in cases like:
6118 // selector { ...
6119 // selector: { ...
6120 // selector: @base { ...
6121 const auto *LBrace = AfterRight;
6122 if (LBrace && LBrace->is(Kind: tok::colon)) {
6123 LBrace = LBrace->Next;
6124 if (LBrace && LBrace->is(Kind: tok::at)) {
6125 LBrace = LBrace->Next;
6126 if (LBrace)
6127 LBrace = LBrace->Next;
6128 }
6129 }
6130 if (LBrace &&
6131 // The scope opener is one of {, [, <:
6132 // selector { ... }
6133 // selector [ ... ]
6134 // selector < ... >
6135 //
6136 // In case of selector { ... }, the l_brace is TT_DictLiteral.
6137 // In case of an empty selector {}, the l_brace is not TT_DictLiteral,
6138 // so we check for immediately following r_brace.
6139 ((LBrace->is(Kind: tok::l_brace) &&
6140 (LBrace->is(TT: TT_DictLiteral) ||
6141 (LBrace->Next && LBrace->Next->is(Kind: tok::r_brace)))) ||
6142 LBrace->isOneOf(K1: TT_ArrayInitializerLSquare, K2: tok::less))) {
6143 // If Left.ParameterCount is 0, then this submessage entry is not the
6144 // first in its parent submessage, and we want to break before this entry.
6145 // If Left.ParameterCount is greater than 0, then its parent submessage
6146 // might contain 1 or more entries and we want to break before this entry
6147 // if it contains at least 2 entries. We deal with this case later by
6148 // detecting and breaking before the next entry in the parent submessage.
6149 if (Left.ParameterCount == 0)
6150 return true;
6151 // However, if this submessage is the first entry in its parent
6152 // submessage, Left.ParameterCount might be 1 in some cases.
6153 // We deal with this case later by detecting an entry
6154 // following a closing paren of this submessage.
6155 }
6156
6157 // If this is an entry immediately following a submessage, it will be
6158 // preceded by a closing paren of that submessage, like in:
6159 // left---. .---right
6160 // v v
6161 // sub: { ... } key: value
6162 // If there was a comment between `}` an `key` above, then `key` would be
6163 // put on a new line anyways.
6164 if (Left.isOneOf(K1: tok::r_brace, K2: tok::greater, Ks: tok::r_square))
6165 return true;
6166 }
6167
6168 return false;
6169}
6170
6171bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
6172 const FormatToken &Right) const {
6173 const FormatToken &Left = *Right.Previous;
6174 // Language-specific stuff.
6175 if (Style.isCSharp()) {
6176 if (Left.isOneOf(K1: TT_CSharpNamedArgumentColon, K2: TT_AttributeColon) ||
6177 Right.isOneOf(K1: TT_CSharpNamedArgumentColon, K2: TT_AttributeColon)) {
6178 return false;
6179 }
6180 // Only break after commas for generic type constraints.
6181 if (Line.First->is(TT: TT_CSharpGenericTypeConstraint))
6182 return Left.is(TT: TT_CSharpGenericTypeConstraintComma);
6183 // Keep nullable operators attached to their identifiers.
6184 if (Right.is(TT: TT_CSharpNullable))
6185 return false;
6186 } else if (Style.isJava()) {
6187 if (Left.isOneOf(K1: Keywords.kw_throws, K2: Keywords.kw_extends,
6188 Ks: Keywords.kw_implements)) {
6189 return false;
6190 }
6191 if (Right.isOneOf(K1: Keywords.kw_throws, K2: Keywords.kw_extends,
6192 Ks: Keywords.kw_implements)) {
6193 return true;
6194 }
6195 } else if (Style.isJavaScript()) {
6196 const FormatToken *NonComment = Right.getPreviousNonComment();
6197 if (NonComment &&
6198 (NonComment->isAccessSpecifierKeyword() ||
6199 NonComment->isOneOf(
6200 K1: tok::kw_return, K2: Keywords.kw_yield, Ks: tok::kw_continue, Ks: tok::kw_break,
6201 Ks: tok::kw_throw, Ks: Keywords.kw_interface, Ks: Keywords.kw_type,
6202 Ks: tok::kw_static, Ks: Keywords.kw_readonly, Ks: Keywords.kw_override,
6203 Ks: Keywords.kw_abstract, Ks: Keywords.kw_get, Ks: Keywords.kw_set,
6204 Ks: Keywords.kw_async, Ks: Keywords.kw_await))) {
6205 return false; // Otherwise automatic semicolon insertion would trigger.
6206 }
6207 if (Right.NestingLevel == 0 &&
6208 (Left.Tok.getIdentifierInfo() ||
6209 Left.isOneOf(K1: tok::r_square, K2: tok::r_paren)) &&
6210 Right.isOneOf(K1: tok::l_square, K2: tok::l_paren)) {
6211 return false; // Otherwise automatic semicolon insertion would trigger.
6212 }
6213 if (NonComment && NonComment->is(Kind: tok::identifier) &&
6214 NonComment->TokenText == "asserts") {
6215 return false;
6216 }
6217 if (Left.is(TT: TT_FatArrow) && Right.is(Kind: tok::l_brace))
6218 return false;
6219 if (Left.is(TT: TT_JsTypeColon))
6220 return true;
6221 // Don't wrap between ":" and "!" of a strict prop init ("field!: type;").
6222 if (Left.is(Kind: tok::exclaim) && Right.is(Kind: tok::colon))
6223 return false;
6224 // Look for is type annotations like:
6225 // function f(): a is B { ... }
6226 // Do not break before is in these cases.
6227 if (Right.is(II: Keywords.kw_is)) {
6228 const FormatToken *Next = Right.getNextNonComment();
6229 // If `is` is followed by a colon, it's likely that it's a dict key, so
6230 // ignore it for this check.
6231 // For example this is common in Polymer:
6232 // Polymer({
6233 // is: 'name',
6234 // ...
6235 // });
6236 if (!Next || Next->isNot(Kind: tok::colon))
6237 return false;
6238 }
6239 if (Left.is(II: Keywords.kw_in))
6240 return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None;
6241 if (Right.is(II: Keywords.kw_in))
6242 return Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None;
6243 if (Right.is(II: Keywords.kw_as))
6244 return false; // must not break before as in 'x as type' casts
6245 if (Right.isOneOf(K1: Keywords.kw_extends, K2: Keywords.kw_infer)) {
6246 // extends and infer can appear as keywords in conditional types:
6247 // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#conditional-types
6248 // do not break before them, as the expressions are subject to ASI.
6249 return false;
6250 }
6251 if (Left.is(II: Keywords.kw_as))
6252 return true;
6253 if (Left.is(TT: TT_NonNullAssertion))
6254 return true;
6255 if (Left.is(II: Keywords.kw_declare) &&
6256 Right.isOneOf(K1: Keywords.kw_module, K2: tok::kw_namespace,
6257 Ks: Keywords.kw_function, Ks: tok::kw_class, Ks: tok::kw_enum,
6258 Ks: Keywords.kw_interface, Ks: Keywords.kw_type, Ks: Keywords.kw_var,
6259 Ks: Keywords.kw_let, Ks: tok::kw_const)) {
6260 // See grammar for 'declare' statements at:
6261 // https://github.com/Microsoft/TypeScript/blob/main/doc/spec-ARCHIVED.md#A.10
6262 return false;
6263 }
6264 if (Left.isOneOf(K1: Keywords.kw_module, K2: tok::kw_namespace) &&
6265 Right.isOneOf(K1: tok::identifier, K2: tok::string_literal)) {
6266 return false; // must not break in "module foo { ...}"
6267 }
6268 if (Right.is(TT: TT_TemplateString) && Right.closesScope())
6269 return false;
6270 // Don't split tagged template literal so there is a break between the tag
6271 // identifier and template string.
6272 if (Left.is(Kind: tok::identifier) && Right.is(TT: TT_TemplateString))
6273 return false;
6274 if (Left.is(TT: TT_TemplateString) && Left.opensScope())
6275 return true;
6276 } else if (Style.isTableGen()) {
6277 // Avoid to break after "def", "class", "let" and so on.
6278 if (Keywords.isTableGenDefinition(Tok: Left))
6279 return false;
6280 // Avoid to break after '(' in the cases that is in bang operators.
6281 if (Right.is(Kind: tok::l_paren)) {
6282 return Left.isNoneOf(Ks: TT_TableGenBangOperator, Ks: TT_TableGenCondOperator,
6283 Ks: TT_TemplateCloser);
6284 }
6285 // Avoid to break between the value and its suffix part.
6286 if (Left.is(TT: TT_TableGenValueSuffix))
6287 return false;
6288 // Avoid to break around paste operator.
6289 if (Left.is(Kind: tok::hash) || Right.is(Kind: tok::hash))
6290 return false;
6291 if (Left.isOneOf(K1: TT_TableGenBangOperator, K2: TT_TableGenCondOperator))
6292 return false;
6293 }
6294
6295 // We can break before an r_brace if there was a break after the matching
6296 // l_brace, which is tracked by BreakBeforeClosingBrace, or if we are in a
6297 // block-indented initialization list.
6298 if (Right.is(Kind: tok::r_brace)) {
6299 return Right.MatchingParen && (Right.MatchingParen->is(BBK: BK_Block) ||
6300 (Right.isBlockIndentedInitRBrace(Style)));
6301 }
6302
6303 // We can break before r_paren if we're in a block indented context or
6304 // a control statement with an explicit style option.
6305 if (Right.is(Kind: tok::r_paren)) {
6306 if (!Right.MatchingParen)
6307 return false;
6308 auto Next = Right.Next;
6309 if (Next && Next->is(Kind: tok::r_paren))
6310 Next = Next->Next;
6311 if (Next && Next->is(Kind: tok::l_paren))
6312 return false;
6313 const FormatToken *Previous = Right.MatchingParen->Previous;
6314 if (!Previous)
6315 return false;
6316 if (Previous->isIf())
6317 return Style.BreakBeforeCloseBracketIf;
6318 if (Previous->isLoop(Style))
6319 return Style.BreakBeforeCloseBracketLoop;
6320 if (Previous->is(Kind: tok::kw_switch))
6321 return Style.BreakBeforeCloseBracketSwitch;
6322 return Style.BreakBeforeCloseBracketFunction;
6323 }
6324
6325 if (Left.isOneOf(K1: tok::r_paren, K2: TT_TrailingAnnotation) &&
6326 Right.is(TT: TT_TrailingAnnotation) &&
6327 Style.BreakBeforeCloseBracketFunction) {
6328 return false;
6329 }
6330
6331 if (Right.is(TT: TT_TemplateCloser))
6332 return Style.BreakBeforeTemplateCloser;
6333
6334 if (Left.isOneOf(K1: tok::at, K2: tok::objc_interface))
6335 return false;
6336 if (Left.isOneOf(K1: TT_JavaAnnotation, K2: TT_LeadingJavaAnnotation))
6337 return Right.isNot(Kind: tok::l_paren);
6338 if (Right.is(TT: TT_PointerOrReference)) {
6339 return Line.IsMultiVariableDeclStmt ||
6340 (getTokenPointerOrReferenceAlignment(PointerOrReference: Right) ==
6341 FormatStyle::PAS_Right &&
6342 !(Right.Next &&
6343 Right.Next->isOneOf(K1: TT_FunctionDeclarationName, K2: tok::kw_const)));
6344 }
6345 if (Right.isOneOf(K1: TT_StartOfName, K2: TT_FunctionDeclarationName,
6346 Ks: TT_ClassHeadName, Ks: TT_QtProperty, Ks: tok::kw_operator)) {
6347 return true;
6348 }
6349 if (Left.is(TT: TT_PointerOrReference))
6350 return false;
6351 if (Right.isTrailingComment()) {
6352 // We rely on MustBreakBefore being set correctly here as we should not
6353 // change the "binding" behavior of a comment.
6354 // The first comment in a braced lists is always interpreted as belonging to
6355 // the first list element. Otherwise, it should be placed outside of the
6356 // list.
6357 return Left.is(BBK: BK_BracedInit) ||
6358 (Left.is(TT: TT_CtorInitializerColon) && Right.NewlinesBefore > 0 &&
6359 Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon);
6360 }
6361 if (Left.is(Kind: tok::question) && Right.is(Kind: tok::colon))
6362 return false;
6363 if (Right.isOneOf(K1: TT_ConditionalExpr, K2: tok::question))
6364 return Style.BreakBeforeTernaryOperators;
6365 if (Left.isOneOf(K1: TT_ConditionalExpr, K2: tok::question))
6366 return !Style.BreakBeforeTernaryOperators;
6367 if (Left.is(TT: TT_InheritanceColon))
6368 return Style.BreakInheritanceList == FormatStyle::BILS_AfterColon;
6369 if (Right.is(TT: TT_InheritanceColon))
6370 return Style.BreakInheritanceList != FormatStyle::BILS_AfterColon;
6371 // When the method parameter has no name, allow breaking before the colon.
6372 if (Right.is(TT: TT_ObjCMethodExpr) && Right.isNot(Kind: tok::r_square) &&
6373 Left.isNot(Kind: TT_SelectorName)) {
6374 return true;
6375 }
6376
6377 if (Right.is(Kind: tok::colon) &&
6378 Right.isNoneOf(Ks: TT_CtorInitializerColon, Ks: TT_InlineASMColon,
6379 Ks: TT_BitFieldColon)) {
6380 return false;
6381 }
6382 if (Left.is(Kind: tok::colon) && Left.isOneOf(K1: TT_ObjCSelector, K2: TT_ObjCMethodExpr))
6383 return true;
6384 if (Left.is(Kind: tok::colon) && Left.is(TT: TT_DictLiteral)) {
6385 if (Style.isProto()) {
6386 if (!Style.AlwaysBreakBeforeMultilineStrings && Right.isStringLiteral())
6387 return false;
6388 // Prevent cases like:
6389 //
6390 // submessage:
6391 // { key: valueeeeeeeeeeee }
6392 //
6393 // when the snippet does not fit into one line.
6394 // Prefer:
6395 //
6396 // submessage: {
6397 // key: valueeeeeeeeeeee
6398 // }
6399 //
6400 // instead, even if it is longer by one line.
6401 //
6402 // Note that this allows the "{" to go over the column limit
6403 // when the column limit is just between ":" and "{", but that does
6404 // not happen too often and alternative formattings in this case are
6405 // not much better.
6406 //
6407 // The code covers the cases:
6408 //
6409 // submessage: { ... }
6410 // submessage: < ... >
6411 // repeated: [ ... ]
6412 if ((Right.isOneOf(K1: tok::l_brace, K2: tok::less) &&
6413 Right.is(TT: TT_DictLiteral)) ||
6414 Right.is(TT: TT_ArrayInitializerLSquare)) {
6415 return false;
6416 }
6417 }
6418 return true;
6419 }
6420 if (Right.is(Kind: tok::r_square) && Right.MatchingParen &&
6421 Right.MatchingParen->is(TT: TT_ProtoExtensionLSquare)) {
6422 return false;
6423 }
6424 if (Right.is(TT: TT_SelectorName) || (Right.is(Kind: tok::identifier) && Right.Next &&
6425 Right.Next->is(TT: TT_ObjCMethodExpr))) {
6426 return Left.isNot(Kind: tok::period); // FIXME: Properly parse ObjC calls.
6427 }
6428 if (Left.is(Kind: tok::r_paren) && Line.Type == LT_ObjCProperty)
6429 return true;
6430 if (Right.is(Kind: tok::kw_concept))
6431 return Style.BreakBeforeConceptDeclarations != FormatStyle::BBCDS_Never;
6432 if (Right.is(TT: TT_RequiresClause))
6433 return true;
6434 if (Left.ClosesTemplateDeclaration) {
6435 return Style.BreakTemplateDeclarations != FormatStyle::BTDS_Leave ||
6436 Right.NewlinesBefore > 0;
6437 }
6438 if (Left.is(TT: TT_FunctionAnnotationRParen))
6439 return true;
6440 if (Left.ClosesRequiresClause)
6441 return true;
6442 if (Right.isOneOf(K1: TT_RangeBasedForLoopColon, K2: TT_OverloadedOperatorLParen,
6443 Ks: TT_OverloadedOperator)) {
6444 return false;
6445 }
6446 if (Left.is(TT: TT_RangeBasedForLoopColon))
6447 return true;
6448 if (Right.is(TT: TT_RangeBasedForLoopColon))
6449 return false;
6450 if (Left.is(TT: TT_TemplateCloser) && Right.is(TT: TT_TemplateOpener))
6451 return true;
6452 if ((Left.is(Kind: tok::greater) && Right.is(Kind: tok::greater)) ||
6453 (Left.is(Kind: tok::less) && Right.is(Kind: tok::less))) {
6454 return false;
6455 }
6456 if (Right.is(TT: TT_BinaryOperator) &&
6457 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None &&
6458 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All ||
6459 Right.getPrecedence() != prec::Assignment)) {
6460 return true;
6461 }
6462 if (Left.isOneOf(K1: TT_TemplateCloser, K2: TT_UnaryOperator, Ks: tok::kw_operator))
6463 return false;
6464 if (Left.is(Kind: tok::equal) && Right.isNoneOf(Ks: tok::kw_default, Ks: tok::kw_delete) &&
6465 Line.Type == LT_VirtualFunctionDecl && Left.NestingLevel == 0) {
6466 return false;
6467 }
6468 if (Left.is(Kind: tok::equal) && Right.is(Kind: tok::l_brace) &&
6469 Style.Cpp11BracedListStyle == FormatStyle::BLS_Block) {
6470 return false;
6471 }
6472 if (Left.is(TT: TT_AttributeLParen) ||
6473 (Left.is(Kind: tok::l_paren) && Left.is(TT: TT_TypeDeclarationParen))) {
6474 return false;
6475 }
6476 if (Left.is(Kind: tok::l_paren) && Left.Previous &&
6477 (Left.Previous->isOneOf(K1: TT_BinaryOperator, K2: TT_CastRParen))) {
6478 return false;
6479 }
6480 if (Right.is(TT: TT_ImplicitStringLiteral))
6481 return false;
6482
6483 if (Right.is(Kind: tok::r_square) && Right.MatchingParen &&
6484 Right.MatchingParen->is(TT: TT_LambdaLSquare)) {
6485 return false;
6486 }
6487
6488 // Allow breaking after a trailing annotation, e.g. after a method
6489 // declaration.
6490 if (Left.is(TT: TT_TrailingAnnotation)) {
6491 return Right.isNoneOf(Ks: tok::l_brace, Ks: tok::semi, Ks: tok::equal, Ks: tok::l_paren,
6492 Ks: tok::less, Ks: tok::coloncolon);
6493 }
6494
6495 if (Right.isAttribute())
6496 return true;
6497
6498 if (Right.is(TT: TT_AttributeLSquare)) {
6499 assert(Left.isNot(tok::l_square));
6500 return true;
6501 }
6502
6503 if (Left.is(Kind: tok::identifier) && Right.is(Kind: tok::string_literal))
6504 return true;
6505
6506 if (Right.is(Kind: tok::identifier) && Right.Next && Right.Next->is(TT: TT_DictLiteral))
6507 return true;
6508
6509 if (Left.is(TT: TT_CtorInitializerColon)) {
6510 return Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
6511 (!Right.isTrailingComment() || Right.NewlinesBefore > 0);
6512 }
6513 if (Right.is(TT: TT_CtorInitializerColon))
6514 return Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon;
6515 if (Left.is(TT: TT_CtorInitializerComma) &&
6516 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
6517 return false;
6518 }
6519 if (Right.is(TT: TT_CtorInitializerComma) &&
6520 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
6521 return true;
6522 }
6523 if (Left.is(TT: TT_InheritanceComma) &&
6524 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) {
6525 return false;
6526 }
6527 if (Right.is(TT: TT_InheritanceComma) &&
6528 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) {
6529 return true;
6530 }
6531 if (Left.is(TT: TT_ArrayInitializerLSquare))
6532 return true;
6533 if (Right.is(Kind: tok::kw_typename) && Left.isNot(Kind: tok::kw_const))
6534 return true;
6535 if ((Left.isBinaryOperator() || Left.is(TT: TT_BinaryOperator)) &&
6536 Left.isNoneOf(Ks: tok::arrowstar, Ks: tok::lessless) &&
6537 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All &&
6538 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None ||
6539 Left.getPrecedence() == prec::Assignment)) {
6540 return true;
6541 }
6542 if (Left.is(TT: TT_AttributeLSquare) && Right.is(Kind: tok::l_square)) {
6543 assert(Right.isNot(TT_AttributeLSquare));
6544 return false;
6545 }
6546 if (Left.is(Kind: tok::r_square) && Right.is(TT: TT_AttributeRSquare)) {
6547 assert(Left.isNot(TT_AttributeRSquare));
6548 return false;
6549 }
6550
6551 auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
6552 if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT: TT_LambdaLBrace)) {
6553 if (isAllmanLambdaBrace(Tok: Left))
6554 return !isEmptyLambdaAllowed(Tok: Left, ShortLambdaOption);
6555 if (isAllmanLambdaBrace(Tok: Right))
6556 return !isEmptyLambdaAllowed(Tok: Right, ShortLambdaOption);
6557 }
6558
6559 if (Right.is(Kind: tok::kw_noexcept) && Right.is(TT: TT_TrailingAnnotation)) {
6560 switch (Style.AllowBreakBeforeNoexceptSpecifier) {
6561 case FormatStyle::BBNSS_Never:
6562 return false;
6563 case FormatStyle::BBNSS_Always:
6564 return true;
6565 case FormatStyle::BBNSS_OnlyWithParen:
6566 return Right.Next && Right.Next->is(Kind: tok::l_paren);
6567 }
6568 }
6569
6570 return Left.isOneOf(K1: tok::comma, K2: tok::coloncolon, Ks: tok::semi, Ks: tok::l_brace,
6571 Ks: tok::kw_class, Ks: tok::kw_struct, Ks: tok::comment) ||
6572 Right.isMemberAccess() ||
6573 Right.isOneOf(K1: TT_TrailingReturnArrow, K2: TT_LambdaArrow, Ks: tok::lessless,
6574 Ks: tok::colon, Ks: tok::l_square, Ks: tok::at) ||
6575 (Left.is(Kind: tok::r_paren) &&
6576 Right.isOneOf(K1: tok::identifier, K2: tok::kw_const)) ||
6577 (Left.is(Kind: tok::l_paren) && Right.isNot(Kind: tok::r_paren)) ||
6578 (Left.is(TT: TT_TemplateOpener) && Right.isNot(Kind: TT_TemplateCloser));
6579}
6580
6581void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) const {
6582 llvm::errs() << "AnnotatedTokens(L=" << Line.Level << ", P=" << Line.PPLevel
6583 << ", T=" << Line.Type << ", C=" << Line.IsContinuation
6584 << "):\n";
6585 const FormatToken *Tok = Line.First;
6586 while (Tok) {
6587 llvm::errs() << " I=" << Tok->IndentLevel << " M=" << Tok->MustBreakBefore
6588 << " C=" << Tok->CanBreakBefore
6589 << " T=" << getTokenTypeName(Type: Tok->getType())
6590 << " S=" << Tok->SpacesRequiredBefore
6591 << " F=" << Tok->Finalized << " B=" << Tok->BlockParameterCount
6592 << " BK=" << Tok->getBlockKind() << " P=" << Tok->SplitPenalty
6593 << " Name=" << Tok->Tok.getName() << " N=" << Tok->NestingLevel
6594 << " L=" << Tok->TotalLength
6595 << " PPK=" << Tok->getPackingKind() << " FakeLParens=";
6596 for (prec::Level LParen : Tok->FakeLParens)
6597 llvm::errs() << LParen << "/";
6598 llvm::errs() << " FakeRParens=" << Tok->FakeRParens;
6599 llvm::errs() << " II=" << Tok->Tok.getIdentifierInfo();
6600 llvm::errs() << " Text='" << Tok->TokenText << "'\n";
6601 if (!Tok->Next)
6602 assert(Tok == Line.Last);
6603 Tok = Tok->Next;
6604 }
6605 llvm::errs() << "----\n";
6606}
6607
6608FormatStyle::PointerAlignmentStyle
6609TokenAnnotator::getTokenReferenceAlignment(const FormatToken &Reference) const {
6610 assert(Reference.isOneOf(tok::amp, tok::ampamp));
6611 switch (Style.ReferenceAlignment) {
6612 case FormatStyle::RAS_Pointer:
6613 return Style.PointerAlignment;
6614 case FormatStyle::RAS_Left:
6615 return FormatStyle::PAS_Left;
6616 case FormatStyle::RAS_Right:
6617 return FormatStyle::PAS_Right;
6618 case FormatStyle::RAS_Middle:
6619 return FormatStyle::PAS_Middle;
6620 }
6621 assert(0); //"Unhandled value of ReferenceAlignment"
6622 return Style.PointerAlignment;
6623}
6624
6625FormatStyle::PointerAlignmentStyle
6626TokenAnnotator::getTokenPointerOrReferenceAlignment(
6627 const FormatToken &PointerOrReference) const {
6628 if (PointerOrReference.isOneOf(K1: tok::amp, K2: tok::ampamp))
6629 return getTokenReferenceAlignment(Reference: PointerOrReference);
6630 assert(PointerOrReference.is(tok::star));
6631 return Style.PointerAlignment;
6632}
6633
6634} // namespace format
6635} // namespace clang
6636