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