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