Thanks to visit codestin.com
Credit goes to clang.llvm.org

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