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->NewlinesBefore < 2 && 3069 Line->isComment() && !isClangFormatOff(Line->First->TokenText) && 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->is(tok::r_paren) && Previous->is(TT_TypeDeclarationParen)) { 3182 assert(Previous->MatchingParen); 3183 assert(Previous->MatchingParen->is(tok::l_paren)); 3184 assert(Previous->MatchingParen->is(TT_TypeDeclarationParen)); 3185 return true; 3186 } 3187 if (!Previous->isOneOf(tok::star, tok::amp, tok::ampamp, TT_TemplateCloser)) 3188 return false; 3189 Next = skipOperatorName(Next); 3190 } else { 3191 if (!Current.is(TT_StartOfName) || Current.NestingLevel != 0) 3192 return false; 3193 for (; Next; Next = Next->Next) { 3194 if (Next->is(TT_TemplateOpener) && Next->MatchingParen) { 3195 Next = Next->MatchingParen; 3196 } else if (Next->is(tok::coloncolon)) { 3197 Next = Next->Next; 3198 if (!Next) 3199 return false; 3200 if (Next->is(tok::kw_operator)) { 3201 Next = skipOperatorName(Next->Next); 3202 break; 3203 } 3204 if (!Next->is(tok::identifier)) 3205 return false; 3206 } else if (isCppAttribute(IsCpp, *Next)) { 3207 Next = Next->MatchingParen; 3208 if (!Next) 3209 return false; 3210 } else if (Next->is(tok::l_paren)) { 3211 break; 3212 } else { 3213 return false; 3214 } 3215 } 3216 } 3217 3218 // Check whether parameter list can belong to a function declaration. 3219 if (!Next || !Next->is(tok::l_paren) || !Next->MatchingParen) 3220 return false; 3221 // If the lines ends with "{", this is likely a function definition. 3222 if (Line.Last->is(tok::l_brace)) 3223 return true; 3224 if (Next->Next == Next->MatchingParen) 3225 return true; // Empty parentheses. 3226 // If there is an &/&& after the r_paren, this is likely a function. 3227 if (Next->MatchingParen->Next && 3228 Next->MatchingParen->Next->is(TT_PointerOrReference)) { 3229 return true; 3230 } 3231 3232 // Check for K&R C function definitions (and C++ function definitions with 3233 // unnamed parameters), e.g.: 3234 // int f(i) 3235 // { 3236 // return i + 1; 3237 // } 3238 // bool g(size_t = 0, bool b = false) 3239 // { 3240 // return !b; 3241 // } 3242 if (IsCpp && Next->Next && Next->Next->is(tok::identifier) && 3243 !Line.endsWith(tok::semi)) { 3244 return true; 3245 } 3246 3247 for (const FormatToken *Tok = Next->Next; Tok && Tok != Next->MatchingParen; 3248 Tok = Tok->Next) { 3249 if (Tok->is(TT_TypeDeclarationParen)) 3250 return true; 3251 if (Tok->isOneOf(tok::l_paren, TT_TemplateOpener) && Tok->MatchingParen) { 3252 Tok = Tok->MatchingParen; 3253 continue; 3254 } 3255 if (Tok->is(tok::kw_const) || Tok->isSimpleTypeSpecifier() || 3256 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) { 3257 return true; 3258 } 3259 if (Tok->isOneOf(tok::l_brace, tok::string_literal, TT_ObjCMethodExpr) || 3260 Tok->Tok.isLiteral()) { 3261 return false; 3262 } 3263 } 3264 return false; 3265 } 3266 3267 bool TokenAnnotator::mustBreakForReturnType(const AnnotatedLine &Line) const { 3268 assert(Line.MightBeFunctionDecl); 3269 3270 if ((Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_TopLevel || 3271 Style.AlwaysBreakAfterReturnType == 3272 FormatStyle::RTBS_TopLevelDefinitions) && 3273 Line.Level > 0) { 3274 return false; 3275 } 3276 3277 switch (Style.AlwaysBreakAfterReturnType) { 3278 case FormatStyle::RTBS_None: 3279 return false; 3280 case FormatStyle::RTBS_All: 3281 case FormatStyle::RTBS_TopLevel: 3282 return true; 3283 case FormatStyle::RTBS_AllDefinitions: 3284 case FormatStyle::RTBS_TopLevelDefinitions: 3285 return Line.mightBeFunctionDefinition(); 3286 } 3287 3288 return false; 3289 } 3290 3291 static bool mustBreakAfterAttributes(const FormatToken &Tok, 3292 const FormatStyle &Style) { 3293 switch (Style.BreakAfterAttributes) { 3294 case FormatStyle::ABS_Always: 3295 return true; 3296 case FormatStyle::ABS_Leave: 3297 return Tok.NewlinesBefore > 0; 3298 default: 3299 return false; 3300 } 3301 } 3302 3303 void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const { 3304 for (AnnotatedLine *ChildLine : Line.Children) 3305 calculateFormattingInformation(*ChildLine); 3306 3307 Line.First->TotalLength = 3308 Line.First->IsMultiline ? Style.ColumnLimit 3309 : Line.FirstStartColumn + Line.First->ColumnWidth; 3310 FormatToken *Current = Line.First->Next; 3311 bool InFunctionDecl = Line.MightBeFunctionDecl; 3312 bool AlignArrayOfStructures = 3313 (Style.AlignArrayOfStructures != FormatStyle::AIAS_None && 3314 Line.Type == LT_ArrayOfStructInitializer); 3315 if (AlignArrayOfStructures) 3316 calculateArrayInitializerColumnList(Line); 3317 3318 bool LineIsFunctionDeclaration = false; 3319 for (FormatToken *Tok = Current, *AfterLastAttribute = nullptr; Tok; 3320 Tok = Tok->Next) { 3321 if (isFunctionDeclarationName(Style.isCpp(), *Tok, Line)) { 3322 LineIsFunctionDeclaration = true; 3323 Tok->setType(TT_FunctionDeclarationName); 3324 if (AfterLastAttribute && 3325 mustBreakAfterAttributes(*AfterLastAttribute, Style)) { 3326 AfterLastAttribute->MustBreakBefore = true; 3327 Line.ReturnTypeWrapped = true; 3328 } 3329 break; 3330 } 3331 if (Tok->Previous->EndsCppAttributeGroup) 3332 AfterLastAttribute = Tok; 3333 } 3334 3335 if (Style.isCpp() && !LineIsFunctionDeclaration) { 3336 // Annotate */&/&& in `operator` function calls as binary operators. 3337 for (const auto *Tok = Line.First; Tok; Tok = Tok->Next) { 3338 if (Tok->isNot(tok::kw_operator)) 3339 continue; 3340 do { 3341 Tok = Tok->Next; 3342 } while (Tok && Tok->isNot(TT_OverloadedOperatorLParen)); 3343 if (!Tok) 3344 break; 3345 const auto *LeftParen = Tok; 3346 for (Tok = Tok->Next; Tok && Tok != LeftParen->MatchingParen; 3347 Tok = Tok->Next) { 3348 if (Tok->isNot(tok::identifier)) 3349 continue; 3350 auto *Next = Tok->Next; 3351 const bool NextIsBinaryOperator = 3352 Next && Next->isOneOf(tok::star, tok::amp, tok::ampamp) && 3353 Next->Next && Next->Next->is(tok::identifier); 3354 if (!NextIsBinaryOperator) 3355 continue; 3356 Next->setType(TT_BinaryOperator); 3357 Tok = Next; 3358 } 3359 } 3360 } 3361 3362 while (Current) { 3363 const FormatToken *Prev = Current->Previous; 3364 if (Current->is(TT_LineComment)) { 3365 if (Prev->is(BK_BracedInit) && Prev->opensScope()) { 3366 Current->SpacesRequiredBefore = 3367 (Style.Cpp11BracedListStyle && !Style.SpacesInParensOptions.Other) 3368 ? 0 3369 : 1; 3370 } else if (Prev->is(TT_VerilogMultiLineListLParen)) { 3371 Current->SpacesRequiredBefore = 0; 3372 } else { 3373 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments; 3374 } 3375 3376 // If we find a trailing comment, iterate backwards to determine whether 3377 // it seems to relate to a specific parameter. If so, break before that 3378 // parameter to avoid changing the comment's meaning. E.g. don't move 'b' 3379 // to the previous line in: 3380 // SomeFunction(a, 3381 // b, // comment 3382 // c); 3383 if (!Current->HasUnescapedNewline) { 3384 for (FormatToken *Parameter = Current->Previous; Parameter; 3385 Parameter = Parameter->Previous) { 3386 if (Parameter->isOneOf(tok::comment, tok::r_brace)) 3387 break; 3388 if (Parameter->Previous && Parameter->Previous->is(tok::comma)) { 3389 if (!Parameter->Previous->is(TT_CtorInitializerComma) && 3390 Parameter->HasUnescapedNewline) { 3391 Parameter->MustBreakBefore = true; 3392 } 3393 break; 3394 } 3395 } 3396 } 3397 } else if (Current->SpacesRequiredBefore == 0 && 3398 spaceRequiredBefore(Line, *Current)) { 3399 Current->SpacesRequiredBefore = 1; 3400 } 3401 3402 const auto &Children = Prev->Children; 3403 if (!Children.empty() && Children.back()->Last->is(TT_LineComment)) { 3404 Current->MustBreakBefore = true; 3405 } else { 3406 Current->MustBreakBefore = 3407 Current->MustBreakBefore || mustBreakBefore(Line, *Current); 3408 if (!Current->MustBreakBefore && InFunctionDecl && 3409 Current->is(TT_FunctionDeclarationName)) { 3410 Current->MustBreakBefore = mustBreakForReturnType(Line); 3411 } 3412 } 3413 3414 Current->CanBreakBefore = 3415 Current->MustBreakBefore || canBreakBefore(Line, *Current); 3416 unsigned ChildSize = 0; 3417 if (Prev->Children.size() == 1) { 3418 FormatToken &LastOfChild = *Prev->Children[0]->Last; 3419 ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit 3420 : LastOfChild.TotalLength + 1; 3421 } 3422 if (Current->MustBreakBefore || Prev->Children.size() > 1 || 3423 (Prev->Children.size() == 1 && 3424 Prev->Children[0]->First->MustBreakBefore) || 3425 Current->IsMultiline) { 3426 Current->TotalLength = Prev->TotalLength + Style.ColumnLimit; 3427 } else { 3428 Current->TotalLength = Prev->TotalLength + Current->ColumnWidth + 3429 ChildSize + Current->SpacesRequiredBefore; 3430 } 3431 3432 if (Current->is(TT_CtorInitializerColon)) 3433 InFunctionDecl = false; 3434 3435 // FIXME: Only calculate this if CanBreakBefore is true once static 3436 // initializers etc. are sorted out. 3437 // FIXME: Move magic numbers to a better place. 3438 3439 // Reduce penalty for aligning ObjC method arguments using the colon 3440 // alignment as this is the canonical way (still prefer fitting everything 3441 // into one line if possible). Trying to fit a whole expression into one 3442 // line should not force other line breaks (e.g. when ObjC method 3443 // expression is a part of other expression). 3444 Current->SplitPenalty = splitPenalty(Line, *Current, InFunctionDecl); 3445 if (Style.Language == FormatStyle::LK_ObjC && 3446 Current->is(TT_SelectorName) && Current->ParameterIndex > 0) { 3447 if (Current->ParameterIndex == 1) 3448 Current->SplitPenalty += 5 * Current->BindingStrength; 3449 } else { 3450 Current->SplitPenalty += 20 * Current->BindingStrength; 3451 } 3452 3453 Current = Current->Next; 3454 } 3455 3456 calculateUnbreakableTailLengths(Line); 3457 unsigned IndentLevel = Line.Level; 3458 for (Current = Line.First; Current; Current = Current->Next) { 3459 if (Current->Role) 3460 Current->Role->precomputeFormattingInfos(Current); 3461 if (Current->MatchingParen && 3462 Current->MatchingParen->opensBlockOrBlockTypeList(Style) && 3463 IndentLevel > 0) { 3464 --IndentLevel; 3465 } 3466 Current->IndentLevel = IndentLevel; 3467 if (Current->opensBlockOrBlockTypeList(Style)) 3468 ++IndentLevel; 3469 } 3470 3471 LLVM_DEBUG({ printDebugInfo(Line); }); 3472 } 3473 3474 void TokenAnnotator::calculateUnbreakableTailLengths( 3475 AnnotatedLine &Line) const { 3476 unsigned UnbreakableTailLength = 0; 3477 FormatToken *Current = Line.Last; 3478 while (Current) { 3479 Current->UnbreakableTailLength = UnbreakableTailLength; 3480 if (Current->CanBreakBefore || 3481 Current->isOneOf(tok::comment, tok::string_literal)) { 3482 UnbreakableTailLength = 0; 3483 } else { 3484 UnbreakableTailLength += 3485 Current->ColumnWidth + Current->SpacesRequiredBefore; 3486 } 3487 Current = Current->Previous; 3488 } 3489 } 3490 3491 void TokenAnnotator::calculateArrayInitializerColumnList( 3492 AnnotatedLine &Line) const { 3493 if (Line.First == Line.Last) 3494 return; 3495 auto *CurrentToken = Line.First; 3496 CurrentToken->ArrayInitializerLineStart = true; 3497 unsigned Depth = 0; 3498 while (CurrentToken && CurrentToken != Line.Last) { 3499 if (CurrentToken->is(tok::l_brace)) { 3500 CurrentToken->IsArrayInitializer = true; 3501 if (CurrentToken->Next) 3502 CurrentToken->Next->MustBreakBefore = true; 3503 CurrentToken = 3504 calculateInitializerColumnList(Line, CurrentToken->Next, Depth + 1); 3505 } else { 3506 CurrentToken = CurrentToken->Next; 3507 } 3508 } 3509 } 3510 3511 FormatToken *TokenAnnotator::calculateInitializerColumnList( 3512 AnnotatedLine &Line, FormatToken *CurrentToken, unsigned Depth) const { 3513 while (CurrentToken && CurrentToken != Line.Last) { 3514 if (CurrentToken->is(tok::l_brace)) 3515 ++Depth; 3516 else if (CurrentToken->is(tok::r_brace)) 3517 --Depth; 3518 if (Depth == 2 && CurrentToken->isOneOf(tok::l_brace, tok::comma)) { 3519 CurrentToken = CurrentToken->Next; 3520 if (!CurrentToken) 3521 break; 3522 CurrentToken->StartsColumn = true; 3523 CurrentToken = CurrentToken->Previous; 3524 } 3525 CurrentToken = CurrentToken->Next; 3526 } 3527 return CurrentToken; 3528 } 3529 3530 unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line, 3531 const FormatToken &Tok, 3532 bool InFunctionDecl) const { 3533 const FormatToken &Left = *Tok.Previous; 3534 const FormatToken &Right = Tok; 3535 3536 if (Left.is(tok::semi)) 3537 return 0; 3538 3539 // Language specific handling. 3540 if (Style.Language == FormatStyle::LK_Java) { 3541 if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws)) 3542 return 1; 3543 if (Right.is(Keywords.kw_implements)) 3544 return 2; 3545 if (Left.is(tok::comma) && Left.NestingLevel == 0) 3546 return 3; 3547 } else if (Style.isJavaScript()) { 3548 if (Right.is(Keywords.kw_function) && Left.isNot(tok::comma)) 3549 return 100; 3550 if (Left.is(TT_JsTypeColon)) 3551 return 35; 3552 if ((Left.is(TT_TemplateString) && Left.TokenText.endswith("${")) || 3553 (Right.is(TT_TemplateString) && Right.TokenText.startswith("}"))) { 3554 return 100; 3555 } 3556 // Prefer breaking call chains (".foo") over empty "{}", "[]" or "()". 3557 if (Left.opensScope() && Right.closesScope()) 3558 return 200; 3559 } else if (Style.isProto()) { 3560 if (Right.is(tok::l_square)) 3561 return 1; 3562 if (Right.is(tok::period)) 3563 return 500; 3564 } 3565 3566 if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral)) 3567 return 1; 3568 if (Right.is(tok::l_square)) { 3569 if (Left.is(tok::r_square)) 3570 return 200; 3571 // Slightly prefer formatting local lambda definitions like functions. 3572 if (Right.is(TT_LambdaLSquare) && Left.is(tok::equal)) 3573 return 35; 3574 if (!Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare, 3575 TT_ArrayInitializerLSquare, 3576 TT_DesignatedInitializerLSquare, TT_AttributeSquare)) { 3577 return 500; 3578 } 3579 } 3580 3581 if (Left.is(tok::coloncolon)) 3582 return 500; 3583 if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) || 3584 Right.is(tok::kw_operator)) { 3585 if (Line.startsWith(tok::kw_for) && Right.PartOfMultiVariableDeclStmt) 3586 return 3; 3587 if (Left.is(TT_StartOfName)) 3588 return 110; 3589 if (InFunctionDecl && Right.NestingLevel == 0) 3590 return Style.PenaltyReturnTypeOnItsOwnLine; 3591 return 200; 3592 } 3593 if (Right.is(TT_PointerOrReference)) 3594 return 190; 3595 if (Right.is(TT_LambdaArrow)) 3596 return 110; 3597 if (Left.is(tok::equal) && Right.is(tok::l_brace)) 3598 return 160; 3599 if (Left.is(TT_CastRParen)) 3600 return 100; 3601 if (Left.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union)) 3602 return 5000; 3603 if (Left.is(tok::comment)) 3604 return 1000; 3605 3606 if (Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon, 3607 TT_CtorInitializerColon)) { 3608 return 2; 3609 } 3610 3611 if (Right.isMemberAccess()) { 3612 // Breaking before the "./->" of a chained call/member access is reasonably 3613 // cheap, as formatting those with one call per line is generally 3614 // desirable. In particular, it should be cheaper to break before the call 3615 // than it is to break inside a call's parameters, which could lead to weird 3616 // "hanging" indents. The exception is the very last "./->" to support this 3617 // frequent pattern: 3618 // 3619 // aaaaaaaa.aaaaaaaa.bbbbbbb().ccccccccccccccccccccc( 3620 // dddddddd); 3621 // 3622 // which might otherwise be blown up onto many lines. Here, clang-format 3623 // won't produce "hanging" indents anyway as there is no other trailing 3624 // call. 3625 // 3626 // Also apply higher penalty is not a call as that might lead to a wrapping 3627 // like: 3628 // 3629 // aaaaaaa 3630 // .aaaaaaaaa.bbbbbbbb(cccccccc); 3631 return !Right.NextOperator || !Right.NextOperator->Previous->closesScope() 3632 ? 150 3633 : 35; 3634 } 3635 3636 if (Right.is(TT_TrailingAnnotation) && 3637 (!Right.Next || Right.Next->isNot(tok::l_paren))) { 3638 // Moving trailing annotations to the next line is fine for ObjC method 3639 // declarations. 3640 if (Line.startsWith(TT_ObjCMethodSpecifier)) 3641 return 10; 3642 // Generally, breaking before a trailing annotation is bad unless it is 3643 // function-like. It seems to be especially preferable to keep standard 3644 // annotations (i.e. "const", "final" and "override") on the same line. 3645 // Use a slightly higher penalty after ")" so that annotations like 3646 // "const override" are kept together. 3647 bool is_short_annotation = Right.TokenText.size() < 10; 3648 return (Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0); 3649 } 3650 3651 // In for-loops, prefer breaking at ',' and ';'. 3652 if (Line.startsWith(tok::kw_for) && Left.is(tok::equal)) 3653 return 4; 3654 3655 // In Objective-C method expressions, prefer breaking before "param:" over 3656 // breaking after it. 3657 if (Right.is(TT_SelectorName)) 3658 return 0; 3659 if (Left.is(tok::colon) && Left.is(TT_ObjCMethodExpr)) 3660 return Line.MightBeFunctionDecl ? 50 : 500; 3661 3662 // In Objective-C type declarations, avoid breaking after the category's 3663 // open paren (we'll prefer breaking after the protocol list's opening 3664 // angle bracket, if present). 3665 if (Line.Type == LT_ObjCDecl && Left.is(tok::l_paren) && Left.Previous && 3666 Left.Previous->isOneOf(tok::identifier, tok::greater)) { 3667 return 500; 3668 } 3669 3670 if (Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0) 3671 return Style.PenaltyBreakOpenParenthesis; 3672 if (Left.is(tok::l_paren) && InFunctionDecl && 3673 Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign) { 3674 return 100; 3675 } 3676 if (Left.is(tok::l_paren) && Left.Previous && 3677 (Left.Previous->isOneOf(tok::kw_for, tok::kw__Generic) || 3678 Left.Previous->isIf())) { 3679 return 1000; 3680 } 3681 if (Left.is(tok::equal) && InFunctionDecl) 3682 return 110; 3683 if (Right.is(tok::r_brace)) 3684 return 1; 3685 if (Left.is(TT_TemplateOpener)) 3686 return 100; 3687 if (Left.opensScope()) { 3688 // If we aren't aligning after opening parens/braces we can always break 3689 // here unless the style does not want us to place all arguments on the 3690 // next line. 3691 if (Style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign && 3692 (Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) { 3693 return 0; 3694 } 3695 if (Left.is(tok::l_brace) && !Style.Cpp11BracedListStyle) 3696 return 19; 3697 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter 3698 : 19; 3699 } 3700 if (Left.is(TT_JavaAnnotation)) 3701 return 50; 3702 3703 if (Left.is(TT_UnaryOperator)) 3704 return 60; 3705 if (Left.isOneOf(tok::plus, tok::comma) && Left.Previous && 3706 Left.Previous->isLabelString() && 3707 (Left.NextOperator || Left.OperatorIndex != 0)) { 3708 return 50; 3709 } 3710 if (Right.is(tok::plus) && Left.isLabelString() && 3711 (Right.NextOperator || Right.OperatorIndex != 0)) { 3712 return 25; 3713 } 3714 if (Left.is(tok::comma)) 3715 return 1; 3716 if (Right.is(tok::lessless) && Left.isLabelString() && 3717 (Right.NextOperator || Right.OperatorIndex != 1)) { 3718 return 25; 3719 } 3720 if (Right.is(tok::lessless)) { 3721 // Breaking at a << is really cheap. 3722 if (!Left.is(tok::r_paren) || Right.OperatorIndex > 0) { 3723 // Slightly prefer to break before the first one in log-like statements. 3724 return 2; 3725 } 3726 return 1; 3727 } 3728 if (Left.ClosesTemplateDeclaration) 3729 return Style.PenaltyBreakTemplateDeclaration; 3730 if (Left.ClosesRequiresClause) 3731 return 0; 3732 if (Left.is(TT_ConditionalExpr)) 3733 return prec::Conditional; 3734 prec::Level Level = Left.getPrecedence(); 3735 if (Level == prec::Unknown) 3736 Level = Right.getPrecedence(); 3737 if (Level == prec::Assignment) 3738 return Style.PenaltyBreakAssignment; 3739 if (Level != prec::Unknown) 3740 return Level; 3741 3742 return 3; 3743 } 3744 3745 bool TokenAnnotator::spaceRequiredBeforeParens(const FormatToken &Right) const { 3746 if (Style.SpaceBeforeParens == FormatStyle::SBPO_Always) 3747 return true; 3748 if (Right.is(TT_OverloadedOperatorLParen) && 3749 Style.SpaceBeforeParensOptions.AfterOverloadedOperator) { 3750 return true; 3751 } 3752 if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses && 3753 Right.ParameterCount > 0) { 3754 return true; 3755 } 3756 return false; 3757 } 3758 3759 bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, 3760 const FormatToken &Left, 3761 const FormatToken &Right) const { 3762 if (Left.is(tok::kw_return) && 3763 !Right.isOneOf(tok::semi, tok::r_paren, tok::hashhash)) { 3764 return true; 3765 } 3766 if (Left.is(tok::kw_throw) && Right.is(tok::l_paren) && Right.MatchingParen && 3767 Right.MatchingParen->is(TT_CastRParen)) { 3768 return true; 3769 } 3770 if (Left.is(Keywords.kw_assert) && Style.Language == FormatStyle::LK_Java) 3771 return true; 3772 if (Style.ObjCSpaceAfterProperty && Line.Type == LT_ObjCProperty && 3773 Left.Tok.getObjCKeywordID() == tok::objc_property) { 3774 return true; 3775 } 3776 if (Right.is(tok::hashhash)) 3777 return Left.is(tok::hash); 3778 if (Left.isOneOf(tok::hashhash, tok::hash)) 3779 return Right.is(tok::hash); 3780 if ((Left.is(tok::l_paren) && Right.is(tok::r_paren)) || 3781 (Left.is(tok::l_brace) && Left.isNot(BK_Block) && 3782 Right.is(tok::r_brace) && Right.isNot(BK_Block))) { 3783 return Style.SpacesInParensOptions.InEmptyParentheses; 3784 } 3785 if (Style.SpacesInParensOptions.InConditionalStatements) { 3786 const FormatToken *LeftParen = nullptr; 3787 if (Left.is(tok::l_paren)) 3788 LeftParen = &Left; 3789 else if (Right.is(tok::r_paren) && Right.MatchingParen) 3790 LeftParen = Right.MatchingParen; 3791 if (LeftParen) { 3792 if (LeftParen->is(TT_ConditionLParen)) 3793 return true; 3794 if (LeftParen->Previous && isKeywordWithCondition(*LeftParen->Previous)) 3795 return true; 3796 } 3797 } 3798 3799 // trailing return type 'auto': []() -> auto {}, auto foo() -> auto {} 3800 if (Left.is(tok::kw_auto) && Right.isOneOf(TT_LambdaLBrace, TT_FunctionLBrace, 3801 // function return type 'auto' 3802 TT_FunctionTypeLParen)) { 3803 return true; 3804 } 3805 3806 // auto{x} auto(x) 3807 if (Left.is(tok::kw_auto) && Right.isOneOf(tok::l_paren, tok::l_brace)) 3808 return false; 3809 3810 // operator co_await(x) 3811 if (Right.is(tok::l_paren) && Left.is(tok::kw_co_await) && Left.Previous && 3812 Left.Previous->is(tok::kw_operator)) { 3813 return false; 3814 } 3815 // co_await (x), co_yield (x), co_return (x) 3816 if (Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) && 3817 !Right.isOneOf(tok::semi, tok::r_paren)) { 3818 return true; 3819 } 3820 3821 if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) { 3822 return (Right.is(TT_CastRParen) || 3823 (Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen))) 3824 ? Style.SpacesInParensOptions.InCStyleCasts 3825 : Style.SpacesInParensOptions.Other; 3826 } 3827 if (Right.isOneOf(tok::semi, tok::comma)) 3828 return false; 3829 if (Right.is(tok::less) && Line.Type == LT_ObjCDecl) { 3830 bool IsLightweightGeneric = Right.MatchingParen && 3831 Right.MatchingParen->Next && 3832 Right.MatchingParen->Next->is(tok::colon); 3833 return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList; 3834 } 3835 if (Right.is(tok::less) && Left.is(tok::kw_template)) 3836 return Style.SpaceAfterTemplateKeyword; 3837 if (Left.isOneOf(tok::exclaim, tok::tilde)) 3838 return false; 3839 if (Left.is(tok::at) && 3840 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant, 3841 tok::numeric_constant, tok::l_paren, tok::l_brace, 3842 tok::kw_true, tok::kw_false)) { 3843 return false; 3844 } 3845 if (Left.is(tok::colon)) 3846 return !Left.is(TT_ObjCMethodExpr); 3847 if (Left.is(tok::coloncolon)) 3848 return false; 3849 if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less)) { 3850 if (Style.Language == FormatStyle::LK_TextProto || 3851 (Style.Language == FormatStyle::LK_Proto && 3852 (Left.is(TT_DictLiteral) || Right.is(TT_DictLiteral)))) { 3853 // Format empty list as `<>`. 3854 if (Left.is(tok::less) && Right.is(tok::greater)) 3855 return false; 3856 return !Style.Cpp11BracedListStyle; 3857 } 3858 // Don't attempt to format operator<(), as it is handled later. 3859 if (Right.isNot(TT_OverloadedOperatorLParen)) 3860 return false; 3861 } 3862 if (Right.is(tok::ellipsis)) { 3863 return Left.Tok.isLiteral() || (Left.is(tok::identifier) && Left.Previous && 3864 Left.Previous->is(tok::kw_case)); 3865 } 3866 if (Left.is(tok::l_square) && Right.is(tok::amp)) 3867 return Style.SpacesInSquareBrackets; 3868 if (Right.is(TT_PointerOrReference)) { 3869 if (Left.is(tok::r_paren) && Line.MightBeFunctionDecl) { 3870 if (!Left.MatchingParen) 3871 return true; 3872 FormatToken *TokenBeforeMatchingParen = 3873 Left.MatchingParen->getPreviousNonComment(); 3874 if (!TokenBeforeMatchingParen || !Left.is(TT_TypeDeclarationParen)) 3875 return true; 3876 } 3877 // Add a space if the previous token is a pointer qualifier or the closing 3878 // parenthesis of __attribute__(()) expression and the style requires spaces 3879 // after pointer qualifiers. 3880 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_After || 3881 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) && 3882 (Left.is(TT_AttributeParen) || 3883 Left.canBePointerOrReferenceQualifier())) { 3884 return true; 3885 } 3886 if (Left.Tok.isLiteral()) 3887 return true; 3888 // for (auto a = 0, b = 0; const auto & c : {1, 2, 3}) 3889 if (Left.isTypeOrIdentifier() && Right.Next && Right.Next->Next && 3890 Right.Next->Next->is(TT_RangeBasedForLoopColon)) { 3891 return getTokenPointerOrReferenceAlignment(Right) != 3892 FormatStyle::PAS_Left; 3893 } 3894 return !Left.isOneOf(TT_PointerOrReference, tok::l_paren) && 3895 (getTokenPointerOrReferenceAlignment(Right) != 3896 FormatStyle::PAS_Left || 3897 (Line.IsMultiVariableDeclStmt && 3898 (Left.NestingLevel == 0 || 3899 (Left.NestingLevel == 1 && startsWithInitStatement(Line))))); 3900 } 3901 if (Right.is(TT_FunctionTypeLParen) && Left.isNot(tok::l_paren) && 3902 (!Left.is(TT_PointerOrReference) || 3903 (getTokenPointerOrReferenceAlignment(Left) != FormatStyle::PAS_Right && 3904 !Line.IsMultiVariableDeclStmt))) { 3905 return true; 3906 } 3907 if (Left.is(TT_PointerOrReference)) { 3908 // Add a space if the next token is a pointer qualifier and the style 3909 // requires spaces before pointer qualifiers. 3910 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Before || 3911 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) && 3912 Right.canBePointerOrReferenceQualifier()) { 3913 return true; 3914 } 3915 // & 1 3916 if (Right.Tok.isLiteral()) 3917 return true; 3918 // & /* comment 3919 if (Right.is(TT_BlockComment)) 3920 return true; 3921 // foo() -> const Bar * override/final 3922 // S::foo() & noexcept/requires 3923 if (Right.isOneOf(Keywords.kw_override, Keywords.kw_final, tok::kw_noexcept, 3924 TT_RequiresClause) && 3925 !Right.is(TT_StartOfName)) { 3926 return true; 3927 } 3928 // & { 3929 if (Right.is(tok::l_brace) && Right.is(BK_Block)) 3930 return true; 3931 // for (auto a = 0, b = 0; const auto& c : {1, 2, 3}) 3932 if (Left.Previous && Left.Previous->isTypeOrIdentifier() && Right.Next && 3933 Right.Next->is(TT_RangeBasedForLoopColon)) { 3934 return getTokenPointerOrReferenceAlignment(Left) != 3935 FormatStyle::PAS_Right; 3936 } 3937 if (Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare, 3938 tok::l_paren)) { 3939 return false; 3940 } 3941 if (getTokenPointerOrReferenceAlignment(Left) == FormatStyle::PAS_Right) 3942 return false; 3943 // FIXME: Setting IsMultiVariableDeclStmt for the whole line is error-prone, 3944 // because it does not take into account nested scopes like lambdas. 3945 // In multi-variable declaration statements, attach */& to the variable 3946 // independently of the style. However, avoid doing it if we are in a nested 3947 // scope, e.g. lambda. We still need to special-case statements with 3948 // initializers. 3949 if (Line.IsMultiVariableDeclStmt && 3950 (Left.NestingLevel == Line.First->NestingLevel || 3951 ((Left.NestingLevel == Line.First->NestingLevel + 1) && 3952 startsWithInitStatement(Line)))) { 3953 return false; 3954 } 3955 return Left.Previous && !Left.Previous->isOneOf( 3956 tok::l_paren, tok::coloncolon, tok::l_square); 3957 } 3958 // Ensure right pointer alignment with ellipsis e.g. int *...P 3959 if (Left.is(tok::ellipsis) && Left.Previous && 3960 Left.Previous->isOneOf(tok::star, tok::amp, tok::ampamp)) { 3961 return Style.PointerAlignment != FormatStyle::PAS_Right; 3962 } 3963 3964 if (Right.is(tok::star) && Left.is(tok::l_paren)) 3965 return false; 3966 if (Left.is(tok::star) && Right.isOneOf(tok::star, tok::amp, tok::ampamp)) 3967 return false; 3968 if (Right.isOneOf(tok::star, tok::amp, tok::ampamp)) { 3969 const FormatToken *Previous = &Left; 3970 while (Previous && !Previous->is(tok::kw_operator)) { 3971 if (Previous->is(tok::identifier) || Previous->isSimpleTypeSpecifier()) { 3972 Previous = Previous->getPreviousNonComment(); 3973 continue; 3974 } 3975 if (Previous->is(TT_TemplateCloser) && Previous->MatchingParen) { 3976 Previous = Previous->MatchingParen->getPreviousNonComment(); 3977 continue; 3978 } 3979 if (Previous->is(tok::coloncolon)) { 3980 Previous = Previous->getPreviousNonComment(); 3981 continue; 3982 } 3983 break; 3984 } 3985 // Space between the type and the * in: 3986 // operator void*() 3987 // operator char*() 3988 // operator void const*() 3989 // operator void volatile*() 3990 // operator /*comment*/ const char*() 3991 // operator volatile /*comment*/ char*() 3992 // operator Foo*() 3993 // operator C<T>*() 3994 // operator std::Foo*() 3995 // operator C<T>::D<U>*() 3996 // dependent on PointerAlignment style. 3997 if (Previous) { 3998 if (Previous->endsSequence(tok::kw_operator)) 3999 return Style.PointerAlignment != FormatStyle::PAS_Left; 4000 if (Previous->is(tok::kw_const) || Previous->is(tok::kw_volatile)) { 4001 return (Style.PointerAlignment != FormatStyle::PAS_Left) || 4002 (Style.SpaceAroundPointerQualifiers == 4003 FormatStyle::SAPQ_After) || 4004 (Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both); 4005 } 4006 } 4007 } 4008 if (Style.isCSharp() && Left.is(Keywords.kw_is) && Right.is(tok::l_square)) 4009 return true; 4010 const auto SpaceRequiredForArrayInitializerLSquare = 4011 [](const FormatToken &LSquareTok, const FormatStyle &Style) { 4012 return Style.SpacesInContainerLiterals || 4013 ((Style.Language == FormatStyle::LK_Proto || 4014 Style.Language == FormatStyle::LK_TextProto) && 4015 !Style.Cpp11BracedListStyle && 4016 LSquareTok.endsSequence(tok::l_square, tok::colon, 4017 TT_SelectorName)); 4018 }; 4019 if (Left.is(tok::l_square)) { 4020 return (Left.is(TT_ArrayInitializerLSquare) && Right.isNot(tok::r_square) && 4021 SpaceRequiredForArrayInitializerLSquare(Left, Style)) || 4022 (Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare, 4023 TT_LambdaLSquare) && 4024 Style.SpacesInSquareBrackets && Right.isNot(tok::r_square)); 4025 } 4026 if (Right.is(tok::r_square)) { 4027 return Right.MatchingParen && 4028 ((Right.MatchingParen->is(TT_ArrayInitializerLSquare) && 4029 SpaceRequiredForArrayInitializerLSquare(*Right.MatchingParen, 4030 Style)) || 4031 (Style.SpacesInSquareBrackets && 4032 Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare, 4033 TT_StructuredBindingLSquare, 4034 TT_LambdaLSquare)) || 4035 Right.MatchingParen->is(TT_AttributeParen)); 4036 } 4037 if (Right.is(tok::l_square) && 4038 !Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare, 4039 TT_DesignatedInitializerLSquare, 4040 TT_StructuredBindingLSquare, TT_AttributeSquare) && 4041 !Left.isOneOf(tok::numeric_constant, TT_DictLiteral) && 4042 !(!Left.is(tok::r_square) && Style.SpaceBeforeSquareBrackets && 4043 Right.is(TT_ArraySubscriptLSquare))) { 4044 return false; 4045 } 4046 if (Left.is(tok::l_brace) && Right.is(tok::r_brace)) 4047 return !Left.Children.empty(); // No spaces in "{}". 4048 if ((Left.is(tok::l_brace) && Left.isNot(BK_Block)) || 4049 (Right.is(tok::r_brace) && Right.MatchingParen && 4050 Right.MatchingParen->isNot(BK_Block))) { 4051 return Style.Cpp11BracedListStyle ? Style.SpacesInParensOptions.Other 4052 : true; 4053 } 4054 if (Left.is(TT_BlockComment)) { 4055 // No whitespace in x(/*foo=*/1), except for JavaScript. 4056 return Style.isJavaScript() || !Left.TokenText.endswith("=*/"); 4057 } 4058 4059 // Space between template and attribute. 4060 // e.g. template <typename T> [[nodiscard]] ... 4061 if (Left.is(TT_TemplateCloser) && Right.is(TT_AttributeSquare)) 4062 return true; 4063 // Space before parentheses common for all languages 4064 if (Right.is(tok::l_paren)) { 4065 if (Left.is(TT_TemplateCloser) && Right.isNot(TT_FunctionTypeLParen)) 4066 return spaceRequiredBeforeParens(Right); 4067 if (Left.isOneOf(TT_RequiresClause, 4068 TT_RequiresClauseInARequiresExpression)) { 4069 return Style.SpaceBeforeParensOptions.AfterRequiresInClause || 4070 spaceRequiredBeforeParens(Right); 4071 } 4072 if (Left.is(TT_RequiresExpression)) { 4073 return Style.SpaceBeforeParensOptions.AfterRequiresInExpression || 4074 spaceRequiredBeforeParens(Right); 4075 } 4076 if ((Left.is(tok::r_paren) && Left.is(TT_AttributeParen)) || 4077 (Left.is(tok::r_square) && Left.is(TT_AttributeSquare))) { 4078 return true; 4079 } 4080 if (Left.is(TT_ForEachMacro)) { 4081 return Style.SpaceBeforeParensOptions.AfterForeachMacros || 4082 spaceRequiredBeforeParens(Right); 4083 } 4084 if (Left.is(TT_IfMacro)) { 4085 return Style.SpaceBeforeParensOptions.AfterIfMacros || 4086 spaceRequiredBeforeParens(Right); 4087 } 4088 if (Line.Type == LT_ObjCDecl) 4089 return true; 4090 if (Left.is(tok::semi)) 4091 return true; 4092 if (Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while, tok::kw_switch, 4093 tok::kw_case, TT_ForEachMacro, TT_ObjCForIn) || 4094 Left.isIf(Line.Type != LT_PreprocessorDirective) || 4095 Right.is(TT_ConditionLParen)) { 4096 return Style.SpaceBeforeParensOptions.AfterControlStatements || 4097 spaceRequiredBeforeParens(Right); 4098 } 4099 4100 // TODO add Operator overloading specific Options to 4101 // SpaceBeforeParensOptions 4102 if (Right.is(TT_OverloadedOperatorLParen)) 4103 return spaceRequiredBeforeParens(Right); 4104 // Function declaration or definition 4105 if (Line.MightBeFunctionDecl && (Left.is(TT_FunctionDeclarationName))) { 4106 if (Line.mightBeFunctionDefinition()) { 4107 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName || 4108 spaceRequiredBeforeParens(Right); 4109 } else { 4110 return Style.SpaceBeforeParensOptions.AfterFunctionDeclarationName || 4111 spaceRequiredBeforeParens(Right); 4112 } 4113 } 4114 // Lambda 4115 if (Line.Type != LT_PreprocessorDirective && Left.is(tok::r_square) && 4116 Left.MatchingParen && Left.MatchingParen->is(TT_LambdaLSquare)) { 4117 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName || 4118 spaceRequiredBeforeParens(Right); 4119 } 4120 if (!Left.Previous || Left.Previous->isNot(tok::period)) { 4121 if (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) { 4122 return Style.SpaceBeforeParensOptions.AfterControlStatements || 4123 spaceRequiredBeforeParens(Right); 4124 } 4125 if (Left.isOneOf(tok::kw_new, tok::kw_delete)) { 4126 return ((!Line.MightBeFunctionDecl || !Left.Previous) && 4127 Style.SpaceBeforeParens != FormatStyle::SBPO_Never) || 4128 spaceRequiredBeforeParens(Right); 4129 } 4130 4131 if (Left.is(tok::r_square) && Left.MatchingParen && 4132 Left.MatchingParen->Previous && 4133 Left.MatchingParen->Previous->is(tok::kw_delete)) { 4134 return (Style.SpaceBeforeParens != FormatStyle::SBPO_Never) || 4135 spaceRequiredBeforeParens(Right); 4136 } 4137 } 4138 // Handle builtins like identifiers. 4139 if (Line.Type != LT_PreprocessorDirective && 4140 (Left.Tok.getIdentifierInfo() || Left.is(tok::r_paren))) { 4141 return spaceRequiredBeforeParens(Right); 4142 } 4143 return false; 4144 } 4145 if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword) 4146 return false; 4147 if (Right.is(TT_UnaryOperator)) { 4148 return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) && 4149 (Left.isNot(tok::colon) || Left.isNot(TT_ObjCMethodExpr)); 4150 } 4151 // No space between the variable name and the initializer list. 4152 // A a1{1}; 4153 // Verilog doesn't have such syntax, but it has word operators that are C++ 4154 // identifiers like `a inside {b, c}`. So the rule is not applicable. 4155 if (!Style.isVerilog() && 4156 (Left.isOneOf(tok::identifier, tok::greater, tok::r_square, 4157 tok::r_paren) || 4158 Left.isSimpleTypeSpecifier()) && 4159 Right.is(tok::l_brace) && Right.getNextNonComment() && 4160 Right.isNot(BK_Block)) { 4161 return false; 4162 } 4163 if (Left.is(tok::period) || Right.is(tok::period)) 4164 return false; 4165 // u#str, U#str, L#str, u8#str 4166 // uR#str, UR#str, LR#str, u8R#str 4167 if (Right.is(tok::hash) && Left.is(tok::identifier) && 4168 (Left.TokenText == "L" || Left.TokenText == "u" || 4169 Left.TokenText == "U" || Left.TokenText == "u8" || 4170 Left.TokenText == "LR" || Left.TokenText == "uR" || 4171 Left.TokenText == "UR" || Left.TokenText == "u8R")) { 4172 return false; 4173 } 4174 if (Left.is(TT_TemplateCloser) && Left.MatchingParen && 4175 Left.MatchingParen->Previous && 4176 (Left.MatchingParen->Previous->is(tok::period) || 4177 Left.MatchingParen->Previous->is(tok::coloncolon))) { 4178 // Java call to generic function with explicit type: 4179 // A.<B<C<...>>>DoSomething(); 4180 // A::<B<C<...>>>DoSomething(); // With a Java 8 method reference. 4181 return false; 4182 } 4183 if (Left.is(TT_TemplateCloser) && Right.is(tok::l_square)) 4184 return false; 4185 if (Left.is(tok::l_brace) && Left.endsSequence(TT_DictLiteral, tok::at)) { 4186 // Objective-C dictionary literal -> no space after opening brace. 4187 return false; 4188 } 4189 if (Right.is(tok::r_brace) && Right.MatchingParen && 4190 Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at)) { 4191 // Objective-C dictionary literal -> no space before closing brace. 4192 return false; 4193 } 4194 if (Right.getType() == TT_TrailingAnnotation && 4195 Right.isOneOf(tok::amp, tok::ampamp) && 4196 Left.isOneOf(tok::kw_const, tok::kw_volatile) && 4197 (!Right.Next || Right.Next->is(tok::semi))) { 4198 // Match const and volatile ref-qualifiers without any additional 4199 // qualifiers such as 4200 // void Fn() const &; 4201 return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left; 4202 } 4203 4204 return true; 4205 } 4206 4207 bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, 4208 const FormatToken &Right) const { 4209 const FormatToken &Left = *Right.Previous; 4210 4211 // If the token is finalized don't touch it (as it could be in a 4212 // clang-format-off section). 4213 if (Left.Finalized) 4214 return Right.hasWhitespaceBefore(); 4215 4216 // Never ever merge two words. 4217 if (Keywords.isWordLike(Right) && Keywords.isWordLike(Left)) 4218 return true; 4219 4220 // Leave a space between * and /* to avoid C4138 `comment end` found outside 4221 // of comment. 4222 if (Left.is(tok::star) && Right.is(tok::comment)) 4223 return true; 4224 4225 if (Style.isCpp()) { 4226 if (Left.is(TT_OverloadedOperator) && 4227 Right.isOneOf(TT_TemplateOpener, TT_TemplateCloser)) { 4228 return true; 4229 } 4230 // Space between UDL and dot: auto b = 4s .count(); 4231 if (Right.is(tok::period) && Left.is(tok::numeric_constant)) 4232 return true; 4233 // Space between import <iostream>. 4234 // or import .....; 4235 if (Left.is(Keywords.kw_import) && Right.isOneOf(tok::less, tok::ellipsis)) 4236 return true; 4237 // Space between `module :` and `import :`. 4238 if (Left.isOneOf(Keywords.kw_module, Keywords.kw_import) && 4239 Right.is(TT_ModulePartitionColon)) { 4240 return true; 4241 } 4242 // No space between import foo:bar but keep a space between import :bar; 4243 if (Left.is(tok::identifier) && Right.is(TT_ModulePartitionColon)) 4244 return false; 4245 // No space between :bar; 4246 if (Left.is(TT_ModulePartitionColon) && 4247 Right.isOneOf(tok::identifier, tok::kw_private)) { 4248 return false; 4249 } 4250 if (Left.is(tok::ellipsis) && Right.is(tok::identifier) && 4251 Line.First->is(Keywords.kw_import)) { 4252 return false; 4253 } 4254 // Space in __attribute__((attr)) ::type. 4255 if (Left.is(TT_AttributeParen) && Right.is(tok::coloncolon)) 4256 return true; 4257 4258 if (Left.is(tok::kw_operator)) 4259 return Right.is(tok::coloncolon); 4260 if (Right.is(tok::l_brace) && Right.is(BK_BracedInit) && 4261 !Left.opensScope() && Style.SpaceBeforeCpp11BracedList) { 4262 return true; 4263 } 4264 if (Left.is(tok::less) && Left.is(TT_OverloadedOperator) && 4265 Right.is(TT_TemplateOpener)) { 4266 return true; 4267 } 4268 } else if (Style.Language == FormatStyle::LK_Proto || 4269 Style.Language == FormatStyle::LK_TextProto) { 4270 if (Right.is(tok::period) && 4271 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required, 4272 Keywords.kw_repeated, Keywords.kw_extend)) { 4273 return true; 4274 } 4275 if (Right.is(tok::l_paren) && 4276 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)) { 4277 return true; 4278 } 4279 if (Right.isOneOf(tok::l_brace, tok::less) && Left.is(TT_SelectorName)) 4280 return true; 4281 // Slashes occur in text protocol extension syntax: [type/type] { ... }. 4282 if (Left.is(tok::slash) || Right.is(tok::slash)) 4283 return false; 4284 if (Left.MatchingParen && 4285 Left.MatchingParen->is(TT_ProtoExtensionLSquare) && 4286 Right.isOneOf(tok::l_brace, tok::less)) { 4287 return !Style.Cpp11BracedListStyle; 4288 } 4289 // A percent is probably part of a formatting specification, such as %lld. 4290 if (Left.is(tok::percent)) 4291 return false; 4292 // Preserve the existence of a space before a percent for cases like 0x%04x 4293 // and "%d %d" 4294 if (Left.is(tok::numeric_constant) && Right.is(tok::percent)) 4295 return Right.hasWhitespaceBefore(); 4296 } else if (Style.isJson()) { 4297 if (Right.is(tok::colon) && Left.is(tok::string_literal)) 4298 return Style.SpaceBeforeJsonColon; 4299 } else if (Style.isCSharp()) { 4300 // Require spaces around '{' and before '}' unless they appear in 4301 // interpolated strings. Interpolated strings are merged into a single token 4302 // so cannot have spaces inserted by this function. 4303 4304 // No space between 'this' and '[' 4305 if (Left.is(tok::kw_this) && Right.is(tok::l_square)) 4306 return false; 4307 4308 // No space between 'new' and '(' 4309 if (Left.is(tok::kw_new) && Right.is(tok::l_paren)) 4310 return false; 4311 4312 // Space before { (including space within '{ {'). 4313 if (Right.is(tok::l_brace)) 4314 return true; 4315 4316 // Spaces inside braces. 4317 if (Left.is(tok::l_brace) && Right.isNot(tok::r_brace)) 4318 return true; 4319 4320 if (Left.isNot(tok::l_brace) && Right.is(tok::r_brace)) 4321 return true; 4322 4323 // Spaces around '=>'. 4324 if (Left.is(TT_FatArrow) || Right.is(TT_FatArrow)) 4325 return true; 4326 4327 // No spaces around attribute target colons 4328 if (Left.is(TT_AttributeColon) || Right.is(TT_AttributeColon)) 4329 return false; 4330 4331 // space between type and variable e.g. Dictionary<string,string> foo; 4332 if (Left.is(TT_TemplateCloser) && Right.is(TT_StartOfName)) 4333 return true; 4334 4335 // spaces inside square brackets. 4336 if (Left.is(tok::l_square) || Right.is(tok::r_square)) 4337 return Style.SpacesInSquareBrackets; 4338 4339 // No space before ? in nullable types. 4340 if (Right.is(TT_CSharpNullable)) 4341 return false; 4342 4343 // No space before null forgiving '!'. 4344 if (Right.is(TT_NonNullAssertion)) 4345 return false; 4346 4347 // No space between consecutive commas '[,,]'. 4348 if (Left.is(tok::comma) && Right.is(tok::comma)) 4349 return false; 4350 4351 // space after var in `var (key, value)` 4352 if (Left.is(Keywords.kw_var) && Right.is(tok::l_paren)) 4353 return true; 4354 4355 // space between keywords and paren e.g. "using (" 4356 if (Right.is(tok::l_paren)) { 4357 if (Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when, 4358 Keywords.kw_lock)) { 4359 return Style.SpaceBeforeParensOptions.AfterControlStatements || 4360 spaceRequiredBeforeParens(Right); 4361 } 4362 } 4363 4364 // space between method modifier and opening parenthesis of a tuple return 4365 // type 4366 if (Left.isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected, 4367 tok::kw_virtual, tok::kw_extern, tok::kw_static, 4368 Keywords.kw_internal, Keywords.kw_abstract, 4369 Keywords.kw_sealed, Keywords.kw_override, 4370 Keywords.kw_async, Keywords.kw_unsafe) && 4371 Right.is(tok::l_paren)) { 4372 return true; 4373 } 4374 } else if (Style.isJavaScript()) { 4375 if (Left.is(TT_FatArrow)) 4376 return true; 4377 // for await ( ... 4378 if (Right.is(tok::l_paren) && Left.is(Keywords.kw_await) && Left.Previous && 4379 Left.Previous->is(tok::kw_for)) { 4380 return true; 4381 } 4382 if (Left.is(Keywords.kw_async) && Right.is(tok::l_paren) && 4383 Right.MatchingParen) { 4384 const FormatToken *Next = Right.MatchingParen->getNextNonComment(); 4385 // An async arrow function, for example: `x = async () => foo();`, 4386 // as opposed to calling a function called async: `x = async();` 4387 if (Next && Next->is(TT_FatArrow)) 4388 return true; 4389 } 4390 if ((Left.is(TT_TemplateString) && Left.TokenText.endswith("${")) || 4391 (Right.is(TT_TemplateString) && Right.TokenText.startswith("}"))) { 4392 return false; 4393 } 4394 // In tagged template literals ("html`bar baz`"), there is no space between 4395 // the tag identifier and the template string. 4396 if (Keywords.IsJavaScriptIdentifier(Left, 4397 /* AcceptIdentifierName= */ false) && 4398 Right.is(TT_TemplateString)) { 4399 return false; 4400 } 4401 if (Right.is(tok::star) && 4402 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)) { 4403 return false; 4404 } 4405 if (Right.isOneOf(tok::l_brace, tok::l_square) && 4406 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield, 4407 Keywords.kw_extends, Keywords.kw_implements)) { 4408 return true; 4409 } 4410 if (Right.is(tok::l_paren)) { 4411 // JS methods can use some keywords as names (e.g. `delete()`). 4412 if (Line.MustBeDeclaration && Left.Tok.getIdentifierInfo()) 4413 return false; 4414 // Valid JS method names can include keywords, e.g. `foo.delete()` or 4415 // `bar.instanceof()`. Recognize call positions by preceding period. 4416 if (Left.Previous && Left.Previous->is(tok::period) && 4417 Left.Tok.getIdentifierInfo()) { 4418 return false; 4419 } 4420 // Additional unary JavaScript operators that need a space after. 4421 if (Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof, 4422 tok::kw_void)) { 4423 return true; 4424 } 4425 } 4426 // `foo as const;` casts into a const type. 4427 if (Left.endsSequence(tok::kw_const, Keywords.kw_as)) 4428 return false; 4429 if ((Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in, 4430 tok::kw_const) || 4431 // "of" is only a keyword if it appears after another identifier 4432 // (e.g. as "const x of y" in a for loop), or after a destructuring 4433 // operation (const [x, y] of z, const {a, b} of c). 4434 (Left.is(Keywords.kw_of) && Left.Previous && 4435 (Left.Previous->is(tok::identifier) || 4436 Left.Previous->isOneOf(tok::r_square, tok::r_brace)))) && 4437 (!Left.Previous || !Left.Previous->is(tok::period))) { 4438 return true; 4439 } 4440 if (Left.isOneOf(tok::kw_for, Keywords.kw_as) && Left.Previous && 4441 Left.Previous->is(tok::period) && Right.is(tok::l_paren)) { 4442 return false; 4443 } 4444 if (Left.is(Keywords.kw_as) && 4445 Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)) { 4446 return true; 4447 } 4448 if (Left.is(tok::kw_default) && Left.Previous && 4449 Left.Previous->is(tok::kw_export)) { 4450 return true; 4451 } 4452 if (Left.is(Keywords.kw_is) && Right.is(tok::l_brace)) 4453 return true; 4454 if (Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion)) 4455 return false; 4456 if (Left.is(TT_JsTypeOperator) || Right.is(TT_JsTypeOperator)) 4457 return false; 4458 if ((Left.is(tok::l_brace) || Right.is(tok::r_brace)) && 4459 Line.First->isOneOf(Keywords.kw_import, tok::kw_export)) { 4460 return false; 4461 } 4462 if (Left.is(tok::ellipsis)) 4463 return false; 4464 if (Left.is(TT_TemplateCloser) && 4465 !Right.isOneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square, 4466 Keywords.kw_implements, Keywords.kw_extends)) { 4467 // Type assertions ('<type>expr') are not followed by whitespace. Other 4468 // locations that should have whitespace following are identified by the 4469 // above set of follower tokens. 4470 return false; 4471 } 4472 if (Right.is(TT_NonNullAssertion)) 4473 return false; 4474 if (Left.is(TT_NonNullAssertion) && 4475 Right.isOneOf(Keywords.kw_as, Keywords.kw_in)) { 4476 return true; // "x! as string", "x! in y" 4477 } 4478 } else if (Style.Language == FormatStyle::LK_Java) { 4479 if (Left.is(tok::r_square) && Right.is(tok::l_brace)) 4480 return true; 4481 if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren)) { 4482 return Style.SpaceBeforeParensOptions.AfterControlStatements || 4483 spaceRequiredBeforeParens(Right); 4484 } 4485 if ((Left.isOneOf(tok::kw_static, tok::kw_public, tok::kw_private, 4486 tok::kw_protected) || 4487 Left.isOneOf(Keywords.kw_final, Keywords.kw_abstract, 4488 Keywords.kw_native)) && 4489 Right.is(TT_TemplateOpener)) { 4490 return true; 4491 } 4492 } else if (Style.isVerilog()) { 4493 // An escaped identifier ends with whitespace. 4494 if (Style.isVerilog() && Left.is(tok::identifier) && 4495 Left.TokenText[0] == '\\') { 4496 return true; 4497 } 4498 // Add space between things in a primitive's state table unless in a 4499 // transition like `(0?)`. 4500 if ((Left.is(TT_VerilogTableItem) && 4501 !Right.isOneOf(tok::r_paren, tok::semi)) || 4502 (Right.is(TT_VerilogTableItem) && Left.isNot(tok::l_paren))) { 4503 const FormatToken *Next = Right.getNextNonComment(); 4504 return !(Next && Next->is(tok::r_paren)); 4505 } 4506 // Don't add space within a delay like `#0`. 4507 if (Left.isNot(TT_BinaryOperator) && 4508 Left.isOneOf(Keywords.kw_verilogHash, Keywords.kw_verilogHashHash)) { 4509 return false; 4510 } 4511 // Add space after a delay. 4512 if (!Right.is(tok::semi) && 4513 (Left.endsSequence(tok::numeric_constant, Keywords.kw_verilogHash) || 4514 Left.endsSequence(tok::numeric_constant, 4515 Keywords.kw_verilogHashHash) || 4516 (Left.is(tok::r_paren) && Left.MatchingParen && 4517 Left.MatchingParen->endsSequence(tok::l_paren, tok::at)))) { 4518 return true; 4519 } 4520 // Don't add embedded spaces in a number literal like `16'h1?ax` or an array 4521 // literal like `'{}`. 4522 if (Left.is(Keywords.kw_apostrophe) || 4523 (Left.is(TT_VerilogNumberBase) && Right.is(tok::numeric_constant))) { 4524 return false; 4525 } 4526 // Don't add spaces between two at signs. Like in a coverage event. 4527 // Don't add spaces between at and a sensitivity list like 4528 // `@(posedge clk)`. 4529 if (Left.is(tok::at) && Right.isOneOf(tok::l_paren, tok::star, tok::at)) 4530 return false; 4531 // Add space between the type name and dimension like `logic [1:0]`. 4532 if (Right.is(tok::l_square) && 4533 Left.isOneOf(TT_VerilogDimensionedTypeName, Keywords.kw_function)) { 4534 return true; 4535 } 4536 // Don't add spaces between a casting type and the quote or repetition count 4537 // and the brace. 4538 if ((Right.is(Keywords.kw_apostrophe) || 4539 (Right.is(BK_BracedInit) && Right.is(tok::l_brace))) && 4540 !(Left.isOneOf(Keywords.kw_assign, Keywords.kw_unique) || 4541 Keywords.isVerilogWordOperator(Left)) && 4542 (Left.isOneOf(tok::r_square, tok::r_paren, tok::r_brace, 4543 tok::numeric_constant) || 4544 Keywords.isWordLike(Left))) { 4545 return false; 4546 } 4547 // Don't add spaces in imports like `import foo::*;`. 4548 if ((Right.is(tok::star) && Left.is(tok::coloncolon)) || 4549 (Left.is(tok::star) && Right.is(tok::semi))) { 4550 return false; 4551 } 4552 // Add space in attribute like `(* ASYNC_REG = "TRUE" *)`. 4553 if (Left.endsSequence(tok::star, tok::l_paren) && Right.is(tok::identifier)) 4554 return true; 4555 // Add space before drive strength like in `wire (strong1, pull0)`. 4556 if (Right.is(tok::l_paren) && Right.is(TT_VerilogStrength)) 4557 return true; 4558 // Don't add space in a streaming concatenation like `{>>{j}}`. 4559 if ((Left.is(tok::l_brace) && 4560 Right.isOneOf(tok::lessless, tok::greatergreater)) || 4561 (Left.endsSequence(tok::lessless, tok::l_brace) || 4562 Left.endsSequence(tok::greatergreater, tok::l_brace))) { 4563 return false; 4564 } 4565 } 4566 if (Left.is(TT_ImplicitStringLiteral)) 4567 return Right.hasWhitespaceBefore(); 4568 if (Line.Type == LT_ObjCMethodDecl) { 4569 if (Left.is(TT_ObjCMethodSpecifier)) 4570 return true; 4571 if (Left.is(tok::r_paren) && canBeObjCSelectorComponent(Right)) { 4572 // Don't space between ')' and <id> or ')' and 'new'. 'new' is not a 4573 // keyword in Objective-C, and '+ (instancetype)new;' is a standard class 4574 // method declaration. 4575 return false; 4576 } 4577 } 4578 if (Line.Type == LT_ObjCProperty && 4579 (Right.is(tok::equal) || Left.is(tok::equal))) { 4580 return false; 4581 } 4582 4583 if (Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) || 4584 Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow)) { 4585 return true; 4586 } 4587 if (Left.is(tok::comma) && !Right.is(TT_OverloadedOperatorLParen) && 4588 // In an unexpanded macro call we only find the parentheses and commas 4589 // in a line; the commas and closing parenthesis do not require a space. 4590 (Left.Children.empty() || !Left.MacroParent)) { 4591 return true; 4592 } 4593 if (Right.is(tok::comma)) 4594 return false; 4595 if (Right.is(TT_ObjCBlockLParen)) 4596 return true; 4597 if (Right.is(TT_CtorInitializerColon)) 4598 return Style.SpaceBeforeCtorInitializerColon; 4599 if (Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon) 4600 return false; 4601 if (Right.is(TT_RangeBasedForLoopColon) && 4602 !Style.SpaceBeforeRangeBasedForLoopColon) { 4603 return false; 4604 } 4605 if (Left.is(TT_BitFieldColon)) { 4606 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both || 4607 Style.BitFieldColonSpacing == FormatStyle::BFCS_After; 4608 } 4609 if (Right.is(tok::colon)) { 4610 if (Right.is(TT_CaseLabelColon)) 4611 return Style.SpaceBeforeCaseColon; 4612 if (Right.is(TT_GotoLabelColon)) 4613 return false; 4614 // `private:` and `public:`. 4615 if (!Right.getNextNonComment()) 4616 return false; 4617 if (Right.is(TT_ObjCMethodExpr)) 4618 return false; 4619 if (Left.is(tok::question)) 4620 return false; 4621 if (Right.is(TT_InlineASMColon) && Left.is(tok::coloncolon)) 4622 return false; 4623 if (Right.is(TT_DictLiteral)) 4624 return Style.SpacesInContainerLiterals; 4625 if (Right.is(TT_AttributeColon)) 4626 return false; 4627 if (Right.is(TT_CSharpNamedArgumentColon)) 4628 return false; 4629 if (Right.is(TT_GenericSelectionColon)) 4630 return false; 4631 if (Right.is(TT_BitFieldColon)) { 4632 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both || 4633 Style.BitFieldColonSpacing == FormatStyle::BFCS_Before; 4634 } 4635 return true; 4636 } 4637 // Do not merge "- -" into "--". 4638 if ((Left.isOneOf(tok::minus, tok::minusminus) && 4639 Right.isOneOf(tok::minus, tok::minusminus)) || 4640 (Left.isOneOf(tok::plus, tok::plusplus) && 4641 Right.isOneOf(tok::plus, tok::plusplus))) { 4642 return true; 4643 } 4644 if (Left.is(TT_UnaryOperator)) { 4645 if (!Right.is(tok::l_paren)) { 4646 // The alternative operators for ~ and ! are "compl" and "not". 4647 // If they are used instead, we do not want to combine them with 4648 // the token to the right, unless that is a left paren. 4649 if (Left.is(tok::exclaim) && Left.TokenText == "not") 4650 return true; 4651 if (Left.is(tok::tilde) && Left.TokenText == "compl") 4652 return true; 4653 // Lambda captures allow for a lone &, so "&]" needs to be properly 4654 // handled. 4655 if (Left.is(tok::amp) && Right.is(tok::r_square)) 4656 return Style.SpacesInSquareBrackets; 4657 } 4658 return (Style.SpaceAfterLogicalNot && Left.is(tok::exclaim)) || 4659 Right.is(TT_BinaryOperator); 4660 } 4661 4662 // If the next token is a binary operator or a selector name, we have 4663 // incorrectly classified the parenthesis as a cast. FIXME: Detect correctly. 4664 if (Left.is(TT_CastRParen)) { 4665 return Style.SpaceAfterCStyleCast || 4666 Right.isOneOf(TT_BinaryOperator, TT_SelectorName); 4667 } 4668 4669 auto ShouldAddSpacesInAngles = [this, &Right]() { 4670 if (this->Style.SpacesInAngles == FormatStyle::SIAS_Always) 4671 return true; 4672 if (this->Style.SpacesInAngles == FormatStyle::SIAS_Leave) 4673 return Right.hasWhitespaceBefore(); 4674 return false; 4675 }; 4676 4677 if (Left.is(tok::greater) && Right.is(tok::greater)) { 4678 if (Style.Language == FormatStyle::LK_TextProto || 4679 (Style.Language == FormatStyle::LK_Proto && Left.is(TT_DictLiteral))) { 4680 return !Style.Cpp11BracedListStyle; 4681 } 4682 return Right.is(TT_TemplateCloser) && Left.is(TT_TemplateCloser) && 4683 ((Style.Standard < FormatStyle::LS_Cpp11) || 4684 ShouldAddSpacesInAngles()); 4685 } 4686 if (Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) || 4687 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) || 4688 (Right.is(tok::period) && Right.isNot(TT_DesignatedInitializerPeriod))) { 4689 return false; 4690 } 4691 if (!Style.SpaceBeforeAssignmentOperators && Left.isNot(TT_TemplateCloser) && 4692 Right.getPrecedence() == prec::Assignment) { 4693 return false; 4694 } 4695 if (Style.Language == FormatStyle::LK_Java && Right.is(tok::coloncolon) && 4696 (Left.is(tok::identifier) || Left.is(tok::kw_this))) { 4697 return false; 4698 } 4699 if (Right.is(tok::coloncolon) && Left.is(tok::identifier)) { 4700 // Generally don't remove existing spaces between an identifier and "::". 4701 // The identifier might actually be a macro name such as ALWAYS_INLINE. If 4702 // this turns out to be too lenient, add analysis of the identifier itself. 4703 return Right.hasWhitespaceBefore(); 4704 } 4705 if (Right.is(tok::coloncolon) && 4706 !Left.isOneOf(tok::l_brace, tok::comment, tok::l_paren)) { 4707 // Put a space between < and :: in vector< ::std::string > 4708 return (Left.is(TT_TemplateOpener) && 4709 ((Style.Standard < FormatStyle::LS_Cpp11) || 4710 ShouldAddSpacesInAngles())) || 4711 !(Left.isOneOf(tok::l_paren, tok::r_paren, tok::l_square, 4712 tok::kw___super, TT_TemplateOpener, 4713 TT_TemplateCloser)) || 4714 (Left.is(tok::l_paren) && Style.SpacesInParensOptions.Other); 4715 } 4716 if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser))) 4717 return ShouldAddSpacesInAngles(); 4718 // Space before TT_StructuredBindingLSquare. 4719 if (Right.is(TT_StructuredBindingLSquare)) { 4720 return !Left.isOneOf(tok::amp, tok::ampamp) || 4721 getTokenReferenceAlignment(Left) != FormatStyle::PAS_Right; 4722 } 4723 // Space before & or && following a TT_StructuredBindingLSquare. 4724 if (Right.Next && Right.Next->is(TT_StructuredBindingLSquare) && 4725 Right.isOneOf(tok::amp, tok::ampamp)) { 4726 return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left; 4727 } 4728 if ((Right.is(TT_BinaryOperator) && !Left.is(tok::l_paren)) || 4729 (Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) && 4730 !Right.is(tok::r_paren))) { 4731 return true; 4732 } 4733 if (Right.is(TT_TemplateOpener) && Left.is(tok::r_paren) && 4734 Left.MatchingParen && 4735 Left.MatchingParen->is(TT_OverloadedOperatorLParen)) { 4736 return false; 4737 } 4738 if (Right.is(tok::less) && Left.isNot(tok::l_paren) && 4739 Line.Type == LT_ImportStatement) { 4740 return true; 4741 } 4742 if (Right.is(TT_TrailingUnaryOperator)) 4743 return false; 4744 if (Left.is(TT_RegexLiteral)) 4745 return false; 4746 return spaceRequiredBetween(Line, Left, Right); 4747 } 4748 4749 // Returns 'true' if 'Tok' is a brace we'd want to break before in Allman style. 4750 static bool isAllmanBrace(const FormatToken &Tok) { 4751 return Tok.is(tok::l_brace) && Tok.is(BK_Block) && 4752 !Tok.isOneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral); 4753 } 4754 4755 // Returns 'true' if 'Tok' is a function argument. 4756 static bool IsFunctionArgument(const FormatToken &Tok) { 4757 return Tok.MatchingParen && Tok.MatchingParen->Next && 4758 Tok.MatchingParen->Next->isOneOf(tok::comma, tok::r_paren); 4759 } 4760 4761 static bool 4762 isItAnEmptyLambdaAllowed(const FormatToken &Tok, 4763 FormatStyle::ShortLambdaStyle ShortLambdaOption) { 4764 return Tok.Children.empty() && ShortLambdaOption != FormatStyle::SLS_None; 4765 } 4766 4767 static bool isAllmanLambdaBrace(const FormatToken &Tok) { 4768 return Tok.is(tok::l_brace) && Tok.is(BK_Block) && 4769 !Tok.isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral); 4770 } 4771 4772 // Returns the first token on the line that is not a comment. 4773 static const FormatToken *getFirstNonComment(const AnnotatedLine &Line) { 4774 const FormatToken *Next = Line.First; 4775 if (!Next) 4776 return Next; 4777 if (Next->is(tok::comment)) 4778 Next = Next->getNextNonComment(); 4779 return Next; 4780 } 4781 4782 bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, 4783 const FormatToken &Right) const { 4784 const FormatToken &Left = *Right.Previous; 4785 if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0) 4786 return true; 4787 4788 if (Style.isCSharp()) { 4789 if (Left.is(TT_FatArrow) && Right.is(tok::l_brace) && 4790 Style.BraceWrapping.AfterFunction) { 4791 return true; 4792 } 4793 if (Right.is(TT_CSharpNamedArgumentColon) || 4794 Left.is(TT_CSharpNamedArgumentColon)) { 4795 return false; 4796 } 4797 if (Right.is(TT_CSharpGenericTypeConstraint)) 4798 return true; 4799 if (Right.Next && Right.Next->is(TT_FatArrow) && 4800 (Right.is(tok::numeric_constant) || 4801 (Right.is(tok::identifier) && Right.TokenText == "_"))) { 4802 return true; 4803 } 4804 4805 // Break after C# [...] and before public/protected/private/internal. 4806 if (Left.is(TT_AttributeSquare) && Left.is(tok::r_square) && 4807 (Right.isAccessSpecifier(/*ColonRequired=*/false) || 4808 Right.is(Keywords.kw_internal))) { 4809 return true; 4810 } 4811 // Break between ] and [ but only when there are really 2 attributes. 4812 if (Left.is(TT_AttributeSquare) && Right.is(TT_AttributeSquare) && 4813 Left.is(tok::r_square) && Right.is(tok::l_square)) { 4814 return true; 4815 } 4816 4817 } else if (Style.isJavaScript()) { 4818 // FIXME: This might apply to other languages and token kinds. 4819 if (Right.is(tok::string_literal) && Left.is(tok::plus) && Left.Previous && 4820 Left.Previous->is(tok::string_literal)) { 4821 return true; 4822 } 4823 if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) && Line.Level == 0 && 4824 Left.Previous && Left.Previous->is(tok::equal) && 4825 Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export, 4826 tok::kw_const) && 4827 // kw_var/kw_let are pseudo-tokens that are tok::identifier, so match 4828 // above. 4829 !Line.First->isOneOf(Keywords.kw_var, Keywords.kw_let)) { 4830 // Object literals on the top level of a file are treated as "enum-style". 4831 // Each key/value pair is put on a separate line, instead of bin-packing. 4832 return true; 4833 } 4834 if (Left.is(tok::l_brace) && Line.Level == 0 && 4835 (Line.startsWith(tok::kw_enum) || 4836 Line.startsWith(tok::kw_const, tok::kw_enum) || 4837 Line.startsWith(tok::kw_export, tok::kw_enum) || 4838 Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum))) { 4839 // JavaScript top-level enum key/value pairs are put on separate lines 4840 // instead of bin-packing. 4841 return true; 4842 } 4843 if (Right.is(tok::r_brace) && Left.is(tok::l_brace) && Left.Previous && 4844 Left.Previous->is(TT_FatArrow)) { 4845 // JS arrow function (=> {...}). 4846 switch (Style.AllowShortLambdasOnASingleLine) { 4847 case FormatStyle::SLS_All: 4848 return false; 4849 case FormatStyle::SLS_None: 4850 return true; 4851 case FormatStyle::SLS_Empty: 4852 return !Left.Children.empty(); 4853 case FormatStyle::SLS_Inline: 4854 // allow one-lining inline (e.g. in function call args) and empty arrow 4855 // functions. 4856 return (Left.NestingLevel == 0 && Line.Level == 0) && 4857 !Left.Children.empty(); 4858 } 4859 llvm_unreachable("Unknown FormatStyle::ShortLambdaStyle enum"); 4860 } 4861 4862 if (Right.is(tok::r_brace) && Left.is(tok::l_brace) && 4863 !Left.Children.empty()) { 4864 // Support AllowShortFunctionsOnASingleLine for JavaScript. 4865 return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None || 4866 Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Empty || 4867 (Left.NestingLevel == 0 && Line.Level == 0 && 4868 Style.AllowShortFunctionsOnASingleLine & 4869 FormatStyle::SFS_InlineOnly); 4870 } 4871 } else if (Style.Language == FormatStyle::LK_Java) { 4872 if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next && 4873 Right.Next->is(tok::string_literal)) { 4874 return true; 4875 } 4876 } else if (Style.isVerilog()) { 4877 // Break between assignments. 4878 if (Left.is(TT_VerilogAssignComma)) 4879 return true; 4880 // Break between ports of different types. 4881 if (Left.is(TT_VerilogTypeComma)) 4882 return true; 4883 // Break between ports in a module instantiation and after the parameter 4884 // list. 4885 if (Style.VerilogBreakBetweenInstancePorts && 4886 (Left.is(TT_VerilogInstancePortComma) || 4887 (Left.is(tok::r_paren) && Keywords.isVerilogIdentifier(Right) && 4888 Left.MatchingParen && 4889 Left.MatchingParen->is(TT_VerilogInstancePortLParen)))) { 4890 return true; 4891 } 4892 // Break after labels. In Verilog labels don't have the 'case' keyword, so 4893 // it is hard to identify them in UnwrappedLineParser. 4894 if (!Keywords.isVerilogBegin(Right) && Keywords.isVerilogEndOfLabel(Left)) 4895 return true; 4896 } else if (Style.Language == FormatStyle::LK_Cpp || 4897 Style.Language == FormatStyle::LK_ObjC || 4898 Style.Language == FormatStyle::LK_Proto || 4899 Style.Language == FormatStyle::LK_TableGen || 4900 Style.Language == FormatStyle::LK_TextProto) { 4901 if (Left.isStringLiteral() && Right.isStringLiteral()) 4902 return true; 4903 } 4904 4905 // Basic JSON newline processing. 4906 if (Style.isJson()) { 4907 // Always break after a JSON record opener. 4908 // { 4909 // } 4910 if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace)) 4911 return true; 4912 // Always break after a JSON array opener based on BreakArrays. 4913 if ((Left.is(TT_ArrayInitializerLSquare) && Left.is(tok::l_square) && 4914 Right.isNot(tok::r_square)) || 4915 Left.is(tok::comma)) { 4916 if (Right.is(tok::l_brace)) 4917 return true; 4918 // scan to the right if an we see an object or an array inside 4919 // then break. 4920 for (const auto *Tok = &Right; Tok; Tok = Tok->Next) { 4921 if (Tok->isOneOf(tok::l_brace, tok::l_square)) 4922 return true; 4923 if (Tok->isOneOf(tok::r_brace, tok::r_square)) 4924 break; 4925 } 4926 return Style.BreakArrays; 4927 } 4928 } 4929 4930 if (Line.startsWith(tok::kw_asm) && Right.is(TT_InlineASMColon) && 4931 Style.BreakBeforeInlineASMColon == FormatStyle::BBIAS_Always) { 4932 return true; 4933 } 4934 4935 // If the last token before a '}', ']', or ')' is a comma or a trailing 4936 // comment, the intention is to insert a line break after it in order to make 4937 // shuffling around entries easier. Import statements, especially in 4938 // JavaScript, can be an exception to this rule. 4939 if (Style.JavaScriptWrapImports || Line.Type != LT_ImportStatement) { 4940 const FormatToken *BeforeClosingBrace = nullptr; 4941 if ((Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) || 4942 (Style.isJavaScript() && Left.is(tok::l_paren))) && 4943 Left.isNot(BK_Block) && Left.MatchingParen) { 4944 BeforeClosingBrace = Left.MatchingParen->Previous; 4945 } else if (Right.MatchingParen && 4946 (Right.MatchingParen->isOneOf(tok::l_brace, 4947 TT_ArrayInitializerLSquare) || 4948 (Style.isJavaScript() && 4949 Right.MatchingParen->is(tok::l_paren)))) { 4950 BeforeClosingBrace = &Left; 4951 } 4952 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) || 4953 BeforeClosingBrace->isTrailingComment())) { 4954 return true; 4955 } 4956 } 4957 4958 if (Right.is(tok::comment)) { 4959 return Left.isNot(BK_BracedInit) && Left.isNot(TT_CtorInitializerColon) && 4960 (Right.NewlinesBefore > 0 && Right.HasUnescapedNewline); 4961 } 4962 if (Left.isTrailingComment()) 4963 return true; 4964 if (Left.IsUnterminatedLiteral) 4965 return true; 4966 if (Right.is(tok::lessless) && Right.Next && Left.is(tok::string_literal) && 4967 Right.Next->is(tok::string_literal)) { 4968 return true; 4969 } 4970 if (Right.is(TT_RequiresClause)) { 4971 switch (Style.RequiresClausePosition) { 4972 case FormatStyle::RCPS_OwnLine: 4973 case FormatStyle::RCPS_WithFollowing: 4974 return true; 4975 default: 4976 break; 4977 } 4978 } 4979 // Can break after template<> declaration 4980 if (Left.ClosesTemplateDeclaration && Left.MatchingParen && 4981 Left.MatchingParen->NestingLevel == 0) { 4982 // Put concepts on the next line e.g. 4983 // template<typename T> 4984 // concept ... 4985 if (Right.is(tok::kw_concept)) 4986 return Style.BreakBeforeConceptDeclarations == FormatStyle::BBCDS_Always; 4987 return Style.AlwaysBreakTemplateDeclarations == FormatStyle::BTDS_Yes; 4988 } 4989 if (Left.ClosesRequiresClause && Right.isNot(tok::semi)) { 4990 switch (Style.RequiresClausePosition) { 4991 case FormatStyle::RCPS_OwnLine: 4992 case FormatStyle::RCPS_WithPreceding: 4993 return true; 4994 default: 4995 break; 4996 } 4997 } 4998 if (Style.PackConstructorInitializers == FormatStyle::PCIS_Never) { 4999 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon && 5000 (Left.is(TT_CtorInitializerComma) || 5001 Right.is(TT_CtorInitializerColon))) { 5002 return true; 5003 } 5004 5005 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon && 5006 Left.isOneOf(TT_CtorInitializerColon, TT_CtorInitializerComma)) { 5007 return true; 5008 } 5009 } 5010 if (Style.PackConstructorInitializers < FormatStyle::PCIS_CurrentLine && 5011 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma && 5012 Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) { 5013 return true; 5014 } 5015 if (Style.PackConstructorInitializers == FormatStyle::PCIS_NextLineOnly) { 5016 if ((Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon || 5017 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) && 5018 Right.is(TT_CtorInitializerColon)) { 5019 return true; 5020 } 5021 5022 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon && 5023 Left.is(TT_CtorInitializerColon)) { 5024 return true; 5025 } 5026 } 5027 // Break only if we have multiple inheritance. 5028 if (Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma && 5029 Right.is(TT_InheritanceComma)) { 5030 return true; 5031 } 5032 if (Style.BreakInheritanceList == FormatStyle::BILS_AfterComma && 5033 Left.is(TT_InheritanceComma)) { 5034 return true; 5035 } 5036 if (Right.is(tok::string_literal) && Right.TokenText.startswith("R\"")) { 5037 // Multiline raw string literals are special wrt. line breaks. The author 5038 // has made a deliberate choice and might have aligned the contents of the 5039 // string literal accordingly. Thus, we try keep existing line breaks. 5040 return Right.IsMultiline && Right.NewlinesBefore > 0; 5041 } 5042 if ((Left.is(tok::l_brace) || (Left.is(tok::less) && Left.Previous && 5043 Left.Previous->is(tok::equal))) && 5044 Right.NestingLevel == 1 && Style.Language == FormatStyle::LK_Proto) { 5045 // Don't put enums or option definitions onto single lines in protocol 5046 // buffers. 5047 return true; 5048 } 5049 if (Right.is(TT_InlineASMBrace)) 5050 return Right.HasUnescapedNewline; 5051 5052 if (isAllmanBrace(Left) || isAllmanBrace(Right)) { 5053 auto FirstNonComment = getFirstNonComment(Line); 5054 bool AccessSpecifier = 5055 FirstNonComment && 5056 FirstNonComment->isOneOf(Keywords.kw_internal, tok::kw_public, 5057 tok::kw_private, tok::kw_protected); 5058 5059 if (Style.BraceWrapping.AfterEnum) { 5060 if (Line.startsWith(tok::kw_enum) || 5061 Line.startsWith(tok::kw_typedef, tok::kw_enum)) { 5062 return true; 5063 } 5064 // Ensure BraceWrapping for `public enum A {`. 5065 if (AccessSpecifier && FirstNonComment->Next && 5066 FirstNonComment->Next->is(tok::kw_enum)) { 5067 return true; 5068 } 5069 } 5070 5071 // Ensure BraceWrapping for `public interface A {`. 5072 if (Style.BraceWrapping.AfterClass && 5073 ((AccessSpecifier && FirstNonComment->Next && 5074 FirstNonComment->Next->is(Keywords.kw_interface)) || 5075 Line.startsWith(Keywords.kw_interface))) { 5076 return true; 5077 } 5078 5079 // Don't attempt to interpret struct return types as structs. 5080 if (Right.isNot(TT_FunctionLBrace)) { 5081 return (Line.startsWith(tok::kw_class) && 5082 Style.BraceWrapping.AfterClass) || 5083 (Line.startsWith(tok::kw_struct) && 5084 Style.BraceWrapping.AfterStruct); 5085 } 5086 } 5087 5088 if (Left.is(TT_ObjCBlockLBrace) && 5089 Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never) { 5090 return true; 5091 } 5092 5093 // Ensure wrapping after __attribute__((XX)) and @interface etc. 5094 if (Left.is(TT_AttributeParen) && Right.is(TT_ObjCDecl)) 5095 return true; 5096 5097 if (Left.is(TT_LambdaLBrace)) { 5098 if (IsFunctionArgument(Left) && 5099 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline) { 5100 return false; 5101 } 5102 5103 if (Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_None || 5104 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline || 5105 (!Left.Children.empty() && 5106 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Empty)) { 5107 return true; 5108 } 5109 } 5110 5111 if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace) && 5112 Left.isOneOf(tok::star, tok::amp, tok::ampamp, TT_TemplateCloser)) { 5113 return true; 5114 } 5115 5116 // Put multiple Java annotation on a new line. 5117 if ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) && 5118 Left.is(TT_LeadingJavaAnnotation) && 5119 Right.isNot(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) && 5120 (Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) { 5121 return true; 5122 } 5123 5124 if (Right.is(TT_ProtoExtensionLSquare)) 5125 return true; 5126 5127 // In text proto instances if a submessage contains at least 2 entries and at 5128 // least one of them is a submessage, like A { ... B { ... } ... }, 5129 // put all of the entries of A on separate lines by forcing the selector of 5130 // the submessage B to be put on a newline. 5131 // 5132 // Example: these can stay on one line: 5133 // a { scalar_1: 1 scalar_2: 2 } 5134 // a { b { key: value } } 5135 // 5136 // and these entries need to be on a new line even if putting them all in one 5137 // line is under the column limit: 5138 // a { 5139 // scalar: 1 5140 // b { key: value } 5141 // } 5142 // 5143 // We enforce this by breaking before a submessage field that has previous 5144 // siblings, *and* breaking before a field that follows a submessage field. 5145 // 5146 // Be careful to exclude the case [proto.ext] { ... } since the `]` is 5147 // the TT_SelectorName there, but we don't want to break inside the brackets. 5148 // 5149 // Another edge case is @submessage { key: value }, which is a common 5150 // substitution placeholder. In this case we want to keep `@` and `submessage` 5151 // together. 5152 // 5153 // We ensure elsewhere that extensions are always on their own line. 5154 if ((Style.Language == FormatStyle::LK_Proto || 5155 Style.Language == FormatStyle::LK_TextProto) && 5156 Right.is(TT_SelectorName) && !Right.is(tok::r_square) && Right.Next) { 5157 // Keep `@submessage` together in: 5158 // @submessage { key: value } 5159 if (Left.is(tok::at)) 5160 return false; 5161 // Look for the scope opener after selector in cases like: 5162 // selector { ... 5163 // selector: { ... 5164 // selector: @base { ... 5165 FormatToken *LBrace = Right.Next; 5166 if (LBrace && LBrace->is(tok::colon)) { 5167 LBrace = LBrace->Next; 5168 if (LBrace && LBrace->is(tok::at)) { 5169 LBrace = LBrace->Next; 5170 if (LBrace) 5171 LBrace = LBrace->Next; 5172 } 5173 } 5174 if (LBrace && 5175 // The scope opener is one of {, [, <: 5176 // selector { ... } 5177 // selector [ ... ] 5178 // selector < ... > 5179 // 5180 // In case of selector { ... }, the l_brace is TT_DictLiteral. 5181 // In case of an empty selector {}, the l_brace is not TT_DictLiteral, 5182 // so we check for immediately following r_brace. 5183 ((LBrace->is(tok::l_brace) && 5184 (LBrace->is(TT_DictLiteral) || 5185 (LBrace->Next && LBrace->Next->is(tok::r_brace)))) || 5186 LBrace->is(TT_ArrayInitializerLSquare) || LBrace->is(tok::less))) { 5187 // If Left.ParameterCount is 0, then this submessage entry is not the 5188 // first in its parent submessage, and we want to break before this entry. 5189 // If Left.ParameterCount is greater than 0, then its parent submessage 5190 // might contain 1 or more entries and we want to break before this entry 5191 // if it contains at least 2 entries. We deal with this case later by 5192 // detecting and breaking before the next entry in the parent submessage. 5193 if (Left.ParameterCount == 0) 5194 return true; 5195 // However, if this submessage is the first entry in its parent 5196 // submessage, Left.ParameterCount might be 1 in some cases. 5197 // We deal with this case later by detecting an entry 5198 // following a closing paren of this submessage. 5199 } 5200 5201 // If this is an entry immediately following a submessage, it will be 5202 // preceded by a closing paren of that submessage, like in: 5203 // left---. .---right 5204 // v v 5205 // sub: { ... } key: value 5206 // If there was a comment between `}` an `key` above, then `key` would be 5207 // put on a new line anyways. 5208 if (Left.isOneOf(tok::r_brace, tok::greater, tok::r_square)) 5209 return true; 5210 } 5211 5212 // Deal with lambda arguments in C++ - we want consistent line breaks whether 5213 // they happen to be at arg0, arg1 or argN. The selection is a bit nuanced 5214 // as aggressive line breaks are placed when the lambda is not the last arg. 5215 if ((Style.Language == FormatStyle::LK_Cpp || 5216 Style.Language == FormatStyle::LK_ObjC) && 5217 Left.is(tok::l_paren) && Left.BlockParameterCount > 0 && 5218 !Right.isOneOf(tok::l_paren, TT_LambdaLSquare)) { 5219 // Multiple lambdas in the same function call force line breaks. 5220 if (Left.BlockParameterCount > 1) 5221 return true; 5222 5223 // A lambda followed by another arg forces a line break. 5224 if (!Left.Role) 5225 return false; 5226 auto Comma = Left.Role->lastComma(); 5227 if (!Comma) 5228 return false; 5229 auto Next = Comma->getNextNonComment(); 5230 if (!Next) 5231 return false; 5232 if (!Next->isOneOf(TT_LambdaLSquare, tok::l_brace, tok::caret)) 5233 return true; 5234 } 5235 5236 return false; 5237 } 5238 5239 bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, 5240 const FormatToken &Right) const { 5241 const FormatToken &Left = *Right.Previous; 5242 // Language-specific stuff. 5243 if (Style.isCSharp()) { 5244 if (Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) || 5245 Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon)) { 5246 return false; 5247 } 5248 // Only break after commas for generic type constraints. 5249 if (Line.First->is(TT_CSharpGenericTypeConstraint)) 5250 return Left.is(TT_CSharpGenericTypeConstraintComma); 5251 // Keep nullable operators attached to their identifiers. 5252 if (Right.is(TT_CSharpNullable)) 5253 return false; 5254 } else if (Style.Language == FormatStyle::LK_Java) { 5255 if (Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends, 5256 Keywords.kw_implements)) { 5257 return false; 5258 } 5259 if (Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends, 5260 Keywords.kw_implements)) { 5261 return true; 5262 } 5263 } else if (Style.isJavaScript()) { 5264 const FormatToken *NonComment = Right.getPreviousNonComment(); 5265 if (NonComment && 5266 NonComment->isOneOf( 5267 tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break, 5268 tok::kw_throw, Keywords.kw_interface, Keywords.kw_type, 5269 tok::kw_static, tok::kw_public, tok::kw_private, tok::kw_protected, 5270 Keywords.kw_readonly, Keywords.kw_override, Keywords.kw_abstract, 5271 Keywords.kw_get, Keywords.kw_set, Keywords.kw_async, 5272 Keywords.kw_await)) { 5273 return false; // Otherwise automatic semicolon insertion would trigger. 5274 } 5275 if (Right.NestingLevel == 0 && 5276 (Left.Tok.getIdentifierInfo() || 5277 Left.isOneOf(tok::r_square, tok::r_paren)) && 5278 Right.isOneOf(tok::l_square, tok::l_paren)) { 5279 return false; // Otherwise automatic semicolon insertion would trigger. 5280 } 5281 if (NonComment && NonComment->is(tok::identifier) && 5282 NonComment->TokenText == "asserts") { 5283 return false; 5284 } 5285 if (Left.is(TT_FatArrow) && Right.is(tok::l_brace)) 5286 return false; 5287 if (Left.is(TT_JsTypeColon)) 5288 return true; 5289 // Don't wrap between ":" and "!" of a strict prop init ("field!: type;"). 5290 if (Left.is(tok::exclaim) && Right.is(tok::colon)) 5291 return false; 5292 // Look for is type annotations like: 5293 // function f(): a is B { ... } 5294 // Do not break before is in these cases. 5295 if (Right.is(Keywords.kw_is)) { 5296 const FormatToken *Next = Right.getNextNonComment(); 5297 // If `is` is followed by a colon, it's likely that it's a dict key, so 5298 // ignore it for this check. 5299 // For example this is common in Polymer: 5300 // Polymer({ 5301 // is: 'name', 5302 // ... 5303 // }); 5304 if (!Next || !Next->is(tok::colon)) 5305 return false; 5306 } 5307 if (Left.is(Keywords.kw_in)) 5308 return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None; 5309 if (Right.is(Keywords.kw_in)) 5310 return Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None; 5311 if (Right.is(Keywords.kw_as)) 5312 return false; // must not break before as in 'x as type' casts 5313 if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) { 5314 // extends and infer can appear as keywords in conditional types: 5315 // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#conditional-types 5316 // do not break before them, as the expressions are subject to ASI. 5317 return false; 5318 } 5319 if (Left.is(Keywords.kw_as)) 5320 return true; 5321 if (Left.is(TT_NonNullAssertion)) 5322 return true; 5323 if (Left.is(Keywords.kw_declare) && 5324 Right.isOneOf(Keywords.kw_module, tok::kw_namespace, 5325 Keywords.kw_function, tok::kw_class, tok::kw_enum, 5326 Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var, 5327 Keywords.kw_let, tok::kw_const)) { 5328 // See grammar for 'declare' statements at: 5329 // https://github.com/Microsoft/TypeScript/blob/main/doc/spec-ARCHIVED.md#A.10 5330 return false; 5331 } 5332 if (Left.isOneOf(Keywords.kw_module, tok::kw_namespace) && 5333 Right.isOneOf(tok::identifier, tok::string_literal)) { 5334 return false; // must not break in "module foo { ...}" 5335 } 5336 if (Right.is(TT_TemplateString) && Right.closesScope()) 5337 return false; 5338 // Don't split tagged template literal so there is a break between the tag 5339 // identifier and template string. 5340 if (Left.is(tok::identifier) && Right.is(TT_TemplateString)) 5341 return false; 5342 if (Left.is(TT_TemplateString) && Left.opensScope()) 5343 return true; 5344 } 5345 5346 if (Left.is(tok::at)) 5347 return false; 5348 if (Left.Tok.getObjCKeywordID() == tok::objc_interface) 5349 return false; 5350 if (Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation)) 5351 return !Right.is(tok::l_paren); 5352 if (Right.is(TT_PointerOrReference)) { 5353 return Line.IsMultiVariableDeclStmt || 5354 (getTokenPointerOrReferenceAlignment(Right) == 5355 FormatStyle::PAS_Right && 5356 (!Right.Next || Right.Next->isNot(TT_FunctionDeclarationName))); 5357 } 5358 if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) || 5359 Right.is(tok::kw_operator)) { 5360 return true; 5361 } 5362 if (Left.is(TT_PointerOrReference)) 5363 return false; 5364 if (Right.isTrailingComment()) { 5365 // We rely on MustBreakBefore being set correctly here as we should not 5366 // change the "binding" behavior of a comment. 5367 // The first comment in a braced lists is always interpreted as belonging to 5368 // the first list element. Otherwise, it should be placed outside of the 5369 // list. 5370 return Left.is(BK_BracedInit) || 5371 (Left.is(TT_CtorInitializerColon) && Right.NewlinesBefore > 0 && 5372 Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon); 5373 } 5374 if (Left.is(tok::question) && Right.is(tok::colon)) 5375 return false; 5376 if (Right.is(TT_ConditionalExpr) || Right.is(tok::question)) 5377 return Style.BreakBeforeTernaryOperators; 5378 if (Left.is(TT_ConditionalExpr) || Left.is(tok::question)) 5379 return !Style.BreakBeforeTernaryOperators; 5380 if (Left.is(TT_InheritanceColon)) 5381 return Style.BreakInheritanceList == FormatStyle::BILS_AfterColon; 5382 if (Right.is(TT_InheritanceColon)) 5383 return Style.BreakInheritanceList != FormatStyle::BILS_AfterColon; 5384 if (Right.is(TT_ObjCMethodExpr) && !Right.is(tok::r_square) && 5385 Left.isNot(TT_SelectorName)) { 5386 return true; 5387 } 5388 5389 if (Right.is(tok::colon) && 5390 !Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon)) { 5391 return false; 5392 } 5393 if (Left.is(tok::colon) && Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) { 5394 if (Style.Language == FormatStyle::LK_Proto || 5395 Style.Language == FormatStyle::LK_TextProto) { 5396 if (!Style.AlwaysBreakBeforeMultilineStrings && Right.isStringLiteral()) 5397 return false; 5398 // Prevent cases like: 5399 // 5400 // submessage: 5401 // { key: valueeeeeeeeeeee } 5402 // 5403 // when the snippet does not fit into one line. 5404 // Prefer: 5405 // 5406 // submessage: { 5407 // key: valueeeeeeeeeeee 5408 // } 5409 // 5410 // instead, even if it is longer by one line. 5411 // 5412 // Note that this allows the "{" to go over the column limit 5413 // when the column limit is just between ":" and "{", but that does 5414 // not happen too often and alternative formattings in this case are 5415 // not much better. 5416 // 5417 // The code covers the cases: 5418 // 5419 // submessage: { ... } 5420 // submessage: < ... > 5421 // repeated: [ ... ] 5422 if (((Right.is(tok::l_brace) || Right.is(tok::less)) && 5423 Right.is(TT_DictLiteral)) || 5424 Right.is(TT_ArrayInitializerLSquare)) { 5425 return false; 5426 } 5427 } 5428 return true; 5429 } 5430 if (Right.is(tok::r_square) && Right.MatchingParen && 5431 Right.MatchingParen->is(TT_ProtoExtensionLSquare)) { 5432 return false; 5433 } 5434 if (Right.is(TT_SelectorName) || (Right.is(tok::identifier) && Right.Next && 5435 Right.Next->is(TT_ObjCMethodExpr))) { 5436 return Left.isNot(tok::period); // FIXME: Properly parse ObjC calls. 5437 } 5438 if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty) 5439 return true; 5440 if (Right.is(tok::kw_concept)) 5441 return Style.BreakBeforeConceptDeclarations != FormatStyle::BBCDS_Never; 5442 if (Right.is(TT_RequiresClause)) 5443 return true; 5444 if (Left.ClosesTemplateDeclaration || Left.is(TT_FunctionAnnotationRParen)) 5445 return true; 5446 if (Left.ClosesRequiresClause) 5447 return true; 5448 if (Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen, 5449 TT_OverloadedOperator)) { 5450 return false; 5451 } 5452 if (Left.is(TT_RangeBasedForLoopColon)) 5453 return true; 5454 if (Right.is(TT_RangeBasedForLoopColon)) 5455 return false; 5456 if (Left.is(TT_TemplateCloser) && Right.is(TT_TemplateOpener)) 5457 return true; 5458 if ((Left.is(tok::greater) && Right.is(tok::greater)) || 5459 (Left.is(tok::less) && Right.is(tok::less))) { 5460 return false; 5461 } 5462 if (Right.is(TT_BinaryOperator) && 5463 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None && 5464 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All || 5465 Right.getPrecedence() != prec::Assignment)) { 5466 return true; 5467 } 5468 if (Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator) || 5469 Left.is(tok::kw_operator)) { 5470 return false; 5471 } 5472 if (Left.is(tok::equal) && !Right.isOneOf(tok::kw_default, tok::kw_delete) && 5473 Line.Type == LT_VirtualFunctionDecl && Left.NestingLevel == 0) { 5474 return false; 5475 } 5476 if (Left.is(tok::equal) && Right.is(tok::l_brace) && 5477 !Style.Cpp11BracedListStyle) { 5478 return false; 5479 } 5480 if (Left.is(tok::l_paren) && 5481 Left.isOneOf(TT_AttributeParen, TT_TypeDeclarationParen)) { 5482 return false; 5483 } 5484 if (Left.is(tok::l_paren) && Left.Previous && 5485 (Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen))) { 5486 return false; 5487 } 5488 if (Right.is(TT_ImplicitStringLiteral)) 5489 return false; 5490 5491 if (Right.is(TT_TemplateCloser)) 5492 return false; 5493 if (Right.is(tok::r_square) && Right.MatchingParen && 5494 Right.MatchingParen->is(TT_LambdaLSquare)) { 5495 return false; 5496 } 5497 5498 // We only break before r_brace if there was a corresponding break before 5499 // the l_brace, which is tracked by BreakBeforeClosingBrace. 5500 if (Right.is(tok::r_brace)) { 5501 return Right.MatchingParen && (Right.MatchingParen->is(BK_Block) || 5502 (Right.isBlockIndentedInitRBrace(Style))); 5503 } 5504 5505 // We only break before r_paren if we're in a block indented context. 5506 if (Right.is(tok::r_paren)) { 5507 if (Style.AlignAfterOpenBracket != FormatStyle::BAS_BlockIndent || 5508 !Right.MatchingParen) { 5509 return false; 5510 } 5511 auto Next = Right.Next; 5512 if (Next && Next->is(tok::r_paren)) 5513 Next = Next->Next; 5514 if (Next && Next->is(tok::l_paren)) 5515 return false; 5516 const FormatToken *Previous = Right.MatchingParen->Previous; 5517 return !(Previous && (Previous->is(tok::kw_for) || Previous->isIf())); 5518 } 5519 5520 // Allow breaking after a trailing annotation, e.g. after a method 5521 // declaration. 5522 if (Left.is(TT_TrailingAnnotation)) { 5523 return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren, 5524 tok::less, tok::coloncolon); 5525 } 5526 5527 if (Right.is(tok::kw___attribute) || 5528 (Right.is(tok::l_square) && Right.is(TT_AttributeSquare))) { 5529 return !Left.is(TT_AttributeSquare); 5530 } 5531 5532 if (Left.is(tok::identifier) && Right.is(tok::string_literal)) 5533 return true; 5534 5535 if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral)) 5536 return true; 5537 5538 if (Left.is(TT_CtorInitializerColon)) { 5539 return Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon && 5540 (!Right.isTrailingComment() || Right.NewlinesBefore > 0); 5541 } 5542 if (Right.is(TT_CtorInitializerColon)) 5543 return Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon; 5544 if (Left.is(TT_CtorInitializerComma) && 5545 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) { 5546 return false; 5547 } 5548 if (Right.is(TT_CtorInitializerComma) && 5549 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) { 5550 return true; 5551 } 5552 if (Left.is(TT_InheritanceComma) && 5553 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) { 5554 return false; 5555 } 5556 if (Right.is(TT_InheritanceComma) && 5557 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) { 5558 return true; 5559 } 5560 if (Left.is(TT_ArrayInitializerLSquare)) 5561 return true; 5562 if (Right.is(tok::kw_typename) && Left.isNot(tok::kw_const)) 5563 return true; 5564 if ((Left.isBinaryOperator() || Left.is(TT_BinaryOperator)) && 5565 !Left.isOneOf(tok::arrowstar, tok::lessless) && 5566 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All && 5567 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None || 5568 Left.getPrecedence() == prec::Assignment)) { 5569 return true; 5570 } 5571 if ((Left.is(TT_AttributeSquare) && Right.is(tok::l_square)) || 5572 (Left.is(tok::r_square) && Right.is(TT_AttributeSquare))) { 5573 return false; 5574 } 5575 5576 auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine; 5577 if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace)) { 5578 if (isAllmanLambdaBrace(Left)) 5579 return !isItAnEmptyLambdaAllowed(Left, ShortLambdaOption); 5580 if (isAllmanLambdaBrace(Right)) 5581 return !isItAnEmptyLambdaAllowed(Right, ShortLambdaOption); 5582 } 5583 5584 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace, 5585 tok::kw_class, tok::kw_struct, tok::comment) || 5586 Right.isMemberAccess() || 5587 Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless, 5588 tok::colon, tok::l_square, tok::at) || 5589 (Left.is(tok::r_paren) && 5590 Right.isOneOf(tok::identifier, tok::kw_const)) || 5591 (Left.is(tok::l_paren) && !Right.is(tok::r_paren)) || 5592 (Left.is(TT_TemplateOpener) && !Right.is(TT_TemplateCloser)); 5593 } 5594 5595 void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) const { 5596 llvm::errs() << "AnnotatedTokens(L=" << Line.Level << ", P=" << Line.PPLevel 5597 << ", T=" << Line.Type << ", C=" << Line.IsContinuation 5598 << "):\n"; 5599 const FormatToken *Tok = Line.First; 5600 while (Tok) { 5601 llvm::errs() << " M=" << Tok->MustBreakBefore 5602 << " C=" << Tok->CanBreakBefore 5603 << " T=" << getTokenTypeName(Tok->getType()) 5604 << " S=" << Tok->SpacesRequiredBefore 5605 << " F=" << Tok->Finalized << " B=" << Tok->BlockParameterCount 5606 << " BK=" << Tok->getBlockKind() << " P=" << Tok->SplitPenalty 5607 << " Name=" << Tok->Tok.getName() << " L=" << Tok->TotalLength 5608 << " PPK=" << Tok->getPackingKind() << " FakeLParens="; 5609 for (prec::Level LParen : Tok->FakeLParens) 5610 llvm::errs() << LParen << "/"; 5611 llvm::errs() << " FakeRParens=" << Tok->FakeRParens; 5612 llvm::errs() << " II=" << Tok->Tok.getIdentifierInfo(); 5613 llvm::errs() << " Text='" << Tok->TokenText << "'\n"; 5614 if (!Tok->Next) 5615 assert(Tok == Line.Last); 5616 Tok = Tok->Next; 5617 } 5618 llvm::errs() << "----\n"; 5619 } 5620 5621 FormatStyle::PointerAlignmentStyle 5622 TokenAnnotator::getTokenReferenceAlignment(const FormatToken &Reference) const { 5623 assert(Reference.isOneOf(tok::amp, tok::ampamp)); 5624 switch (Style.ReferenceAlignment) { 5625 case FormatStyle::RAS_Pointer: 5626 return Style.PointerAlignment; 5627 case FormatStyle::RAS_Left: 5628 return FormatStyle::PAS_Left; 5629 case FormatStyle::RAS_Right: 5630 return FormatStyle::PAS_Right; 5631 case FormatStyle::RAS_Middle: 5632 return FormatStyle::PAS_Middle; 5633 } 5634 assert(0); //"Unhandled value of ReferenceAlignment" 5635 return Style.PointerAlignment; 5636 } 5637 5638 FormatStyle::PointerAlignmentStyle 5639 TokenAnnotator::getTokenPointerOrReferenceAlignment( 5640 const FormatToken &PointerOrReference) const { 5641 if (PointerOrReference.isOneOf(tok::amp, tok::ampamp)) { 5642 switch (Style.ReferenceAlignment) { 5643 case FormatStyle::RAS_Pointer: 5644 return Style.PointerAlignment; 5645 case FormatStyle::RAS_Left: 5646 return FormatStyle::PAS_Left; 5647 case FormatStyle::RAS_Right: 5648 return FormatStyle::PAS_Right; 5649 case FormatStyle::RAS_Middle: 5650 return FormatStyle::PAS_Middle; 5651 } 5652 } 5653 assert(PointerOrReference.is(tok::star)); 5654 return Style.PointerAlignment; 5655 } 5656 5657 } // namespace format 5658 } // namespace clang 5659