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