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