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