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

source code of clang/lib/Format/TokenAnnotator.cpp