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 token can be used as an identifier in 30 /// an Objective-C \c \@selector, \c false otherwise. 31 /// 32 /// Because getFormattingLangOpts() always lexes source code as 33 /// Objective-C++, C++ keywords like \c new and \c delete are 34 /// lexed as tok::kw_*, not tok::identifier, even for Objective-C. 35 /// 36 /// For Objective-C and Objective-C++, both identifiers and keywords 37 /// are valid inside @selector(...) (or a macro which 38 /// invokes @selector(...)). So, we allow treat any identifier or 39 /// keyword as a potential Objective-C selector component. 40 static bool canBeObjCSelectorComponent(const FormatToken &Tok) { 41 return Tok.Tok.getIdentifierInfo() != nullptr; 42 } 43 44 /// With `Left` being '(', check if we're at either `[...](` or 45 /// `[...]<...>(`, where the [ opens a lambda capture list. 46 static bool isLambdaParameterList(const FormatToken *Left) { 47 // Skip <...> if present. 48 if (Left->Previous && Left->Previous->is(tok::greater) && 49 Left->Previous->MatchingParen && 50 Left->Previous->MatchingParen->is(TT_TemplateOpener)) 51 Left = Left->Previous->MatchingParen; 52 53 // Check for `[...]`. 54 return Left->Previous && Left->Previous->is(tok::r_square) && 55 Left->Previous->MatchingParen && 56 Left->Previous->MatchingParen->is(TT_LambdaLSquare); 57 } 58 59 /// Returns \c true if the token is followed by a boolean condition, \c false 60 /// otherwise. 61 static bool isKeywordWithCondition(const FormatToken &Tok) { 62 return Tok.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch, 63 tok::kw_constexpr, tok::kw_catch); 64 } 65 66 /// A parser that gathers additional information about tokens. 67 /// 68 /// The \c TokenAnnotator tries to match parenthesis and square brakets and 69 /// store a parenthesis levels. It also tries to resolve matching "<" and ">" 70 /// into template parameter lists. 71 class AnnotatingParser { 72 public: 73 AnnotatingParser(const FormatStyle &Style, AnnotatedLine &Line, 74 const AdditionalKeywords &Keywords) 75 : Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(false), 76 Keywords(Keywords) { 77 Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false)); 78 resetTokenMetadata(CurrentToken); 79 } 80 81 private: 82 bool parseAngle() { 83 if (!CurrentToken || !CurrentToken->Previous) 84 return false; 85 if (NonTemplateLess.count(CurrentToken->Previous)) 86 return false; 87 88 const FormatToken &Previous = *CurrentToken->Previous; // The '<'. 89 if (Previous.Previous) { 90 if (Previous.Previous->Tok.isLiteral()) 91 return false; 92 if (Previous.Previous->is(tok::r_paren) && Contexts.size() > 1 && 93 (!Previous.Previous->MatchingParen || 94 !Previous.Previous->MatchingParen->is(TT_OverloadedOperatorLParen))) 95 return false; 96 } 97 98 FormatToken *Left = CurrentToken->Previous; 99 Left->ParentBracket = Contexts.back().ContextKind; 100 ScopedContextCreator ContextCreator(*this, tok::less, 12); 101 102 // If this angle is in the context of an expression, we need to be more 103 // hesitant to detect it as opening template parameters. 104 bool InExprContext = Contexts.back().IsExpression; 105 106 Contexts.back().IsExpression = false; 107 // If there's a template keyword before the opening angle bracket, this is a 108 // template parameter, not an argument. 109 Contexts.back().InTemplateArgument = 110 Left->Previous && Left->Previous->Tok.isNot(tok::kw_template); 111 112 if (Style.Language == FormatStyle::LK_Java && 113 CurrentToken->is(tok::question)) 114 next(); 115 116 while (CurrentToken) { 117 if (CurrentToken->is(tok::greater)) { 118 // Try to do a better job at looking for ">>" within the condition of 119 // a statement. 120 if (CurrentToken->Next && CurrentToken->Next->is(tok::greater) && 121 Left->ParentBracket != tok::less && 122 isKeywordWithCondition(*Line.First)) 123 return false; 124 Left->MatchingParen = CurrentToken; 125 CurrentToken->MatchingParen = Left; 126 // In TT_Proto, we must distignuish between: 127 // map<key, value> 128 // msg < item: data > 129 // msg: < item: data > 130 // In TT_TextProto, map<key, value> does not occur. 131 if (Style.Language == FormatStyle::LK_TextProto || 132 (Style.Language == FormatStyle::LK_Proto && Left->Previous && 133 Left->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) 134 CurrentToken->setType(TT_DictLiteral); 135 else 136 CurrentToken->setType(TT_TemplateCloser); 137 next(); 138 return true; 139 } 140 if (CurrentToken->is(tok::question) && 141 Style.Language == FormatStyle::LK_Java) { 142 next(); 143 continue; 144 } 145 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace) || 146 (CurrentToken->isOneOf(tok::colon, tok::question) && InExprContext && 147 !Style.isCSharp() && Style.Language != FormatStyle::LK_Proto && 148 Style.Language != FormatStyle::LK_TextProto)) 149 return false; 150 // If a && or || is found and interpreted as a binary operator, this set 151 // of angles is likely part of something like "a < b && c > d". If the 152 // angles are inside an expression, the ||/&& might also be a binary 153 // operator that was misinterpreted because we are parsing template 154 // parameters. 155 // FIXME: This is getting out of hand, write a decent parser. 156 if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) && 157 CurrentToken->Previous->is(TT_BinaryOperator) && 158 Contexts[Contexts.size() - 2].IsExpression && 159 !Line.startsWith(tok::kw_template)) 160 return false; 161 updateParameterCount(Left, CurrentToken); 162 if (Style.Language == FormatStyle::LK_Proto) { 163 if (FormatToken *Previous = CurrentToken->getPreviousNonComment()) { 164 if (CurrentToken->is(tok::colon) || 165 (CurrentToken->isOneOf(tok::l_brace, tok::less) && 166 Previous->isNot(tok::colon))) 167 Previous->setType(TT_SelectorName); 168 } 169 } 170 if (!consumeToken()) 171 return false; 172 } 173 return false; 174 } 175 176 bool parseUntouchableParens() { 177 while (CurrentToken) { 178 CurrentToken->Finalized = true; 179 switch (CurrentToken->Tok.getKind()) { 180 case tok::l_paren: 181 next(); 182 if (!parseUntouchableParens()) 183 return false; 184 continue; 185 case tok::r_paren: 186 next(); 187 return true; 188 default: 189 // no-op 190 break; 191 } 192 next(); 193 } 194 return false; 195 } 196 197 bool parseParens(bool LookForDecls = false) { 198 if (!CurrentToken) 199 return false; 200 FormatToken *Left = CurrentToken->Previous; 201 assert(Left && "Unknown previous token"); 202 FormatToken *PrevNonComment = Left->getPreviousNonComment(); 203 Left->ParentBracket = Contexts.back().ContextKind; 204 ScopedContextCreator ContextCreator(*this, tok::l_paren, 1); 205 206 // FIXME: This is a bit of a hack. Do better. 207 Contexts.back().ColonIsForRangeExpr = 208 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr; 209 210 if (Left->Previous && Left->Previous->is(TT_UntouchableMacroFunc)) { 211 Left->Finalized = true; 212 return parseUntouchableParens(); 213 } 214 215 bool StartsObjCMethodExpr = false; 216 if (FormatToken *MaybeSel = Left->Previous) { 217 // @selector( starts a selector. 218 if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && MaybeSel->Previous && 219 MaybeSel->Previous->is(tok::at)) { 220 StartsObjCMethodExpr = true; 221 } 222 } 223 224 if (Left->is(TT_OverloadedOperatorLParen)) { 225 Contexts.back().IsExpression = false; 226 } else if (Style.Language == FormatStyle::LK_JavaScript && 227 (Line.startsWith(Keywords.kw_type, tok::identifier) || 228 Line.startsWith(tok::kw_export, Keywords.kw_type, 229 tok::identifier))) { 230 // type X = (...); 231 // export type X = (...); 232 Contexts.back().IsExpression = false; 233 } else if (Left->Previous && 234 (Left->Previous->isOneOf(tok::kw_static_assert, tok::kw_while, 235 tok::l_paren, tok::comma) || 236 Left->Previous->isIf() || 237 Left->Previous->is(TT_BinaryOperator))) { 238 // static_assert, if and while usually contain expressions. 239 Contexts.back().IsExpression = true; 240 } else if (Style.Language == FormatStyle::LK_JavaScript && Left->Previous && 241 (Left->Previous->is(Keywords.kw_function) || 242 (Left->Previous->endsSequence(tok::identifier, 243 Keywords.kw_function)))) { 244 // function(...) or function f(...) 245 Contexts.back().IsExpression = false; 246 } else if (Style.Language == FormatStyle::LK_JavaScript && Left->Previous && 247 Left->Previous->is(TT_JsTypeColon)) { 248 // let x: (SomeType); 249 Contexts.back().IsExpression = false; 250 } else if (isLambdaParameterList(Left)) { 251 // This is a parameter list of a lambda expression. 252 Contexts.back().IsExpression = false; 253 } else if (Line.InPPDirective && 254 (!Left->Previous || !Left->Previous->is(tok::identifier))) { 255 Contexts.back().IsExpression = true; 256 } else if (Contexts[Contexts.size() - 2].CaretFound) { 257 // This is the parameter list of an ObjC block. 258 Contexts.back().IsExpression = false; 259 } else if (Left->Previous && Left->Previous->is(TT_ForEachMacro)) { 260 // The first argument to a foreach macro is a declaration. 261 Contexts.back().IsForEachMacro = true; 262 Contexts.back().IsExpression = false; 263 } else if (Left->Previous && Left->Previous->MatchingParen && 264 Left->Previous->MatchingParen->is(TT_ObjCBlockLParen)) { 265 Contexts.back().IsExpression = false; 266 } else if (!Line.MustBeDeclaration && !Line.InPPDirective) { 267 bool IsForOrCatch = 268 Left->Previous && Left->Previous->isOneOf(tok::kw_for, tok::kw_catch); 269 Contexts.back().IsExpression = !IsForOrCatch; 270 } 271 272 // Infer the role of the l_paren based on the previous token if we haven't 273 // detected one one yet. 274 if (PrevNonComment && Left->is(TT_Unknown)) { 275 if (PrevNonComment->is(tok::kw___attribute)) { 276 Left->setType(TT_AttributeParen); 277 } else if (PrevNonComment->isOneOf(TT_TypenameMacro, tok::kw_decltype, 278 tok::kw_typeof, tok::kw__Atomic, 279 tok::kw___underlying_type)) { 280 Left->setType(TT_TypeDeclarationParen); 281 // decltype() and typeof() usually contain expressions. 282 if (PrevNonComment->isOneOf(tok::kw_decltype, tok::kw_typeof)) 283 Contexts.back().IsExpression = true; 284 } 285 } 286 287 if (StartsObjCMethodExpr) { 288 Contexts.back().ColonIsObjCMethodExpr = true; 289 Left->setType(TT_ObjCMethodExpr); 290 } 291 292 // MightBeFunctionType and ProbablyFunctionType are used for 293 // function pointer and reference types as well as Objective-C 294 // block types: 295 // 296 // void (*FunctionPointer)(void); 297 // void (&FunctionReference)(void); 298 // void (^ObjCBlock)(void); 299 bool MightBeFunctionType = !Contexts[Contexts.size() - 2].IsExpression; 300 bool ProbablyFunctionType = 301 CurrentToken->isOneOf(tok::star, tok::amp, tok::caret); 302 bool HasMultipleLines = false; 303 bool HasMultipleParametersOnALine = false; 304 bool MightBeObjCForRangeLoop = 305 Left->Previous && Left->Previous->is(tok::kw_for); 306 FormatToken *PossibleObjCForInToken = nullptr; 307 while (CurrentToken) { 308 // LookForDecls is set when "if (" has been seen. Check for 309 // 'identifier' '*' 'identifier' followed by not '=' -- this 310 // '*' has to be a binary operator but determineStarAmpUsage() will 311 // categorize it as an unary operator, so set the right type here. 312 if (LookForDecls && CurrentToken->Next) { 313 FormatToken *Prev = CurrentToken->getPreviousNonComment(); 314 if (Prev) { 315 FormatToken *PrevPrev = Prev->getPreviousNonComment(); 316 FormatToken *Next = CurrentToken->Next; 317 if (PrevPrev && PrevPrev->is(tok::identifier) && 318 Prev->isOneOf(tok::star, tok::amp, tok::ampamp) && 319 CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) { 320 Prev->setType(TT_BinaryOperator); 321 LookForDecls = false; 322 } 323 } 324 } 325 326 if (CurrentToken->Previous->is(TT_PointerOrReference) && 327 CurrentToken->Previous->Previous->isOneOf(tok::l_paren, 328 tok::coloncolon)) 329 ProbablyFunctionType = true; 330 if (CurrentToken->is(tok::comma)) 331 MightBeFunctionType = false; 332 if (CurrentToken->Previous->is(TT_BinaryOperator)) 333 Contexts.back().IsExpression = true; 334 if (CurrentToken->is(tok::r_paren)) { 335 if (MightBeFunctionType && ProbablyFunctionType && CurrentToken->Next && 336 (CurrentToken->Next->is(tok::l_paren) || 337 (CurrentToken->Next->is(tok::l_square) && Line.MustBeDeclaration))) 338 Left->setType(Left->Next->is(tok::caret) ? TT_ObjCBlockLParen 339 : TT_FunctionTypeLParen); 340 Left->MatchingParen = CurrentToken; 341 CurrentToken->MatchingParen = Left; 342 343 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_brace) && 344 Left->Previous && Left->Previous->is(tok::l_paren)) { 345 // Detect the case where macros are used to generate lambdas or 346 // function bodies, e.g.: 347 // auto my_lambda = MARCO((Type *type, int i) { .. body .. }); 348 for (FormatToken *Tok = Left; Tok != CurrentToken; Tok = Tok->Next) { 349 if (Tok->is(TT_BinaryOperator) && 350 Tok->isOneOf(tok::star, tok::amp, tok::ampamp)) 351 Tok->setType(TT_PointerOrReference); 352 } 353 } 354 355 if (StartsObjCMethodExpr) { 356 CurrentToken->setType(TT_ObjCMethodExpr); 357 if (Contexts.back().FirstObjCSelectorName) { 358 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 359 Contexts.back().LongestObjCSelectorName; 360 } 361 } 362 363 if (Left->is(TT_AttributeParen)) 364 CurrentToken->setType(TT_AttributeParen); 365 if (Left->is(TT_TypeDeclarationParen)) 366 CurrentToken->setType(TT_TypeDeclarationParen); 367 if (Left->Previous && Left->Previous->is(TT_JavaAnnotation)) 368 CurrentToken->setType(TT_JavaAnnotation); 369 if (Left->Previous && Left->Previous->is(TT_LeadingJavaAnnotation)) 370 CurrentToken->setType(TT_LeadingJavaAnnotation); 371 if (Left->Previous && Left->Previous->is(TT_AttributeSquare)) 372 CurrentToken->setType(TT_AttributeSquare); 373 374 if (!HasMultipleLines) 375 Left->setPackingKind(PPK_Inconclusive); 376 else if (HasMultipleParametersOnALine) 377 Left->setPackingKind(PPK_BinPacked); 378 else 379 Left->setPackingKind(PPK_OnePerLine); 380 381 next(); 382 return true; 383 } 384 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace)) 385 return false; 386 387 if (CurrentToken->is(tok::l_brace)) 388 Left->setType(TT_Unknown); // Not TT_ObjCBlockLParen 389 if (CurrentToken->is(tok::comma) && CurrentToken->Next && 390 !CurrentToken->Next->HasUnescapedNewline && 391 !CurrentToken->Next->isTrailingComment()) 392 HasMultipleParametersOnALine = true; 393 if ((CurrentToken->Previous->isOneOf(tok::kw_const, tok::kw_auto) || 394 CurrentToken->Previous->isSimpleTypeSpecifier()) && 395 !CurrentToken->is(tok::l_brace)) 396 Contexts.back().IsExpression = false; 397 if (CurrentToken->isOneOf(tok::semi, tok::colon)) { 398 MightBeObjCForRangeLoop = false; 399 if (PossibleObjCForInToken) { 400 PossibleObjCForInToken->setType(TT_Unknown); 401 PossibleObjCForInToken = nullptr; 402 } 403 } 404 if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in)) { 405 PossibleObjCForInToken = CurrentToken; 406 PossibleObjCForInToken->setType(TT_ObjCForIn); 407 } 408 // When we discover a 'new', we set CanBeExpression to 'false' in order to 409 // parse the type correctly. Reset that after a comma. 410 if (CurrentToken->is(tok::comma)) 411 Contexts.back().CanBeExpression = true; 412 413 FormatToken *Tok = CurrentToken; 414 if (!consumeToken()) 415 return false; 416 updateParameterCount(Left, Tok); 417 if (CurrentToken && CurrentToken->HasUnescapedNewline) 418 HasMultipleLines = true; 419 } 420 return false; 421 } 422 423 bool isCSharpAttributeSpecifier(const FormatToken &Tok) { 424 if (!Style.isCSharp()) 425 return false; 426 427 // `identifier[i]` is not an attribute. 428 if (Tok.Previous && Tok.Previous->is(tok::identifier)) 429 return false; 430 431 // Chains of [] in `identifier[i][j][k]` are not attributes. 432 if (Tok.Previous && Tok.Previous->is(tok::r_square)) { 433 auto *MatchingParen = Tok.Previous->MatchingParen; 434 if (!MatchingParen || MatchingParen->is(TT_ArraySubscriptLSquare)) 435 return false; 436 } 437 438 const FormatToken *AttrTok = Tok.Next; 439 if (!AttrTok) 440 return false; 441 442 // Just an empty declaration e.g. string []. 443 if (AttrTok->is(tok::r_square)) 444 return false; 445 446 // Move along the tokens inbetween the '[' and ']' e.g. [STAThread]. 447 while (AttrTok && AttrTok->isNot(tok::r_square)) { 448 AttrTok = AttrTok->Next; 449 } 450 451 if (!AttrTok) 452 return false; 453 454 // Allow an attribute to be the only content of a file. 455 AttrTok = AttrTok->Next; 456 if (!AttrTok) 457 return true; 458 459 // Limit this to being an access modifier that follows. 460 if (AttrTok->isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected, 461 tok::comment, tok::kw_class, tok::kw_static, 462 tok::l_square, Keywords.kw_internal)) { 463 return true; 464 } 465 466 // incase its a [XXX] retval func(.... 467 if (AttrTok->Next && 468 AttrTok->Next->startsSequence(tok::identifier, tok::l_paren)) 469 return true; 470 471 return false; 472 } 473 474 bool isCpp11AttributeSpecifier(const FormatToken &Tok) { 475 if (!Style.isCpp() || !Tok.startsSequence(tok::l_square, tok::l_square)) 476 return false; 477 // The first square bracket is part of an ObjC array literal 478 if (Tok.Previous && Tok.Previous->is(tok::at)) { 479 return false; 480 } 481 const FormatToken *AttrTok = Tok.Next->Next; 482 if (!AttrTok) 483 return false; 484 // C++17 '[[using ns: foo, bar(baz, blech)]]' 485 // We assume nobody will name an ObjC variable 'using'. 486 if (AttrTok->startsSequence(tok::kw_using, tok::identifier, tok::colon)) 487 return true; 488 if (AttrTok->isNot(tok::identifier)) 489 return false; 490 while (AttrTok && !AttrTok->startsSequence(tok::r_square, tok::r_square)) { 491 // ObjC message send. We assume nobody will use : in a C++11 attribute 492 // specifier parameter, although this is technically valid: 493 // [[foo(:)]]. 494 if (AttrTok->is(tok::colon) || 495 AttrTok->startsSequence(tok::identifier, tok::identifier) || 496 AttrTok->startsSequence(tok::r_paren, tok::identifier)) 497 return false; 498 if (AttrTok->is(tok::ellipsis)) 499 return true; 500 AttrTok = AttrTok->Next; 501 } 502 return AttrTok && AttrTok->startsSequence(tok::r_square, tok::r_square); 503 } 504 505 bool parseSquare() { 506 if (!CurrentToken) 507 return false; 508 509 // A '[' could be an index subscript (after an identifier or after 510 // ')' or ']'), it could be the start of an Objective-C method 511 // expression, it could the start of an Objective-C array literal, 512 // or it could be a C++ attribute specifier [[foo::bar]]. 513 FormatToken *Left = CurrentToken->Previous; 514 Left->ParentBracket = Contexts.back().ContextKind; 515 FormatToken *Parent = Left->getPreviousNonComment(); 516 517 // Cases where '>' is followed by '['. 518 // In C++, this can happen either in array of templates (foo<int>[10]) 519 // or when array is a nested template type (unique_ptr<type1<type2>[]>). 520 bool CppArrayTemplates = 521 Style.isCpp() && Parent && Parent->is(TT_TemplateCloser) && 522 (Contexts.back().CanBeExpression || Contexts.back().IsExpression || 523 Contexts.back().InTemplateArgument); 524 525 bool IsCpp11AttributeSpecifier = isCpp11AttributeSpecifier(*Left) || 526 Contexts.back().InCpp11AttributeSpecifier; 527 528 // Treat C# Attributes [STAThread] much like C++ attributes [[...]]. 529 bool IsCSharpAttributeSpecifier = 530 isCSharpAttributeSpecifier(*Left) || 531 Contexts.back().InCSharpAttributeSpecifier; 532 533 bool InsideInlineASM = Line.startsWith(tok::kw_asm); 534 bool IsCppStructuredBinding = Left->isCppStructuredBinding(Style); 535 bool StartsObjCMethodExpr = 536 !IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates && 537 Style.isCpp() && !IsCpp11AttributeSpecifier && 538 !IsCSharpAttributeSpecifier && Contexts.back().CanBeExpression && 539 Left->isNot(TT_LambdaLSquare) && 540 !CurrentToken->isOneOf(tok::l_brace, tok::r_square) && 541 (!Parent || 542 Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren, 543 tok::kw_return, tok::kw_throw) || 544 Parent->isUnaryOperator() || 545 // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen. 546 Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) || 547 (getBinOpPrecedence(Parent->Tok.getKind(), true, true) > 548 prec::Unknown)); 549 bool ColonFound = false; 550 551 unsigned BindingIncrease = 1; 552 if (IsCppStructuredBinding) { 553 Left->setType(TT_StructuredBindingLSquare); 554 } else if (Left->is(TT_Unknown)) { 555 if (StartsObjCMethodExpr) { 556 Left->setType(TT_ObjCMethodExpr); 557 } else if (InsideInlineASM) { 558 Left->setType(TT_InlineASMSymbolicNameLSquare); 559 } else if (IsCpp11AttributeSpecifier) { 560 Left->setType(TT_AttributeSquare); 561 } else if (Style.Language == FormatStyle::LK_JavaScript && Parent && 562 Contexts.back().ContextKind == tok::l_brace && 563 Parent->isOneOf(tok::l_brace, tok::comma)) { 564 Left->setType(TT_JsComputedPropertyName); 565 } else if (Style.isCpp() && Contexts.back().ContextKind == tok::l_brace && 566 Parent && Parent->isOneOf(tok::l_brace, tok::comma)) { 567 Left->setType(TT_DesignatedInitializerLSquare); 568 } else if (IsCSharpAttributeSpecifier) { 569 Left->setType(TT_AttributeSquare); 570 } else if (CurrentToken->is(tok::r_square) && Parent && 571 Parent->is(TT_TemplateCloser)) { 572 Left->setType(TT_ArraySubscriptLSquare); 573 } else if (Style.Language == FormatStyle::LK_Proto || 574 Style.Language == FormatStyle::LK_TextProto) { 575 // Square braces in LK_Proto can either be message field attributes: 576 // 577 // optional Aaa aaa = 1 [ 578 // (aaa) = aaa 579 // ]; 580 // 581 // extensions 123 [ 582 // (aaa) = aaa 583 // ]; 584 // 585 // or text proto extensions (in options): 586 // 587 // option (Aaa.options) = { 588 // [type.type/type] { 589 // key: value 590 // } 591 // } 592 // 593 // or repeated fields (in options): 594 // 595 // option (Aaa.options) = { 596 // keys: [ 1, 2, 3 ] 597 // } 598 // 599 // In the first and the third case we want to spread the contents inside 600 // the square braces; in the second we want to keep them inline. 601 Left->setType(TT_ArrayInitializerLSquare); 602 if (!Left->endsSequence(tok::l_square, tok::numeric_constant, 603 tok::equal) && 604 !Left->endsSequence(tok::l_square, tok::numeric_constant, 605 tok::identifier) && 606 !Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)) { 607 Left->setType(TT_ProtoExtensionLSquare); 608 BindingIncrease = 10; 609 } 610 } else if (!CppArrayTemplates && Parent && 611 Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at, 612 tok::comma, tok::l_paren, tok::l_square, 613 tok::question, tok::colon, tok::kw_return, 614 // Should only be relevant to JavaScript: 615 tok::kw_default)) { 616 Left->setType(TT_ArrayInitializerLSquare); 617 } else { 618 BindingIncrease = 10; 619 Left->setType(TT_ArraySubscriptLSquare); 620 } 621 } 622 623 ScopedContextCreator ContextCreator(*this, tok::l_square, BindingIncrease); 624 Contexts.back().IsExpression = true; 625 if (Style.Language == FormatStyle::LK_JavaScript && Parent && 626 Parent->is(TT_JsTypeColon)) 627 Contexts.back().IsExpression = false; 628 629 Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr; 630 Contexts.back().InCpp11AttributeSpecifier = IsCpp11AttributeSpecifier; 631 Contexts.back().InCSharpAttributeSpecifier = IsCSharpAttributeSpecifier; 632 633 while (CurrentToken) { 634 if (CurrentToken->is(tok::r_square)) { 635 if (IsCpp11AttributeSpecifier) 636 CurrentToken->setType(TT_AttributeSquare); 637 if (IsCSharpAttributeSpecifier) 638 CurrentToken->setType(TT_AttributeSquare); 639 else if (((CurrentToken->Next && 640 CurrentToken->Next->is(tok::l_paren)) || 641 (CurrentToken->Previous && 642 CurrentToken->Previous->Previous == Left)) && 643 Left->is(TT_ObjCMethodExpr)) { 644 // An ObjC method call is rarely followed by an open parenthesis. It 645 // also can't be composed of just one token, unless it's a macro that 646 // will be expanded to more tokens. 647 // FIXME: Do we incorrectly label ":" with this? 648 StartsObjCMethodExpr = false; 649 Left->setType(TT_Unknown); 650 } 651 if (StartsObjCMethodExpr && CurrentToken->Previous != Left) { 652 CurrentToken->setType(TT_ObjCMethodExpr); 653 // If we haven't seen a colon yet, make sure the last identifier 654 // before the r_square is tagged as a selector name component. 655 if (!ColonFound && CurrentToken->Previous && 656 CurrentToken->Previous->is(TT_Unknown) && 657 canBeObjCSelectorComponent(*CurrentToken->Previous)) 658 CurrentToken->Previous->setType(TT_SelectorName); 659 // determineStarAmpUsage() thinks that '*' '[' is allocating an 660 // array of pointers, but if '[' starts a selector then '*' is a 661 // binary operator. 662 if (Parent && Parent->is(TT_PointerOrReference)) 663 Parent->setType(TT_BinaryOperator); 664 } 665 // An arrow after an ObjC method expression is not a lambda arrow. 666 if (CurrentToken->getType() == TT_ObjCMethodExpr && 667 CurrentToken->Next && CurrentToken->Next->is(TT_LambdaArrow)) 668 CurrentToken->Next->setType(TT_Unknown); 669 Left->MatchingParen = CurrentToken; 670 CurrentToken->MatchingParen = Left; 671 // FirstObjCSelectorName is set when a colon is found. This does 672 // not work, however, when the method has no parameters. 673 // Here, we set FirstObjCSelectorName when the end of the method call is 674 // reached, in case it was not set already. 675 if (!Contexts.back().FirstObjCSelectorName) { 676 FormatToken *Previous = CurrentToken->getPreviousNonComment(); 677 if (Previous && Previous->is(TT_SelectorName)) { 678 Previous->ObjCSelectorNameParts = 1; 679 Contexts.back().FirstObjCSelectorName = Previous; 680 } 681 } else { 682 Left->ParameterCount = 683 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts; 684 } 685 if (Contexts.back().FirstObjCSelectorName) { 686 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 687 Contexts.back().LongestObjCSelectorName; 688 if (Left->BlockParameterCount > 1) 689 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0; 690 } 691 next(); 692 return true; 693 } 694 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace)) 695 return false; 696 if (CurrentToken->is(tok::colon)) { 697 if (IsCpp11AttributeSpecifier && 698 CurrentToken->endsSequence(tok::colon, tok::identifier, 699 tok::kw_using)) { 700 // Remember that this is a [[using ns: foo]] C++ attribute, so we 701 // don't add a space before the colon (unlike other colons). 702 CurrentToken->setType(TT_AttributeColon); 703 } else if (Left->isOneOf(TT_ArraySubscriptLSquare, 704 TT_DesignatedInitializerLSquare)) { 705 Left->setType(TT_ObjCMethodExpr); 706 StartsObjCMethodExpr = true; 707 Contexts.back().ColonIsObjCMethodExpr = true; 708 if (Parent && Parent->is(tok::r_paren)) 709 // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen. 710 Parent->setType(TT_CastRParen); 711 } 712 ColonFound = true; 713 } 714 if (CurrentToken->is(tok::comma) && Left->is(TT_ObjCMethodExpr) && 715 !ColonFound) 716 Left->setType(TT_ArrayInitializerLSquare); 717 FormatToken *Tok = CurrentToken; 718 if (!consumeToken()) 719 return false; 720 updateParameterCount(Left, Tok); 721 } 722 return false; 723 } 724 725 bool parseBrace() { 726 if (CurrentToken) { 727 FormatToken *Left = CurrentToken->Previous; 728 Left->ParentBracket = Contexts.back().ContextKind; 729 730 if (Contexts.back().CaretFound) 731 Left->setType(TT_ObjCBlockLBrace); 732 Contexts.back().CaretFound = false; 733 734 ScopedContextCreator ContextCreator(*this, tok::l_brace, 1); 735 Contexts.back().ColonIsDictLiteral = true; 736 if (Left->is(BK_BracedInit)) 737 Contexts.back().IsExpression = true; 738 if (Style.Language == FormatStyle::LK_JavaScript && Left->Previous && 739 Left->Previous->is(TT_JsTypeColon)) 740 Contexts.back().IsExpression = false; 741 742 while (CurrentToken) { 743 if (CurrentToken->is(tok::r_brace)) { 744 Left->MatchingParen = CurrentToken; 745 CurrentToken->MatchingParen = Left; 746 next(); 747 return true; 748 } 749 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square)) 750 return false; 751 updateParameterCount(Left, CurrentToken); 752 if (CurrentToken->isOneOf(tok::colon, tok::l_brace, tok::less)) { 753 FormatToken *Previous = CurrentToken->getPreviousNonComment(); 754 if (Previous->is(TT_JsTypeOptionalQuestion)) 755 Previous = Previous->getPreviousNonComment(); 756 if ((CurrentToken->is(tok::colon) && 757 (!Contexts.back().ColonIsDictLiteral || !Style.isCpp())) || 758 Style.Language == FormatStyle::LK_Proto || 759 Style.Language == FormatStyle::LK_TextProto) { 760 Left->setType(TT_DictLiteral); 761 if (Previous->Tok.getIdentifierInfo() || 762 Previous->is(tok::string_literal)) 763 Previous->setType(TT_SelectorName); 764 } 765 if (CurrentToken->is(tok::colon) || 766 Style.Language == FormatStyle::LK_JavaScript) 767 Left->setType(TT_DictLiteral); 768 } 769 if (CurrentToken->is(tok::comma) && 770 Style.Language == FormatStyle::LK_JavaScript) 771 Left->setType(TT_DictLiteral); 772 if (!consumeToken()) 773 return false; 774 } 775 } 776 return true; 777 } 778 779 void updateParameterCount(FormatToken *Left, FormatToken *Current) { 780 // For ObjC methods, the number of parameters is calculated differently as 781 // method declarations have a different structure (the parameters are not 782 // inside a bracket scope). 783 if (Current->is(tok::l_brace) && Current->is(BK_Block)) 784 ++Left->BlockParameterCount; 785 if (Current->is(tok::comma)) { 786 ++Left->ParameterCount; 787 if (!Left->Role) 788 Left->Role.reset(new CommaSeparatedList(Style)); 789 Left->Role->CommaFound(Current); 790 } else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) { 791 Left->ParameterCount = 1; 792 } 793 } 794 795 bool parseConditional() { 796 while (CurrentToken) { 797 if (CurrentToken->is(tok::colon)) { 798 CurrentToken->setType(TT_ConditionalExpr); 799 next(); 800 return true; 801 } 802 if (!consumeToken()) 803 return false; 804 } 805 return false; 806 } 807 808 bool parseTemplateDeclaration() { 809 if (CurrentToken && CurrentToken->is(tok::less)) { 810 CurrentToken->setType(TT_TemplateOpener); 811 next(); 812 if (!parseAngle()) 813 return false; 814 if (CurrentToken) 815 CurrentToken->Previous->ClosesTemplateDeclaration = true; 816 return true; 817 } 818 return false; 819 } 820 821 bool consumeToken() { 822 FormatToken *Tok = CurrentToken; 823 next(); 824 switch (Tok->Tok.getKind()) { 825 case tok::plus: 826 case tok::minus: 827 if (!Tok->Previous && Line.MustBeDeclaration) 828 Tok->setType(TT_ObjCMethodSpecifier); 829 break; 830 case tok::colon: 831 if (!Tok->Previous) 832 return false; 833 // Colons from ?: are handled in parseConditional(). 834 if (Style.Language == FormatStyle::LK_JavaScript) { 835 if (Contexts.back().ColonIsForRangeExpr || // colon in for loop 836 (Contexts.size() == 1 && // switch/case labels 837 !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) || 838 Contexts.back().ContextKind == tok::l_paren || // function params 839 Contexts.back().ContextKind == tok::l_square || // array type 840 (!Contexts.back().IsExpression && 841 Contexts.back().ContextKind == tok::l_brace) || // object type 842 (Contexts.size() == 1 && 843 Line.MustBeDeclaration)) { // method/property declaration 844 Contexts.back().IsExpression = false; 845 Tok->setType(TT_JsTypeColon); 846 break; 847 } 848 } else if (Style.isCSharp()) { 849 if (Contexts.back().InCSharpAttributeSpecifier) { 850 Tok->setType(TT_AttributeColon); 851 break; 852 } 853 if (Contexts.back().ContextKind == tok::l_paren) { 854 Tok->setType(TT_CSharpNamedArgumentColon); 855 break; 856 } 857 } 858 if (Contexts.back().ColonIsDictLiteral || 859 Style.Language == FormatStyle::LK_Proto || 860 Style.Language == FormatStyle::LK_TextProto) { 861 Tok->setType(TT_DictLiteral); 862 if (Style.Language == FormatStyle::LK_TextProto) { 863 if (FormatToken *Previous = Tok->getPreviousNonComment()) 864 Previous->setType(TT_SelectorName); 865 } 866 } else if (Contexts.back().ColonIsObjCMethodExpr || 867 Line.startsWith(TT_ObjCMethodSpecifier)) { 868 Tok->setType(TT_ObjCMethodExpr); 869 const FormatToken *BeforePrevious = Tok->Previous->Previous; 870 // Ensure we tag all identifiers in method declarations as 871 // TT_SelectorName. 872 bool UnknownIdentifierInMethodDeclaration = 873 Line.startsWith(TT_ObjCMethodSpecifier) && 874 Tok->Previous->is(tok::identifier) && Tok->Previous->is(TT_Unknown); 875 if (!BeforePrevious || 876 // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen. 877 !(BeforePrevious->is(TT_CastRParen) || 878 (BeforePrevious->is(TT_ObjCMethodExpr) && 879 BeforePrevious->is(tok::colon))) || 880 BeforePrevious->is(tok::r_square) || 881 Contexts.back().LongestObjCSelectorName == 0 || 882 UnknownIdentifierInMethodDeclaration) { 883 Tok->Previous->setType(TT_SelectorName); 884 if (!Contexts.back().FirstObjCSelectorName) 885 Contexts.back().FirstObjCSelectorName = Tok->Previous; 886 else if (Tok->Previous->ColumnWidth > 887 Contexts.back().LongestObjCSelectorName) 888 Contexts.back().LongestObjCSelectorName = 889 Tok->Previous->ColumnWidth; 890 Tok->Previous->ParameterIndex = 891 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts; 892 ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts; 893 } 894 } else if (Contexts.back().ColonIsForRangeExpr) { 895 Tok->setType(TT_RangeBasedForLoopColon); 896 } else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) { 897 Tok->setType(TT_BitFieldColon); 898 } else if (Contexts.size() == 1 && 899 !Line.First->isOneOf(tok::kw_enum, tok::kw_case, 900 tok::kw_default)) { 901 FormatToken *Prev = Tok->getPreviousNonComment(); 902 if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept)) 903 Tok->setType(TT_CtorInitializerColon); 904 else if (Prev->is(tok::kw_try)) { 905 // Member initializer list within function try block. 906 FormatToken *PrevPrev = Prev->getPreviousNonComment(); 907 if (PrevPrev && PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept)) 908 Tok->setType(TT_CtorInitializerColon); 909 } else 910 Tok->setType(TT_InheritanceColon); 911 } else if (canBeObjCSelectorComponent(*Tok->Previous) && Tok->Next && 912 (Tok->Next->isOneOf(tok::r_paren, tok::comma) || 913 (canBeObjCSelectorComponent(*Tok->Next) && Tok->Next->Next && 914 Tok->Next->Next->is(tok::colon)))) { 915 // This handles a special macro in ObjC code where selectors including 916 // the colon are passed as macro arguments. 917 Tok->setType(TT_ObjCMethodExpr); 918 } else if (Contexts.back().ContextKind == tok::l_paren) { 919 Tok->setType(TT_InlineASMColon); 920 } 921 break; 922 case tok::pipe: 923 case tok::amp: 924 // | and & in declarations/type expressions represent union and 925 // intersection types, respectively. 926 if (Style.Language == FormatStyle::LK_JavaScript && 927 !Contexts.back().IsExpression) 928 Tok->setType(TT_JsTypeOperator); 929 break; 930 case tok::kw_if: 931 case tok::kw_while: 932 if (Tok->is(tok::kw_if) && CurrentToken && 933 CurrentToken->isOneOf(tok::kw_constexpr, tok::identifier)) 934 next(); 935 if (CurrentToken && CurrentToken->is(tok::l_paren)) { 936 next(); 937 if (!parseParens(/*LookForDecls=*/true)) 938 return false; 939 } 940 break; 941 case tok::kw_for: 942 if (Style.Language == FormatStyle::LK_JavaScript) { 943 // x.for and {for: ...} 944 if ((Tok->Previous && Tok->Previous->is(tok::period)) || 945 (Tok->Next && Tok->Next->is(tok::colon))) 946 break; 947 // JS' for await ( ... 948 if (CurrentToken && CurrentToken->is(Keywords.kw_await)) 949 next(); 950 } 951 Contexts.back().ColonIsForRangeExpr = true; 952 next(); 953 if (!parseParens()) 954 return false; 955 break; 956 case tok::l_paren: 957 // When faced with 'operator()()', the kw_operator handler incorrectly 958 // marks the first l_paren as a OverloadedOperatorLParen. Here, we make 959 // the first two parens OverloadedOperators and the second l_paren an 960 // OverloadedOperatorLParen. 961 if (Tok->Previous && Tok->Previous->is(tok::r_paren) && 962 Tok->Previous->MatchingParen && 963 Tok->Previous->MatchingParen->is(TT_OverloadedOperatorLParen)) { 964 Tok->Previous->setType(TT_OverloadedOperator); 965 Tok->Previous->MatchingParen->setType(TT_OverloadedOperator); 966 Tok->setType(TT_OverloadedOperatorLParen); 967 } 968 969 if (!parseParens()) 970 return false; 971 if (Line.MustBeDeclaration && Contexts.size() == 1 && 972 !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) && 973 !Tok->is(TT_TypeDeclarationParen) && 974 (!Tok->Previous || !Tok->Previous->isOneOf(tok::kw___attribute, 975 TT_LeadingJavaAnnotation))) 976 Line.MightBeFunctionDecl = true; 977 break; 978 case tok::l_square: 979 if (!parseSquare()) 980 return false; 981 break; 982 case tok::l_brace: 983 if (Style.Language == FormatStyle::LK_TextProto) { 984 FormatToken *Previous = Tok->getPreviousNonComment(); 985 if (Previous && Previous->getType() != TT_DictLiteral) 986 Previous->setType(TT_SelectorName); 987 } 988 if (!parseBrace()) 989 return false; 990 break; 991 case tok::less: 992 if (parseAngle()) { 993 Tok->setType(TT_TemplateOpener); 994 // In TT_Proto, we must distignuish between: 995 // map<key, value> 996 // msg < item: data > 997 // msg: < item: data > 998 // In TT_TextProto, map<key, value> does not occur. 999 if (Style.Language == FormatStyle::LK_TextProto || 1000 (Style.Language == FormatStyle::LK_Proto && Tok->Previous && 1001 Tok->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) { 1002 Tok->setType(TT_DictLiteral); 1003 FormatToken *Previous = Tok->getPreviousNonComment(); 1004 if (Previous && Previous->getType() != TT_DictLiteral) 1005 Previous->setType(TT_SelectorName); 1006 } 1007 } else { 1008 Tok->setType(TT_BinaryOperator); 1009 NonTemplateLess.insert(Tok); 1010 CurrentToken = Tok; 1011 next(); 1012 } 1013 break; 1014 case tok::r_paren: 1015 case tok::r_square: 1016 return false; 1017 case tok::r_brace: 1018 // Lines can start with '}'. 1019 if (Tok->Previous) 1020 return false; 1021 break; 1022 case tok::greater: 1023 if (Style.Language != FormatStyle::LK_TextProto) 1024 Tok->setType(TT_BinaryOperator); 1025 if (Tok->Previous && Tok->Previous->is(TT_TemplateCloser)) 1026 Tok->SpacesRequiredBefore = 1; 1027 break; 1028 case tok::kw_operator: 1029 if (Style.Language == FormatStyle::LK_TextProto || 1030 Style.Language == FormatStyle::LK_Proto) 1031 break; 1032 while (CurrentToken && 1033 !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) { 1034 if (CurrentToken->isOneOf(tok::star, tok::amp)) 1035 CurrentToken->setType(TT_PointerOrReference); 1036 consumeToken(); 1037 if (CurrentToken && CurrentToken->is(tok::comma) && 1038 CurrentToken->Previous->isNot(tok::kw_operator)) 1039 break; 1040 if (CurrentToken && CurrentToken->Previous->isOneOf( 1041 TT_BinaryOperator, TT_UnaryOperator, tok::comma, 1042 tok::star, tok::arrow, tok::amp, tok::ampamp)) 1043 CurrentToken->Previous->setType(TT_OverloadedOperator); 1044 } 1045 if (CurrentToken && CurrentToken->is(tok::l_paren)) 1046 CurrentToken->setType(TT_OverloadedOperatorLParen); 1047 if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator)) 1048 CurrentToken->Previous->setType(TT_OverloadedOperator); 1049 break; 1050 case tok::question: 1051 if (Tok->is(TT_CSharpNullConditionalLSquare)) { 1052 if (!parseSquare()) 1053 return false; 1054 break; 1055 } 1056 if (Tok->isOneOf(TT_CSharpNullConditional, TT_CSharpNullCoalescing)) 1057 break; 1058 if (Style.Language == FormatStyle::LK_JavaScript && Tok->Next && 1059 Tok->Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren, 1060 tok::r_brace)) { 1061 // Question marks before semicolons, colons, etc. indicate optional 1062 // types (fields, parameters), e.g. 1063 // function(x?: string, y?) {...} 1064 // class X { y?; } 1065 Tok->setType(TT_JsTypeOptionalQuestion); 1066 break; 1067 } 1068 // Declarations cannot be conditional expressions, this can only be part 1069 // of a type declaration. 1070 if (Line.MustBeDeclaration && !Contexts.back().IsExpression && 1071 Style.Language == FormatStyle::LK_JavaScript) 1072 break; 1073 if (Style.isCSharp()) { 1074 // `Type?)`, `Type?>`, `Type? name;` and `Type? name =` can only be 1075 // nullable types. 1076 // Line.MustBeDeclaration will be true for `Type? name;`. 1077 if ((!Contexts.back().IsExpression && Line.MustBeDeclaration) || 1078 (Tok->Next && Tok->Next->isOneOf(tok::r_paren, tok::greater)) || 1079 (Tok->Next && Tok->Next->is(tok::identifier) && Tok->Next->Next && 1080 Tok->Next->Next->is(tok::equal))) { 1081 Tok->setType(TT_CSharpNullable); 1082 break; 1083 } 1084 } 1085 parseConditional(); 1086 break; 1087 case tok::kw_template: 1088 parseTemplateDeclaration(); 1089 break; 1090 case tok::comma: 1091 if (Contexts.back().InCtorInitializer) 1092 Tok->setType(TT_CtorInitializerComma); 1093 else if (Contexts.back().InInheritanceList) 1094 Tok->setType(TT_InheritanceComma); 1095 else if (Contexts.back().FirstStartOfName && 1096 (Contexts.size() == 1 || Line.startsWith(tok::kw_for))) { 1097 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true; 1098 Line.IsMultiVariableDeclStmt = true; 1099 } 1100 if (Contexts.back().IsForEachMacro) 1101 Contexts.back().IsExpression = true; 1102 break; 1103 case tok::identifier: 1104 if (Tok->isOneOf(Keywords.kw___has_include, 1105 Keywords.kw___has_include_next)) { 1106 parseHasInclude(); 1107 } 1108 if (Style.isCSharp() && Tok->is(Keywords.kw_where) && Tok->Next && 1109 Tok->Next->isNot(tok::l_paren)) { 1110 Tok->setType(TT_CSharpGenericTypeConstraint); 1111 parseCSharpGenericTypeConstraint(); 1112 } 1113 break; 1114 default: 1115 break; 1116 } 1117 return true; 1118 } 1119 1120 void parseCSharpGenericTypeConstraint() { 1121 int OpenAngleBracketsCount = 0; 1122 while (CurrentToken) { 1123 if (CurrentToken->is(tok::less)) { 1124 // parseAngle is too greedy and will consume the whole line. 1125 CurrentToken->setType(TT_TemplateOpener); 1126 ++OpenAngleBracketsCount; 1127 next(); 1128 } else if (CurrentToken->is(tok::greater)) { 1129 CurrentToken->setType(TT_TemplateCloser); 1130 --OpenAngleBracketsCount; 1131 next(); 1132 } else if (CurrentToken->is(tok::comma) && OpenAngleBracketsCount == 0) { 1133 // We allow line breaks after GenericTypeConstraintComma's 1134 // so do not flag commas in Generics as GenericTypeConstraintComma's. 1135 CurrentToken->setType(TT_CSharpGenericTypeConstraintComma); 1136 next(); 1137 } else if (CurrentToken->is(Keywords.kw_where)) { 1138 CurrentToken->setType(TT_CSharpGenericTypeConstraint); 1139 next(); 1140 } else if (CurrentToken->is(tok::colon)) { 1141 CurrentToken->setType(TT_CSharpGenericTypeConstraintColon); 1142 next(); 1143 } else { 1144 next(); 1145 } 1146 } 1147 } 1148 1149 void parseIncludeDirective() { 1150 if (CurrentToken && CurrentToken->is(tok::less)) { 1151 next(); 1152 while (CurrentToken) { 1153 // Mark tokens up to the trailing line comments as implicit string 1154 // literals. 1155 if (CurrentToken->isNot(tok::comment) && 1156 !CurrentToken->TokenText.startswith("//")) 1157 CurrentToken->setType(TT_ImplicitStringLiteral); 1158 next(); 1159 } 1160 } 1161 } 1162 1163 void parseWarningOrError() { 1164 next(); 1165 // We still want to format the whitespace left of the first token of the 1166 // warning or error. 1167 next(); 1168 while (CurrentToken) { 1169 CurrentToken->setType(TT_ImplicitStringLiteral); 1170 next(); 1171 } 1172 } 1173 1174 void parsePragma() { 1175 next(); // Consume "pragma". 1176 if (CurrentToken && 1177 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option)) { 1178 bool IsMark = CurrentToken->is(Keywords.kw_mark); 1179 next(); // Consume "mark". 1180 next(); // Consume first token (so we fix leading whitespace). 1181 while (CurrentToken) { 1182 if (IsMark || CurrentToken->Previous->is(TT_BinaryOperator)) 1183 CurrentToken->setType(TT_ImplicitStringLiteral); 1184 next(); 1185 } 1186 } 1187 } 1188 1189 void parseHasInclude() { 1190 if (!CurrentToken || !CurrentToken->is(tok::l_paren)) 1191 return; 1192 next(); // '(' 1193 parseIncludeDirective(); 1194 next(); // ')' 1195 } 1196 1197 LineType parsePreprocessorDirective() { 1198 bool IsFirstToken = CurrentToken->IsFirst; 1199 LineType Type = LT_PreprocessorDirective; 1200 next(); 1201 if (!CurrentToken) 1202 return Type; 1203 1204 if (Style.Language == FormatStyle::LK_JavaScript && IsFirstToken) { 1205 // JavaScript files can contain shebang lines of the form: 1206 // #!/usr/bin/env node 1207 // Treat these like C++ #include directives. 1208 while (CurrentToken) { 1209 // Tokens cannot be comments here. 1210 CurrentToken->setType(TT_ImplicitStringLiteral); 1211 next(); 1212 } 1213 return LT_ImportStatement; 1214 } 1215 1216 if (CurrentToken->Tok.is(tok::numeric_constant)) { 1217 CurrentToken->SpacesRequiredBefore = 1; 1218 return Type; 1219 } 1220 // Hashes in the middle of a line can lead to any strange token 1221 // sequence. 1222 if (!CurrentToken->Tok.getIdentifierInfo()) 1223 return Type; 1224 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) { 1225 case tok::pp_include: 1226 case tok::pp_include_next: 1227 case tok::pp_import: 1228 next(); 1229 parseIncludeDirective(); 1230 Type = LT_ImportStatement; 1231 break; 1232 case tok::pp_error: 1233 case tok::pp_warning: 1234 parseWarningOrError(); 1235 break; 1236 case tok::pp_pragma: 1237 parsePragma(); 1238 break; 1239 case tok::pp_if: 1240 case tok::pp_elif: 1241 Contexts.back().IsExpression = true; 1242 next(); 1243 parseLine(); 1244 break; 1245 default: 1246 break; 1247 } 1248 while (CurrentToken) { 1249 FormatToken *Tok = CurrentToken; 1250 next(); 1251 if (Tok->is(tok::l_paren)) 1252 parseParens(); 1253 else if (Tok->isOneOf(Keywords.kw___has_include, 1254 Keywords.kw___has_include_next)) 1255 parseHasInclude(); 1256 } 1257 return Type; 1258 } 1259 1260 public: 1261 LineType parseLine() { 1262 if (!CurrentToken) 1263 return LT_Invalid; 1264 NonTemplateLess.clear(); 1265 if (CurrentToken->is(tok::hash)) 1266 return parsePreprocessorDirective(); 1267 1268 // Directly allow to 'import <string-literal>' to support protocol buffer 1269 // definitions (github.com/google/protobuf) or missing "#" (either way we 1270 // should not break the line). 1271 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo(); 1272 if ((Style.Language == FormatStyle::LK_Java && 1273 CurrentToken->is(Keywords.kw_package)) || 1274 (Info && Info->getPPKeywordID() == tok::pp_import && 1275 CurrentToken->Next && 1276 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier, 1277 tok::kw_static))) { 1278 next(); 1279 parseIncludeDirective(); 1280 return LT_ImportStatement; 1281 } 1282 1283 // If this line starts and ends in '<' and '>', respectively, it is likely 1284 // part of "#define <a/b.h>". 1285 if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) { 1286 parseIncludeDirective(); 1287 return LT_ImportStatement; 1288 } 1289 1290 // In .proto files, top-level options and package statements are very 1291 // similar to import statements and should not be line-wrapped. 1292 if (Style.Language == FormatStyle::LK_Proto && Line.Level == 0 && 1293 CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) { 1294 next(); 1295 if (CurrentToken && CurrentToken->is(tok::identifier)) { 1296 while (CurrentToken) 1297 next(); 1298 return LT_ImportStatement; 1299 } 1300 } 1301 1302 bool KeywordVirtualFound = false; 1303 bool ImportStatement = false; 1304 1305 // import {...} from '...'; 1306 if (Style.Language == FormatStyle::LK_JavaScript && 1307 CurrentToken->is(Keywords.kw_import)) 1308 ImportStatement = true; 1309 1310 while (CurrentToken) { 1311 if (CurrentToken->is(tok::kw_virtual)) 1312 KeywordVirtualFound = true; 1313 if (Style.Language == FormatStyle::LK_JavaScript) { 1314 // export {...} from '...'; 1315 // An export followed by "from 'some string';" is a re-export from 1316 // another module identified by a URI and is treated as a 1317 // LT_ImportStatement (i.e. prevent wraps on it for long URIs). 1318 // Just "export {...};" or "export class ..." should not be treated as 1319 // an import in this sense. 1320 if (Line.First->is(tok::kw_export) && 1321 CurrentToken->is(Keywords.kw_from) && CurrentToken->Next && 1322 CurrentToken->Next->isStringLiteral()) 1323 ImportStatement = true; 1324 if (isClosureImportStatement(*CurrentToken)) 1325 ImportStatement = true; 1326 } 1327 if (!consumeToken()) 1328 return LT_Invalid; 1329 } 1330 if (KeywordVirtualFound) 1331 return LT_VirtualFunctionDecl; 1332 if (ImportStatement) 1333 return LT_ImportStatement; 1334 1335 if (Line.startsWith(TT_ObjCMethodSpecifier)) { 1336 if (Contexts.back().FirstObjCSelectorName) 1337 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 1338 Contexts.back().LongestObjCSelectorName; 1339 return LT_ObjCMethodDecl; 1340 } 1341 1342 return LT_Other; 1343 } 1344 1345 private: 1346 bool isClosureImportStatement(const FormatToken &Tok) { 1347 // FIXME: Closure-library specific stuff should not be hard-coded but be 1348 // configurable. 1349 return Tok.TokenText == "goog" && Tok.Next && Tok.Next->is(tok::period) && 1350 Tok.Next->Next && 1351 (Tok.Next->Next->TokenText == "module" || 1352 Tok.Next->Next->TokenText == "provide" || 1353 Tok.Next->Next->TokenText == "require" || 1354 Tok.Next->Next->TokenText == "requireType" || 1355 Tok.Next->Next->TokenText == "forwardDeclare") && 1356 Tok.Next->Next->Next && Tok.Next->Next->Next->is(tok::l_paren); 1357 } 1358 1359 void resetTokenMetadata(FormatToken *Token) { 1360 if (!Token) 1361 return; 1362 1363 // Reset token type in case we have already looked at it and then 1364 // recovered from an error (e.g. failure to find the matching >). 1365 if (!CurrentToken->isOneOf( 1366 TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, 1367 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace, 1368 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_JsFatArrow, 1369 TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator, 1370 TT_RegexLiteral, TT_TemplateString, TT_ObjCStringLiteral, 1371 TT_UntouchableMacroFunc, TT_ConstraintJunctions)) 1372 CurrentToken->setType(TT_Unknown); 1373 CurrentToken->Role.reset(); 1374 CurrentToken->MatchingParen = nullptr; 1375 CurrentToken->FakeLParens.clear(); 1376 CurrentToken->FakeRParens = 0; 1377 } 1378 1379 void next() { 1380 if (CurrentToken) { 1381 CurrentToken->NestingLevel = Contexts.size() - 1; 1382 CurrentToken->BindingStrength = Contexts.back().BindingStrength; 1383 modifyContext(*CurrentToken); 1384 determineTokenType(*CurrentToken); 1385 CurrentToken = CurrentToken->Next; 1386 } 1387 1388 resetTokenMetadata(CurrentToken); 1389 } 1390 1391 /// A struct to hold information valid in a specific context, e.g. 1392 /// a pair of parenthesis. 1393 struct Context { 1394 Context(tok::TokenKind ContextKind, unsigned BindingStrength, 1395 bool IsExpression) 1396 : ContextKind(ContextKind), BindingStrength(BindingStrength), 1397 IsExpression(IsExpression) {} 1398 1399 tok::TokenKind ContextKind; 1400 unsigned BindingStrength; 1401 bool IsExpression; 1402 unsigned LongestObjCSelectorName = 0; 1403 bool ColonIsForRangeExpr = false; 1404 bool ColonIsDictLiteral = false; 1405 bool ColonIsObjCMethodExpr = false; 1406 FormatToken *FirstObjCSelectorName = nullptr; 1407 FormatToken *FirstStartOfName = nullptr; 1408 bool CanBeExpression = true; 1409 bool InTemplateArgument = false; 1410 bool InCtorInitializer = false; 1411 bool InInheritanceList = false; 1412 bool CaretFound = false; 1413 bool IsForEachMacro = false; 1414 bool InCpp11AttributeSpecifier = false; 1415 bool InCSharpAttributeSpecifier = false; 1416 }; 1417 1418 /// Puts a new \c Context onto the stack \c Contexts for the lifetime 1419 /// of each instance. 1420 struct ScopedContextCreator { 1421 AnnotatingParser &P; 1422 1423 ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind, 1424 unsigned Increase) 1425 : P(P) { 1426 P.Contexts.push_back(Context(ContextKind, 1427 P.Contexts.back().BindingStrength + Increase, 1428 P.Contexts.back().IsExpression)); 1429 } 1430 1431 ~ScopedContextCreator() { P.Contexts.pop_back(); } 1432 }; 1433 1434 void modifyContext(const FormatToken &Current) { 1435 if (Current.getPrecedence() == prec::Assignment && 1436 !Line.First->isOneOf(tok::kw_template, tok::kw_using, tok::kw_return) && 1437 // Type aliases use `type X = ...;` in TypeScript and can be exported 1438 // using `export type ...`. 1439 !(Style.Language == FormatStyle::LK_JavaScript && 1440 (Line.startsWith(Keywords.kw_type, tok::identifier) || 1441 Line.startsWith(tok::kw_export, Keywords.kw_type, 1442 tok::identifier))) && 1443 (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) { 1444 Contexts.back().IsExpression = true; 1445 if (!Line.startsWith(TT_UnaryOperator)) { 1446 for (FormatToken *Previous = Current.Previous; 1447 Previous && Previous->Previous && 1448 !Previous->Previous->isOneOf(tok::comma, tok::semi); 1449 Previous = Previous->Previous) { 1450 if (Previous->isOneOf(tok::r_square, tok::r_paren)) { 1451 Previous = Previous->MatchingParen; 1452 if (!Previous) 1453 break; 1454 } 1455 if (Previous->opensScope()) 1456 break; 1457 if (Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) && 1458 Previous->isOneOf(tok::star, tok::amp, tok::ampamp) && 1459 Previous->Previous && Previous->Previous->isNot(tok::equal)) 1460 Previous->setType(TT_PointerOrReference); 1461 } 1462 } 1463 } else if (Current.is(tok::lessless) && 1464 (!Current.Previous || !Current.Previous->is(tok::kw_operator))) { 1465 Contexts.back().IsExpression = true; 1466 } else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) { 1467 Contexts.back().IsExpression = true; 1468 } else if (Current.is(TT_TrailingReturnArrow)) { 1469 Contexts.back().IsExpression = false; 1470 } else if (Current.is(TT_LambdaArrow) || Current.is(Keywords.kw_assert)) { 1471 Contexts.back().IsExpression = Style.Language == FormatStyle::LK_Java; 1472 } else if (Current.Previous && 1473 Current.Previous->is(TT_CtorInitializerColon)) { 1474 Contexts.back().IsExpression = true; 1475 Contexts.back().InCtorInitializer = true; 1476 } else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) { 1477 Contexts.back().InInheritanceList = true; 1478 } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) { 1479 for (FormatToken *Previous = Current.Previous; 1480 Previous && Previous->isOneOf(tok::star, tok::amp); 1481 Previous = Previous->Previous) 1482 Previous->setType(TT_PointerOrReference); 1483 if (Line.MustBeDeclaration && !Contexts.front().InCtorInitializer) 1484 Contexts.back().IsExpression = false; 1485 } else if (Current.is(tok::kw_new)) { 1486 Contexts.back().CanBeExpression = false; 1487 } else if (Current.is(tok::semi) || 1488 (Current.is(tok::exclaim) && Current.Previous && 1489 !Current.Previous->is(tok::kw_operator))) { 1490 // This should be the condition or increment in a for-loop. 1491 // But not operator !() (can't use TT_OverloadedOperator here as its not 1492 // been annotated yet). 1493 Contexts.back().IsExpression = true; 1494 } 1495 } 1496 1497 static FormatToken *untilMatchingParen(FormatToken *Current) { 1498 // Used when `MatchingParen` is not yet established. 1499 int ParenLevel = 0; 1500 while (Current) { 1501 if (Current->is(tok::l_paren)) 1502 ParenLevel++; 1503 if (Current->is(tok::r_paren)) 1504 ParenLevel--; 1505 if (ParenLevel < 1) 1506 break; 1507 Current = Current->Next; 1508 } 1509 return Current; 1510 } 1511 1512 static bool isDeductionGuide(FormatToken &Current) { 1513 // Look for a deduction guide template<T> A(...) -> A<...>; 1514 if (Current.Previous && Current.Previous->is(tok::r_paren) && 1515 Current.startsSequence(tok::arrow, tok::identifier, tok::less)) { 1516 // Find the TemplateCloser. 1517 FormatToken *TemplateCloser = Current.Next->Next; 1518 int NestingLevel = 0; 1519 while (TemplateCloser) { 1520 // Skip over an expressions in parens A<(3 < 2)>; 1521 if (TemplateCloser->is(tok::l_paren)) { 1522 // No Matching Paren yet so skip to matching paren 1523 TemplateCloser = untilMatchingParen(TemplateCloser); 1524 } 1525 if (TemplateCloser->is(tok::less)) 1526 NestingLevel++; 1527 if (TemplateCloser->is(tok::greater)) 1528 NestingLevel--; 1529 if (NestingLevel < 1) 1530 break; 1531 TemplateCloser = TemplateCloser->Next; 1532 } 1533 // Assuming we have found the end of the template ensure its followed 1534 // with a semi-colon. 1535 if (TemplateCloser && TemplateCloser->Next && 1536 TemplateCloser->Next->is(tok::semi) && 1537 Current.Previous->MatchingParen) { 1538 // Determine if the identifier `A` prior to the A<..>; is the same as 1539 // prior to the A(..) 1540 FormatToken *LeadingIdentifier = 1541 Current.Previous->MatchingParen->Previous; 1542 1543 // Differentiate a deduction guide by seeing the 1544 // > of the template prior to the leading identifier. 1545 if (LeadingIdentifier) { 1546 FormatToken *PriorLeadingIdentifier = LeadingIdentifier->Previous; 1547 // Skip back past explicit decoration 1548 if (PriorLeadingIdentifier && 1549 PriorLeadingIdentifier->is(tok::kw_explicit)) 1550 PriorLeadingIdentifier = PriorLeadingIdentifier->Previous; 1551 1552 return (PriorLeadingIdentifier && 1553 PriorLeadingIdentifier->is(TT_TemplateCloser) && 1554 LeadingIdentifier->TokenText == Current.Next->TokenText); 1555 } 1556 } 1557 } 1558 return false; 1559 } 1560 1561 void determineTokenType(FormatToken &Current) { 1562 if (!Current.is(TT_Unknown)) 1563 // The token type is already known. 1564 return; 1565 1566 if (Style.isCSharp() && CurrentToken->is(tok::question)) { 1567 if (CurrentToken->TokenText == "??") { 1568 Current.setType(TT_CSharpNullCoalescing); 1569 return; 1570 } 1571 if (CurrentToken->TokenText == "?.") { 1572 Current.setType(TT_CSharpNullConditional); 1573 return; 1574 } 1575 if (CurrentToken->TokenText == "?[") { 1576 Current.setType(TT_CSharpNullConditionalLSquare); 1577 return; 1578 } 1579 } 1580 1581 if (Style.Language == FormatStyle::LK_JavaScript) { 1582 if (Current.is(tok::exclaim)) { 1583 if (Current.Previous && 1584 (Keywords.IsJavaScriptIdentifier( 1585 *Current.Previous, /* AcceptIdentifierName= */ true) || 1586 Current.Previous->isOneOf( 1587 tok::kw_namespace, tok::r_paren, tok::r_square, tok::r_brace, 1588 Keywords.kw_type, Keywords.kw_get, Keywords.kw_set) || 1589 Current.Previous->Tok.isLiteral())) { 1590 Current.setType(TT_JsNonNullAssertion); 1591 return; 1592 } 1593 if (Current.Next && 1594 Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) { 1595 Current.setType(TT_JsNonNullAssertion); 1596 return; 1597 } 1598 } 1599 } 1600 1601 // Line.MightBeFunctionDecl can only be true after the parentheses of a 1602 // function declaration have been found. In this case, 'Current' is a 1603 // trailing token of this declaration and thus cannot be a name. 1604 if (Current.is(Keywords.kw_instanceof)) { 1605 Current.setType(TT_BinaryOperator); 1606 } else if (isStartOfName(Current) && 1607 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) { 1608 Contexts.back().FirstStartOfName = &Current; 1609 Current.setType(TT_StartOfName); 1610 } else if (Current.is(tok::semi)) { 1611 // Reset FirstStartOfName after finding a semicolon so that a for loop 1612 // with multiple increment statements is not confused with a for loop 1613 // having multiple variable declarations. 1614 Contexts.back().FirstStartOfName = nullptr; 1615 } else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) { 1616 AutoFound = true; 1617 } else if (Current.is(tok::arrow) && 1618 Style.Language == FormatStyle::LK_Java) { 1619 Current.setType(TT_LambdaArrow); 1620 } else if (Current.is(tok::arrow) && AutoFound && Line.MustBeDeclaration && 1621 Current.NestingLevel == 0 && 1622 !Current.Previous->is(tok::kw_operator)) { 1623 // not auto operator->() -> xxx; 1624 Current.setType(TT_TrailingReturnArrow); 1625 } else if (Current.is(tok::arrow) && Current.Previous && 1626 Current.Previous->is(tok::r_brace)) { 1627 // Concept implicit conversion contraint needs to be treated like 1628 // a trailing return type ... } -> <type>. 1629 Current.setType(TT_TrailingReturnArrow); 1630 } else if (isDeductionGuide(Current)) { 1631 // Deduction guides trailing arrow " A(...) -> A<T>;". 1632 Current.setType(TT_TrailingReturnArrow); 1633 } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) { 1634 Current.setType(determineStarAmpUsage( 1635 Current, 1636 Contexts.back().CanBeExpression && Contexts.back().IsExpression, 1637 Contexts.back().InTemplateArgument)); 1638 } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) { 1639 Current.setType(determinePlusMinusCaretUsage(Current)); 1640 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret)) 1641 Contexts.back().CaretFound = true; 1642 } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) { 1643 Current.setType(determineIncrementUsage(Current)); 1644 } else if (Current.isOneOf(tok::exclaim, tok::tilde)) { 1645 Current.setType(TT_UnaryOperator); 1646 } else if (Current.is(tok::question)) { 1647 if (Style.Language == FormatStyle::LK_JavaScript && 1648 Line.MustBeDeclaration && !Contexts.back().IsExpression) { 1649 // In JavaScript, `interface X { foo?(): bar; }` is an optional method 1650 // on the interface, not a ternary expression. 1651 Current.setType(TT_JsTypeOptionalQuestion); 1652 } else { 1653 Current.setType(TT_ConditionalExpr); 1654 } 1655 } else if (Current.isBinaryOperator() && 1656 (!Current.Previous || Current.Previous->isNot(tok::l_square)) && 1657 (!Current.is(tok::greater) && 1658 Style.Language != FormatStyle::LK_TextProto)) { 1659 Current.setType(TT_BinaryOperator); 1660 } else if (Current.is(tok::comment)) { 1661 if (Current.TokenText.startswith("/*")) { 1662 if (Current.TokenText.endswith("*/")) 1663 Current.setType(TT_BlockComment); 1664 else 1665 // The lexer has for some reason determined a comment here. But we 1666 // cannot really handle it, if it isn't properly terminated. 1667 Current.Tok.setKind(tok::unknown); 1668 } else { 1669 Current.setType(TT_LineComment); 1670 } 1671 } else if (Current.is(tok::r_paren)) { 1672 if (rParenEndsCast(Current)) 1673 Current.setType(TT_CastRParen); 1674 if (Current.MatchingParen && Current.Next && 1675 !Current.Next->isBinaryOperator() && 1676 !Current.Next->isOneOf(tok::semi, tok::colon, tok::l_brace, 1677 tok::comma, tok::period, tok::arrow, 1678 tok::coloncolon)) 1679 if (FormatToken *AfterParen = Current.MatchingParen->Next) { 1680 // Make sure this isn't the return type of an Obj-C block declaration 1681 if (AfterParen->Tok.isNot(tok::caret)) { 1682 if (FormatToken *BeforeParen = Current.MatchingParen->Previous) 1683 if (BeforeParen->is(tok::identifier) && 1684 !BeforeParen->is(TT_TypenameMacro) && 1685 BeforeParen->TokenText == BeforeParen->TokenText.upper() && 1686 (!BeforeParen->Previous || 1687 BeforeParen->Previous->ClosesTemplateDeclaration)) 1688 Current.setType(TT_FunctionAnnotationRParen); 1689 } 1690 } 1691 } else if (Current.is(tok::at) && Current.Next && 1692 Style.Language != FormatStyle::LK_JavaScript && 1693 Style.Language != FormatStyle::LK_Java) { 1694 // In Java & JavaScript, "@..." is a decorator or annotation. In ObjC, it 1695 // marks declarations and properties that need special formatting. 1696 switch (Current.Next->Tok.getObjCKeywordID()) { 1697 case tok::objc_interface: 1698 case tok::objc_implementation: 1699 case tok::objc_protocol: 1700 Current.setType(TT_ObjCDecl); 1701 break; 1702 case tok::objc_property: 1703 Current.setType(TT_ObjCProperty); 1704 break; 1705 default: 1706 break; 1707 } 1708 } else if (Current.is(tok::period)) { 1709 FormatToken *PreviousNoComment = Current.getPreviousNonComment(); 1710 if (PreviousNoComment && 1711 PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) 1712 Current.setType(TT_DesignatedInitializerPeriod); 1713 else if (Style.Language == FormatStyle::LK_Java && Current.Previous && 1714 Current.Previous->isOneOf(TT_JavaAnnotation, 1715 TT_LeadingJavaAnnotation)) { 1716 Current.setType(Current.Previous->getType()); 1717 } 1718 } else if (canBeObjCSelectorComponent(Current) && 1719 // FIXME(bug 36976): ObjC return types shouldn't use 1720 // TT_CastRParen. 1721 Current.Previous && Current.Previous->is(TT_CastRParen) && 1722 Current.Previous->MatchingParen && 1723 Current.Previous->MatchingParen->Previous && 1724 Current.Previous->MatchingParen->Previous->is( 1725 TT_ObjCMethodSpecifier)) { 1726 // This is the first part of an Objective-C selector name. (If there's no 1727 // colon after this, this is the only place which annotates the identifier 1728 // as a selector.) 1729 Current.setType(TT_SelectorName); 1730 } else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept, 1731 tok::kw_requires) && 1732 Current.Previous && 1733 !Current.Previous->isOneOf(tok::equal, tok::at) && 1734 Line.MightBeFunctionDecl && Contexts.size() == 1) { 1735 // Line.MightBeFunctionDecl can only be true after the parentheses of a 1736 // function declaration have been found. 1737 Current.setType(TT_TrailingAnnotation); 1738 } else if ((Style.Language == FormatStyle::LK_Java || 1739 Style.Language == FormatStyle::LK_JavaScript) && 1740 Current.Previous) { 1741 if (Current.Previous->is(tok::at) && 1742 Current.isNot(Keywords.kw_interface)) { 1743 const FormatToken &AtToken = *Current.Previous; 1744 const FormatToken *Previous = AtToken.getPreviousNonComment(); 1745 if (!Previous || Previous->is(TT_LeadingJavaAnnotation)) 1746 Current.setType(TT_LeadingJavaAnnotation); 1747 else 1748 Current.setType(TT_JavaAnnotation); 1749 } else if (Current.Previous->is(tok::period) && 1750 Current.Previous->isOneOf(TT_JavaAnnotation, 1751 TT_LeadingJavaAnnotation)) { 1752 Current.setType(Current.Previous->getType()); 1753 } 1754 } 1755 } 1756 1757 /// Take a guess at whether \p Tok starts a name of a function or 1758 /// variable declaration. 1759 /// 1760 /// This is a heuristic based on whether \p Tok is an identifier following 1761 /// something that is likely a type. 1762 bool isStartOfName(const FormatToken &Tok) { 1763 if (Tok.isNot(tok::identifier) || !Tok.Previous) 1764 return false; 1765 1766 if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof, 1767 Keywords.kw_as)) 1768 return false; 1769 if (Style.Language == FormatStyle::LK_JavaScript && 1770 Tok.Previous->is(Keywords.kw_in)) 1771 return false; 1772 1773 // Skip "const" as it does not have an influence on whether this is a name. 1774 FormatToken *PreviousNotConst = Tok.getPreviousNonComment(); 1775 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const)) 1776 PreviousNotConst = PreviousNotConst->getPreviousNonComment(); 1777 1778 if (!PreviousNotConst) 1779 return false; 1780 1781 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) && 1782 PreviousNotConst->Previous && 1783 PreviousNotConst->Previous->is(tok::hash); 1784 1785 if (PreviousNotConst->is(TT_TemplateCloser)) 1786 return PreviousNotConst && PreviousNotConst->MatchingParen && 1787 PreviousNotConst->MatchingParen->Previous && 1788 PreviousNotConst->MatchingParen->Previous->isNot(tok::period) && 1789 PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template); 1790 1791 if (PreviousNotConst->is(tok::r_paren) && 1792 PreviousNotConst->is(TT_TypeDeclarationParen)) 1793 return true; 1794 1795 return (!IsPPKeyword && 1796 PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto)) || 1797 PreviousNotConst->is(TT_PointerOrReference) || 1798 PreviousNotConst->isSimpleTypeSpecifier(); 1799 } 1800 1801 /// Determine whether ')' is ending a cast. 1802 bool rParenEndsCast(const FormatToken &Tok) { 1803 // C-style casts are only used in C++, C# and Java. 1804 if (!Style.isCSharp() && !Style.isCpp() && 1805 Style.Language != FormatStyle::LK_Java) 1806 return false; 1807 1808 // Empty parens aren't casts and there are no casts at the end of the line. 1809 if (Tok.Previous == Tok.MatchingParen || !Tok.Next || !Tok.MatchingParen) 1810 return false; 1811 1812 FormatToken *LeftOfParens = Tok.MatchingParen->getPreviousNonComment(); 1813 if (LeftOfParens) { 1814 // If there is a closing parenthesis left of the current parentheses, 1815 // look past it as these might be chained casts. 1816 if (LeftOfParens->is(tok::r_paren)) { 1817 if (!LeftOfParens->MatchingParen || 1818 !LeftOfParens->MatchingParen->Previous) 1819 return false; 1820 LeftOfParens = LeftOfParens->MatchingParen->Previous; 1821 } 1822 1823 // If there is an identifier (or with a few exceptions a keyword) right 1824 // before the parentheses, this is unlikely to be a cast. 1825 if (LeftOfParens->Tok.getIdentifierInfo() && 1826 !LeftOfParens->isOneOf(Keywords.kw_in, tok::kw_return, tok::kw_case, 1827 tok::kw_delete)) 1828 return false; 1829 1830 // Certain other tokens right before the parentheses are also signals that 1831 // this cannot be a cast. 1832 if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator, 1833 TT_TemplateCloser, tok::ellipsis)) 1834 return false; 1835 } 1836 1837 if (Tok.Next->is(tok::question)) 1838 return false; 1839 1840 // `foreach((A a, B b) in someList)` should not be seen as a cast. 1841 if (Tok.Next->is(Keywords.kw_in) && Style.isCSharp()) 1842 return false; 1843 1844 // Functions which end with decorations like volatile, noexcept are unlikely 1845 // to be casts. 1846 if (Tok.Next->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const, 1847 tok::kw_requires, tok::kw_throw, tok::arrow, 1848 Keywords.kw_override, Keywords.kw_final) || 1849 isCpp11AttributeSpecifier(*Tok.Next)) 1850 return false; 1851 1852 // As Java has no function types, a "(" after the ")" likely means that this 1853 // is a cast. 1854 if (Style.Language == FormatStyle::LK_Java && Tok.Next->is(tok::l_paren)) 1855 return true; 1856 1857 // If a (non-string) literal follows, this is likely a cast. 1858 if (Tok.Next->isNot(tok::string_literal) && 1859 (Tok.Next->Tok.isLiteral() || 1860 Tok.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof))) 1861 return true; 1862 1863 // Heuristically try to determine whether the parentheses contain a type. 1864 auto IsQualifiedPointerOrReference = [](FormatToken *T) { 1865 // This is used to handle cases such as x = (foo *const)&y; 1866 assert(!T->isSimpleTypeSpecifier() && "Should have already been checked"); 1867 // Strip trailing qualifiers such as const or volatile when checking 1868 // whether the parens could be a cast to a pointer/reference type. 1869 while (T) { 1870 if (T->is(TT_AttributeParen)) { 1871 // Handle `x = (foo *__attribute__((foo)))&v;`: 1872 if (T->MatchingParen && T->MatchingParen->Previous && 1873 T->MatchingParen->Previous->is(tok::kw___attribute)) { 1874 T = T->MatchingParen->Previous->Previous; 1875 continue; 1876 } 1877 } else if (T->is(TT_AttributeSquare)) { 1878 // Handle `x = (foo *[[clang::foo]])&v;`: 1879 if (T->MatchingParen && T->MatchingParen->Previous) { 1880 T = T->MatchingParen->Previous; 1881 continue; 1882 } 1883 } else if (T->canBePointerOrReferenceQualifier()) { 1884 T = T->Previous; 1885 continue; 1886 } 1887 break; 1888 } 1889 return T && T->is(TT_PointerOrReference); 1890 }; 1891 bool ParensAreType = 1892 !Tok.Previous || 1893 Tok.Previous->isOneOf(TT_TemplateCloser, TT_TypeDeclarationParen) || 1894 Tok.Previous->isSimpleTypeSpecifier() || 1895 IsQualifiedPointerOrReference(Tok.Previous); 1896 bool ParensCouldEndDecl = 1897 Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater); 1898 if (ParensAreType && !ParensCouldEndDecl) 1899 return true; 1900 1901 // At this point, we heuristically assume that there are no casts at the 1902 // start of the line. We assume that we have found most cases where there 1903 // are by the logic above, e.g. "(void)x;". 1904 if (!LeftOfParens) 1905 return false; 1906 1907 // Certain token types inside the parentheses mean that this can't be a 1908 // cast. 1909 for (const FormatToken *Token = Tok.MatchingParen->Next; Token != &Tok; 1910 Token = Token->Next) 1911 if (Token->is(TT_BinaryOperator)) 1912 return false; 1913 1914 // If the following token is an identifier or 'this', this is a cast. All 1915 // cases where this can be something else are handled above. 1916 if (Tok.Next->isOneOf(tok::identifier, tok::kw_this)) 1917 return true; 1918 1919 if (Tok.Next->is(tok::l_paren) && 1920 !(Tok.Previous && Tok.Previous->is(tok::identifier) && 1921 Tok.Previous->Previous && 1922 Tok.Previous->Previous->isOneOf(tok::arrowstar, tok::arrow, 1923 tok::star))) 1924 return true; 1925 1926 if (!Tok.Next->Next) 1927 return false; 1928 1929 // If the next token after the parenthesis is a unary operator, assume 1930 // that this is cast, unless there are unexpected tokens inside the 1931 // parenthesis. 1932 bool NextIsUnary = 1933 Tok.Next->isUnaryOperator() || Tok.Next->isOneOf(tok::amp, tok::star); 1934 if (!NextIsUnary || Tok.Next->is(tok::plus) || 1935 !Tok.Next->Next->isOneOf(tok::identifier, tok::numeric_constant)) 1936 return false; 1937 // Search for unexpected tokens. 1938 for (FormatToken *Prev = Tok.Previous; Prev != Tok.MatchingParen; 1939 Prev = Prev->Previous) { 1940 if (!Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon)) 1941 return false; 1942 } 1943 return true; 1944 } 1945 1946 /// Return the type of the given token assuming it is * or &. 1947 TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression, 1948 bool InTemplateArgument) { 1949 if (Style.Language == FormatStyle::LK_JavaScript) 1950 return TT_BinaryOperator; 1951 1952 // && in C# must be a binary operator. 1953 if (Style.isCSharp() && Tok.is(tok::ampamp)) 1954 return TT_BinaryOperator; 1955 1956 const FormatToken *PrevToken = Tok.getPreviousNonComment(); 1957 if (!PrevToken) 1958 return TT_UnaryOperator; 1959 1960 const FormatToken *NextToken = Tok.getNextNonComment(); 1961 if (!NextToken || 1962 NextToken->isOneOf(tok::arrow, tok::equal, tok::kw_noexcept) || 1963 NextToken->canBePointerOrReferenceQualifier() || 1964 (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) 1965 return TT_PointerOrReference; 1966 1967 if (PrevToken->is(tok::coloncolon)) 1968 return TT_PointerOrReference; 1969 1970 if (PrevToken->is(tok::r_paren) && PrevToken->is(TT_TypeDeclarationParen)) 1971 return TT_PointerOrReference; 1972 1973 if (PrevToken->isOneOf(tok::l_paren, tok::l_square, tok::l_brace, 1974 tok::comma, tok::semi, tok::kw_return, tok::colon, 1975 tok::kw_co_return, tok::kw_co_await, 1976 tok::kw_co_yield, tok::equal, tok::kw_delete, 1977 tok::kw_sizeof, tok::kw_throw) || 1978 PrevToken->isOneOf(TT_BinaryOperator, TT_ConditionalExpr, 1979 TT_UnaryOperator, TT_CastRParen)) 1980 return TT_UnaryOperator; 1981 1982 if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare)) 1983 return TT_PointerOrReference; 1984 if (NextToken->is(tok::kw_operator) && !IsExpression) 1985 return TT_PointerOrReference; 1986 if (NextToken->isOneOf(tok::comma, tok::semi)) 1987 return TT_PointerOrReference; 1988 1989 if (PrevToken->Tok.isLiteral() || 1990 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true, 1991 tok::kw_false, tok::r_brace) || 1992 NextToken->Tok.isLiteral() || 1993 NextToken->isOneOf(tok::kw_true, tok::kw_false) || 1994 NextToken->isUnaryOperator() || 1995 // If we know we're in a template argument, there are no named 1996 // declarations. Thus, having an identifier on the right-hand side 1997 // indicates a binary operator. 1998 (InTemplateArgument && NextToken->Tok.isAnyIdentifier())) 1999 return TT_BinaryOperator; 2000 2001 // "&&(" is quite unlikely to be two successive unary "&". 2002 if (Tok.is(tok::ampamp) && NextToken->is(tok::l_paren)) 2003 return TT_BinaryOperator; 2004 2005 // This catches some cases where evaluation order is used as control flow: 2006 // aaa && aaa->f(); 2007 if (NextToken->Tok.isAnyIdentifier()) { 2008 const FormatToken *NextNextToken = NextToken->getNextNonComment(); 2009 if (NextNextToken && NextNextToken->is(tok::arrow)) 2010 return TT_BinaryOperator; 2011 } 2012 2013 // It is very unlikely that we are going to find a pointer or reference type 2014 // definition on the RHS of an assignment. 2015 if (IsExpression && !Contexts.back().CaretFound) 2016 return TT_BinaryOperator; 2017 2018 return TT_PointerOrReference; 2019 } 2020 2021 TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) { 2022 const FormatToken *PrevToken = Tok.getPreviousNonComment(); 2023 if (!PrevToken) 2024 return TT_UnaryOperator; 2025 2026 if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator)) 2027 // This must be a sequence of leading unary operators. 2028 return TT_UnaryOperator; 2029 2030 // Use heuristics to recognize unary operators. 2031 if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square, 2032 tok::question, tok::colon, tok::kw_return, 2033 tok::kw_case, tok::at, tok::l_brace, tok::kw_throw, 2034 tok::kw_co_return, tok::kw_co_yield)) 2035 return TT_UnaryOperator; 2036 2037 // There can't be two consecutive binary operators. 2038 if (PrevToken->is(TT_BinaryOperator)) 2039 return TT_UnaryOperator; 2040 2041 // Fall back to marking the token as binary operator. 2042 return TT_BinaryOperator; 2043 } 2044 2045 /// Determine whether ++/-- are pre- or post-increments/-decrements. 2046 TokenType determineIncrementUsage(const FormatToken &Tok) { 2047 const FormatToken *PrevToken = Tok.getPreviousNonComment(); 2048 if (!PrevToken || PrevToken->is(TT_CastRParen)) 2049 return TT_UnaryOperator; 2050 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier)) 2051 return TT_TrailingUnaryOperator; 2052 2053 return TT_UnaryOperator; 2054 } 2055 2056 SmallVector<Context, 8> Contexts; 2057 2058 const FormatStyle &Style; 2059 AnnotatedLine &Line; 2060 FormatToken *CurrentToken; 2061 bool AutoFound; 2062 const AdditionalKeywords &Keywords; 2063 2064 // Set of "<" tokens that do not open a template parameter list. If parseAngle 2065 // determines that a specific token can't be a template opener, it will make 2066 // same decision irrespective of the decisions for tokens leading up to it. 2067 // Store this information to prevent this from causing exponential runtime. 2068 llvm::SmallPtrSet<FormatToken *, 16> NonTemplateLess; 2069 }; 2070 2071 static const int PrecedenceUnaryOperator = prec::PointerToMember + 1; 2072 static const int PrecedenceArrowAndPeriod = prec::PointerToMember + 2; 2073 2074 /// Parses binary expressions by inserting fake parenthesis based on 2075 /// operator precedence. 2076 class ExpressionParser { 2077 public: 2078 ExpressionParser(const FormatStyle &Style, const AdditionalKeywords &Keywords, 2079 AnnotatedLine &Line) 2080 : Style(Style), Keywords(Keywords), Current(Line.First) {} 2081 2082 /// Parse expressions with the given operator precedence. 2083 void parse(int Precedence = 0) { 2084 // Skip 'return' and ObjC selector colons as they are not part of a binary 2085 // expression. 2086 while (Current && (Current->is(tok::kw_return) || 2087 (Current->is(tok::colon) && 2088 Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)))) 2089 next(); 2090 2091 if (!Current || Precedence > PrecedenceArrowAndPeriod) 2092 return; 2093 2094 // Conditional expressions need to be parsed separately for proper nesting. 2095 if (Precedence == prec::Conditional) { 2096 parseConditionalExpr(); 2097 return; 2098 } 2099 2100 // Parse unary operators, which all have a higher precedence than binary 2101 // operators. 2102 if (Precedence == PrecedenceUnaryOperator) { 2103 parseUnaryOperator(); 2104 return; 2105 } 2106 2107 FormatToken *Start = Current; 2108 FormatToken *LatestOperator = nullptr; 2109 unsigned OperatorIndex = 0; 2110 2111 while (Current) { 2112 // Consume operators with higher precedence. 2113 parse(Precedence + 1); 2114 2115 int CurrentPrecedence = getCurrentPrecedence(); 2116 2117 if (Current && Current->is(TT_SelectorName) && 2118 Precedence == CurrentPrecedence) { 2119 if (LatestOperator) 2120 addFakeParenthesis(Start, prec::Level(Precedence)); 2121 Start = Current; 2122 } 2123 2124 // At the end of the line or when an operator with higher precedence is 2125 // found, insert fake parenthesis and return. 2126 if (!Current || 2127 (Current->closesScope() && 2128 (Current->MatchingParen || Current->is(TT_TemplateString))) || 2129 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) || 2130 (CurrentPrecedence == prec::Conditional && 2131 Precedence == prec::Assignment && Current->is(tok::colon))) { 2132 break; 2133 } 2134 2135 // Consume scopes: (), [], <> and {} 2136 if (Current->opensScope()) { 2137 // In fragment of a JavaScript template string can look like '}..${' and 2138 // thus close a scope and open a new one at the same time. 2139 while (Current && (!Current->closesScope() || Current->opensScope())) { 2140 next(); 2141 parse(); 2142 } 2143 next(); 2144 } else { 2145 // Operator found. 2146 if (CurrentPrecedence == Precedence) { 2147 if (LatestOperator) 2148 LatestOperator->NextOperator = Current; 2149 LatestOperator = Current; 2150 Current->OperatorIndex = OperatorIndex; 2151 ++OperatorIndex; 2152 } 2153 next(/*SkipPastLeadingComments=*/Precedence > 0); 2154 } 2155 } 2156 2157 if (LatestOperator && (Current || Precedence > 0)) { 2158 // LatestOperator->LastOperator = true; 2159 if (Precedence == PrecedenceArrowAndPeriod) { 2160 // Call expressions don't have a binary operator precedence. 2161 addFakeParenthesis(Start, prec::Unknown); 2162 } else { 2163 addFakeParenthesis(Start, prec::Level(Precedence)); 2164 } 2165 } 2166 } 2167 2168 private: 2169 /// Gets the precedence (+1) of the given token for binary operators 2170 /// and other tokens that we treat like binary operators. 2171 int getCurrentPrecedence() { 2172 if (Current) { 2173 const FormatToken *NextNonComment = Current->getNextNonComment(); 2174 if (Current->is(TT_ConditionalExpr)) 2175 return prec::Conditional; 2176 if (NextNonComment && Current->is(TT_SelectorName) && 2177 (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) || 2178 ((Style.Language == FormatStyle::LK_Proto || 2179 Style.Language == FormatStyle::LK_TextProto) && 2180 NextNonComment->is(tok::less)))) 2181 return prec::Assignment; 2182 if (Current->is(TT_JsComputedPropertyName)) 2183 return prec::Assignment; 2184 if (Current->is(TT_LambdaArrow)) 2185 return prec::Comma; 2186 if (Current->is(TT_JsFatArrow)) 2187 return prec::Assignment; 2188 if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) || 2189 (Current->is(tok::comment) && NextNonComment && 2190 NextNonComment->is(TT_SelectorName))) 2191 return 0; 2192 if (Current->is(TT_RangeBasedForLoopColon)) 2193 return prec::Comma; 2194 if ((Style.Language == FormatStyle::LK_Java || 2195 Style.Language == FormatStyle::LK_JavaScript) && 2196 Current->is(Keywords.kw_instanceof)) 2197 return prec::Relational; 2198 if (Style.Language == FormatStyle::LK_JavaScript && 2199 Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) 2200 return prec::Relational; 2201 if (Current->is(TT_BinaryOperator) || Current->is(tok::comma)) 2202 return Current->getPrecedence(); 2203 if (Current->isOneOf(tok::period, tok::arrow)) 2204 return PrecedenceArrowAndPeriod; 2205 if ((Style.Language == FormatStyle::LK_Java || 2206 Style.Language == FormatStyle::LK_JavaScript) && 2207 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements, 2208 Keywords.kw_throws)) 2209 return 0; 2210 } 2211 return -1; 2212 } 2213 2214 void addFakeParenthesis(FormatToken *Start, prec::Level Precedence) { 2215 Start->FakeLParens.push_back(Precedence); 2216 if (Precedence > prec::Unknown) 2217 Start->StartsBinaryExpression = true; 2218 if (Current) { 2219 FormatToken *Previous = Current->Previous; 2220 while (Previous->is(tok::comment) && Previous->Previous) 2221 Previous = Previous->Previous; 2222 ++Previous->FakeRParens; 2223 if (Precedence > prec::Unknown) 2224 Previous->EndsBinaryExpression = true; 2225 } 2226 } 2227 2228 /// Parse unary operator expressions and surround them with fake 2229 /// parentheses if appropriate. 2230 void parseUnaryOperator() { 2231 llvm::SmallVector<FormatToken *, 2> Tokens; 2232 while (Current && Current->is(TT_UnaryOperator)) { 2233 Tokens.push_back(Current); 2234 next(); 2235 } 2236 parse(PrecedenceArrowAndPeriod); 2237 for (FormatToken *Token : llvm::reverse(Tokens)) 2238 // The actual precedence doesn't matter. 2239 addFakeParenthesis(Token, prec::Unknown); 2240 } 2241 2242 void parseConditionalExpr() { 2243 while (Current && Current->isTrailingComment()) { 2244 next(); 2245 } 2246 FormatToken *Start = Current; 2247 parse(prec::LogicalOr); 2248 if (!Current || !Current->is(tok::question)) 2249 return; 2250 next(); 2251 parse(prec::Assignment); 2252 if (!Current || Current->isNot(TT_ConditionalExpr)) 2253 return; 2254 next(); 2255 parse(prec::Assignment); 2256 addFakeParenthesis(Start, prec::Conditional); 2257 } 2258 2259 void next(bool SkipPastLeadingComments = true) { 2260 if (Current) 2261 Current = Current->Next; 2262 while (Current && 2263 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) && 2264 Current->isTrailingComment()) 2265 Current = Current->Next; 2266 } 2267 2268 const FormatStyle &Style; 2269 const AdditionalKeywords &Keywords; 2270 FormatToken *Current; 2271 }; 2272 2273 } // end anonymous namespace 2274 2275 void TokenAnnotator::setCommentLineLevels( 2276 SmallVectorImpl<AnnotatedLine *> &Lines) { 2277 const AnnotatedLine *NextNonCommentLine = nullptr; 2278 for (SmallVectorImpl<AnnotatedLine *>::reverse_iterator I = Lines.rbegin(), 2279 E = Lines.rend(); 2280 I != E; ++I) { 2281 bool CommentLine = true; 2282 for (const FormatToken *Tok = (*I)->First; Tok; Tok = Tok->Next) { 2283 if (!Tok->is(tok::comment)) { 2284 CommentLine = false; 2285 break; 2286 } 2287 } 2288 2289 // If the comment is currently aligned with the line immediately following 2290 // it, that's probably intentional and we should keep it. 2291 if (NextNonCommentLine && CommentLine && 2292 NextNonCommentLine->First->NewlinesBefore <= 1 && 2293 NextNonCommentLine->First->OriginalColumn == 2294 (*I)->First->OriginalColumn) { 2295 // Align comments for preprocessor lines with the # in column 0 if 2296 // preprocessor lines are not indented. Otherwise, align with the next 2297 // line. 2298 (*I)->Level = 2299 (Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash && 2300 (NextNonCommentLine->Type == LT_PreprocessorDirective || 2301 NextNonCommentLine->Type == LT_ImportStatement)) 2302 ? 0 2303 : NextNonCommentLine->Level; 2304 } else { 2305 NextNonCommentLine = (*I)->First->isNot(tok::r_brace) ? (*I) : nullptr; 2306 } 2307 2308 setCommentLineLevels((*I)->Children); 2309 } 2310 } 2311 2312 static unsigned maxNestingDepth(const AnnotatedLine &Line) { 2313 unsigned Result = 0; 2314 for (const auto *Tok = Line.First; Tok != nullptr; Tok = Tok->Next) 2315 Result = std::max(Result, Tok->NestingLevel); 2316 return Result; 2317 } 2318 2319 void TokenAnnotator::annotate(AnnotatedLine &Line) { 2320 for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(), 2321 E = Line.Children.end(); 2322 I != E; ++I) { 2323 annotate(**I); 2324 } 2325 AnnotatingParser Parser(Style, Line, Keywords); 2326 Line.Type = Parser.parseLine(); 2327 2328 // With very deep nesting, ExpressionParser uses lots of stack and the 2329 // formatting algorithm is very slow. We're not going to do a good job here 2330 // anyway - it's probably generated code being formatted by mistake. 2331 // Just skip the whole line. 2332 if (maxNestingDepth(Line) > 50) 2333 Line.Type = LT_Invalid; 2334 2335 if (Line.Type == LT_Invalid) 2336 return; 2337 2338 ExpressionParser ExprParser(Style, Keywords, Line); 2339 ExprParser.parse(); 2340 2341 if (Line.startsWith(TT_ObjCMethodSpecifier)) 2342 Line.Type = LT_ObjCMethodDecl; 2343 else if (Line.startsWith(TT_ObjCDecl)) 2344 Line.Type = LT_ObjCDecl; 2345 else if (Line.startsWith(TT_ObjCProperty)) 2346 Line.Type = LT_ObjCProperty; 2347 2348 Line.First->SpacesRequiredBefore = 1; 2349 Line.First->CanBreakBefore = Line.First->MustBreakBefore; 2350 } 2351 2352 // This function heuristically determines whether 'Current' starts the name of a 2353 // function declaration. 2354 static bool isFunctionDeclarationName(const FormatToken &Current, 2355 const AnnotatedLine &Line) { 2356 auto skipOperatorName = [](const FormatToken *Next) -> const FormatToken * { 2357 for (; Next; Next = Next->Next) { 2358 if (Next->is(TT_OverloadedOperatorLParen)) 2359 return Next; 2360 if (Next->is(TT_OverloadedOperator)) 2361 continue; 2362 if (Next->isOneOf(tok::kw_new, tok::kw_delete)) { 2363 // For 'new[]' and 'delete[]'. 2364 if (Next->Next && 2365 Next->Next->startsSequence(tok::l_square, tok::r_square)) 2366 Next = Next->Next->Next; 2367 continue; 2368 } 2369 if (Next->startsSequence(tok::l_square, tok::r_square)) { 2370 // For operator[](). 2371 Next = Next->Next; 2372 continue; 2373 } 2374 if ((Next->isSimpleTypeSpecifier() || Next->is(tok::identifier)) && 2375 Next->Next && Next->Next->isOneOf(tok::star, tok::amp, tok::ampamp)) { 2376 // For operator void*(), operator char*(), operator Foo*(). 2377 Next = Next->Next; 2378 continue; 2379 } 2380 if (Next->is(TT_TemplateOpener) && Next->MatchingParen) { 2381 Next = Next->MatchingParen; 2382 continue; 2383 } 2384 2385 break; 2386 } 2387 return nullptr; 2388 }; 2389 2390 // Find parentheses of parameter list. 2391 const FormatToken *Next = Current.Next; 2392 if (Current.is(tok::kw_operator)) { 2393 if (Current.Previous && Current.Previous->is(tok::coloncolon)) 2394 return false; 2395 Next = skipOperatorName(Next); 2396 } else { 2397 if (!Current.is(TT_StartOfName) || Current.NestingLevel != 0) 2398 return false; 2399 for (; Next; Next = Next->Next) { 2400 if (Next->is(TT_TemplateOpener)) { 2401 Next = Next->MatchingParen; 2402 } else if (Next->is(tok::coloncolon)) { 2403 Next = Next->Next; 2404 if (!Next) 2405 return false; 2406 if (Next->is(tok::kw_operator)) { 2407 Next = skipOperatorName(Next->Next); 2408 break; 2409 } 2410 if (!Next->is(tok::identifier)) 2411 return false; 2412 } else if (Next->is(tok::l_paren)) { 2413 break; 2414 } else { 2415 return false; 2416 } 2417 } 2418 } 2419 2420 // Check whether parameter list can belong to a function declaration. 2421 if (!Next || !Next->is(tok::l_paren) || !Next->MatchingParen) 2422 return false; 2423 // If the lines ends with "{", this is likely an function definition. 2424 if (Line.Last->is(tok::l_brace)) 2425 return true; 2426 if (Next->Next == Next->MatchingParen) 2427 return true; // Empty parentheses. 2428 // If there is an &/&& after the r_paren, this is likely a function. 2429 if (Next->MatchingParen->Next && 2430 Next->MatchingParen->Next->is(TT_PointerOrReference)) 2431 return true; 2432 for (const FormatToken *Tok = Next->Next; Tok && Tok != Next->MatchingParen; 2433 Tok = Tok->Next) { 2434 if (Tok->is(TT_TypeDeclarationParen)) 2435 return true; 2436 if (Tok->isOneOf(tok::l_paren, TT_TemplateOpener) && Tok->MatchingParen) { 2437 Tok = Tok->MatchingParen; 2438 continue; 2439 } 2440 if (Tok->is(tok::kw_const) || Tok->isSimpleTypeSpecifier() || 2441 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) 2442 return true; 2443 if (Tok->isOneOf(tok::l_brace, tok::string_literal, TT_ObjCMethodExpr) || 2444 Tok->Tok.isLiteral()) 2445 return false; 2446 } 2447 return false; 2448 } 2449 2450 bool TokenAnnotator::mustBreakForReturnType(const AnnotatedLine &Line) const { 2451 assert(Line.MightBeFunctionDecl); 2452 2453 if ((Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_TopLevel || 2454 Style.AlwaysBreakAfterReturnType == 2455 FormatStyle::RTBS_TopLevelDefinitions) && 2456 Line.Level > 0) 2457 return false; 2458 2459 switch (Style.AlwaysBreakAfterReturnType) { 2460 case FormatStyle::RTBS_None: 2461 return false; 2462 case FormatStyle::RTBS_All: 2463 case FormatStyle::RTBS_TopLevel: 2464 return true; 2465 case FormatStyle::RTBS_AllDefinitions: 2466 case FormatStyle::RTBS_TopLevelDefinitions: 2467 return Line.mightBeFunctionDefinition(); 2468 } 2469 2470 return false; 2471 } 2472 2473 void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) { 2474 for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(), 2475 E = Line.Children.end(); 2476 I != E; ++I) { 2477 calculateFormattingInformation(**I); 2478 } 2479 2480 Line.First->TotalLength = 2481 Line.First->IsMultiline ? Style.ColumnLimit 2482 : Line.FirstStartColumn + Line.First->ColumnWidth; 2483 FormatToken *Current = Line.First->Next; 2484 bool InFunctionDecl = Line.MightBeFunctionDecl; 2485 while (Current) { 2486 if (isFunctionDeclarationName(*Current, Line)) 2487 Current->setType(TT_FunctionDeclarationName); 2488 if (Current->is(TT_LineComment)) { 2489 if (Current->Previous->is(BK_BracedInit) && 2490 Current->Previous->opensScope()) 2491 Current->SpacesRequiredBefore = 2492 (Style.Cpp11BracedListStyle && !Style.SpacesInParentheses) ? 0 : 1; 2493 else 2494 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments; 2495 2496 // If we find a trailing comment, iterate backwards to determine whether 2497 // it seems to relate to a specific parameter. If so, break before that 2498 // parameter to avoid changing the comment's meaning. E.g. don't move 'b' 2499 // to the previous line in: 2500 // SomeFunction(a, 2501 // b, // comment 2502 // c); 2503 if (!Current->HasUnescapedNewline) { 2504 for (FormatToken *Parameter = Current->Previous; Parameter; 2505 Parameter = Parameter->Previous) { 2506 if (Parameter->isOneOf(tok::comment, tok::r_brace)) 2507 break; 2508 if (Parameter->Previous && Parameter->Previous->is(tok::comma)) { 2509 if (!Parameter->Previous->is(TT_CtorInitializerComma) && 2510 Parameter->HasUnescapedNewline) 2511 Parameter->MustBreakBefore = true; 2512 break; 2513 } 2514 } 2515 } 2516 } else if (Current->SpacesRequiredBefore == 0 && 2517 spaceRequiredBefore(Line, *Current)) { 2518 Current->SpacesRequiredBefore = 1; 2519 } 2520 2521 Current->MustBreakBefore = 2522 Current->MustBreakBefore || mustBreakBefore(Line, *Current); 2523 2524 if (!Current->MustBreakBefore && InFunctionDecl && 2525 Current->is(TT_FunctionDeclarationName)) 2526 Current->MustBreakBefore = mustBreakForReturnType(Line); 2527 2528 Current->CanBreakBefore = 2529 Current->MustBreakBefore || canBreakBefore(Line, *Current); 2530 unsigned ChildSize = 0; 2531 if (Current->Previous->Children.size() == 1) { 2532 FormatToken &LastOfChild = *Current->Previous->Children[0]->Last; 2533 ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit 2534 : LastOfChild.TotalLength + 1; 2535 } 2536 const FormatToken *Prev = Current->Previous; 2537 if (Current->MustBreakBefore || Prev->Children.size() > 1 || 2538 (Prev->Children.size() == 1 && 2539 Prev->Children[0]->First->MustBreakBefore) || 2540 Current->IsMultiline) 2541 Current->TotalLength = Prev->TotalLength + Style.ColumnLimit; 2542 else 2543 Current->TotalLength = Prev->TotalLength + Current->ColumnWidth + 2544 ChildSize + Current->SpacesRequiredBefore; 2545 2546 if (Current->is(TT_CtorInitializerColon)) 2547 InFunctionDecl = false; 2548 2549 // FIXME: Only calculate this if CanBreakBefore is true once static 2550 // initializers etc. are sorted out. 2551 // FIXME: Move magic numbers to a better place. 2552 2553 // Reduce penalty for aligning ObjC method arguments using the colon 2554 // alignment as this is the canonical way (still prefer fitting everything 2555 // into one line if possible). Trying to fit a whole expression into one 2556 // line should not force other line breaks (e.g. when ObjC method 2557 // expression is a part of other expression). 2558 Current->SplitPenalty = splitPenalty(Line, *Current, InFunctionDecl); 2559 if (Style.Language == FormatStyle::LK_ObjC && 2560 Current->is(TT_SelectorName) && Current->ParameterIndex > 0) { 2561 if (Current->ParameterIndex == 1) 2562 Current->SplitPenalty += 5 * Current->BindingStrength; 2563 } else { 2564 Current->SplitPenalty += 20 * Current->BindingStrength; 2565 } 2566 2567 Current = Current->Next; 2568 } 2569 2570 calculateUnbreakableTailLengths(Line); 2571 unsigned IndentLevel = Line.Level; 2572 for (Current = Line.First; Current != nullptr; Current = Current->Next) { 2573 if (Current->Role) 2574 Current->Role->precomputeFormattingInfos(Current); 2575 if (Current->MatchingParen && 2576 Current->MatchingParen->opensBlockOrBlockTypeList(Style)) { 2577 assert(IndentLevel > 0); 2578 --IndentLevel; 2579 } 2580 Current->IndentLevel = IndentLevel; 2581 if (Current->opensBlockOrBlockTypeList(Style)) 2582 ++IndentLevel; 2583 } 2584 2585 LLVM_DEBUG({ printDebugInfo(Line); }); 2586 } 2587 2588 void TokenAnnotator::calculateUnbreakableTailLengths(AnnotatedLine &Line) { 2589 unsigned UnbreakableTailLength = 0; 2590 FormatToken *Current = Line.Last; 2591 while (Current) { 2592 Current->UnbreakableTailLength = UnbreakableTailLength; 2593 if (Current->CanBreakBefore || 2594 Current->isOneOf(tok::comment, tok::string_literal)) { 2595 UnbreakableTailLength = 0; 2596 } else { 2597 UnbreakableTailLength += 2598 Current->ColumnWidth + Current->SpacesRequiredBefore; 2599 } 2600 Current = Current->Previous; 2601 } 2602 } 2603 2604 unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line, 2605 const FormatToken &Tok, 2606 bool InFunctionDecl) { 2607 const FormatToken &Left = *Tok.Previous; 2608 const FormatToken &Right = Tok; 2609 2610 if (Left.is(tok::semi)) 2611 return 0; 2612 2613 if (Style.Language == FormatStyle::LK_Java) { 2614 if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws)) 2615 return 1; 2616 if (Right.is(Keywords.kw_implements)) 2617 return 2; 2618 if (Left.is(tok::comma) && Left.NestingLevel == 0) 2619 return 3; 2620 } else if (Style.Language == FormatStyle::LK_JavaScript) { 2621 if (Right.is(Keywords.kw_function) && Left.isNot(tok::comma)) 2622 return 100; 2623 if (Left.is(TT_JsTypeColon)) 2624 return 35; 2625 if ((Left.is(TT_TemplateString) && Left.TokenText.endswith("${")) || 2626 (Right.is(TT_TemplateString) && Right.TokenText.startswith("}"))) 2627 return 100; 2628 // Prefer breaking call chains (".foo") over empty "{}", "[]" or "()". 2629 if (Left.opensScope() && Right.closesScope()) 2630 return 200; 2631 } 2632 2633 if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral)) 2634 return 1; 2635 if (Right.is(tok::l_square)) { 2636 if (Style.Language == FormatStyle::LK_Proto) 2637 return 1; 2638 if (Left.is(tok::r_square)) 2639 return 200; 2640 // Slightly prefer formatting local lambda definitions like functions. 2641 if (Right.is(TT_LambdaLSquare) && Left.is(tok::equal)) 2642 return 35; 2643 if (!Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare, 2644 TT_ArrayInitializerLSquare, 2645 TT_DesignatedInitializerLSquare, TT_AttributeSquare)) 2646 return 500; 2647 } 2648 2649 if (Left.is(tok::coloncolon) || 2650 (Right.is(tok::period) && Style.Language == FormatStyle::LK_Proto)) 2651 return 500; 2652 if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) || 2653 Right.is(tok::kw_operator)) { 2654 if (Line.startsWith(tok::kw_for) && Right.PartOfMultiVariableDeclStmt) 2655 return 3; 2656 if (Left.is(TT_StartOfName)) 2657 return 110; 2658 if (InFunctionDecl && Right.NestingLevel == 0) 2659 return Style.PenaltyReturnTypeOnItsOwnLine; 2660 return 200; 2661 } 2662 if (Right.is(TT_PointerOrReference)) 2663 return 190; 2664 if (Right.is(TT_LambdaArrow)) 2665 return 110; 2666 if (Left.is(tok::equal) && Right.is(tok::l_brace)) 2667 return 160; 2668 if (Left.is(TT_CastRParen)) 2669 return 100; 2670 if (Left.isOneOf(tok::kw_class, tok::kw_struct)) 2671 return 5000; 2672 if (Left.is(tok::comment)) 2673 return 1000; 2674 2675 if (Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon, 2676 TT_CtorInitializerColon)) 2677 return 2; 2678 2679 if (Right.isMemberAccess()) { 2680 // Breaking before the "./->" of a chained call/member access is reasonably 2681 // cheap, as formatting those with one call per line is generally 2682 // desirable. In particular, it should be cheaper to break before the call 2683 // than it is to break inside a call's parameters, which could lead to weird 2684 // "hanging" indents. The exception is the very last "./->" to support this 2685 // frequent pattern: 2686 // 2687 // aaaaaaaa.aaaaaaaa.bbbbbbb().ccccccccccccccccccccc( 2688 // dddddddd); 2689 // 2690 // which might otherwise be blown up onto many lines. Here, clang-format 2691 // won't produce "hanging" indents anyway as there is no other trailing 2692 // call. 2693 // 2694 // Also apply higher penalty is not a call as that might lead to a wrapping 2695 // like: 2696 // 2697 // aaaaaaa 2698 // .aaaaaaaaa.bbbbbbbb(cccccccc); 2699 return !Right.NextOperator || !Right.NextOperator->Previous->closesScope() 2700 ? 150 2701 : 35; 2702 } 2703 2704 if (Right.is(TT_TrailingAnnotation) && 2705 (!Right.Next || Right.Next->isNot(tok::l_paren))) { 2706 // Moving trailing annotations to the next line is fine for ObjC method 2707 // declarations. 2708 if (Line.startsWith(TT_ObjCMethodSpecifier)) 2709 return 10; 2710 // Generally, breaking before a trailing annotation is bad unless it is 2711 // function-like. It seems to be especially preferable to keep standard 2712 // annotations (i.e. "const", "final" and "override") on the same line. 2713 // Use a slightly higher penalty after ")" so that annotations like 2714 // "const override" are kept together. 2715 bool is_short_annotation = Right.TokenText.size() < 10; 2716 return (Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0); 2717 } 2718 2719 // In for-loops, prefer breaking at ',' and ';'. 2720 if (Line.startsWith(tok::kw_for) && Left.is(tok::equal)) 2721 return 4; 2722 2723 // In Objective-C method expressions, prefer breaking before "param:" over 2724 // breaking after it. 2725 if (Right.is(TT_SelectorName)) 2726 return 0; 2727 if (Left.is(tok::colon) && Left.is(TT_ObjCMethodExpr)) 2728 return Line.MightBeFunctionDecl ? 50 : 500; 2729 2730 // In Objective-C type declarations, avoid breaking after the category's 2731 // open paren (we'll prefer breaking after the protocol list's opening 2732 // angle bracket, if present). 2733 if (Line.Type == LT_ObjCDecl && Left.is(tok::l_paren) && Left.Previous && 2734 Left.Previous->isOneOf(tok::identifier, tok::greater)) 2735 return 500; 2736 2737 if (Left.is(tok::l_paren) && InFunctionDecl && 2738 Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign) 2739 return 100; 2740 if (Left.is(tok::l_paren) && Left.Previous && 2741 (Left.Previous->is(tok::kw_for) || Left.Previous->isIf())) 2742 return 1000; 2743 if (Left.is(tok::equal) && InFunctionDecl) 2744 return 110; 2745 if (Right.is(tok::r_brace)) 2746 return 1; 2747 if (Left.is(TT_TemplateOpener)) 2748 return 100; 2749 if (Left.opensScope()) { 2750 // If we aren't aligning after opening parens/braces we can always break 2751 // here unless the style does not want us to place all arguments on the 2752 // next line. 2753 if (Style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign && 2754 (Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) 2755 return 0; 2756 if (Left.is(tok::l_brace) && !Style.Cpp11BracedListStyle) 2757 return 19; 2758 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter 2759 : 19; 2760 } 2761 if (Left.is(TT_JavaAnnotation)) 2762 return 50; 2763 2764 if (Left.is(TT_UnaryOperator)) 2765 return 60; 2766 if (Left.isOneOf(tok::plus, tok::comma) && Left.Previous && 2767 Left.Previous->isLabelString() && 2768 (Left.NextOperator || Left.OperatorIndex != 0)) 2769 return 50; 2770 if (Right.is(tok::plus) && Left.isLabelString() && 2771 (Right.NextOperator || Right.OperatorIndex != 0)) 2772 return 25; 2773 if (Left.is(tok::comma)) 2774 return 1; 2775 if (Right.is(tok::lessless) && Left.isLabelString() && 2776 (Right.NextOperator || Right.OperatorIndex != 1)) 2777 return 25; 2778 if (Right.is(tok::lessless)) { 2779 // Breaking at a << is really cheap. 2780 if (!Left.is(tok::r_paren) || Right.OperatorIndex > 0) 2781 // Slightly prefer to break before the first one in log-like statements. 2782 return 2; 2783 return 1; 2784 } 2785 if (Left.ClosesTemplateDeclaration) 2786 return Style.PenaltyBreakTemplateDeclaration; 2787 if (Left.is(TT_ConditionalExpr)) 2788 return prec::Conditional; 2789 prec::Level Level = Left.getPrecedence(); 2790 if (Level == prec::Unknown) 2791 Level = Right.getPrecedence(); 2792 if (Level == prec::Assignment) 2793 return Style.PenaltyBreakAssignment; 2794 if (Level != prec::Unknown) 2795 return Level; 2796 2797 return 3; 2798 } 2799 2800 bool TokenAnnotator::spaceRequiredBeforeParens(const FormatToken &Right) const { 2801 return Style.SpaceBeforeParens == FormatStyle::SBPO_Always || 2802 (Style.SpaceBeforeParens == FormatStyle::SBPO_NonEmptyParentheses && 2803 Right.ParameterCount > 0); 2804 } 2805 2806 bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, 2807 const FormatToken &Left, 2808 const FormatToken &Right) { 2809 if (Left.is(tok::kw_return) && Right.isNot(tok::semi)) 2810 return true; 2811 if (Left.is(Keywords.kw_assert) && Style.Language == FormatStyle::LK_Java) 2812 return true; 2813 if (Style.ObjCSpaceAfterProperty && Line.Type == LT_ObjCProperty && 2814 Left.Tok.getObjCKeywordID() == tok::objc_property) 2815 return true; 2816 if (Right.is(tok::hashhash)) 2817 return Left.is(tok::hash); 2818 if (Left.isOneOf(tok::hashhash, tok::hash)) 2819 return Right.is(tok::hash); 2820 if ((Left.is(tok::l_paren) && Right.is(tok::r_paren)) || 2821 (Left.is(tok::l_brace) && Left.isNot(BK_Block) && 2822 Right.is(tok::r_brace) && Right.isNot(BK_Block))) 2823 return Style.SpaceInEmptyParentheses; 2824 if (Style.SpacesInConditionalStatement) { 2825 if (Left.is(tok::l_paren) && Left.Previous && 2826 isKeywordWithCondition(*Left.Previous)) 2827 return true; 2828 if (Right.is(tok::r_paren) && Right.MatchingParen && 2829 Right.MatchingParen->Previous && 2830 isKeywordWithCondition(*Right.MatchingParen->Previous)) 2831 return true; 2832 } 2833 2834 // requires ( or requires( 2835 if (Right.is(tok::l_paren) && Left.is(tok::kw_requires)) 2836 return spaceRequiredBeforeParens(Right); 2837 // requires clause Concept1<T> && Concept2<T> 2838 if (Left.is(TT_ConstraintJunctions) && Right.is(tok::identifier)) 2839 return true; 2840 2841 if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) 2842 return (Right.is(TT_CastRParen) || 2843 (Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen))) 2844 ? Style.SpacesInCStyleCastParentheses 2845 : Style.SpacesInParentheses; 2846 if (Right.isOneOf(tok::semi, tok::comma)) 2847 return false; 2848 if (Right.is(tok::less) && Line.Type == LT_ObjCDecl) { 2849 bool IsLightweightGeneric = Right.MatchingParen && 2850 Right.MatchingParen->Next && 2851 Right.MatchingParen->Next->is(tok::colon); 2852 return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList; 2853 } 2854 if (Right.is(tok::less) && Left.is(tok::kw_template)) 2855 return Style.SpaceAfterTemplateKeyword; 2856 if (Left.isOneOf(tok::exclaim, tok::tilde)) 2857 return false; 2858 if (Left.is(tok::at) && 2859 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant, 2860 tok::numeric_constant, tok::l_paren, tok::l_brace, 2861 tok::kw_true, tok::kw_false)) 2862 return false; 2863 if (Left.is(tok::colon)) 2864 return !Left.is(TT_ObjCMethodExpr); 2865 if (Left.is(tok::coloncolon)) 2866 return false; 2867 if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less)) { 2868 if (Style.Language == FormatStyle::LK_TextProto || 2869 (Style.Language == FormatStyle::LK_Proto && 2870 (Left.is(TT_DictLiteral) || Right.is(TT_DictLiteral)))) { 2871 // Format empty list as `<>`. 2872 if (Left.is(tok::less) && Right.is(tok::greater)) 2873 return false; 2874 return !Style.Cpp11BracedListStyle; 2875 } 2876 return false; 2877 } 2878 if (Right.is(tok::ellipsis)) 2879 return Left.Tok.isLiteral() || (Left.is(tok::identifier) && Left.Previous && 2880 Left.Previous->is(tok::kw_case)); 2881 if (Left.is(tok::l_square) && Right.is(tok::amp)) 2882 return Style.SpacesInSquareBrackets; 2883 if (Right.is(TT_PointerOrReference)) { 2884 if (Left.is(tok::r_paren) && Line.MightBeFunctionDecl) { 2885 if (!Left.MatchingParen) 2886 return true; 2887 FormatToken *TokenBeforeMatchingParen = 2888 Left.MatchingParen->getPreviousNonComment(); 2889 if (!TokenBeforeMatchingParen || !Left.is(TT_TypeDeclarationParen)) 2890 return true; 2891 } 2892 // Add a space if the previous token is a pointer qualifer or the closing 2893 // parenthesis of __attribute__(()) expression and the style requires spaces 2894 // after pointer qualifiers. 2895 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_After || 2896 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) && 2897 (Left.is(TT_AttributeParen) || Left.canBePointerOrReferenceQualifier())) 2898 return true; 2899 return (Left.Tok.isLiteral() || 2900 (!Left.isOneOf(TT_PointerOrReference, tok::l_paren) && 2901 (Style.PointerAlignment != FormatStyle::PAS_Left || 2902 (Line.IsMultiVariableDeclStmt && 2903 (Left.NestingLevel == 0 || 2904 (Left.NestingLevel == 1 && Line.First->is(tok::kw_for))))))); 2905 } 2906 if (Right.is(TT_FunctionTypeLParen) && Left.isNot(tok::l_paren) && 2907 (!Left.is(TT_PointerOrReference) || 2908 (Style.PointerAlignment != FormatStyle::PAS_Right && 2909 !Line.IsMultiVariableDeclStmt))) 2910 return true; 2911 if (Left.is(TT_PointerOrReference)) { 2912 // Add a space if the next token is a pointer qualifer and the style 2913 // requires spaces before pointer qualifiers. 2914 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Before || 2915 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) && 2916 Right.canBePointerOrReferenceQualifier()) 2917 return true; 2918 return Right.Tok.isLiteral() || Right.is(TT_BlockComment) || 2919 (Right.isOneOf(Keywords.kw_override, Keywords.kw_final) && 2920 !Right.is(TT_StartOfName)) || 2921 (Right.is(tok::l_brace) && Right.is(BK_Block)) || 2922 (!Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare, 2923 tok::l_paren) && 2924 (Style.PointerAlignment != FormatStyle::PAS_Right && 2925 !Line.IsMultiVariableDeclStmt) && 2926 Left.Previous && 2927 !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon, 2928 tok::l_square)); 2929 } 2930 // Ensure right pointer alignement with ellipsis e.g. int *...P 2931 if (Left.is(tok::ellipsis) && Left.Previous && 2932 Left.Previous->isOneOf(tok::star, tok::amp, tok::ampamp)) 2933 return Style.PointerAlignment != FormatStyle::PAS_Right; 2934 2935 if (Right.is(tok::star) && Left.is(tok::l_paren)) 2936 return false; 2937 if (Left.is(tok::star) && Right.isOneOf(tok::star, tok::amp, tok::ampamp)) 2938 return false; 2939 if (Right.isOneOf(tok::star, tok::amp, tok::ampamp)) { 2940 const FormatToken *Previous = &Left; 2941 while (Previous && !Previous->is(tok::kw_operator)) { 2942 if (Previous->is(tok::identifier) || Previous->isSimpleTypeSpecifier()) { 2943 Previous = Previous->getPreviousNonComment(); 2944 continue; 2945 } 2946 if (Previous->is(TT_TemplateCloser) && Previous->MatchingParen) { 2947 Previous = Previous->MatchingParen->getPreviousNonComment(); 2948 continue; 2949 } 2950 if (Previous->is(tok::coloncolon)) { 2951 Previous = Previous->getPreviousNonComment(); 2952 continue; 2953 } 2954 break; 2955 } 2956 // Space between the type and the * in: 2957 // operator void*() 2958 // operator char*() 2959 // operator /*comment*/ const char*() 2960 // operator volatile /*comment*/ char*() 2961 // operator Foo*() 2962 // operator C<T>*() 2963 // operator std::Foo*() 2964 // operator C<T>::D<U>*() 2965 // dependent on PointerAlignment style. 2966 if (Previous && 2967 (Previous->endsSequence(tok::kw_operator) || 2968 Previous->endsSequence(tok::kw_const, tok::kw_operator) || 2969 Previous->endsSequence(tok::kw_volatile, tok::kw_operator))) 2970 return (Style.PointerAlignment != FormatStyle::PAS_Left); 2971 } 2972 const auto SpaceRequiredForArrayInitializerLSquare = 2973 [](const FormatToken &LSquareTok, const FormatStyle &Style) { 2974 return Style.SpacesInContainerLiterals || 2975 ((Style.Language == FormatStyle::LK_Proto || 2976 Style.Language == FormatStyle::LK_TextProto) && 2977 !Style.Cpp11BracedListStyle && 2978 LSquareTok.endsSequence(tok::l_square, tok::colon, 2979 TT_SelectorName)); 2980 }; 2981 if (Left.is(tok::l_square)) 2982 return (Left.is(TT_ArrayInitializerLSquare) && Right.isNot(tok::r_square) && 2983 SpaceRequiredForArrayInitializerLSquare(Left, Style)) || 2984 (Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare, 2985 TT_LambdaLSquare) && 2986 Style.SpacesInSquareBrackets && Right.isNot(tok::r_square)); 2987 if (Right.is(tok::r_square)) 2988 return Right.MatchingParen && 2989 ((Right.MatchingParen->is(TT_ArrayInitializerLSquare) && 2990 SpaceRequiredForArrayInitializerLSquare(*Right.MatchingParen, 2991 Style)) || 2992 (Style.SpacesInSquareBrackets && 2993 Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare, 2994 TT_StructuredBindingLSquare, 2995 TT_LambdaLSquare)) || 2996 Right.MatchingParen->is(TT_AttributeParen)); 2997 if (Right.is(tok::l_square) && 2998 !Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare, 2999 TT_DesignatedInitializerLSquare, 3000 TT_StructuredBindingLSquare, TT_AttributeSquare) && 3001 !Left.isOneOf(tok::numeric_constant, TT_DictLiteral) && 3002 !(!Left.is(tok::r_square) && Style.SpaceBeforeSquareBrackets && 3003 Right.is(TT_ArraySubscriptLSquare))) 3004 return false; 3005 if (Left.is(tok::l_brace) && Right.is(tok::r_brace)) 3006 return !Left.Children.empty(); // No spaces in "{}". 3007 if ((Left.is(tok::l_brace) && Left.isNot(BK_Block)) || 3008 (Right.is(tok::r_brace) && Right.MatchingParen && 3009 Right.MatchingParen->isNot(BK_Block))) 3010 return Style.Cpp11BracedListStyle ? Style.SpacesInParentheses : true; 3011 if (Left.is(TT_BlockComment)) 3012 // No whitespace in x(/*foo=*/1), except for JavaScript. 3013 return Style.Language == FormatStyle::LK_JavaScript || 3014 !Left.TokenText.endswith("=*/"); 3015 3016 // Space between template and attribute. 3017 // e.g. template <typename T> [[nodiscard]] ... 3018 if (Left.is(TT_TemplateCloser) && Right.is(TT_AttributeSquare)) 3019 return true; 3020 if (Right.is(tok::l_paren)) { 3021 if ((Left.is(tok::r_paren) && Left.is(TT_AttributeParen)) || 3022 (Left.is(tok::r_square) && Left.is(TT_AttributeSquare))) 3023 return true; 3024 if (Style.SpaceBeforeParens == 3025 FormatStyle::SBPO_ControlStatementsExceptForEachMacros && 3026 Left.is(TT_ForEachMacro)) 3027 return false; 3028 return Line.Type == LT_ObjCDecl || Left.is(tok::semi) || 3029 (Style.SpaceBeforeParens != FormatStyle::SBPO_Never && 3030 (Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while, 3031 tok::kw_switch, tok::kw_case, TT_ForEachMacro, 3032 TT_ObjCForIn) || 3033 Left.isIf(Line.Type != LT_PreprocessorDirective) || 3034 (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch, 3035 tok::kw_new, tok::kw_delete) && 3036 (!Left.Previous || Left.Previous->isNot(tok::period))))) || 3037 (spaceRequiredBeforeParens(Right) && 3038 (Left.is(tok::identifier) || Left.isFunctionLikeKeyword() || 3039 Left.is(tok::r_paren) || Left.isSimpleTypeSpecifier() || 3040 (Left.is(tok::r_square) && Left.MatchingParen && 3041 Left.MatchingParen->is(TT_LambdaLSquare))) && 3042 Line.Type != LT_PreprocessorDirective); 3043 } 3044 if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword) 3045 return false; 3046 if (Right.is(TT_UnaryOperator)) 3047 return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) && 3048 (Left.isNot(tok::colon) || Left.isNot(TT_ObjCMethodExpr)); 3049 if ((Left.isOneOf(tok::identifier, tok::greater, tok::r_square, 3050 tok::r_paren) || 3051 Left.isSimpleTypeSpecifier()) && 3052 Right.is(tok::l_brace) && Right.getNextNonComment() && 3053 Right.isNot(BK_Block)) 3054 return false; 3055 if (Left.is(tok::period) || Right.is(tok::period)) 3056 return false; 3057 if (Right.is(tok::hash) && Left.is(tok::identifier) && Left.TokenText == "L") 3058 return false; 3059 if (Left.is(TT_TemplateCloser) && Left.MatchingParen && 3060 Left.MatchingParen->Previous && 3061 (Left.MatchingParen->Previous->is(tok::period) || 3062 Left.MatchingParen->Previous->is(tok::coloncolon))) 3063 // Java call to generic function with explicit type: 3064 // A.<B<C<...>>>DoSomething(); 3065 // A::<B<C<...>>>DoSomething(); // With a Java 8 method reference. 3066 return false; 3067 if (Left.is(TT_TemplateCloser) && Right.is(tok::l_square)) 3068 return false; 3069 if (Left.is(tok::l_brace) && Left.endsSequence(TT_DictLiteral, tok::at)) 3070 // Objective-C dictionary literal -> no space after opening brace. 3071 return false; 3072 if (Right.is(tok::r_brace) && Right.MatchingParen && 3073 Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at)) 3074 // Objective-C dictionary literal -> no space before closing brace. 3075 return false; 3076 if (Right.getType() == TT_TrailingAnnotation && 3077 Right.isOneOf(tok::amp, tok::ampamp) && 3078 Left.isOneOf(tok::kw_const, tok::kw_volatile) && 3079 (!Right.Next || Right.Next->is(tok::semi))) 3080 // Match const and volatile ref-qualifiers without any additional 3081 // qualifiers such as 3082 // void Fn() const &; 3083 return Style.PointerAlignment != FormatStyle::PAS_Left; 3084 return true; 3085 } 3086 3087 bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, 3088 const FormatToken &Right) { 3089 const FormatToken &Left = *Right.Previous; 3090 if (Right.Tok.getIdentifierInfo() && Left.Tok.getIdentifierInfo()) 3091 return true; // Never ever merge two identifiers. 3092 if (Style.isCpp()) { 3093 if (Left.is(tok::kw_operator)) 3094 return Right.is(tok::coloncolon); 3095 if (Right.is(tok::l_brace) && Right.is(BK_BracedInit) && 3096 !Left.opensScope() && Style.SpaceBeforeCpp11BracedList) 3097 return true; 3098 } else if (Style.Language == FormatStyle::LK_Proto || 3099 Style.Language == FormatStyle::LK_TextProto) { 3100 if (Right.is(tok::period) && 3101 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required, 3102 Keywords.kw_repeated, Keywords.kw_extend)) 3103 return true; 3104 if (Right.is(tok::l_paren) && 3105 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)) 3106 return true; 3107 if (Right.isOneOf(tok::l_brace, tok::less) && Left.is(TT_SelectorName)) 3108 return true; 3109 // Slashes occur in text protocol extension syntax: [type/type] { ... }. 3110 if (Left.is(tok::slash) || Right.is(tok::slash)) 3111 return false; 3112 if (Left.MatchingParen && 3113 Left.MatchingParen->is(TT_ProtoExtensionLSquare) && 3114 Right.isOneOf(tok::l_brace, tok::less)) 3115 return !Style.Cpp11BracedListStyle; 3116 // A percent is probably part of a formatting specification, such as %lld. 3117 if (Left.is(tok::percent)) 3118 return false; 3119 // Preserve the existence of a space before a percent for cases like 0x%04x 3120 // and "%d %d" 3121 if (Left.is(tok::numeric_constant) && Right.is(tok::percent)) 3122 return Right.WhitespaceRange.getEnd() != Right.WhitespaceRange.getBegin(); 3123 } else if (Style.isCSharp()) { 3124 // Require spaces around '{' and before '}' unless they appear in 3125 // interpolated strings. Interpolated strings are merged into a single token 3126 // so cannot have spaces inserted by this function. 3127 3128 // No space between 'this' and '[' 3129 if (Left.is(tok::kw_this) && Right.is(tok::l_square)) 3130 return false; 3131 3132 // No space between 'new' and '(' 3133 if (Left.is(tok::kw_new) && Right.is(tok::l_paren)) 3134 return false; 3135 3136 // Space before { (including space within '{ {'). 3137 if (Right.is(tok::l_brace)) 3138 return true; 3139 3140 // Spaces inside braces. 3141 if (Left.is(tok::l_brace) && Right.isNot(tok::r_brace)) 3142 return true; 3143 3144 if (Left.isNot(tok::l_brace) && Right.is(tok::r_brace)) 3145 return true; 3146 3147 // Spaces around '=>'. 3148 if (Left.is(TT_JsFatArrow) || Right.is(TT_JsFatArrow)) 3149 return true; 3150 3151 // No spaces around attribute target colons 3152 if (Left.is(TT_AttributeColon) || Right.is(TT_AttributeColon)) 3153 return false; 3154 3155 // space between type and variable e.g. Dictionary<string,string> foo; 3156 if (Left.is(TT_TemplateCloser) && Right.is(TT_StartOfName)) 3157 return true; 3158 3159 // spaces inside square brackets. 3160 if (Left.is(tok::l_square) || Right.is(tok::r_square)) 3161 return Style.SpacesInSquareBrackets; 3162 3163 // No space before ? in nullable types. 3164 if (Right.is(TT_CSharpNullable)) 3165 return false; 3166 3167 // Require space after ? in nullable types except in generics and casts. 3168 if (Left.is(TT_CSharpNullable)) 3169 return !Right.isOneOf(TT_TemplateCloser, tok::r_paren); 3170 3171 // No space before or after '?.'. 3172 if (Left.is(TT_CSharpNullConditional) || Right.is(TT_CSharpNullConditional)) 3173 return false; 3174 3175 // Space before and after '??'. 3176 if (Left.is(TT_CSharpNullCoalescing) || Right.is(TT_CSharpNullCoalescing)) 3177 return true; 3178 3179 // No space before '?['. 3180 if (Right.is(TT_CSharpNullConditionalLSquare)) 3181 return false; 3182 3183 // No space between consecutive commas '[,,]'. 3184 if (Left.is(tok::comma) && Right.is(tok::comma)) 3185 return false; 3186 3187 // Possible space inside `?[ 0 ]`. 3188 if (Left.is(TT_CSharpNullConditionalLSquare)) 3189 return Style.SpacesInSquareBrackets; 3190 3191 // space after var in `var (key, value)` 3192 if (Left.is(Keywords.kw_var) && Right.is(tok::l_paren)) 3193 return true; 3194 3195 // space between keywords and paren e.g. "using (" 3196 if (Right.is(tok::l_paren)) 3197 if (Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when, 3198 Keywords.kw_lock)) 3199 return Style.SpaceBeforeParens == FormatStyle::SBPO_ControlStatements || 3200 spaceRequiredBeforeParens(Right); 3201 3202 // space between method modifier and opening parenthesis of a tuple return 3203 // type 3204 if (Left.isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected, 3205 tok::kw_virtual, tok::kw_extern, tok::kw_static, 3206 Keywords.kw_internal, Keywords.kw_abstract, 3207 Keywords.kw_sealed, Keywords.kw_override, 3208 Keywords.kw_async, Keywords.kw_unsafe) && 3209 Right.is(tok::l_paren)) 3210 return true; 3211 } else if (Style.Language == FormatStyle::LK_JavaScript) { 3212 if (Left.is(TT_JsFatArrow)) 3213 return true; 3214 // for await ( ... 3215 if (Right.is(tok::l_paren) && Left.is(Keywords.kw_await) && Left.Previous && 3216 Left.Previous->is(tok::kw_for)) 3217 return true; 3218 if (Left.is(Keywords.kw_async) && Right.is(tok::l_paren) && 3219 Right.MatchingParen) { 3220 const FormatToken *Next = Right.MatchingParen->getNextNonComment(); 3221 // An async arrow function, for example: `x = async () => foo();`, 3222 // as opposed to calling a function called async: `x = async();` 3223 if (Next && Next->is(TT_JsFatArrow)) 3224 return true; 3225 } 3226 if ((Left.is(TT_TemplateString) && Left.TokenText.endswith("${")) || 3227 (Right.is(TT_TemplateString) && Right.TokenText.startswith("}"))) 3228 return false; 3229 // In tagged template literals ("html`bar baz`"), there is no space between 3230 // the tag identifier and the template string. 3231 if (Keywords.IsJavaScriptIdentifier(Left, 3232 /* AcceptIdentifierName= */ false) && 3233 Right.is(TT_TemplateString)) 3234 return false; 3235 if (Right.is(tok::star) && 3236 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)) 3237 return false; 3238 if (Right.isOneOf(tok::l_brace, tok::l_square) && 3239 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield, 3240 Keywords.kw_extends, Keywords.kw_implements)) 3241 return true; 3242 if (Right.is(tok::l_paren)) { 3243 // JS methods can use some keywords as names (e.g. `delete()`). 3244 if (Line.MustBeDeclaration && Left.Tok.getIdentifierInfo()) 3245 return false; 3246 // Valid JS method names can include keywords, e.g. `foo.delete()` or 3247 // `bar.instanceof()`. Recognize call positions by preceding period. 3248 if (Left.Previous && Left.Previous->is(tok::period) && 3249 Left.Tok.getIdentifierInfo()) 3250 return false; 3251 // Additional unary JavaScript operators that need a space after. 3252 if (Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof, 3253 tok::kw_void)) 3254 return true; 3255 } 3256 // `foo as const;` casts into a const type. 3257 if (Left.endsSequence(tok::kw_const, Keywords.kw_as)) { 3258 return false; 3259 } 3260 if ((Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in, 3261 tok::kw_const) || 3262 // "of" is only a keyword if it appears after another identifier 3263 // (e.g. as "const x of y" in a for loop), or after a destructuring 3264 // operation (const [x, y] of z, const {a, b} of c). 3265 (Left.is(Keywords.kw_of) && Left.Previous && 3266 (Left.Previous->Tok.is(tok::identifier) || 3267 Left.Previous->isOneOf(tok::r_square, tok::r_brace)))) && 3268 (!Left.Previous || !Left.Previous->is(tok::period))) 3269 return true; 3270 if (Left.isOneOf(tok::kw_for, Keywords.kw_as) && Left.Previous && 3271 Left.Previous->is(tok::period) && Right.is(tok::l_paren)) 3272 return false; 3273 if (Left.is(Keywords.kw_as) && 3274 Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)) 3275 return true; 3276 if (Left.is(tok::kw_default) && Left.Previous && 3277 Left.Previous->is(tok::kw_export)) 3278 return true; 3279 if (Left.is(Keywords.kw_is) && Right.is(tok::l_brace)) 3280 return true; 3281 if (Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion)) 3282 return false; 3283 if (Left.is(TT_JsTypeOperator) || Right.is(TT_JsTypeOperator)) 3284 return false; 3285 if ((Left.is(tok::l_brace) || Right.is(tok::r_brace)) && 3286 Line.First->isOneOf(Keywords.kw_import, tok::kw_export)) 3287 return false; 3288 if (Left.is(tok::ellipsis)) 3289 return false; 3290 if (Left.is(TT_TemplateCloser) && 3291 !Right.isOneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square, 3292 Keywords.kw_implements, Keywords.kw_extends)) 3293 // Type assertions ('<type>expr') are not followed by whitespace. Other 3294 // locations that should have whitespace following are identified by the 3295 // above set of follower tokens. 3296 return false; 3297 if (Right.is(TT_JsNonNullAssertion)) 3298 return false; 3299 if (Left.is(TT_JsNonNullAssertion) && 3300 Right.isOneOf(Keywords.kw_as, Keywords.kw_in)) 3301 return true; // "x! as string", "x! in y" 3302 } else if (Style.Language == FormatStyle::LK_Java) { 3303 if (Left.is(tok::r_square) && Right.is(tok::l_brace)) 3304 return true; 3305 if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren)) 3306 return Style.SpaceBeforeParens != FormatStyle::SBPO_Never; 3307 if ((Left.isOneOf(tok::kw_static, tok::kw_public, tok::kw_private, 3308 tok::kw_protected) || 3309 Left.isOneOf(Keywords.kw_final, Keywords.kw_abstract, 3310 Keywords.kw_native)) && 3311 Right.is(TT_TemplateOpener)) 3312 return true; 3313 } 3314 if (Left.is(TT_ImplicitStringLiteral)) 3315 return Right.WhitespaceRange.getBegin() != Right.WhitespaceRange.getEnd(); 3316 if (Line.Type == LT_ObjCMethodDecl) { 3317 if (Left.is(TT_ObjCMethodSpecifier)) 3318 return true; 3319 if (Left.is(tok::r_paren) && canBeObjCSelectorComponent(Right)) 3320 // Don't space between ')' and <id> or ')' and 'new'. 'new' is not a 3321 // keyword in Objective-C, and '+ (instancetype)new;' is a standard class 3322 // method declaration. 3323 return false; 3324 } 3325 if (Line.Type == LT_ObjCProperty && 3326 (Right.is(tok::equal) || Left.is(tok::equal))) 3327 return false; 3328 3329 if (Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) || 3330 Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow)) 3331 return true; 3332 if (Right.is(TT_OverloadedOperatorLParen)) 3333 return spaceRequiredBeforeParens(Right); 3334 if (Left.is(tok::comma)) 3335 return true; 3336 if (Right.is(tok::comma)) 3337 return false; 3338 if (Right.is(TT_ObjCBlockLParen)) 3339 return true; 3340 if (Right.is(TT_CtorInitializerColon)) 3341 return Style.SpaceBeforeCtorInitializerColon; 3342 if (Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon) 3343 return false; 3344 if (Right.is(TT_RangeBasedForLoopColon) && 3345 !Style.SpaceBeforeRangeBasedForLoopColon) 3346 return false; 3347 if (Left.is(TT_BitFieldColon)) 3348 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both || 3349 Style.BitFieldColonSpacing == FormatStyle::BFCS_After; 3350 if (Right.is(tok::colon)) { 3351 if (Line.First->isOneOf(tok::kw_default, tok::kw_case)) 3352 return Style.SpaceBeforeCaseColon; 3353 if (!Right.getNextNonComment() || Right.getNextNonComment()->is(tok::semi)) 3354 return false; 3355 if (Right.is(TT_ObjCMethodExpr)) 3356 return false; 3357 if (Left.is(tok::question)) 3358 return false; 3359 if (Right.is(TT_InlineASMColon) && Left.is(tok::coloncolon)) 3360 return false; 3361 if (Right.is(TT_DictLiteral)) 3362 return Style.SpacesInContainerLiterals; 3363 if (Right.is(TT_AttributeColon)) 3364 return false; 3365 if (Right.is(TT_CSharpNamedArgumentColon)) 3366 return false; 3367 if (Right.is(TT_BitFieldColon)) 3368 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both || 3369 Style.BitFieldColonSpacing == FormatStyle::BFCS_Before; 3370 return true; 3371 } 3372 if (Left.is(TT_UnaryOperator)) { 3373 if (!Right.is(tok::l_paren)) { 3374 // The alternative operators for ~ and ! are "compl" and "not". 3375 // If they are used instead, we do not want to combine them with 3376 // the token to the right, unless that is a left paren. 3377 if (Left.is(tok::exclaim) && Left.TokenText == "not") 3378 return true; 3379 if (Left.is(tok::tilde) && Left.TokenText == "compl") 3380 return true; 3381 // Lambda captures allow for a lone &, so "&]" needs to be properly 3382 // handled. 3383 if (Left.is(tok::amp) && Right.is(tok::r_square)) 3384 return Style.SpacesInSquareBrackets; 3385 } 3386 return (Style.SpaceAfterLogicalNot && Left.is(tok::exclaim)) || 3387 Right.is(TT_BinaryOperator); 3388 } 3389 3390 // If the next token is a binary operator or a selector name, we have 3391 // incorrectly classified the parenthesis as a cast. FIXME: Detect correctly. 3392 if (Left.is(TT_CastRParen)) 3393 return Style.SpaceAfterCStyleCast || 3394 Right.isOneOf(TT_BinaryOperator, TT_SelectorName); 3395 3396 if (Left.is(tok::greater) && Right.is(tok::greater)) { 3397 if (Style.Language == FormatStyle::LK_TextProto || 3398 (Style.Language == FormatStyle::LK_Proto && Left.is(TT_DictLiteral))) 3399 return !Style.Cpp11BracedListStyle; 3400 return Right.is(TT_TemplateCloser) && Left.is(TT_TemplateCloser) && 3401 (Style.Standard < FormatStyle::LS_Cpp11 || Style.SpacesInAngles); 3402 } 3403 if (Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) || 3404 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) || 3405 (Right.is(tok::period) && Right.isNot(TT_DesignatedInitializerPeriod))) 3406 return false; 3407 if (!Style.SpaceBeforeAssignmentOperators && Left.isNot(TT_TemplateCloser) && 3408 Right.getPrecedence() == prec::Assignment) 3409 return false; 3410 if (Style.Language == FormatStyle::LK_Java && Right.is(tok::coloncolon) && 3411 (Left.is(tok::identifier) || Left.is(tok::kw_this))) 3412 return false; 3413 if (Right.is(tok::coloncolon) && Left.is(tok::identifier)) 3414 // Generally don't remove existing spaces between an identifier and "::". 3415 // The identifier might actually be a macro name such as ALWAYS_INLINE. If 3416 // this turns out to be too lenient, add analysis of the identifier itself. 3417 return Right.WhitespaceRange.getBegin() != Right.WhitespaceRange.getEnd(); 3418 if (Right.is(tok::coloncolon) && 3419 !Left.isOneOf(tok::l_brace, tok::comment, tok::l_paren)) 3420 // Put a space between < and :: in vector< ::std::string > 3421 return (Left.is(TT_TemplateOpener) && 3422 (Style.Standard < FormatStyle::LS_Cpp11 || Style.SpacesInAngles)) || 3423 !(Left.isOneOf(tok::l_paren, tok::r_paren, tok::l_square, 3424 tok::kw___super, TT_TemplateOpener, 3425 TT_TemplateCloser)) || 3426 (Left.is(tok::l_paren) && Style.SpacesInParentheses); 3427 if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser))) 3428 return Style.SpacesInAngles; 3429 // Space before TT_StructuredBindingLSquare. 3430 if (Right.is(TT_StructuredBindingLSquare)) 3431 return !Left.isOneOf(tok::amp, tok::ampamp) || 3432 Style.PointerAlignment != FormatStyle::PAS_Right; 3433 // Space before & or && following a TT_StructuredBindingLSquare. 3434 if (Right.Next && Right.Next->is(TT_StructuredBindingLSquare) && 3435 Right.isOneOf(tok::amp, tok::ampamp)) 3436 return Style.PointerAlignment != FormatStyle::PAS_Left; 3437 if ((Right.is(TT_BinaryOperator) && !Left.is(tok::l_paren)) || 3438 (Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) && 3439 !Right.is(tok::r_paren))) 3440 return true; 3441 if (Left.is(TT_TemplateCloser) && Right.is(tok::l_paren) && 3442 Right.isNot(TT_FunctionTypeLParen)) 3443 return spaceRequiredBeforeParens(Right); 3444 if (Right.is(TT_TemplateOpener) && Left.is(tok::r_paren) && 3445 Left.MatchingParen && Left.MatchingParen->is(TT_OverloadedOperatorLParen)) 3446 return false; 3447 if (Right.is(tok::less) && Left.isNot(tok::l_paren) && 3448 Line.startsWith(tok::hash)) 3449 return true; 3450 if (Right.is(TT_TrailingUnaryOperator)) 3451 return false; 3452 if (Left.is(TT_RegexLiteral)) 3453 return false; 3454 return spaceRequiredBetween(Line, Left, Right); 3455 } 3456 3457 // Returns 'true' if 'Tok' is a brace we'd want to break before in Allman style. 3458 static bool isAllmanBrace(const FormatToken &Tok) { 3459 return Tok.is(tok::l_brace) && Tok.is(BK_Block) && 3460 !Tok.isOneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral); 3461 } 3462 3463 // Returns 'true' if 'Tok' is an function argument. 3464 static bool IsFunctionArgument(const FormatToken &Tok) { 3465 return Tok.MatchingParen && Tok.MatchingParen->Next && 3466 Tok.MatchingParen->Next->isOneOf(tok::comma, tok::r_paren); 3467 } 3468 3469 static bool 3470 isItAnEmptyLambdaAllowed(const FormatToken &Tok, 3471 FormatStyle::ShortLambdaStyle ShortLambdaOption) { 3472 return Tok.Children.empty() && ShortLambdaOption != FormatStyle::SLS_None; 3473 } 3474 3475 static bool 3476 isItAInlineLambdaAllowed(const FormatToken &Tok, 3477 FormatStyle::ShortLambdaStyle ShortLambdaOption) { 3478 return (ShortLambdaOption == FormatStyle::SLS_Inline && 3479 IsFunctionArgument(Tok)) || 3480 (ShortLambdaOption == FormatStyle::SLS_All); 3481 } 3482 3483 static bool isOneChildWithoutMustBreakBefore(const FormatToken &Tok) { 3484 if (Tok.Children.size() != 1) 3485 return false; 3486 FormatToken *curElt = Tok.Children[0]->First; 3487 while (curElt) { 3488 if (curElt->MustBreakBefore) 3489 return false; 3490 curElt = curElt->Next; 3491 } 3492 return true; 3493 } 3494 static bool isAllmanLambdaBrace(const FormatToken &Tok) { 3495 return (Tok.is(tok::l_brace) && Tok.is(BK_Block) && 3496 !Tok.isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral)); 3497 } 3498 3499 static bool isAllmanBraceIncludedBreakableLambda( 3500 const FormatToken &Tok, FormatStyle::ShortLambdaStyle ShortLambdaOption) { 3501 if (!isAllmanLambdaBrace(Tok)) 3502 return false; 3503 3504 if (isItAnEmptyLambdaAllowed(Tok, ShortLambdaOption)) 3505 return false; 3506 3507 return !isItAInlineLambdaAllowed(Tok, ShortLambdaOption) || 3508 !isOneChildWithoutMustBreakBefore(Tok); 3509 } 3510 3511 bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, 3512 const FormatToken &Right) { 3513 const FormatToken &Left = *Right.Previous; 3514 if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0) 3515 return true; 3516 3517 if (Style.isCSharp()) { 3518 if (Right.is(TT_CSharpNamedArgumentColon) || 3519 Left.is(TT_CSharpNamedArgumentColon)) 3520 return false; 3521 if (Right.is(TT_CSharpGenericTypeConstraint)) 3522 return true; 3523 } else if (Style.Language == FormatStyle::LK_JavaScript) { 3524 // FIXME: This might apply to other languages and token kinds. 3525 if (Right.is(tok::string_literal) && Left.is(tok::plus) && Left.Previous && 3526 Left.Previous->is(tok::string_literal)) 3527 return true; 3528 if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) && Line.Level == 0 && 3529 Left.Previous && Left.Previous->is(tok::equal) && 3530 Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export, 3531 tok::kw_const) && 3532 // kw_var/kw_let are pseudo-tokens that are tok::identifier, so match 3533 // above. 3534 !Line.First->isOneOf(Keywords.kw_var, Keywords.kw_let)) 3535 // Object literals on the top level of a file are treated as "enum-style". 3536 // Each key/value pair is put on a separate line, instead of bin-packing. 3537 return true; 3538 if (Left.is(tok::l_brace) && Line.Level == 0 && 3539 (Line.startsWith(tok::kw_enum) || 3540 Line.startsWith(tok::kw_const, tok::kw_enum) || 3541 Line.startsWith(tok::kw_export, tok::kw_enum) || 3542 Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum))) 3543 // JavaScript top-level enum key/value pairs are put on separate lines 3544 // instead of bin-packing. 3545 return true; 3546 if (Right.is(tok::r_brace) && Left.is(tok::l_brace) && Left.Previous && 3547 Left.Previous->is(TT_JsFatArrow)) { 3548 // JS arrow function (=> {...}). 3549 switch (Style.AllowShortLambdasOnASingleLine) { 3550 case FormatStyle::SLS_All: 3551 return false; 3552 case FormatStyle::SLS_None: 3553 return true; 3554 case FormatStyle::SLS_Empty: 3555 return !Left.Children.empty(); 3556 case FormatStyle::SLS_Inline: 3557 // allow one-lining inline (e.g. in function call args) and empty arrow 3558 // functions. 3559 return (Left.NestingLevel == 0 && Line.Level == 0) && 3560 !Left.Children.empty(); 3561 } 3562 llvm_unreachable("Unknown FormatStyle::ShortLambdaStyle enum"); 3563 } 3564 3565 if (Right.is(tok::r_brace) && Left.is(tok::l_brace) && 3566 !Left.Children.empty()) 3567 // Support AllowShortFunctionsOnASingleLine for JavaScript. 3568 return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None || 3569 Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Empty || 3570 (Left.NestingLevel == 0 && Line.Level == 0 && 3571 Style.AllowShortFunctionsOnASingleLine & 3572 FormatStyle::SFS_InlineOnly); 3573 } else if (Style.Language == FormatStyle::LK_Java) { 3574 if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next && 3575 Right.Next->is(tok::string_literal)) 3576 return true; 3577 } else if (Style.Language == FormatStyle::LK_Cpp || 3578 Style.Language == FormatStyle::LK_ObjC || 3579 Style.Language == FormatStyle::LK_Proto || 3580 Style.Language == FormatStyle::LK_TableGen || 3581 Style.Language == FormatStyle::LK_TextProto) { 3582 if (Left.isStringLiteral() && Right.isStringLiteral()) 3583 return true; 3584 } 3585 3586 // If the last token before a '}', ']', or ')' is a comma or a trailing 3587 // comment, the intention is to insert a line break after it in order to make 3588 // shuffling around entries easier. Import statements, especially in 3589 // JavaScript, can be an exception to this rule. 3590 if (Style.JavaScriptWrapImports || Line.Type != LT_ImportStatement) { 3591 const FormatToken *BeforeClosingBrace = nullptr; 3592 if ((Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) || 3593 (Style.Language == FormatStyle::LK_JavaScript && 3594 Left.is(tok::l_paren))) && 3595 Left.isNot(BK_Block) && Left.MatchingParen) 3596 BeforeClosingBrace = Left.MatchingParen->Previous; 3597 else if (Right.MatchingParen && 3598 (Right.MatchingParen->isOneOf(tok::l_brace, 3599 TT_ArrayInitializerLSquare) || 3600 (Style.Language == FormatStyle::LK_JavaScript && 3601 Right.MatchingParen->is(tok::l_paren)))) 3602 BeforeClosingBrace = &Left; 3603 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) || 3604 BeforeClosingBrace->isTrailingComment())) 3605 return true; 3606 } 3607 3608 if (Right.is(tok::comment)) 3609 return Left.isNot(BK_BracedInit) && Left.isNot(TT_CtorInitializerColon) && 3610 (Right.NewlinesBefore > 0 && Right.HasUnescapedNewline); 3611 if (Left.isTrailingComment()) 3612 return true; 3613 if (Right.Previous->IsUnterminatedLiteral) 3614 return true; 3615 if (Right.is(tok::lessless) && Right.Next && 3616 Right.Previous->is(tok::string_literal) && 3617 Right.Next->is(tok::string_literal)) 3618 return true; 3619 // Can break after template<> declaration 3620 if (Right.Previous->ClosesTemplateDeclaration && 3621 Right.Previous->MatchingParen && 3622 Right.Previous->MatchingParen->NestingLevel == 0) { 3623 // Put concepts on the next line e.g. 3624 // template<typename T> 3625 // concept ... 3626 if (Right.is(tok::kw_concept)) 3627 return Style.BreakBeforeConceptDeclarations; 3628 return (Style.AlwaysBreakTemplateDeclarations == FormatStyle::BTDS_Yes); 3629 } 3630 if (Right.is(TT_CtorInitializerComma) && 3631 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma && 3632 !Style.ConstructorInitializerAllOnOneLineOrOnePerLine) 3633 return true; 3634 if (Right.is(TT_CtorInitializerColon) && 3635 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma && 3636 !Style.ConstructorInitializerAllOnOneLineOrOnePerLine) 3637 return true; 3638 // Break only if we have multiple inheritance. 3639 if (Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma && 3640 Right.is(TT_InheritanceComma)) 3641 return true; 3642 if (Right.is(tok::string_literal) && Right.TokenText.startswith("R\"")) 3643 // Multiline raw string literals are special wrt. line breaks. The author 3644 // has made a deliberate choice and might have aligned the contents of the 3645 // string literal accordingly. Thus, we try keep existing line breaks. 3646 return Right.IsMultiline && Right.NewlinesBefore > 0; 3647 if ((Right.Previous->is(tok::l_brace) || 3648 (Right.Previous->is(tok::less) && Right.Previous->Previous && 3649 Right.Previous->Previous->is(tok::equal))) && 3650 Right.NestingLevel == 1 && Style.Language == FormatStyle::LK_Proto) { 3651 // Don't put enums or option definitions onto single lines in protocol 3652 // buffers. 3653 return true; 3654 } 3655 if (Right.is(TT_InlineASMBrace)) 3656 return Right.HasUnescapedNewline; 3657 3658 auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine; 3659 if (Style.BraceWrapping.BeforeLambdaBody && 3660 (isAllmanBraceIncludedBreakableLambda(Left, ShortLambdaOption) || 3661 isAllmanBraceIncludedBreakableLambda(Right, ShortLambdaOption))) { 3662 return true; 3663 } 3664 3665 if (isAllmanBrace(Left) || isAllmanBrace(Right)) 3666 return (Line.startsWith(tok::kw_enum) && Style.BraceWrapping.AfterEnum) || 3667 (Line.startsWith(tok::kw_typedef, tok::kw_enum) && 3668 Style.BraceWrapping.AfterEnum) || 3669 (Line.startsWith(tok::kw_class) && Style.BraceWrapping.AfterClass) || 3670 (Line.startsWith(tok::kw_struct) && Style.BraceWrapping.AfterStruct); 3671 if (Left.is(TT_ObjCBlockLBrace) && 3672 Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never) 3673 return true; 3674 3675 if (Left.is(TT_LambdaLBrace)) { 3676 if (IsFunctionArgument(Left) && 3677 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline) 3678 return false; 3679 3680 if (Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_None || 3681 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline || 3682 (!Left.Children.empty() && 3683 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Empty)) 3684 return true; 3685 } 3686 3687 // Put multiple Java annotation on a new line. 3688 if ((Style.Language == FormatStyle::LK_Java || 3689 Style.Language == FormatStyle::LK_JavaScript) && 3690 Left.is(TT_LeadingJavaAnnotation) && 3691 Right.isNot(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) && 3692 (Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) 3693 return true; 3694 3695 if (Right.is(TT_ProtoExtensionLSquare)) 3696 return true; 3697 3698 // In text proto instances if a submessage contains at least 2 entries and at 3699 // least one of them is a submessage, like A { ... B { ... } ... }, 3700 // put all of the entries of A on separate lines by forcing the selector of 3701 // the submessage B to be put on a newline. 3702 // 3703 // Example: these can stay on one line: 3704 // a { scalar_1: 1 scalar_2: 2 } 3705 // a { b { key: value } } 3706 // 3707 // and these entries need to be on a new line even if putting them all in one 3708 // line is under the column limit: 3709 // a { 3710 // scalar: 1 3711 // b { key: value } 3712 // } 3713 // 3714 // We enforce this by breaking before a submessage field that has previous 3715 // siblings, *and* breaking before a field that follows a submessage field. 3716 // 3717 // Be careful to exclude the case [proto.ext] { ... } since the `]` is 3718 // the TT_SelectorName there, but we don't want to break inside the brackets. 3719 // 3720 // Another edge case is @submessage { key: value }, which is a common 3721 // substitution placeholder. In this case we want to keep `@` and `submessage` 3722 // together. 3723 // 3724 // We ensure elsewhere that extensions are always on their own line. 3725 if ((Style.Language == FormatStyle::LK_Proto || 3726 Style.Language == FormatStyle::LK_TextProto) && 3727 Right.is(TT_SelectorName) && !Right.is(tok::r_square) && Right.Next) { 3728 // Keep `@submessage` together in: 3729 // @submessage { key: value } 3730 if (Right.Previous && Right.Previous->is(tok::at)) 3731 return false; 3732 // Look for the scope opener after selector in cases like: 3733 // selector { ... 3734 // selector: { ... 3735 // selector: @base { ... 3736 FormatToken *LBrace = Right.Next; 3737 if (LBrace && LBrace->is(tok::colon)) { 3738 LBrace = LBrace->Next; 3739 if (LBrace && LBrace->is(tok::at)) { 3740 LBrace = LBrace->Next; 3741 if (LBrace) 3742 LBrace = LBrace->Next; 3743 } 3744 } 3745 if (LBrace && 3746 // The scope opener is one of {, [, <: 3747 // selector { ... } 3748 // selector [ ... ] 3749 // selector < ... > 3750 // 3751 // In case of selector { ... }, the l_brace is TT_DictLiteral. 3752 // In case of an empty selector {}, the l_brace is not TT_DictLiteral, 3753 // so we check for immediately following r_brace. 3754 ((LBrace->is(tok::l_brace) && 3755 (LBrace->is(TT_DictLiteral) || 3756 (LBrace->Next && LBrace->Next->is(tok::r_brace)))) || 3757 LBrace->is(TT_ArrayInitializerLSquare) || LBrace->is(tok::less))) { 3758 // If Left.ParameterCount is 0, then this submessage entry is not the 3759 // first in its parent submessage, and we want to break before this entry. 3760 // If Left.ParameterCount is greater than 0, then its parent submessage 3761 // might contain 1 or more entries and we want to break before this entry 3762 // if it contains at least 2 entries. We deal with this case later by 3763 // detecting and breaking before the next entry in the parent submessage. 3764 if (Left.ParameterCount == 0) 3765 return true; 3766 // However, if this submessage is the first entry in its parent 3767 // submessage, Left.ParameterCount might be 1 in some cases. 3768 // We deal with this case later by detecting an entry 3769 // following a closing paren of this submessage. 3770 } 3771 3772 // If this is an entry immediately following a submessage, it will be 3773 // preceded by a closing paren of that submessage, like in: 3774 // left---. .---right 3775 // v v 3776 // sub: { ... } key: value 3777 // If there was a comment between `}` an `key` above, then `key` would be 3778 // put on a new line anyways. 3779 if (Left.isOneOf(tok::r_brace, tok::greater, tok::r_square)) 3780 return true; 3781 } 3782 3783 // Deal with lambda arguments in C++ - we want consistent line breaks whether 3784 // they happen to be at arg0, arg1 or argN. The selection is a bit nuanced 3785 // as aggressive line breaks are placed when the lambda is not the last arg. 3786 if ((Style.Language == FormatStyle::LK_Cpp || 3787 Style.Language == FormatStyle::LK_ObjC) && 3788 Left.is(tok::l_paren) && Left.BlockParameterCount > 0 && 3789 !Right.isOneOf(tok::l_paren, TT_LambdaLSquare)) { 3790 // Multiple lambdas in the same function call force line breaks. 3791 if (Left.BlockParameterCount > 1) 3792 return true; 3793 3794 // A lambda followed by another arg forces a line break. 3795 if (!Left.Role) 3796 return false; 3797 auto Comma = Left.Role->lastComma(); 3798 if (!Comma) 3799 return false; 3800 auto Next = Comma->getNextNonComment(); 3801 if (!Next) 3802 return false; 3803 if (!Next->isOneOf(TT_LambdaLSquare, tok::l_brace, tok::caret)) 3804 return true; 3805 } 3806 3807 return false; 3808 } 3809 3810 bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, 3811 const FormatToken &Right) { 3812 const FormatToken &Left = *Right.Previous; 3813 // Language-specific stuff. 3814 if (Style.isCSharp()) { 3815 if (Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) || 3816 Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon)) 3817 return false; 3818 // Only break after commas for generic type constraints. 3819 if (Line.First->is(TT_CSharpGenericTypeConstraint)) 3820 return Left.is(TT_CSharpGenericTypeConstraintComma); 3821 } else if (Style.Language == FormatStyle::LK_Java) { 3822 if (Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends, 3823 Keywords.kw_implements)) 3824 return false; 3825 if (Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends, 3826 Keywords.kw_implements)) 3827 return true; 3828 } else if (Style.Language == FormatStyle::LK_JavaScript) { 3829 const FormatToken *NonComment = Right.getPreviousNonComment(); 3830 if (NonComment && 3831 NonComment->isOneOf( 3832 tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break, 3833 tok::kw_throw, Keywords.kw_interface, Keywords.kw_type, 3834 tok::kw_static, tok::kw_public, tok::kw_private, tok::kw_protected, 3835 Keywords.kw_readonly, Keywords.kw_abstract, Keywords.kw_get, 3836 Keywords.kw_set, Keywords.kw_async, Keywords.kw_await)) 3837 return false; // Otherwise automatic semicolon insertion would trigger. 3838 if (Right.NestingLevel == 0 && 3839 (Left.Tok.getIdentifierInfo() || 3840 Left.isOneOf(tok::r_square, tok::r_paren)) && 3841 Right.isOneOf(tok::l_square, tok::l_paren)) 3842 return false; // Otherwise automatic semicolon insertion would trigger. 3843 if (Left.is(TT_JsFatArrow) && Right.is(tok::l_brace)) 3844 return false; 3845 if (Left.is(TT_JsTypeColon)) 3846 return true; 3847 // Don't wrap between ":" and "!" of a strict prop init ("field!: type;"). 3848 if (Left.is(tok::exclaim) && Right.is(tok::colon)) 3849 return false; 3850 // Look for is type annotations like: 3851 // function f(): a is B { ... } 3852 // Do not break before is in these cases. 3853 if (Right.is(Keywords.kw_is)) { 3854 const FormatToken *Next = Right.getNextNonComment(); 3855 // If `is` is followed by a colon, it's likely that it's a dict key, so 3856 // ignore it for this check. 3857 // For example this is common in Polymer: 3858 // Polymer({ 3859 // is: 'name', 3860 // ... 3861 // }); 3862 if (!Next || !Next->is(tok::colon)) 3863 return false; 3864 } 3865 if (Left.is(Keywords.kw_in)) 3866 return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None; 3867 if (Right.is(Keywords.kw_in)) 3868 return Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None; 3869 if (Right.is(Keywords.kw_as)) 3870 return false; // must not break before as in 'x as type' casts 3871 if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) { 3872 // extends and infer can appear as keywords in conditional types: 3873 // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#conditional-types 3874 // do not break before them, as the expressions are subject to ASI. 3875 return false; 3876 } 3877 if (Left.is(Keywords.kw_as)) 3878 return true; 3879 if (Left.is(TT_JsNonNullAssertion)) 3880 return true; 3881 if (Left.is(Keywords.kw_declare) && 3882 Right.isOneOf(Keywords.kw_module, tok::kw_namespace, 3883 Keywords.kw_function, tok::kw_class, tok::kw_enum, 3884 Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var, 3885 Keywords.kw_let, tok::kw_const)) 3886 // See grammar for 'declare' statements at: 3887 // https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#A.10 3888 return false; 3889 if (Left.isOneOf(Keywords.kw_module, tok::kw_namespace) && 3890 Right.isOneOf(tok::identifier, tok::string_literal)) 3891 return false; // must not break in "module foo { ...}" 3892 if (Right.is(TT_TemplateString) && Right.closesScope()) 3893 return false; 3894 // Don't split tagged template literal so there is a break between the tag 3895 // identifier and template string. 3896 if (Left.is(tok::identifier) && Right.is(TT_TemplateString)) { 3897 return false; 3898 } 3899 if (Left.is(TT_TemplateString) && Left.opensScope()) 3900 return true; 3901 } 3902 3903 if (Left.is(tok::at)) 3904 return false; 3905 if (Left.Tok.getObjCKeywordID() == tok::objc_interface) 3906 return false; 3907 if (Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation)) 3908 return !Right.is(tok::l_paren); 3909 if (Right.is(TT_PointerOrReference)) 3910 return Line.IsMultiVariableDeclStmt || 3911 (Style.PointerAlignment == FormatStyle::PAS_Right && 3912 (!Right.Next || Right.Next->isNot(TT_FunctionDeclarationName))); 3913 if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) || 3914 Right.is(tok::kw_operator)) 3915 return true; 3916 if (Left.is(TT_PointerOrReference)) 3917 return false; 3918 if (Right.isTrailingComment()) 3919 // We rely on MustBreakBefore being set correctly here as we should not 3920 // change the "binding" behavior of a comment. 3921 // The first comment in a braced lists is always interpreted as belonging to 3922 // the first list element. Otherwise, it should be placed outside of the 3923 // list. 3924 return Left.is(BK_BracedInit) || 3925 (Left.is(TT_CtorInitializerColon) && 3926 Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon); 3927 if (Left.is(tok::question) && Right.is(tok::colon)) 3928 return false; 3929 if (Right.is(TT_ConditionalExpr) || Right.is(tok::question)) 3930 return Style.BreakBeforeTernaryOperators; 3931 if (Left.is(TT_ConditionalExpr) || Left.is(tok::question)) 3932 return !Style.BreakBeforeTernaryOperators; 3933 if (Left.is(TT_InheritanceColon)) 3934 return Style.BreakInheritanceList == FormatStyle::BILS_AfterColon; 3935 if (Right.is(TT_InheritanceColon)) 3936 return Style.BreakInheritanceList != FormatStyle::BILS_AfterColon; 3937 if (Right.is(TT_ObjCMethodExpr) && !Right.is(tok::r_square) && 3938 Left.isNot(TT_SelectorName)) 3939 return true; 3940 3941 if (Right.is(tok::colon) && 3942 !Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon)) 3943 return false; 3944 if (Left.is(tok::colon) && Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) { 3945 if (Style.Language == FormatStyle::LK_Proto || 3946 Style.Language == FormatStyle::LK_TextProto) { 3947 if (!Style.AlwaysBreakBeforeMultilineStrings && Right.isStringLiteral()) 3948 return false; 3949 // Prevent cases like: 3950 // 3951 // submessage: 3952 // { key: valueeeeeeeeeeee } 3953 // 3954 // when the snippet does not fit into one line. 3955 // Prefer: 3956 // 3957 // submessage: { 3958 // key: valueeeeeeeeeeee 3959 // } 3960 // 3961 // instead, even if it is longer by one line. 3962 // 3963 // Note that this allows allows the "{" to go over the column limit 3964 // when the column limit is just between ":" and "{", but that does 3965 // not happen too often and alternative formattings in this case are 3966 // not much better. 3967 // 3968 // The code covers the cases: 3969 // 3970 // submessage: { ... } 3971 // submessage: < ... > 3972 // repeated: [ ... ] 3973 if (((Right.is(tok::l_brace) || Right.is(tok::less)) && 3974 Right.is(TT_DictLiteral)) || 3975 Right.is(TT_ArrayInitializerLSquare)) 3976 return false; 3977 } 3978 return true; 3979 } 3980 if (Right.is(tok::r_square) && Right.MatchingParen && 3981 Right.MatchingParen->is(TT_ProtoExtensionLSquare)) 3982 return false; 3983 if (Right.is(TT_SelectorName) || (Right.is(tok::identifier) && Right.Next && 3984 Right.Next->is(TT_ObjCMethodExpr))) 3985 return Left.isNot(tok::period); // FIXME: Properly parse ObjC calls. 3986 if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty) 3987 return true; 3988 if (Left.ClosesTemplateDeclaration || Left.is(TT_FunctionAnnotationRParen)) 3989 return true; 3990 if (Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen, 3991 TT_OverloadedOperator)) 3992 return false; 3993 if (Left.is(TT_RangeBasedForLoopColon)) 3994 return true; 3995 if (Right.is(TT_RangeBasedForLoopColon)) 3996 return false; 3997 if (Left.is(TT_TemplateCloser) && Right.is(TT_TemplateOpener)) 3998 return true; 3999 if (Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator) || 4000 Left.is(tok::kw_operator)) 4001 return false; 4002 if (Left.is(tok::equal) && !Right.isOneOf(tok::kw_default, tok::kw_delete) && 4003 Line.Type == LT_VirtualFunctionDecl && Left.NestingLevel == 0) 4004 return false; 4005 if (Left.is(tok::equal) && Right.is(tok::l_brace) && 4006 !Style.Cpp11BracedListStyle) 4007 return false; 4008 if (Left.is(tok::l_paren) && 4009 Left.isOneOf(TT_AttributeParen, TT_TypeDeclarationParen)) 4010 return false; 4011 if (Left.is(tok::l_paren) && Left.Previous && 4012 (Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen))) 4013 return false; 4014 if (Right.is(TT_ImplicitStringLiteral)) 4015 return false; 4016 4017 if (Right.is(tok::r_paren) || Right.is(TT_TemplateCloser)) 4018 return false; 4019 if (Right.is(tok::r_square) && Right.MatchingParen && 4020 Right.MatchingParen->is(TT_LambdaLSquare)) 4021 return false; 4022 4023 // We only break before r_brace if there was a corresponding break before 4024 // the l_brace, which is tracked by BreakBeforeClosingBrace. 4025 if (Right.is(tok::r_brace)) 4026 return Right.MatchingParen && Right.MatchingParen->is(BK_Block); 4027 4028 // Allow breaking after a trailing annotation, e.g. after a method 4029 // declaration. 4030 if (Left.is(TT_TrailingAnnotation)) 4031 return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren, 4032 tok::less, tok::coloncolon); 4033 4034 if (Right.is(tok::kw___attribute) || 4035 (Right.is(tok::l_square) && Right.is(TT_AttributeSquare))) 4036 return !Left.is(TT_AttributeSquare); 4037 4038 if (Left.is(tok::identifier) && Right.is(tok::string_literal)) 4039 return true; 4040 4041 if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral)) 4042 return true; 4043 4044 if (Left.is(TT_CtorInitializerColon)) 4045 return Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon; 4046 if (Right.is(TT_CtorInitializerColon)) 4047 return Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon; 4048 if (Left.is(TT_CtorInitializerComma) && 4049 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) 4050 return false; 4051 if (Right.is(TT_CtorInitializerComma) && 4052 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) 4053 return true; 4054 if (Left.is(TT_InheritanceComma) && 4055 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) 4056 return false; 4057 if (Right.is(TT_InheritanceComma) && 4058 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) 4059 return true; 4060 if ((Left.is(tok::greater) && Right.is(tok::greater)) || 4061 (Left.is(tok::less) && Right.is(tok::less))) 4062 return false; 4063 if (Right.is(TT_BinaryOperator) && 4064 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None && 4065 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All || 4066 Right.getPrecedence() != prec::Assignment)) 4067 return true; 4068 if (Left.is(TT_ArrayInitializerLSquare)) 4069 return true; 4070 if (Right.is(tok::kw_typename) && Left.isNot(tok::kw_const)) 4071 return true; 4072 if ((Left.isBinaryOperator() || Left.is(TT_BinaryOperator)) && 4073 !Left.isOneOf(tok::arrowstar, tok::lessless) && 4074 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All && 4075 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None || 4076 Left.getPrecedence() == prec::Assignment)) 4077 return true; 4078 if ((Left.is(TT_AttributeSquare) && Right.is(tok::l_square)) || 4079 (Left.is(tok::r_square) && Right.is(TT_AttributeSquare))) 4080 return false; 4081 4082 auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine; 4083 if (Style.BraceWrapping.BeforeLambdaBody) { 4084 if (isAllmanLambdaBrace(Left)) 4085 return !isItAnEmptyLambdaAllowed(Left, ShortLambdaOption); 4086 if (isAllmanLambdaBrace(Right)) 4087 return !isItAnEmptyLambdaAllowed(Right, ShortLambdaOption); 4088 } 4089 4090 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace, 4091 tok::kw_class, tok::kw_struct, tok::comment) || 4092 Right.isMemberAccess() || 4093 Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless, 4094 tok::colon, tok::l_square, tok::at) || 4095 (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace)) || 4096 (Left.is(tok::r_paren) && 4097 Right.isOneOf(tok::identifier, tok::kw_const)) || 4098 (Left.is(tok::l_paren) && !Right.is(tok::r_paren)) || 4099 (Left.is(TT_TemplateOpener) && !Right.is(TT_TemplateCloser)); 4100 } 4101 4102 void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) { 4103 llvm::errs() << "AnnotatedTokens(L=" << Line.Level << "):\n"; 4104 const FormatToken *Tok = Line.First; 4105 while (Tok) { 4106 llvm::errs() << " M=" << Tok->MustBreakBefore 4107 << " C=" << Tok->CanBreakBefore 4108 << " T=" << getTokenTypeName(Tok->getType()) 4109 << " S=" << Tok->SpacesRequiredBefore 4110 << " F=" << Tok->Finalized << " B=" << Tok->BlockParameterCount 4111 << " BK=" << Tok->getBlockKind() << " P=" << Tok->SplitPenalty 4112 << " Name=" << Tok->Tok.getName() << " L=" << Tok->TotalLength 4113 << " PPK=" << Tok->getPackingKind() << " FakeLParens="; 4114 for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i) 4115 llvm::errs() << Tok->FakeLParens[i] << "/"; 4116 llvm::errs() << " FakeRParens=" << Tok->FakeRParens; 4117 llvm::errs() << " II=" << Tok->Tok.getIdentifierInfo(); 4118 llvm::errs() << " Text='" << Tok->TokenText << "'\n"; 4119 if (!Tok->Next) 4120 assert(Tok == Line.Last); 4121 Tok = Tok->Next; 4122 } 4123 llvm::errs() << "----\n"; 4124 } 4125 4126 } // namespace format 4127 } // namespace clang 4128