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