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