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