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