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 /// \brief 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 /// \brief A parser that gathers additional information about tokens. 29 /// 30 /// The \c TokenAnnotator tries to match parenthesis and square brakets and 31 /// store a parenthesis levels. It also tries to resolve matching "<" and ">" 32 /// into template parameter lists. 33 class AnnotatingParser { 34 public: 35 AnnotatingParser(const FormatStyle &Style, AnnotatedLine &Line, 36 const AdditionalKeywords &Keywords) 37 : Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(false), 38 Keywords(Keywords) { 39 Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false)); 40 resetTokenMetadata(CurrentToken); 41 } 42 43 private: 44 bool parseAngle() { 45 if (!CurrentToken) 46 return false; 47 FormatToken *Left = CurrentToken->Previous; 48 Left->ParentBracket = Contexts.back().ContextKind; 49 ScopedContextCreator ContextCreator(*this, tok::less, 10); 50 51 // If this angle is in the context of an expression, we need to be more 52 // hesitant to detect it as opening template parameters. 53 bool InExprContext = Contexts.back().IsExpression; 54 55 Contexts.back().IsExpression = false; 56 // If there's a template keyword before the opening angle bracket, this is a 57 // template parameter, not an argument. 58 Contexts.back().InTemplateArgument = 59 Left->Previous && Left->Previous->Tok.isNot(tok::kw_template); 60 61 if (Style.Language == FormatStyle::LK_Java && 62 CurrentToken->is(tok::question)) 63 next(); 64 65 while (CurrentToken) { 66 if (CurrentToken->is(tok::greater)) { 67 Left->MatchingParen = CurrentToken; 68 CurrentToken->MatchingParen = Left; 69 CurrentToken->Type = TT_TemplateCloser; 70 next(); 71 return true; 72 } 73 if (CurrentToken->is(tok::question) && 74 Style.Language == FormatStyle::LK_Java) { 75 next(); 76 continue; 77 } 78 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace) || 79 (CurrentToken->isOneOf(tok::colon, tok::question) && InExprContext)) 80 return false; 81 // If a && or || is found and interpreted as a binary operator, this set 82 // of angles is likely part of something like "a < b && c > d". If the 83 // angles are inside an expression, the ||/&& might also be a binary 84 // operator that was misinterpreted because we are parsing template 85 // parameters. 86 // FIXME: This is getting out of hand, write a decent parser. 87 if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) && 88 CurrentToken->Previous->is(TT_BinaryOperator) && 89 Contexts[Contexts.size() - 2].IsExpression && 90 !Line.startsWith(tok::kw_template)) 91 return false; 92 updateParameterCount(Left, CurrentToken); 93 if (!consumeToken()) 94 return false; 95 } 96 return false; 97 } 98 99 bool parseParens(bool LookForDecls = false) { 100 if (!CurrentToken) 101 return false; 102 FormatToken *Left = CurrentToken->Previous; 103 Left->ParentBracket = Contexts.back().ContextKind; 104 ScopedContextCreator ContextCreator(*this, tok::l_paren, 1); 105 106 // FIXME: This is a bit of a hack. Do better. 107 Contexts.back().ColonIsForRangeExpr = 108 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr; 109 110 bool StartsObjCMethodExpr = false; 111 if (CurrentToken->is(tok::caret)) { 112 // (^ can start a block type. 113 Left->Type = TT_ObjCBlockLParen; 114 } else if (FormatToken *MaybeSel = Left->Previous) { 115 // @selector( starts a selector. 116 if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && MaybeSel->Previous && 117 MaybeSel->Previous->is(tok::at)) { 118 StartsObjCMethodExpr = true; 119 } 120 } 121 122 if (Left->Previous && 123 (Left->Previous->isOneOf(tok::kw_static_assert, tok::kw_decltype, 124 tok::kw_if, tok::kw_while, tok::l_paren, 125 tok::comma) || 126 Left->Previous->is(TT_BinaryOperator))) { 127 // static_assert, if and while usually contain expressions. 128 Contexts.back().IsExpression = true; 129 } else if (Left->Previous && Left->Previous->is(tok::r_square) && 130 Left->Previous->MatchingParen && 131 Left->Previous->MatchingParen->is(TT_LambdaLSquare)) { 132 // This is a parameter list of a lambda expression. 133 Contexts.back().IsExpression = false; 134 } else if (Line.InPPDirective && 135 (!Left->Previous || 136 !Left->Previous->isOneOf(tok::identifier, 137 TT_OverloadedOperator))) { 138 Contexts.back().IsExpression = true; 139 } else if (Contexts[Contexts.size() - 2].CaretFound) { 140 // This is the parameter list of an ObjC block. 141 Contexts.back().IsExpression = false; 142 } else if (Left->Previous && Left->Previous->is(tok::kw___attribute)) { 143 Left->Type = TT_AttributeParen; 144 } else if (Left->Previous && Left->Previous->is(TT_ForEachMacro)) { 145 // The first argument to a foreach macro is a declaration. 146 Contexts.back().IsForEachMacro = true; 147 Contexts.back().IsExpression = false; 148 } else if (Left->Previous && Left->Previous->MatchingParen && 149 Left->Previous->MatchingParen->is(TT_ObjCBlockLParen)) { 150 Contexts.back().IsExpression = false; 151 } 152 153 if (StartsObjCMethodExpr) { 154 Contexts.back().ColonIsObjCMethodExpr = true; 155 Left->Type = TT_ObjCMethodExpr; 156 } 157 158 bool MightBeFunctionType = CurrentToken->isOneOf(tok::star, tok::amp); 159 bool HasMultipleLines = false; 160 bool HasMultipleParametersOnALine = false; 161 bool MightBeObjCForRangeLoop = 162 Left->Previous && Left->Previous->is(tok::kw_for); 163 while (CurrentToken) { 164 // LookForDecls is set when "if (" has been seen. Check for 165 // 'identifier' '*' 'identifier' followed by not '=' -- this 166 // '*' has to be a binary operator but determineStarAmpUsage() will 167 // categorize it as an unary operator, so set the right type here. 168 if (LookForDecls && CurrentToken->Next) { 169 FormatToken *Prev = CurrentToken->getPreviousNonComment(); 170 if (Prev) { 171 FormatToken *PrevPrev = Prev->getPreviousNonComment(); 172 FormatToken *Next = CurrentToken->Next; 173 if (PrevPrev && PrevPrev->is(tok::identifier) && 174 Prev->isOneOf(tok::star, tok::amp, tok::ampamp) && 175 CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) { 176 Prev->Type = TT_BinaryOperator; 177 LookForDecls = false; 178 } 179 } 180 } 181 182 if (CurrentToken->Previous->is(TT_PointerOrReference) && 183 CurrentToken->Previous->Previous->isOneOf(tok::l_paren, 184 tok::coloncolon)) 185 MightBeFunctionType = true; 186 if (CurrentToken->Previous->is(TT_BinaryOperator)) 187 Contexts.back().IsExpression = true; 188 if (CurrentToken->is(tok::r_paren)) { 189 if (MightBeFunctionType && CurrentToken->Next && 190 (CurrentToken->Next->is(tok::l_paren) || 191 (CurrentToken->Next->is(tok::l_square) && 192 !Contexts.back().IsExpression))) 193 Left->Type = TT_FunctionTypeLParen; 194 Left->MatchingParen = CurrentToken; 195 CurrentToken->MatchingParen = Left; 196 197 if (StartsObjCMethodExpr) { 198 CurrentToken->Type = TT_ObjCMethodExpr; 199 if (Contexts.back().FirstObjCSelectorName) { 200 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 201 Contexts.back().LongestObjCSelectorName; 202 } 203 } 204 205 if (Left->is(TT_AttributeParen)) 206 CurrentToken->Type = TT_AttributeParen; 207 if (Left->Previous && Left->Previous->is(TT_JavaAnnotation)) 208 CurrentToken->Type = TT_JavaAnnotation; 209 if (Left->Previous && Left->Previous->is(TT_LeadingJavaAnnotation)) 210 CurrentToken->Type = TT_LeadingJavaAnnotation; 211 212 if (!HasMultipleLines) 213 Left->PackingKind = PPK_Inconclusive; 214 else if (HasMultipleParametersOnALine) 215 Left->PackingKind = PPK_BinPacked; 216 else 217 Left->PackingKind = PPK_OnePerLine; 218 219 next(); 220 return true; 221 } 222 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace)) 223 return false; 224 225 if (CurrentToken->is(tok::l_brace)) 226 Left->Type = TT_Unknown; // Not TT_ObjCBlockLParen 227 if (CurrentToken->is(tok::comma) && CurrentToken->Next && 228 !CurrentToken->Next->HasUnescapedNewline && 229 !CurrentToken->Next->isTrailingComment()) 230 HasMultipleParametersOnALine = true; 231 if (CurrentToken->isOneOf(tok::kw_const, tok::kw_auto) || 232 CurrentToken->isSimpleTypeSpecifier()) 233 Contexts.back().IsExpression = false; 234 if (CurrentToken->isOneOf(tok::semi, tok::colon)) 235 MightBeObjCForRangeLoop = false; 236 if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in)) 237 CurrentToken->Type = TT_ObjCForIn; 238 // When we discover a 'new', we set CanBeExpression to 'false' in order to 239 // parse the type correctly. Reset that after a comma. 240 if (CurrentToken->is(tok::comma)) 241 Contexts.back().CanBeExpression = true; 242 243 FormatToken *Tok = CurrentToken; 244 if (!consumeToken()) 245 return false; 246 updateParameterCount(Left, Tok); 247 if (CurrentToken && CurrentToken->HasUnescapedNewline) 248 HasMultipleLines = true; 249 } 250 return false; 251 } 252 253 bool parseSquare() { 254 if (!CurrentToken) 255 return false; 256 257 // A '[' could be an index subscript (after an identifier or after 258 // ')' or ']'), it could be the start of an Objective-C method 259 // expression, or it could the start of an Objective-C array literal. 260 FormatToken *Left = CurrentToken->Previous; 261 Left->ParentBracket = Contexts.back().ContextKind; 262 FormatToken *Parent = Left->getPreviousNonComment(); 263 bool StartsObjCMethodExpr = 264 Style.Language == FormatStyle::LK_Cpp && 265 Contexts.back().CanBeExpression && Left->isNot(TT_LambdaLSquare) && 266 CurrentToken->isNot(tok::l_brace) && 267 (!Parent || 268 Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren, 269 tok::kw_return, tok::kw_throw) || 270 Parent->isUnaryOperator() || 271 Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) || 272 getBinOpPrecedence(Parent->Tok.getKind(), true, true) > prec::Unknown); 273 bool ColonFound = false; 274 275 unsigned BindingIncrease = 1; 276 if (Left->is(TT_Unknown)) { 277 if (StartsObjCMethodExpr) { 278 Left->Type = TT_ObjCMethodExpr; 279 } else if (Style.Language == FormatStyle::LK_JavaScript && Parent && 280 Contexts.back().ContextKind == tok::l_brace && 281 Parent->isOneOf(tok::l_brace, tok::comma)) { 282 Left->Type = TT_JsComputedPropertyName; 283 } else if (Parent && 284 Parent->isOneOf(tok::at, tok::equal, tok::comma, tok::l_paren, 285 tok::l_square, tok::question, tok::colon, 286 tok::kw_return)) { 287 Left->Type = TT_ArrayInitializerLSquare; 288 } else { 289 BindingIncrease = 10; 290 Left->Type = TT_ArraySubscriptLSquare; 291 } 292 } 293 294 ScopedContextCreator ContextCreator(*this, tok::l_square, BindingIncrease); 295 Contexts.back().IsExpression = true; 296 Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr; 297 298 while (CurrentToken) { 299 if (CurrentToken->is(tok::r_square)) { 300 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_paren) && 301 Left->is(TT_ObjCMethodExpr)) { 302 // An ObjC method call is rarely followed by an open parenthesis. 303 // FIXME: Do we incorrectly label ":" with this? 304 StartsObjCMethodExpr = false; 305 Left->Type = TT_Unknown; 306 } 307 if (StartsObjCMethodExpr && CurrentToken->Previous != Left) { 308 CurrentToken->Type = TT_ObjCMethodExpr; 309 // determineStarAmpUsage() thinks that '*' '[' is allocating an 310 // array of pointers, but if '[' starts a selector then '*' is a 311 // binary operator. 312 if (Parent && Parent->is(TT_PointerOrReference)) 313 Parent->Type = TT_BinaryOperator; 314 } 315 Left->MatchingParen = CurrentToken; 316 CurrentToken->MatchingParen = Left; 317 if (Contexts.back().FirstObjCSelectorName) { 318 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 319 Contexts.back().LongestObjCSelectorName; 320 if (Left->BlockParameterCount > 1) 321 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0; 322 } 323 next(); 324 return true; 325 } 326 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace)) 327 return false; 328 if (CurrentToken->is(tok::colon)) { 329 if (Left->is(TT_ArraySubscriptLSquare)) { 330 Left->Type = TT_ObjCMethodExpr; 331 StartsObjCMethodExpr = true; 332 Contexts.back().ColonIsObjCMethodExpr = true; 333 if (Parent && Parent->is(tok::r_paren)) 334 Parent->Type = TT_CastRParen; 335 } 336 ColonFound = true; 337 } 338 if (CurrentToken->is(tok::comma) && Left->is(TT_ObjCMethodExpr) && 339 !ColonFound) 340 Left->Type = TT_ArrayInitializerLSquare; 341 FormatToken *Tok = CurrentToken; 342 if (!consumeToken()) 343 return false; 344 updateParameterCount(Left, Tok); 345 } 346 return false; 347 } 348 349 bool parseBrace() { 350 if (CurrentToken) { 351 FormatToken *Left = CurrentToken->Previous; 352 Left->ParentBracket = Contexts.back().ContextKind; 353 354 if (Contexts.back().CaretFound) 355 Left->Type = TT_ObjCBlockLBrace; 356 Contexts.back().CaretFound = false; 357 358 ScopedContextCreator ContextCreator(*this, tok::l_brace, 1); 359 Contexts.back().ColonIsDictLiteral = true; 360 if (Left->BlockKind == BK_BracedInit) 361 Contexts.back().IsExpression = true; 362 363 while (CurrentToken) { 364 if (CurrentToken->is(tok::r_brace)) { 365 Left->MatchingParen = CurrentToken; 366 CurrentToken->MatchingParen = Left; 367 next(); 368 return true; 369 } 370 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square)) 371 return false; 372 updateParameterCount(Left, CurrentToken); 373 if (CurrentToken->isOneOf(tok::colon, tok::l_brace)) { 374 FormatToken *Previous = CurrentToken->getPreviousNonComment(); 375 if (((CurrentToken->is(tok::colon) && 376 (!Contexts.back().ColonIsDictLiteral || 377 Style.Language != FormatStyle::LK_Cpp)) || 378 Style.Language == FormatStyle::LK_Proto) && 379 Previous->Tok.getIdentifierInfo()) 380 Previous->Type = TT_SelectorName; 381 if (CurrentToken->is(tok::colon) || 382 Style.Language == FormatStyle::LK_JavaScript) 383 Left->Type = TT_DictLiteral; 384 } 385 if (!consumeToken()) 386 return false; 387 } 388 } 389 return true; 390 } 391 392 void updateParameterCount(FormatToken *Left, FormatToken *Current) { 393 if (Current->is(TT_LambdaLSquare) || 394 (Current->is(tok::caret) && Current->is(TT_UnaryOperator)) || 395 (Style.Language == FormatStyle::LK_JavaScript && 396 Current->is(Keywords.kw_function))) { 397 ++Left->BlockParameterCount; 398 } 399 if (Current->is(tok::comma)) { 400 ++Left->ParameterCount; 401 if (!Left->Role) 402 Left->Role.reset(new CommaSeparatedList(Style)); 403 Left->Role->CommaFound(Current); 404 } else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) { 405 Left->ParameterCount = 1; 406 } 407 } 408 409 bool parseConditional() { 410 while (CurrentToken) { 411 if (CurrentToken->is(tok::colon)) { 412 CurrentToken->Type = TT_ConditionalExpr; 413 next(); 414 return true; 415 } 416 if (!consumeToken()) 417 return false; 418 } 419 return false; 420 } 421 422 bool parseTemplateDeclaration() { 423 if (CurrentToken && CurrentToken->is(tok::less)) { 424 CurrentToken->Type = TT_TemplateOpener; 425 next(); 426 if (!parseAngle()) 427 return false; 428 if (CurrentToken) 429 CurrentToken->Previous->ClosesTemplateDeclaration = true; 430 return true; 431 } 432 return false; 433 } 434 435 bool consumeToken() { 436 FormatToken *Tok = CurrentToken; 437 next(); 438 switch (Tok->Tok.getKind()) { 439 case tok::plus: 440 case tok::minus: 441 if (!Tok->Previous && Line.MustBeDeclaration) 442 Tok->Type = TT_ObjCMethodSpecifier; 443 break; 444 case tok::colon: 445 if (!Tok->Previous) 446 return false; 447 // Colons from ?: are handled in parseConditional(). 448 if (Style.Language == FormatStyle::LK_JavaScript) { 449 if (Contexts.back().ColonIsForRangeExpr || // colon in for loop 450 (Contexts.size() == 1 && // switch/case labels 451 !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) || 452 Contexts.back().ContextKind == tok::l_paren || // function params 453 Contexts.back().ContextKind == tok::l_square || // array type 454 (Contexts.size() == 1 && 455 Line.MustBeDeclaration)) { // method/property declaration 456 Tok->Type = TT_JsTypeColon; 457 break; 458 } 459 } 460 if (Contexts.back().ColonIsDictLiteral) { 461 Tok->Type = TT_DictLiteral; 462 } else if (Contexts.back().ColonIsObjCMethodExpr || 463 Line.startsWith(TT_ObjCMethodSpecifier)) { 464 Tok->Type = TT_ObjCMethodExpr; 465 Tok->Previous->Type = TT_SelectorName; 466 if (Tok->Previous->ColumnWidth > 467 Contexts.back().LongestObjCSelectorName) { 468 Contexts.back().LongestObjCSelectorName = Tok->Previous->ColumnWidth; 469 } 470 if (!Contexts.back().FirstObjCSelectorName) 471 Contexts.back().FirstObjCSelectorName = Tok->Previous; 472 } else if (Contexts.back().ColonIsForRangeExpr) { 473 Tok->Type = TT_RangeBasedForLoopColon; 474 } else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) { 475 Tok->Type = TT_BitFieldColon; 476 } else if (Contexts.size() == 1 && 477 !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) { 478 if (Tok->Previous->is(tok::r_paren)) 479 Tok->Type = TT_CtorInitializerColon; 480 else 481 Tok->Type = TT_InheritanceColon; 482 } else if (Tok->Previous->is(tok::identifier) && Tok->Next && 483 Tok->Next->isOneOf(tok::r_paren, tok::comma)) { 484 // This handles a special macro in ObjC code where selectors including 485 // the colon are passed as macro arguments. 486 Tok->Type = TT_ObjCMethodExpr; 487 } else if (Contexts.back().ContextKind == tok::l_paren) { 488 Tok->Type = TT_InlineASMColon; 489 } 490 break; 491 case tok::kw_if: 492 case tok::kw_while: 493 if (CurrentToken && CurrentToken->is(tok::l_paren)) { 494 next(); 495 if (!parseParens(/*LookForDecls=*/true)) 496 return false; 497 } 498 break; 499 case tok::kw_for: 500 Contexts.back().ColonIsForRangeExpr = true; 501 next(); 502 if (!parseParens()) 503 return false; 504 break; 505 case tok::l_paren: 506 // When faced with 'operator()()', the kw_operator handler incorrectly 507 // marks the first l_paren as a OverloadedOperatorLParen. Here, we make 508 // the first two parens OverloadedOperators and the second l_paren an 509 // OverloadedOperatorLParen. 510 if (Tok->Previous && 511 Tok->Previous->is(tok::r_paren) && 512 Tok->Previous->MatchingParen && 513 Tok->Previous->MatchingParen->is(TT_OverloadedOperatorLParen)) { 514 Tok->Previous->Type = TT_OverloadedOperator; 515 Tok->Previous->MatchingParen->Type = TT_OverloadedOperator; 516 Tok->Type = TT_OverloadedOperatorLParen; 517 } 518 519 if (!parseParens()) 520 return false; 521 if (Line.MustBeDeclaration && Contexts.size() == 1 && 522 !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) && 523 (!Tok->Previous || 524 !Tok->Previous->isOneOf(tok::kw_decltype, tok::kw___attribute, 525 TT_LeadingJavaAnnotation))) 526 Line.MightBeFunctionDecl = true; 527 break; 528 case tok::l_square: 529 if (!parseSquare()) 530 return false; 531 break; 532 case tok::l_brace: 533 if (!parseBrace()) 534 return false; 535 break; 536 case tok::less: 537 if (!NonTemplateLess.count(Tok) && 538 (!Tok->Previous || 539 (!Tok->Previous->Tok.isLiteral() && 540 !(Tok->Previous->is(tok::r_paren) && Contexts.size() > 1))) && 541 parseAngle()) { 542 Tok->Type = TT_TemplateOpener; 543 } else { 544 Tok->Type = TT_BinaryOperator; 545 NonTemplateLess.insert(Tok); 546 CurrentToken = Tok; 547 next(); 548 } 549 break; 550 case tok::r_paren: 551 case tok::r_square: 552 return false; 553 case tok::r_brace: 554 // Lines can start with '}'. 555 if (Tok->Previous) 556 return false; 557 break; 558 case tok::greater: 559 Tok->Type = TT_BinaryOperator; 560 break; 561 case tok::kw_operator: 562 while (CurrentToken && 563 !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) { 564 if (CurrentToken->isOneOf(tok::star, tok::amp)) 565 CurrentToken->Type = TT_PointerOrReference; 566 consumeToken(); 567 if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator)) 568 CurrentToken->Previous->Type = TT_OverloadedOperator; 569 } 570 if (CurrentToken) { 571 CurrentToken->Type = TT_OverloadedOperatorLParen; 572 if (CurrentToken->Previous->is(TT_BinaryOperator)) 573 CurrentToken->Previous->Type = TT_OverloadedOperator; 574 } 575 break; 576 case tok::question: 577 if (Style.Language == FormatStyle::LK_JavaScript && Tok->Next && 578 Tok->Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren, 579 tok::r_brace)) { 580 // Question marks before semicolons, colons, etc. indicate optional 581 // types (fields, parameters), e.g. 582 // function(x?: string, y?) {...} 583 // class X { y?; } 584 Tok->Type = TT_JsTypeOptionalQuestion; 585 break; 586 } 587 // Declarations cannot be conditional expressions, this can only be part 588 // of a type declaration. 589 if (Line.MustBeDeclaration && 590 Style.Language == FormatStyle::LK_JavaScript) 591 break; 592 parseConditional(); 593 break; 594 case tok::kw_template: 595 parseTemplateDeclaration(); 596 break; 597 case tok::comma: 598 if (Contexts.back().InCtorInitializer) 599 Tok->Type = TT_CtorInitializerComma; 600 else if (Contexts.back().FirstStartOfName && 601 (Contexts.size() == 1 || Line.startsWith(tok::kw_for))) { 602 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true; 603 Line.IsMultiVariableDeclStmt = true; 604 } 605 if (Contexts.back().IsForEachMacro) 606 Contexts.back().IsExpression = true; 607 break; 608 default: 609 break; 610 } 611 return true; 612 } 613 614 void parseIncludeDirective() { 615 if (CurrentToken && CurrentToken->is(tok::less)) { 616 next(); 617 while (CurrentToken) { 618 if (CurrentToken->isNot(tok::comment) || CurrentToken->Next) 619 CurrentToken->Type = TT_ImplicitStringLiteral; 620 next(); 621 } 622 } 623 } 624 625 void parseWarningOrError() { 626 next(); 627 // We still want to format the whitespace left of the first token of the 628 // warning or error. 629 next(); 630 while (CurrentToken) { 631 CurrentToken->Type = TT_ImplicitStringLiteral; 632 next(); 633 } 634 } 635 636 void parsePragma() { 637 next(); // Consume "pragma". 638 if (CurrentToken && 639 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option)) { 640 bool IsMark = CurrentToken->is(Keywords.kw_mark); 641 next(); // Consume "mark". 642 next(); // Consume first token (so we fix leading whitespace). 643 while (CurrentToken) { 644 if (IsMark || CurrentToken->Previous->is(TT_BinaryOperator)) 645 CurrentToken->Type = TT_ImplicitStringLiteral; 646 next(); 647 } 648 } 649 } 650 651 LineType parsePreprocessorDirective() { 652 LineType Type = LT_PreprocessorDirective; 653 next(); 654 if (!CurrentToken) 655 return Type; 656 if (CurrentToken->Tok.is(tok::numeric_constant)) { 657 CurrentToken->SpacesRequiredBefore = 1; 658 return Type; 659 } 660 // Hashes in the middle of a line can lead to any strange token 661 // sequence. 662 if (!CurrentToken->Tok.getIdentifierInfo()) 663 return Type; 664 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) { 665 case tok::pp_include: 666 case tok::pp_include_next: 667 case tok::pp_import: 668 next(); 669 parseIncludeDirective(); 670 Type = LT_ImportStatement; 671 break; 672 case tok::pp_error: 673 case tok::pp_warning: 674 parseWarningOrError(); 675 break; 676 case tok::pp_pragma: 677 parsePragma(); 678 break; 679 case tok::pp_if: 680 case tok::pp_elif: 681 Contexts.back().IsExpression = true; 682 parseLine(); 683 break; 684 default: 685 break; 686 } 687 while (CurrentToken) 688 next(); 689 return Type; 690 } 691 692 public: 693 LineType parseLine() { 694 NonTemplateLess.clear(); 695 if (CurrentToken->is(tok::hash)) 696 return parsePreprocessorDirective(); 697 698 // Directly allow to 'import <string-literal>' to support protocol buffer 699 // definitions (code.google.com/p/protobuf) or missing "#" (either way we 700 // should not break the line). 701 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo(); 702 if ((Style.Language == FormatStyle::LK_Java && 703 CurrentToken->is(Keywords.kw_package)) || 704 (Info && Info->getPPKeywordID() == tok::pp_import && 705 CurrentToken->Next && 706 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier, 707 tok::kw_static))) { 708 next(); 709 parseIncludeDirective(); 710 return LT_ImportStatement; 711 } 712 713 // If this line starts and ends in '<' and '>', respectively, it is likely 714 // part of "#define <a/b.h>". 715 if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) { 716 parseIncludeDirective(); 717 return LT_ImportStatement; 718 } 719 720 // In .proto files, top-level options are very similar to import statements 721 // and should not be line-wrapped. 722 if (Style.Language == FormatStyle::LK_Proto && Line.Level == 0 && 723 CurrentToken->is(Keywords.kw_option)) { 724 next(); 725 if (CurrentToken && CurrentToken->is(tok::identifier)) 726 return LT_ImportStatement; 727 } 728 729 bool KeywordVirtualFound = false; 730 bool ImportStatement = false; 731 while (CurrentToken) { 732 if (CurrentToken->is(tok::kw_virtual)) 733 KeywordVirtualFound = true; 734 if (IsImportStatement(*CurrentToken)) 735 ImportStatement = true; 736 if (!consumeToken()) 737 return LT_Invalid; 738 } 739 if (KeywordVirtualFound) 740 return LT_VirtualFunctionDecl; 741 if (ImportStatement) 742 return LT_ImportStatement; 743 744 if (Line.startsWith(TT_ObjCMethodSpecifier)) { 745 if (Contexts.back().FirstObjCSelectorName) 746 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 747 Contexts.back().LongestObjCSelectorName; 748 return LT_ObjCMethodDecl; 749 } 750 751 return LT_Other; 752 } 753 754 private: 755 bool IsImportStatement(const FormatToken &Tok) { 756 // FIXME: Closure-library specific stuff should not be hard-coded but be 757 // configurable. 758 return Style.Language == FormatStyle::LK_JavaScript && 759 Tok.TokenText == "goog" && Tok.Next && Tok.Next->is(tok::period) && 760 Tok.Next->Next && (Tok.Next->Next->TokenText == "module" || 761 Tok.Next->Next->TokenText == "require" || 762 Tok.Next->Next->TokenText == "provide") && 763 Tok.Next->Next->Next && Tok.Next->Next->Next->is(tok::l_paren); 764 } 765 766 void resetTokenMetadata(FormatToken *Token) { 767 if (!Token) 768 return; 769 770 // Reset token type in case we have already looked at it and then 771 // recovered from an error (e.g. failure to find the matching >). 772 if (!CurrentToken->isOneOf(TT_LambdaLSquare, TT_ForEachMacro, 773 TT_FunctionLBrace, TT_ImplicitStringLiteral, 774 TT_InlineASMBrace, TT_JsFatArrow, TT_LambdaArrow, 775 TT_RegexLiteral)) 776 CurrentToken->Type = TT_Unknown; 777 CurrentToken->Role.reset(); 778 CurrentToken->MatchingParen = nullptr; 779 CurrentToken->FakeLParens.clear(); 780 CurrentToken->FakeRParens = 0; 781 } 782 783 void next() { 784 if (CurrentToken) { 785 CurrentToken->NestingLevel = Contexts.size() - 1; 786 CurrentToken->BindingStrength = Contexts.back().BindingStrength; 787 modifyContext(*CurrentToken); 788 determineTokenType(*CurrentToken); 789 CurrentToken = CurrentToken->Next; 790 } 791 792 resetTokenMetadata(CurrentToken); 793 } 794 795 /// \brief A struct to hold information valid in a specific context, e.g. 796 /// a pair of parenthesis. 797 struct Context { 798 Context(tok::TokenKind ContextKind, unsigned BindingStrength, 799 bool IsExpression) 800 : ContextKind(ContextKind), BindingStrength(BindingStrength), 801 IsExpression(IsExpression) {} 802 803 tok::TokenKind ContextKind; 804 unsigned BindingStrength; 805 bool IsExpression; 806 unsigned LongestObjCSelectorName = 0; 807 bool ColonIsForRangeExpr = false; 808 bool ColonIsDictLiteral = false; 809 bool ColonIsObjCMethodExpr = false; 810 FormatToken *FirstObjCSelectorName = nullptr; 811 FormatToken *FirstStartOfName = nullptr; 812 bool CanBeExpression = true; 813 bool InTemplateArgument = false; 814 bool InCtorInitializer = false; 815 bool CaretFound = false; 816 bool IsForEachMacro = false; 817 }; 818 819 /// \brief Puts a new \c Context onto the stack \c Contexts for the lifetime 820 /// of each instance. 821 struct ScopedContextCreator { 822 AnnotatingParser &P; 823 824 ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind, 825 unsigned Increase) 826 : P(P) { 827 P.Contexts.push_back(Context(ContextKind, 828 P.Contexts.back().BindingStrength + Increase, 829 P.Contexts.back().IsExpression)); 830 } 831 832 ~ScopedContextCreator() { P.Contexts.pop_back(); } 833 }; 834 835 void modifyContext(const FormatToken &Current) { 836 if (Current.getPrecedence() == prec::Assignment && 837 !Line.First->isOneOf(tok::kw_template, tok::kw_using) && 838 (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) { 839 Contexts.back().IsExpression = true; 840 if (!Line.startsWith(TT_UnaryOperator)) { 841 for (FormatToken *Previous = Current.Previous; 842 Previous && Previous->Previous && 843 !Previous->Previous->isOneOf(tok::comma, tok::semi); 844 Previous = Previous->Previous) { 845 if (Previous->isOneOf(tok::r_square, tok::r_paren)) { 846 Previous = Previous->MatchingParen; 847 if (!Previous) 848 break; 849 } 850 if (Previous->opensScope()) 851 break; 852 if (Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) && 853 Previous->isOneOf(tok::star, tok::amp, tok::ampamp) && 854 Previous->Previous && Previous->Previous->isNot(tok::equal)) 855 Previous->Type = TT_PointerOrReference; 856 } 857 } 858 } else if (Current.is(tok::lessless) && 859 (!Current.Previous || !Current.Previous->is(tok::kw_operator))) { 860 Contexts.back().IsExpression = true; 861 } else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) { 862 Contexts.back().IsExpression = true; 863 } else if (Current.is(TT_TrailingReturnArrow)) { 864 Contexts.back().IsExpression = false; 865 } else if (Current.is(TT_LambdaArrow) || Current.is(Keywords.kw_assert)) { 866 Contexts.back().IsExpression = Style.Language == FormatStyle::LK_Java; 867 } else if (Current.is(tok::l_paren) && !Line.MustBeDeclaration && 868 !Line.InPPDirective && 869 (!Current.Previous || 870 Current.Previous->isNot(tok::kw_decltype))) { 871 bool ParametersOfFunctionType = 872 Current.Previous && Current.Previous->is(tok::r_paren) && 873 Current.Previous->MatchingParen && 874 Current.Previous->MatchingParen->is(TT_FunctionTypeLParen); 875 bool IsForOrCatch = Current.Previous && 876 Current.Previous->isOneOf(tok::kw_for, tok::kw_catch); 877 Contexts.back().IsExpression = !ParametersOfFunctionType && !IsForOrCatch; 878 } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) { 879 for (FormatToken *Previous = Current.Previous; 880 Previous && Previous->isOneOf(tok::star, tok::amp); 881 Previous = Previous->Previous) 882 Previous->Type = TT_PointerOrReference; 883 if (Line.MustBeDeclaration) 884 Contexts.back().IsExpression = Contexts.front().InCtorInitializer; 885 } else if (Current.Previous && 886 Current.Previous->is(TT_CtorInitializerColon)) { 887 Contexts.back().IsExpression = true; 888 Contexts.back().InCtorInitializer = true; 889 } else if (Current.is(tok::kw_new)) { 890 Contexts.back().CanBeExpression = false; 891 } else if (Current.isOneOf(tok::semi, tok::exclaim)) { 892 // This should be the condition or increment in a for-loop. 893 Contexts.back().IsExpression = true; 894 } 895 } 896 897 void determineTokenType(FormatToken &Current) { 898 if (!Current.is(TT_Unknown)) 899 // The token type is already known. 900 return; 901 902 // Line.MightBeFunctionDecl can only be true after the parentheses of a 903 // function declaration have been found. In this case, 'Current' is a 904 // trailing token of this declaration and thus cannot be a name. 905 if (Current.is(Keywords.kw_instanceof)) { 906 Current.Type = TT_BinaryOperator; 907 } else if (isStartOfName(Current) && 908 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) { 909 Contexts.back().FirstStartOfName = &Current; 910 Current.Type = TT_StartOfName; 911 } else if (Current.is(tok::kw_auto)) { 912 AutoFound = true; 913 } else if (Current.is(tok::arrow) && 914 Style.Language == FormatStyle::LK_Java) { 915 Current.Type = TT_LambdaArrow; 916 } else if (Current.is(tok::arrow) && AutoFound && Line.MustBeDeclaration && 917 Current.NestingLevel == 0) { 918 Current.Type = TT_TrailingReturnArrow; 919 } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) { 920 Current.Type = 921 determineStarAmpUsage(Current, Contexts.back().CanBeExpression && 922 Contexts.back().IsExpression, 923 Contexts.back().InTemplateArgument); 924 } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) { 925 Current.Type = determinePlusMinusCaretUsage(Current); 926 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret)) 927 Contexts.back().CaretFound = true; 928 } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) { 929 Current.Type = determineIncrementUsage(Current); 930 } else if (Current.isOneOf(tok::exclaim, tok::tilde)) { 931 Current.Type = TT_UnaryOperator; 932 } else if (Current.is(tok::question)) { 933 if (Style.Language == FormatStyle::LK_JavaScript && 934 Line.MustBeDeclaration) { 935 // In JavaScript, `interface X { foo?(): bar; }` is an optional method 936 // on the interface, not a ternary expression. 937 Current.Type = TT_JsTypeOptionalQuestion; 938 } else { 939 Current.Type = TT_ConditionalExpr; 940 } 941 } else if (Current.isBinaryOperator() && 942 (!Current.Previous || Current.Previous->isNot(tok::l_square))) { 943 Current.Type = TT_BinaryOperator; 944 } else if (Current.is(tok::comment)) { 945 if (Current.TokenText.startswith("/*")) { 946 if (Current.TokenText.endswith("*/")) 947 Current.Type = TT_BlockComment; 948 else 949 // The lexer has for some reason determined a comment here. But we 950 // cannot really handle it, if it isn't properly terminated. 951 Current.Tok.setKind(tok::unknown); 952 } else { 953 Current.Type = TT_LineComment; 954 } 955 } else if (Current.is(tok::r_paren)) { 956 if (rParenEndsCast(Current)) 957 Current.Type = TT_CastRParen; 958 if (Current.MatchingParen && Current.Next && 959 !Current.Next->isBinaryOperator() && 960 !Current.Next->isOneOf(tok::semi, tok::colon, tok::l_brace)) 961 if (FormatToken *BeforeParen = Current.MatchingParen->Previous) 962 if (BeforeParen->is(tok::identifier) && 963 BeforeParen->TokenText == BeforeParen->TokenText.upper() && 964 (!BeforeParen->Previous || 965 BeforeParen->Previous->ClosesTemplateDeclaration)) 966 Current.Type = TT_FunctionAnnotationRParen; 967 } else if (Current.is(tok::at) && Current.Next) { 968 if (Current.Next->isStringLiteral()) { 969 Current.Type = TT_ObjCStringLiteral; 970 } else { 971 switch (Current.Next->Tok.getObjCKeywordID()) { 972 case tok::objc_interface: 973 case tok::objc_implementation: 974 case tok::objc_protocol: 975 Current.Type = TT_ObjCDecl; 976 break; 977 case tok::objc_property: 978 Current.Type = TT_ObjCProperty; 979 break; 980 default: 981 break; 982 } 983 } 984 } else if (Current.is(tok::period)) { 985 FormatToken *PreviousNoComment = Current.getPreviousNonComment(); 986 if (PreviousNoComment && 987 PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) 988 Current.Type = TT_DesignatedInitializerPeriod; 989 else if (Style.Language == FormatStyle::LK_Java && Current.Previous && 990 Current.Previous->isOneOf(TT_JavaAnnotation, 991 TT_LeadingJavaAnnotation)) { 992 Current.Type = Current.Previous->Type; 993 } 994 } else if (Current.isOneOf(tok::identifier, tok::kw_const) && 995 Current.Previous && 996 !Current.Previous->isOneOf(tok::equal, tok::at) && 997 Line.MightBeFunctionDecl && Contexts.size() == 1) { 998 // Line.MightBeFunctionDecl can only be true after the parentheses of a 999 // function declaration have been found. 1000 Current.Type = TT_TrailingAnnotation; 1001 } else if ((Style.Language == FormatStyle::LK_Java || 1002 Style.Language == FormatStyle::LK_JavaScript) && 1003 Current.Previous) { 1004 if (Current.Previous->is(tok::at) && 1005 Current.isNot(Keywords.kw_interface)) { 1006 const FormatToken &AtToken = *Current.Previous; 1007 const FormatToken *Previous = AtToken.getPreviousNonComment(); 1008 if (!Previous || Previous->is(TT_LeadingJavaAnnotation)) 1009 Current.Type = TT_LeadingJavaAnnotation; 1010 else 1011 Current.Type = TT_JavaAnnotation; 1012 } else if (Current.Previous->is(tok::period) && 1013 Current.Previous->isOneOf(TT_JavaAnnotation, 1014 TT_LeadingJavaAnnotation)) { 1015 Current.Type = Current.Previous->Type; 1016 } 1017 } 1018 } 1019 1020 /// \brief Take a guess at whether \p Tok starts a name of a function or 1021 /// variable declaration. 1022 /// 1023 /// This is a heuristic based on whether \p Tok is an identifier following 1024 /// something that is likely a type. 1025 bool isStartOfName(const FormatToken &Tok) { 1026 if (Tok.isNot(tok::identifier) || !Tok.Previous) 1027 return false; 1028 1029 if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof)) 1030 return false; 1031 1032 // Skip "const" as it does not have an influence on whether this is a name. 1033 FormatToken *PreviousNotConst = Tok.Previous; 1034 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const)) 1035 PreviousNotConst = PreviousNotConst->Previous; 1036 1037 if (!PreviousNotConst) 1038 return false; 1039 1040 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) && 1041 PreviousNotConst->Previous && 1042 PreviousNotConst->Previous->is(tok::hash); 1043 1044 if (PreviousNotConst->is(TT_TemplateCloser)) 1045 return PreviousNotConst && PreviousNotConst->MatchingParen && 1046 PreviousNotConst->MatchingParen->Previous && 1047 PreviousNotConst->MatchingParen->Previous->isNot(tok::period) && 1048 PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template); 1049 1050 if (PreviousNotConst->is(tok::r_paren) && PreviousNotConst->MatchingParen && 1051 PreviousNotConst->MatchingParen->Previous && 1052 PreviousNotConst->MatchingParen->Previous->is(tok::kw_decltype)) 1053 return true; 1054 1055 return (!IsPPKeyword && 1056 PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto)) || 1057 PreviousNotConst->is(TT_PointerOrReference) || 1058 PreviousNotConst->isSimpleTypeSpecifier(); 1059 } 1060 1061 /// \brief Determine whether ')' is ending a cast. 1062 bool rParenEndsCast(const FormatToken &Tok) { 1063 FormatToken *LeftOfParens = nullptr; 1064 if (Tok.MatchingParen) 1065 LeftOfParens = Tok.MatchingParen->getPreviousNonComment(); 1066 if (LeftOfParens && LeftOfParens->is(tok::r_paren) && 1067 LeftOfParens->MatchingParen) 1068 LeftOfParens = LeftOfParens->MatchingParen->Previous; 1069 if (LeftOfParens && LeftOfParens->is(tok::r_square) && 1070 LeftOfParens->MatchingParen && 1071 LeftOfParens->MatchingParen->is(TT_LambdaLSquare)) 1072 return false; 1073 if (Tok.Next) { 1074 if (Tok.Next->is(tok::question)) 1075 return false; 1076 if (Style.Language == FormatStyle::LK_JavaScript && 1077 Tok.Next->is(Keywords.kw_in)) 1078 return false; 1079 if (Style.Language == FormatStyle::LK_Java && Tok.Next->is(tok::l_paren)) 1080 return true; 1081 } 1082 bool IsCast = false; 1083 bool ParensAreEmpty = Tok.Previous == Tok.MatchingParen; 1084 bool ParensAreType = 1085 !Tok.Previous || 1086 Tok.Previous->isOneOf(TT_PointerOrReference, TT_TemplateCloser) || 1087 Tok.Previous->isSimpleTypeSpecifier(); 1088 bool ParensCouldEndDecl = 1089 Tok.Next && Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace); 1090 bool IsSizeOfOrAlignOf = 1091 LeftOfParens && LeftOfParens->isOneOf(tok::kw_sizeof, tok::kw_alignof); 1092 if (ParensAreType && !ParensCouldEndDecl && !IsSizeOfOrAlignOf && 1093 (Contexts.size() > 1 && Contexts[Contexts.size() - 2].IsExpression)) 1094 IsCast = true; 1095 else if (Tok.Next && Tok.Next->isNot(tok::string_literal) && 1096 (Tok.Next->Tok.isLiteral() || 1097 Tok.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof))) 1098 IsCast = true; 1099 // If there is an identifier after the (), it is likely a cast, unless 1100 // there is also an identifier before the (). 1101 else if (LeftOfParens && Tok.Next && 1102 (LeftOfParens->Tok.getIdentifierInfo() == nullptr || 1103 LeftOfParens->isOneOf(tok::kw_return, tok::kw_case)) && 1104 !LeftOfParens->isOneOf(TT_OverloadedOperator, tok::at, 1105 TT_TemplateCloser)) { 1106 if (Tok.Next->isOneOf(tok::identifier, tok::numeric_constant)) { 1107 IsCast = true; 1108 } else { 1109 // Use heuristics to recognize c style casting. 1110 FormatToken *Prev = Tok.Previous; 1111 if (Prev && Prev->isOneOf(tok::amp, tok::star)) 1112 Prev = Prev->Previous; 1113 1114 if (Prev && Tok.Next && Tok.Next->Next) { 1115 bool NextIsUnary = Tok.Next->isUnaryOperator() || 1116 Tok.Next->isOneOf(tok::amp, tok::star); 1117 IsCast = 1118 NextIsUnary && !Tok.Next->is(tok::plus) && 1119 Tok.Next->Next->isOneOf(tok::identifier, tok::numeric_constant); 1120 } 1121 1122 for (; Prev != Tok.MatchingParen; Prev = Prev->Previous) { 1123 if (!Prev || 1124 !Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon)) { 1125 IsCast = false; 1126 break; 1127 } 1128 } 1129 } 1130 } 1131 return IsCast && !ParensAreEmpty; 1132 } 1133 1134 /// \brief Return the type of the given token assuming it is * or &. 1135 TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression, 1136 bool InTemplateArgument) { 1137 if (Style.Language == FormatStyle::LK_JavaScript) 1138 return TT_BinaryOperator; 1139 1140 const FormatToken *PrevToken = Tok.getPreviousNonComment(); 1141 if (!PrevToken) 1142 return TT_UnaryOperator; 1143 1144 const FormatToken *NextToken = Tok.getNextNonComment(); 1145 if (!NextToken || 1146 NextToken->isOneOf(tok::arrow, Keywords.kw_final, 1147 Keywords.kw_override) || 1148 (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) 1149 return TT_PointerOrReference; 1150 1151 if (PrevToken->is(tok::coloncolon)) 1152 return TT_PointerOrReference; 1153 1154 if (PrevToken->isOneOf(tok::l_paren, tok::l_square, tok::l_brace, 1155 tok::comma, tok::semi, tok::kw_return, tok::colon, 1156 tok::equal, tok::kw_delete, tok::kw_sizeof) || 1157 PrevToken->isOneOf(TT_BinaryOperator, TT_ConditionalExpr, 1158 TT_UnaryOperator, TT_CastRParen)) 1159 return TT_UnaryOperator; 1160 1161 if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare)) 1162 return TT_PointerOrReference; 1163 if (NextToken->is(tok::kw_operator) && !IsExpression) 1164 return TT_PointerOrReference; 1165 if (NextToken->isOneOf(tok::comma, tok::semi)) 1166 return TT_PointerOrReference; 1167 1168 if (PrevToken->is(tok::r_paren) && PrevToken->MatchingParen && 1169 PrevToken->MatchingParen->Previous && 1170 PrevToken->MatchingParen->Previous->isOneOf(tok::kw_typeof, 1171 tok::kw_decltype)) 1172 return TT_PointerOrReference; 1173 1174 if (PrevToken->Tok.isLiteral() || 1175 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true, 1176 tok::kw_false, tok::r_brace) || 1177 NextToken->Tok.isLiteral() || 1178 NextToken->isOneOf(tok::kw_true, tok::kw_false) || 1179 NextToken->isUnaryOperator() || 1180 // If we know we're in a template argument, there are no named 1181 // declarations. Thus, having an identifier on the right-hand side 1182 // indicates a binary operator. 1183 (InTemplateArgument && NextToken->Tok.isAnyIdentifier())) 1184 return TT_BinaryOperator; 1185 1186 // "&&(" is quite unlikely to be two successive unary "&". 1187 if (Tok.is(tok::ampamp) && NextToken && NextToken->is(tok::l_paren)) 1188 return TT_BinaryOperator; 1189 1190 // This catches some cases where evaluation order is used as control flow: 1191 // aaa && aaa->f(); 1192 const FormatToken *NextNextToken = NextToken->getNextNonComment(); 1193 if (NextNextToken && NextNextToken->is(tok::arrow)) 1194 return TT_BinaryOperator; 1195 1196 // It is very unlikely that we are going to find a pointer or reference type 1197 // definition on the RHS of an assignment. 1198 if (IsExpression && !Contexts.back().CaretFound) 1199 return TT_BinaryOperator; 1200 1201 return TT_PointerOrReference; 1202 } 1203 1204 TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) { 1205 const FormatToken *PrevToken = Tok.getPreviousNonComment(); 1206 if (!PrevToken || PrevToken->is(TT_CastRParen)) 1207 return TT_UnaryOperator; 1208 1209 // Use heuristics to recognize unary operators. 1210 if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square, 1211 tok::question, tok::colon, tok::kw_return, 1212 tok::kw_case, tok::at, tok::l_brace)) 1213 return TT_UnaryOperator; 1214 1215 // There can't be two consecutive binary operators. 1216 if (PrevToken->is(TT_BinaryOperator)) 1217 return TT_UnaryOperator; 1218 1219 // Fall back to marking the token as binary operator. 1220 return TT_BinaryOperator; 1221 } 1222 1223 /// \brief Determine whether ++/-- are pre- or post-increments/-decrements. 1224 TokenType determineIncrementUsage(const FormatToken &Tok) { 1225 const FormatToken *PrevToken = Tok.getPreviousNonComment(); 1226 if (!PrevToken || PrevToken->is(TT_CastRParen)) 1227 return TT_UnaryOperator; 1228 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier)) 1229 return TT_TrailingUnaryOperator; 1230 1231 return TT_UnaryOperator; 1232 } 1233 1234 SmallVector<Context, 8> Contexts; 1235 1236 const FormatStyle &Style; 1237 AnnotatedLine &Line; 1238 FormatToken *CurrentToken; 1239 bool AutoFound; 1240 const AdditionalKeywords &Keywords; 1241 1242 // Set of "<" tokens that do not open a template parameter list. If parseAngle 1243 // determines that a specific token can't be a template opener, it will make 1244 // same decision irrespective of the decisions for tokens leading up to it. 1245 // Store this information to prevent this from causing exponential runtime. 1246 llvm::SmallPtrSet<FormatToken *, 16> NonTemplateLess; 1247 }; 1248 1249 static const int PrecedenceUnaryOperator = prec::PointerToMember + 1; 1250 static const int PrecedenceArrowAndPeriod = prec::PointerToMember + 2; 1251 1252 /// \brief Parses binary expressions by inserting fake parenthesis based on 1253 /// operator precedence. 1254 class ExpressionParser { 1255 public: 1256 ExpressionParser(const FormatStyle &Style, const AdditionalKeywords &Keywords, 1257 AnnotatedLine &Line) 1258 : Style(Style), Keywords(Keywords), Current(Line.First) {} 1259 1260 /// \brief Parse expressions with the given operatore precedence. 1261 void parse(int Precedence = 0) { 1262 // Skip 'return' and ObjC selector colons as they are not part of a binary 1263 // expression. 1264 while (Current && (Current->is(tok::kw_return) || 1265 (Current->is(tok::colon) && 1266 Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)))) 1267 next(); 1268 1269 if (!Current || Precedence > PrecedenceArrowAndPeriod) 1270 return; 1271 1272 // Conditional expressions need to be parsed separately for proper nesting. 1273 if (Precedence == prec::Conditional) { 1274 parseConditionalExpr(); 1275 return; 1276 } 1277 1278 // Parse unary operators, which all have a higher precedence than binary 1279 // operators. 1280 if (Precedence == PrecedenceUnaryOperator) { 1281 parseUnaryOperator(); 1282 return; 1283 } 1284 1285 FormatToken *Start = Current; 1286 FormatToken *LatestOperator = nullptr; 1287 unsigned OperatorIndex = 0; 1288 1289 while (Current) { 1290 // Consume operators with higher precedence. 1291 parse(Precedence + 1); 1292 1293 int CurrentPrecedence = getCurrentPrecedence(); 1294 1295 if (Current && Current->is(TT_SelectorName) && 1296 Precedence == CurrentPrecedence) { 1297 if (LatestOperator) 1298 addFakeParenthesis(Start, prec::Level(Precedence)); 1299 Start = Current; 1300 } 1301 1302 // At the end of the line or when an operator with higher precedence is 1303 // found, insert fake parenthesis and return. 1304 if (!Current || (Current->closesScope() && Current->MatchingParen) || 1305 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) || 1306 (CurrentPrecedence == prec::Conditional && 1307 Precedence == prec::Assignment && Current->is(tok::colon))) { 1308 break; 1309 } 1310 1311 // Consume scopes: (), [], <> and {} 1312 if (Current->opensScope()) { 1313 while (Current && !Current->closesScope()) { 1314 next(); 1315 parse(); 1316 } 1317 next(); 1318 } else { 1319 // Operator found. 1320 if (CurrentPrecedence == Precedence) { 1321 LatestOperator = Current; 1322 Current->OperatorIndex = OperatorIndex; 1323 ++OperatorIndex; 1324 } 1325 next(/*SkipPastLeadingComments=*/Precedence > 0); 1326 } 1327 } 1328 1329 if (LatestOperator && (Current || Precedence > 0)) { 1330 LatestOperator->LastOperator = true; 1331 if (Precedence == PrecedenceArrowAndPeriod) { 1332 // Call expressions don't have a binary operator precedence. 1333 addFakeParenthesis(Start, prec::Unknown); 1334 } else { 1335 addFakeParenthesis(Start, prec::Level(Precedence)); 1336 } 1337 } 1338 } 1339 1340 private: 1341 /// \brief Gets the precedence (+1) of the given token for binary operators 1342 /// and other tokens that we treat like binary operators. 1343 int getCurrentPrecedence() { 1344 if (Current) { 1345 const FormatToken *NextNonComment = Current->getNextNonComment(); 1346 if (Current->is(TT_ConditionalExpr)) 1347 return prec::Conditional; 1348 if (NextNonComment && NextNonComment->is(tok::colon) && 1349 NextNonComment->is(TT_DictLiteral)) 1350 return prec::Comma; 1351 if (Current->is(TT_LambdaArrow)) 1352 return prec::Comma; 1353 if (Current->is(TT_JsFatArrow)) 1354 return prec::Assignment; 1355 if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName, 1356 TT_JsComputedPropertyName) || 1357 (Current->is(tok::comment) && NextNonComment && 1358 NextNonComment->is(TT_SelectorName))) 1359 return 0; 1360 if (Current->is(TT_RangeBasedForLoopColon)) 1361 return prec::Comma; 1362 if ((Style.Language == FormatStyle::LK_Java || 1363 Style.Language == FormatStyle::LK_JavaScript) && 1364 Current->is(Keywords.kw_instanceof)) 1365 return prec::Relational; 1366 if (Current->is(TT_BinaryOperator) || Current->is(tok::comma)) 1367 return Current->getPrecedence(); 1368 if (Current->isOneOf(tok::period, tok::arrow)) 1369 return PrecedenceArrowAndPeriod; 1370 if (Style.Language == FormatStyle::LK_Java && 1371 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements, 1372 Keywords.kw_throws)) 1373 return 0; 1374 } 1375 return -1; 1376 } 1377 1378 void addFakeParenthesis(FormatToken *Start, prec::Level Precedence) { 1379 Start->FakeLParens.push_back(Precedence); 1380 if (Precedence > prec::Unknown) 1381 Start->StartsBinaryExpression = true; 1382 if (Current) { 1383 FormatToken *Previous = Current->Previous; 1384 while (Previous->is(tok::comment) && Previous->Previous) 1385 Previous = Previous->Previous; 1386 ++Previous->FakeRParens; 1387 if (Precedence > prec::Unknown) 1388 Previous->EndsBinaryExpression = true; 1389 } 1390 } 1391 1392 /// \brief Parse unary operator expressions and surround them with fake 1393 /// parentheses if appropriate. 1394 void parseUnaryOperator() { 1395 if (!Current || Current->isNot(TT_UnaryOperator)) { 1396 parse(PrecedenceArrowAndPeriod); 1397 return; 1398 } 1399 1400 FormatToken *Start = Current; 1401 next(); 1402 parseUnaryOperator(); 1403 1404 // The actual precedence doesn't matter. 1405 addFakeParenthesis(Start, prec::Unknown); 1406 } 1407 1408 void parseConditionalExpr() { 1409 while (Current && Current->isTrailingComment()) { 1410 next(); 1411 } 1412 FormatToken *Start = Current; 1413 parse(prec::LogicalOr); 1414 if (!Current || !Current->is(tok::question)) 1415 return; 1416 next(); 1417 parse(prec::Assignment); 1418 if (!Current || Current->isNot(TT_ConditionalExpr)) 1419 return; 1420 next(); 1421 parse(prec::Assignment); 1422 addFakeParenthesis(Start, prec::Conditional); 1423 } 1424 1425 void next(bool SkipPastLeadingComments = true) { 1426 if (Current) 1427 Current = Current->Next; 1428 while (Current && 1429 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) && 1430 Current->isTrailingComment()) 1431 Current = Current->Next; 1432 } 1433 1434 const FormatStyle &Style; 1435 const AdditionalKeywords &Keywords; 1436 FormatToken *Current; 1437 }; 1438 1439 } // end anonymous namespace 1440 1441 void TokenAnnotator::setCommentLineLevels( 1442 SmallVectorImpl<AnnotatedLine *> &Lines) { 1443 const AnnotatedLine *NextNonCommentLine = nullptr; 1444 for (SmallVectorImpl<AnnotatedLine *>::reverse_iterator I = Lines.rbegin(), 1445 E = Lines.rend(); 1446 I != E; ++I) { 1447 if (NextNonCommentLine && (*I)->First->is(tok::comment) && 1448 (*I)->First->Next == nullptr) 1449 (*I)->Level = NextNonCommentLine->Level; 1450 else 1451 NextNonCommentLine = (*I)->First->isNot(tok::r_brace) ? (*I) : nullptr; 1452 1453 setCommentLineLevels((*I)->Children); 1454 } 1455 } 1456 1457 void TokenAnnotator::annotate(AnnotatedLine &Line) { 1458 for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(), 1459 E = Line.Children.end(); 1460 I != E; ++I) { 1461 annotate(**I); 1462 } 1463 AnnotatingParser Parser(Style, Line, Keywords); 1464 Line.Type = Parser.parseLine(); 1465 if (Line.Type == LT_Invalid) 1466 return; 1467 1468 ExpressionParser ExprParser(Style, Keywords, Line); 1469 ExprParser.parse(); 1470 1471 if (Line.startsWith(TT_ObjCMethodSpecifier)) 1472 Line.Type = LT_ObjCMethodDecl; 1473 else if (Line.startsWith(TT_ObjCDecl)) 1474 Line.Type = LT_ObjCDecl; 1475 else if (Line.startsWith(TT_ObjCProperty)) 1476 Line.Type = LT_ObjCProperty; 1477 1478 Line.First->SpacesRequiredBefore = 1; 1479 Line.First->CanBreakBefore = Line.First->MustBreakBefore; 1480 } 1481 1482 // This function heuristically determines whether 'Current' starts the name of a 1483 // function declaration. 1484 static bool isFunctionDeclarationName(const FormatToken &Current) { 1485 auto skipOperatorName = [](const FormatToken* Next) -> const FormatToken* { 1486 for (; Next; Next = Next->Next) { 1487 if (Next->is(TT_OverloadedOperatorLParen)) 1488 return Next; 1489 if (Next->is(TT_OverloadedOperator)) 1490 continue; 1491 if (Next->isOneOf(tok::kw_new, tok::kw_delete)) { 1492 // For 'new[]' and 'delete[]'. 1493 if (Next->Next && Next->Next->is(tok::l_square) && 1494 Next->Next->Next && Next->Next->Next->is(tok::r_square)) 1495 Next = Next->Next->Next; 1496 continue; 1497 } 1498 1499 break; 1500 } 1501 return nullptr; 1502 }; 1503 1504 const FormatToken *Next = Current.Next; 1505 if (Current.is(tok::kw_operator)) { 1506 if (Current.Previous && Current.Previous->is(tok::coloncolon)) 1507 return false; 1508 Next = skipOperatorName(Next); 1509 } else { 1510 if (!Current.is(TT_StartOfName) || Current.NestingLevel != 0) 1511 return false; 1512 for (; Next; Next = Next->Next) { 1513 if (Next->is(TT_TemplateOpener)) { 1514 Next = Next->MatchingParen; 1515 } else if (Next->is(tok::coloncolon)) { 1516 Next = Next->Next; 1517 if (!Next) 1518 return false; 1519 if (Next->is(tok::kw_operator)) { 1520 Next = skipOperatorName(Next->Next); 1521 break; 1522 } 1523 if (!Next->is(tok::identifier)) 1524 return false; 1525 } else if (Next->is(tok::l_paren)) { 1526 break; 1527 } else { 1528 return false; 1529 } 1530 } 1531 } 1532 1533 if (!Next || !Next->is(tok::l_paren)) 1534 return false; 1535 if (Next->Next == Next->MatchingParen) 1536 return true; 1537 for (const FormatToken *Tok = Next->Next; Tok && Tok != Next->MatchingParen; 1538 Tok = Tok->Next) { 1539 if (Tok->is(tok::kw_const) || Tok->isSimpleTypeSpecifier() || 1540 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName)) 1541 return true; 1542 if (Tok->isOneOf(tok::l_brace, tok::string_literal, TT_ObjCMethodExpr) || 1543 Tok->Tok.isLiteral()) 1544 return false; 1545 } 1546 return false; 1547 } 1548 1549 void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) { 1550 for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(), 1551 E = Line.Children.end(); 1552 I != E; ++I) { 1553 calculateFormattingInformation(**I); 1554 } 1555 1556 Line.First->TotalLength = 1557 Line.First->IsMultiline ? Style.ColumnLimit : Line.First->ColumnWidth; 1558 if (!Line.First->Next) 1559 return; 1560 FormatToken *Current = Line.First->Next; 1561 bool InFunctionDecl = Line.MightBeFunctionDecl; 1562 while (Current) { 1563 if (isFunctionDeclarationName(*Current)) 1564 Current->Type = TT_FunctionDeclarationName; 1565 if (Current->is(TT_LineComment)) { 1566 if (Current->Previous->BlockKind == BK_BracedInit && 1567 Current->Previous->opensScope()) 1568 Current->SpacesRequiredBefore = Style.Cpp11BracedListStyle ? 0 : 1; 1569 else 1570 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments; 1571 1572 // If we find a trailing comment, iterate backwards to determine whether 1573 // it seems to relate to a specific parameter. If so, break before that 1574 // parameter to avoid changing the comment's meaning. E.g. don't move 'b' 1575 // to the previous line in: 1576 // SomeFunction(a, 1577 // b, // comment 1578 // c); 1579 if (!Current->HasUnescapedNewline) { 1580 for (FormatToken *Parameter = Current->Previous; Parameter; 1581 Parameter = Parameter->Previous) { 1582 if (Parameter->isOneOf(tok::comment, tok::r_brace)) 1583 break; 1584 if (Parameter->Previous && Parameter->Previous->is(tok::comma)) { 1585 if (!Parameter->Previous->is(TT_CtorInitializerComma) && 1586 Parameter->HasUnescapedNewline) 1587 Parameter->MustBreakBefore = true; 1588 break; 1589 } 1590 } 1591 } 1592 } else if (Current->SpacesRequiredBefore == 0 && 1593 spaceRequiredBefore(Line, *Current)) { 1594 Current->SpacesRequiredBefore = 1; 1595 } 1596 1597 Current->MustBreakBefore = 1598 Current->MustBreakBefore || mustBreakBefore(Line, *Current); 1599 1600 if ((Style.AlwaysBreakAfterDefinitionReturnType == FormatStyle::DRTBS_All || 1601 (Style.AlwaysBreakAfterDefinitionReturnType == 1602 FormatStyle::DRTBS_TopLevel && 1603 Line.Level == 0)) && 1604 InFunctionDecl && Current->is(TT_FunctionDeclarationName) && 1605 !Line.Last->isOneOf(tok::semi, tok::comment)) // Only for definitions. 1606 // FIXME: Line.Last points to other characters than tok::semi 1607 // and tok::lbrace. 1608 Current->MustBreakBefore = true; 1609 1610 Current->CanBreakBefore = 1611 Current->MustBreakBefore || canBreakBefore(Line, *Current); 1612 unsigned ChildSize = 0; 1613 if (Current->Previous->Children.size() == 1) { 1614 FormatToken &LastOfChild = *Current->Previous->Children[0]->Last; 1615 ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit 1616 : LastOfChild.TotalLength + 1; 1617 } 1618 const FormatToken *Prev = Current->Previous; 1619 if (Current->MustBreakBefore || Prev->Children.size() > 1 || 1620 (Prev->Children.size() == 1 && 1621 Prev->Children[0]->First->MustBreakBefore) || 1622 Current->IsMultiline) 1623 Current->TotalLength = Prev->TotalLength + Style.ColumnLimit; 1624 else 1625 Current->TotalLength = Prev->TotalLength + Current->ColumnWidth + 1626 ChildSize + Current->SpacesRequiredBefore; 1627 1628 if (Current->is(TT_CtorInitializerColon)) 1629 InFunctionDecl = false; 1630 1631 // FIXME: Only calculate this if CanBreakBefore is true once static 1632 // initializers etc. are sorted out. 1633 // FIXME: Move magic numbers to a better place. 1634 Current->SplitPenalty = 20 * Current->BindingStrength + 1635 splitPenalty(Line, *Current, InFunctionDecl); 1636 1637 Current = Current->Next; 1638 } 1639 1640 calculateUnbreakableTailLengths(Line); 1641 for (Current = Line.First; Current != nullptr; Current = Current->Next) { 1642 if (Current->Role) 1643 Current->Role->precomputeFormattingInfos(Current); 1644 } 1645 1646 DEBUG({ printDebugInfo(Line); }); 1647 } 1648 1649 void TokenAnnotator::calculateUnbreakableTailLengths(AnnotatedLine &Line) { 1650 unsigned UnbreakableTailLength = 0; 1651 FormatToken *Current = Line.Last; 1652 while (Current) { 1653 Current->UnbreakableTailLength = UnbreakableTailLength; 1654 if (Current->CanBreakBefore || 1655 Current->isOneOf(tok::comment, tok::string_literal)) { 1656 UnbreakableTailLength = 0; 1657 } else { 1658 UnbreakableTailLength += 1659 Current->ColumnWidth + Current->SpacesRequiredBefore; 1660 } 1661 Current = Current->Previous; 1662 } 1663 } 1664 1665 unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line, 1666 const FormatToken &Tok, 1667 bool InFunctionDecl) { 1668 const FormatToken &Left = *Tok.Previous; 1669 const FormatToken &Right = Tok; 1670 1671 if (Left.is(tok::semi)) 1672 return 0; 1673 1674 if (Style.Language == FormatStyle::LK_Java) { 1675 if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws)) 1676 return 1; 1677 if (Right.is(Keywords.kw_implements)) 1678 return 2; 1679 if (Left.is(tok::comma) && Left.NestingLevel == 0) 1680 return 3; 1681 } else if (Style.Language == FormatStyle::LK_JavaScript) { 1682 if (Right.is(Keywords.kw_function) && Left.isNot(tok::comma)) 1683 return 100; 1684 if (Left.is(TT_JsTypeColon)) 1685 return 100; 1686 } 1687 1688 if (Left.is(tok::comma) || (Right.is(tok::identifier) && Right.Next && 1689 Right.Next->is(TT_DictLiteral))) 1690 return 1; 1691 if (Right.is(tok::l_square)) { 1692 if (Style.Language == FormatStyle::LK_Proto) 1693 return 1; 1694 // Slightly prefer formatting local lambda definitions like functions. 1695 if (Right.is(TT_LambdaLSquare) && Left.is(tok::equal)) 1696 return 50; 1697 if (!Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare, 1698 TT_ArrayInitializerLSquare)) 1699 return 500; 1700 } 1701 1702 if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) || 1703 Right.is(tok::kw_operator)) { 1704 if (Line.startsWith(tok::kw_for) && Right.PartOfMultiVariableDeclStmt) 1705 return 3; 1706 if (Left.is(TT_StartOfName)) 1707 return 110; 1708 if (InFunctionDecl && Right.NestingLevel == 0) 1709 return Style.PenaltyReturnTypeOnItsOwnLine; 1710 return 200; 1711 } 1712 if (Right.is(TT_PointerOrReference)) 1713 return 190; 1714 if (Right.is(TT_LambdaArrow)) 1715 return 110; 1716 if (Left.is(tok::equal) && Right.is(tok::l_brace)) 1717 return 150; 1718 if (Left.is(TT_CastRParen)) 1719 return 100; 1720 if (Left.is(tok::coloncolon) || 1721 (Right.is(tok::period) && Style.Language == FormatStyle::LK_Proto)) 1722 return 500; 1723 if (Left.isOneOf(tok::kw_class, tok::kw_struct)) 1724 return 5000; 1725 1726 if (Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon)) 1727 return 2; 1728 1729 if (Right.isMemberAccess()) { 1730 if (Left.is(tok::r_paren) && Left.MatchingParen && 1731 Left.MatchingParen->ParameterCount > 0) 1732 return 20; // Should be smaller than breaking at a nested comma. 1733 return 150; 1734 } 1735 1736 if (Right.is(TT_TrailingAnnotation) && 1737 (!Right.Next || Right.Next->isNot(tok::l_paren))) { 1738 // Moving trailing annotations to the next line is fine for ObjC method 1739 // declarations. 1740 if (Line.startsWith(TT_ObjCMethodSpecifier)) 1741 return 10; 1742 // Generally, breaking before a trailing annotation is bad unless it is 1743 // function-like. It seems to be especially preferable to keep standard 1744 // annotations (i.e. "const", "final" and "override") on the same line. 1745 // Use a slightly higher penalty after ")" so that annotations like 1746 // "const override" are kept together. 1747 bool is_short_annotation = Right.TokenText.size() < 10; 1748 return (Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0); 1749 } 1750 1751 // In for-loops, prefer breaking at ',' and ';'. 1752 if (Line.startsWith(tok::kw_for) && Left.is(tok::equal)) 1753 return 4; 1754 1755 // In Objective-C method expressions, prefer breaking before "param:" over 1756 // breaking after it. 1757 if (Right.is(TT_SelectorName)) 1758 return 0; 1759 if (Left.is(tok::colon) && Left.is(TT_ObjCMethodExpr)) 1760 return Line.MightBeFunctionDecl ? 50 : 500; 1761 1762 if (Left.is(tok::l_paren) && InFunctionDecl && Style.AlignAfterOpenBracket) 1763 return 100; 1764 if (Left.is(tok::l_paren) && Left.Previous && 1765 Left.Previous->isOneOf(tok::kw_if, tok::kw_for)) 1766 return 1000; 1767 if (Left.is(tok::equal) && InFunctionDecl) 1768 return 110; 1769 if (Right.is(tok::r_brace)) 1770 return 1; 1771 if (Left.is(TT_TemplateOpener)) 1772 return 100; 1773 if (Left.opensScope()) { 1774 if (!Style.AlignAfterOpenBracket) 1775 return 0; 1776 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter 1777 : 19; 1778 } 1779 if (Left.is(TT_JavaAnnotation)) 1780 return 50; 1781 1782 if (Right.is(tok::lessless)) { 1783 if (Left.is(tok::string_literal) && 1784 (!Right.LastOperator || Right.OperatorIndex != 1)) { 1785 StringRef Content = Left.TokenText; 1786 if (Content.startswith("\"")) 1787 Content = Content.drop_front(1); 1788 if (Content.endswith("\"")) 1789 Content = Content.drop_back(1); 1790 Content = Content.trim(); 1791 if (Content.size() > 1 && 1792 (Content.back() == ':' || Content.back() == '=')) 1793 return 25; 1794 } 1795 return 1; // Breaking at a << is really cheap. 1796 } 1797 if (Left.is(TT_ConditionalExpr)) 1798 return prec::Conditional; 1799 prec::Level Level = Left.getPrecedence(); 1800 if (Level != prec::Unknown) 1801 return Level; 1802 Level = Right.getPrecedence(); 1803 if (Level != prec::Unknown) 1804 return Level; 1805 1806 return 3; 1807 } 1808 1809 bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, 1810 const FormatToken &Left, 1811 const FormatToken &Right) { 1812 if (Left.is(tok::kw_return) && Right.isNot(tok::semi)) 1813 return true; 1814 if (Style.ObjCSpaceAfterProperty && Line.Type == LT_ObjCProperty && 1815 Left.Tok.getObjCKeywordID() == tok::objc_property) 1816 return true; 1817 if (Right.is(tok::hashhash)) 1818 return Left.is(tok::hash); 1819 if (Left.isOneOf(tok::hashhash, tok::hash)) 1820 return Right.is(tok::hash); 1821 if (Left.is(tok::l_paren) && Right.is(tok::r_paren)) 1822 return Style.SpaceInEmptyParentheses; 1823 if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) 1824 return (Right.is(TT_CastRParen) || 1825 (Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen))) 1826 ? Style.SpacesInCStyleCastParentheses 1827 : Style.SpacesInParentheses; 1828 if (Right.isOneOf(tok::semi, tok::comma)) 1829 return false; 1830 if (Right.is(tok::less) && 1831 (Left.is(tok::kw_template) || 1832 (Line.Type == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList))) 1833 return true; 1834 if (Left.isOneOf(tok::exclaim, tok::tilde)) 1835 return false; 1836 if (Left.is(tok::at) && 1837 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant, 1838 tok::numeric_constant, tok::l_paren, tok::l_brace, 1839 tok::kw_true, tok::kw_false)) 1840 return false; 1841 if (Left.is(tok::coloncolon)) 1842 return false; 1843 if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less)) 1844 return false; 1845 if (Right.is(tok::ellipsis)) 1846 return Left.Tok.isLiteral(); 1847 if (Left.is(tok::l_square) && Right.is(tok::amp)) 1848 return false; 1849 if (Right.is(TT_PointerOrReference)) 1850 return (Left.is(tok::r_paren) && Left.MatchingParen && 1851 (Left.MatchingParen->is(TT_OverloadedOperatorLParen) || 1852 (Left.MatchingParen->Previous && 1853 Left.MatchingParen->Previous->is(TT_FunctionDeclarationName)))) || 1854 (Left.Tok.isLiteral() || 1855 (!Left.isOneOf(TT_PointerOrReference, tok::l_paren) && 1856 (Style.PointerAlignment != FormatStyle::PAS_Left || 1857 Line.IsMultiVariableDeclStmt))); 1858 if (Right.is(TT_FunctionTypeLParen) && Left.isNot(tok::l_paren) && 1859 (!Left.is(TT_PointerOrReference) || 1860 (Style.PointerAlignment != FormatStyle::PAS_Right && 1861 !Line.IsMultiVariableDeclStmt))) 1862 return true; 1863 if (Left.is(TT_PointerOrReference)) 1864 return Right.Tok.isLiteral() || 1865 Right.isOneOf(TT_BlockComment, Keywords.kw_final, 1866 Keywords.kw_override) || 1867 (Right.is(tok::l_brace) && Right.BlockKind == BK_Block) || 1868 (!Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare, 1869 tok::l_paren) && 1870 (Style.PointerAlignment != FormatStyle::PAS_Right && 1871 !Line.IsMultiVariableDeclStmt) && 1872 Left.Previous && 1873 !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon)); 1874 if (Right.is(tok::star) && Left.is(tok::l_paren)) 1875 return false; 1876 if (Left.is(tok::l_square)) 1877 return (Left.is(TT_ArrayInitializerLSquare) && 1878 Style.SpacesInContainerLiterals && Right.isNot(tok::r_square)) || 1879 (Left.is(TT_ArraySubscriptLSquare) && Style.SpacesInSquareBrackets && 1880 Right.isNot(tok::r_square)); 1881 if (Right.is(tok::r_square)) 1882 return Right.MatchingParen && 1883 ((Style.SpacesInContainerLiterals && 1884 Right.MatchingParen->is(TT_ArrayInitializerLSquare)) || 1885 (Style.SpacesInSquareBrackets && 1886 Right.MatchingParen->is(TT_ArraySubscriptLSquare))); 1887 if (Right.is(tok::l_square) && 1888 !Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare) && 1889 !Left.isOneOf(tok::numeric_constant, TT_DictLiteral)) 1890 return false; 1891 if (Left.is(tok::colon)) 1892 return !Left.is(TT_ObjCMethodExpr); 1893 if (Left.is(tok::l_brace) && Right.is(tok::r_brace)) 1894 return !Left.Children.empty(); // No spaces in "{}". 1895 if ((Left.is(tok::l_brace) && Left.BlockKind != BK_Block) || 1896 (Right.is(tok::r_brace) && Right.MatchingParen && 1897 Right.MatchingParen->BlockKind != BK_Block)) 1898 return !Style.Cpp11BracedListStyle; 1899 if (Left.is(TT_BlockComment)) 1900 return !Left.TokenText.endswith("=*/"); 1901 if (Right.is(tok::l_paren)) { 1902 if (Left.is(tok::r_paren) && Left.is(TT_AttributeParen)) 1903 return true; 1904 return Line.Type == LT_ObjCDecl || Left.is(tok::semi) || 1905 (Style.SpaceBeforeParens != FormatStyle::SBPO_Never && 1906 (Left.isOneOf(tok::kw_if, tok::pp_elif, tok::kw_for, tok::kw_while, 1907 tok::kw_switch, tok::kw_case, TT_ForEachMacro, 1908 TT_ObjCForIn) || 1909 (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch, 1910 tok::kw_new, tok::kw_delete) && 1911 (!Left.Previous || Left.Previous->isNot(tok::period))))) || 1912 (Style.SpaceBeforeParens == FormatStyle::SBPO_Always && 1913 (Left.is(tok::identifier) || Left.isFunctionLikeKeyword() || 1914 Left.is(tok::r_paren)) && 1915 Line.Type != LT_PreprocessorDirective); 1916 } 1917 if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword) 1918 return false; 1919 if (Right.is(TT_UnaryOperator)) 1920 return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) && 1921 (Left.isNot(tok::colon) || Left.isNot(TT_ObjCMethodExpr)); 1922 if ((Left.isOneOf(tok::identifier, tok::greater, tok::r_square, 1923 tok::r_paren) || 1924 Left.isSimpleTypeSpecifier()) && 1925 Right.is(tok::l_brace) && Right.getNextNonComment() && 1926 Right.BlockKind != BK_Block) 1927 return false; 1928 if (Left.is(tok::period) || Right.is(tok::period)) 1929 return false; 1930 if (Right.is(tok::hash) && Left.is(tok::identifier) && Left.TokenText == "L") 1931 return false; 1932 if (Left.is(TT_TemplateCloser) && Left.MatchingParen && 1933 Left.MatchingParen->Previous && 1934 Left.MatchingParen->Previous->is(tok::period)) 1935 // A.<B>DoSomething(); 1936 return false; 1937 if (Left.is(TT_TemplateCloser) && Right.is(tok::l_square)) 1938 return false; 1939 return true; 1940 } 1941 1942 bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, 1943 const FormatToken &Right) { 1944 const FormatToken &Left = *Right.Previous; 1945 if (Right.Tok.getIdentifierInfo() && Left.Tok.getIdentifierInfo()) 1946 return true; // Never ever merge two identifiers. 1947 if (Style.Language == FormatStyle::LK_Cpp) { 1948 if (Left.is(tok::kw_operator)) 1949 return Right.is(tok::coloncolon); 1950 } else if (Style.Language == FormatStyle::LK_Proto) { 1951 if (Right.is(tok::period) && 1952 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required, 1953 Keywords.kw_repeated)) 1954 return true; 1955 if (Right.is(tok::l_paren) && 1956 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)) 1957 return true; 1958 } else if (Style.Language == FormatStyle::LK_JavaScript) { 1959 if (Left.isOneOf(Keywords.kw_let, Keywords.kw_var, TT_JsFatArrow)) 1960 return true; 1961 if (Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion)) 1962 return false; 1963 if ((Left.is(tok::l_brace) || Right.is(tok::r_brace)) && 1964 Line.First->isOneOf(Keywords.kw_import, tok::kw_export)) 1965 return false; 1966 if (Left.is(tok::ellipsis)) 1967 return false; 1968 if (Left.is(TT_TemplateCloser) && 1969 !Right.isOneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square, 1970 Keywords.kw_implements, Keywords.kw_extends)) 1971 // Type assertions ('<type>expr') are not followed by whitespace. Other 1972 // locations that should have whitespace following are identified by the 1973 // above set of follower tokens. 1974 return false; 1975 } else if (Style.Language == FormatStyle::LK_Java) { 1976 if (Left.is(tok::r_square) && Right.is(tok::l_brace)) 1977 return true; 1978 if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren)) 1979 return Style.SpaceBeforeParens != FormatStyle::SBPO_Never; 1980 if ((Left.isOneOf(tok::kw_static, tok::kw_public, tok::kw_private, 1981 tok::kw_protected) || 1982 Left.isOneOf(Keywords.kw_final, Keywords.kw_abstract, 1983 Keywords.kw_native)) && 1984 Right.is(TT_TemplateOpener)) 1985 return true; 1986 } 1987 if (Left.is(TT_ImplicitStringLiteral)) 1988 return Right.WhitespaceRange.getBegin() != Right.WhitespaceRange.getEnd(); 1989 if (Line.Type == LT_ObjCMethodDecl) { 1990 if (Left.is(TT_ObjCMethodSpecifier)) 1991 return true; 1992 if (Left.is(tok::r_paren) && Right.is(tok::identifier)) 1993 // Don't space between ')' and <id> 1994 return false; 1995 } 1996 if (Line.Type == LT_ObjCProperty && 1997 (Right.is(tok::equal) || Left.is(tok::equal))) 1998 return false; 1999 2000 if (Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) || 2001 Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow)) 2002 return true; 2003 if (Left.is(tok::comma)) 2004 return true; 2005 if (Right.is(tok::comma)) 2006 return false; 2007 if (Right.isOneOf(TT_CtorInitializerColon, TT_ObjCBlockLParen)) 2008 return true; 2009 if (Right.is(TT_OverloadedOperatorLParen)) 2010 return Style.SpaceBeforeParens == FormatStyle::SBPO_Always; 2011 if (Right.is(tok::colon)) { 2012 if (Line.First->isOneOf(tok::kw_case, tok::kw_default) || 2013 !Right.getNextNonComment() || Right.getNextNonComment()->is(tok::semi)) 2014 return false; 2015 if (Right.is(TT_ObjCMethodExpr)) 2016 return false; 2017 if (Left.is(tok::question)) 2018 return false; 2019 if (Right.is(TT_InlineASMColon) && Left.is(tok::coloncolon)) 2020 return false; 2021 if (Right.is(TT_DictLiteral)) 2022 return Style.SpacesInContainerLiterals; 2023 return true; 2024 } 2025 if (Left.is(TT_UnaryOperator)) 2026 return Right.is(TT_BinaryOperator); 2027 2028 // If the next token is a binary operator or a selector name, we have 2029 // incorrectly classified the parenthesis as a cast. FIXME: Detect correctly. 2030 if (Left.is(TT_CastRParen)) 2031 return Style.SpaceAfterCStyleCast || 2032 Right.isOneOf(TT_BinaryOperator, TT_SelectorName); 2033 2034 if (Left.is(tok::greater) && Right.is(tok::greater)) 2035 return Right.is(TT_TemplateCloser) && Left.is(TT_TemplateCloser) && 2036 (Style.Standard != FormatStyle::LS_Cpp11 || Style.SpacesInAngles); 2037 if (Right.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) || 2038 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar)) 2039 return false; 2040 if (!Style.SpaceBeforeAssignmentOperators && 2041 Right.getPrecedence() == prec::Assignment) 2042 return false; 2043 if (Right.is(tok::coloncolon) && Left.isNot(tok::l_brace)) 2044 return (Left.is(TT_TemplateOpener) && 2045 Style.Standard == FormatStyle::LS_Cpp03) || 2046 !(Left.isOneOf(tok::identifier, tok::l_paren, tok::r_paren) || 2047 Left.isOneOf(TT_TemplateCloser, TT_TemplateOpener)); 2048 if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser))) 2049 return Style.SpacesInAngles; 2050 if ((Right.is(TT_BinaryOperator) && !Left.is(tok::l_paren)) || 2051 Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr)) 2052 return true; 2053 if (Left.is(TT_TemplateCloser) && Right.is(tok::l_paren) && 2054 Right.isNot(TT_FunctionTypeLParen)) 2055 return Style.SpaceBeforeParens == FormatStyle::SBPO_Always; 2056 if (Right.is(TT_TemplateOpener) && Left.is(tok::r_paren) && 2057 Left.MatchingParen && Left.MatchingParen->is(TT_OverloadedOperatorLParen)) 2058 return false; 2059 if (Right.is(tok::less) && Left.isNot(tok::l_paren) && 2060 Line.startsWith(tok::hash)) 2061 return true; 2062 if (Right.is(TT_TrailingUnaryOperator)) 2063 return false; 2064 if (Left.is(TT_RegexLiteral)) 2065 return false; 2066 return spaceRequiredBetween(Line, Left, Right); 2067 } 2068 2069 // Returns 'true' if 'Tok' is a brace we'd want to break before in Allman style. 2070 static bool isAllmanBrace(const FormatToken &Tok) { 2071 return Tok.is(tok::l_brace) && Tok.BlockKind == BK_Block && 2072 !Tok.isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral); 2073 } 2074 2075 bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, 2076 const FormatToken &Right) { 2077 const FormatToken &Left = *Right.Previous; 2078 if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0) 2079 return true; 2080 2081 if (Style.Language == FormatStyle::LK_JavaScript) { 2082 // FIXME: This might apply to other languages and token kinds. 2083 if (Right.is(tok::char_constant) && Left.is(tok::plus) && Left.Previous && 2084 Left.Previous->is(tok::char_constant)) 2085 return true; 2086 if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) && Line.Level == 0 && 2087 Left.Previous && Left.Previous->is(tok::equal) && 2088 Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export, 2089 tok::kw_const) && 2090 // kw_var/kw_let are pseudo-tokens that are tok::identifier, so match 2091 // above. 2092 !Line.First->isOneOf(Keywords.kw_var, Keywords.kw_let)) 2093 // Object literals on the top level of a file are treated as "enum-style". 2094 // Each key/value pair is put on a separate line, instead of bin-packing. 2095 return true; 2096 if (Left.is(tok::l_brace) && Line.Level == 0 && 2097 (Line.startsWith(tok::kw_enum) || 2098 Line.startsWith(tok::kw_export, tok::kw_enum))) 2099 // JavaScript top-level enum key/value pairs are put on separate lines 2100 // instead of bin-packing. 2101 return true; 2102 if (Right.is(tok::r_brace) && Left.is(tok::l_brace) && 2103 !Left.Children.empty()) 2104 // Support AllowShortFunctionsOnASingleLine for JavaScript. 2105 return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None || 2106 (Left.NestingLevel == 0 && Line.Level == 0 && 2107 Style.AllowShortFunctionsOnASingleLine == 2108 FormatStyle::SFS_Inline); 2109 } else if (Style.Language == FormatStyle::LK_Java) { 2110 if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next && 2111 Right.Next->is(tok::string_literal)) 2112 return true; 2113 } 2114 2115 // If the last token before a '}' is a comma or a trailing comment, the 2116 // intention is to insert a line break after it in order to make shuffling 2117 // around entries easier. 2118 const FormatToken *BeforeClosingBrace = nullptr; 2119 if (Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) && 2120 Left.BlockKind != BK_Block && Left.MatchingParen) 2121 BeforeClosingBrace = Left.MatchingParen->Previous; 2122 else if (Right.MatchingParen && 2123 Right.MatchingParen->isOneOf(tok::l_brace, 2124 TT_ArrayInitializerLSquare)) 2125 BeforeClosingBrace = &Left; 2126 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) || 2127 BeforeClosingBrace->isTrailingComment())) 2128 return true; 2129 2130 if (Right.is(tok::comment)) 2131 return Left.BlockKind != BK_BracedInit && 2132 Left.isNot(TT_CtorInitializerColon) && 2133 (Right.NewlinesBefore > 0 && Right.HasUnescapedNewline); 2134 if (Left.isTrailingComment()) 2135 return true; 2136 if (Left.isStringLiteral() && 2137 (Right.isStringLiteral() || Right.is(TT_ObjCStringLiteral))) 2138 return true; 2139 if (Right.Previous->IsUnterminatedLiteral) 2140 return true; 2141 if (Right.is(tok::lessless) && Right.Next && 2142 Right.Previous->is(tok::string_literal) && 2143 Right.Next->is(tok::string_literal)) 2144 return true; 2145 if (Right.Previous->ClosesTemplateDeclaration && 2146 Right.Previous->MatchingParen && 2147 Right.Previous->MatchingParen->NestingLevel == 0 && 2148 Style.AlwaysBreakTemplateDeclarations) 2149 return true; 2150 if ((Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) && 2151 Style.BreakConstructorInitializersBeforeComma && 2152 !Style.ConstructorInitializerAllOnOneLineOrOnePerLine) 2153 return true; 2154 if (Right.is(tok::string_literal) && Right.TokenText.startswith("R\"")) 2155 // Raw string literals are special wrt. line breaks. The author has made a 2156 // deliberate choice and might have aligned the contents of the string 2157 // literal accordingly. Thus, we try keep existing line breaks. 2158 return Right.NewlinesBefore > 0; 2159 if (Right.Previous->is(tok::l_brace) && Right.NestingLevel == 1 && 2160 Style.Language == FormatStyle::LK_Proto) 2161 // Don't put enums onto single lines in protocol buffers. 2162 return true; 2163 if (Right.is(TT_InlineASMBrace)) 2164 return Right.HasUnescapedNewline; 2165 if (isAllmanBrace(Left) || isAllmanBrace(Right)) 2166 return (Line.startsWith(tok::kw_enum) && Style.BraceWrapping.AfterEnum) || 2167 (Line.startsWith(tok::kw_class) && Style.BraceWrapping.AfterClass) || 2168 (Line.startsWith(tok::kw_struct) && Style.BraceWrapping.AfterStruct); 2169 if (Style.Language == FormatStyle::LK_Proto && Left.isNot(tok::l_brace) && 2170 Right.is(TT_SelectorName)) 2171 return true; 2172 if (Left.is(TT_ObjCBlockLBrace) && !Style.AllowShortBlocksOnASingleLine) 2173 return true; 2174 2175 if ((Style.Language == FormatStyle::LK_Java || 2176 Style.Language == FormatStyle::LK_JavaScript) && 2177 Left.is(TT_LeadingJavaAnnotation) && 2178 Right.isNot(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) && 2179 (Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) 2180 return true; 2181 2182 return false; 2183 } 2184 2185 bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, 2186 const FormatToken &Right) { 2187 const FormatToken &Left = *Right.Previous; 2188 2189 // Language-specific stuff. 2190 if (Style.Language == FormatStyle::LK_Java) { 2191 if (Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends, 2192 Keywords.kw_implements)) 2193 return false; 2194 if (Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends, 2195 Keywords.kw_implements)) 2196 return true; 2197 } else if (Style.Language == FormatStyle::LK_JavaScript) { 2198 if (Left.is(TT_JsFatArrow) && Right.is(tok::l_brace)) 2199 return false; 2200 if (Left.is(TT_JsTypeColon)) 2201 return true; 2202 } 2203 2204 if (Left.is(tok::at)) 2205 return false; 2206 if (Left.Tok.getObjCKeywordID() == tok::objc_interface) 2207 return false; 2208 if (Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation)) 2209 return !Right.is(tok::l_paren); 2210 if (Right.is(TT_PointerOrReference)) 2211 return Line.IsMultiVariableDeclStmt || 2212 (Style.PointerAlignment == FormatStyle::PAS_Right && 2213 (!Right.Next || Right.Next->isNot(TT_FunctionDeclarationName))); 2214 if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) || 2215 Right.is(tok::kw_operator)) 2216 return true; 2217 if (Left.is(TT_PointerOrReference)) 2218 return false; 2219 if (Right.isTrailingComment()) 2220 // We rely on MustBreakBefore being set correctly here as we should not 2221 // change the "binding" behavior of a comment. 2222 // The first comment in a braced lists is always interpreted as belonging to 2223 // the first list element. Otherwise, it should be placed outside of the 2224 // list. 2225 return Left.BlockKind == BK_BracedInit; 2226 if (Left.is(tok::question) && Right.is(tok::colon)) 2227 return false; 2228 if (Right.is(TT_ConditionalExpr) || Right.is(tok::question)) 2229 return Style.BreakBeforeTernaryOperators; 2230 if (Left.is(TT_ConditionalExpr) || Left.is(tok::question)) 2231 return !Style.BreakBeforeTernaryOperators; 2232 if (Right.is(TT_InheritanceColon)) 2233 return true; 2234 if (Right.is(tok::colon) && 2235 !Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon)) 2236 return false; 2237 if (Left.is(tok::colon) && (Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr))) 2238 return true; 2239 if (Right.is(TT_SelectorName) || (Right.is(tok::identifier) && Right.Next && 2240 Right.Next->is(TT_ObjCMethodExpr))) 2241 return Left.isNot(tok::period); // FIXME: Properly parse ObjC calls. 2242 if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty) 2243 return true; 2244 if (Left.ClosesTemplateDeclaration || Left.is(TT_FunctionAnnotationRParen)) 2245 return true; 2246 if (Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen, 2247 TT_OverloadedOperator)) 2248 return false; 2249 if (Left.is(TT_RangeBasedForLoopColon)) 2250 return true; 2251 if (Right.is(TT_RangeBasedForLoopColon)) 2252 return false; 2253 if (Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator) || 2254 Left.is(tok::kw_operator)) 2255 return false; 2256 if (Left.is(tok::equal) && !Right.isOneOf(tok::kw_default, tok::kw_delete) && 2257 Line.Type == LT_VirtualFunctionDecl && Left.NestingLevel == 0) 2258 return false; 2259 if (Left.is(tok::l_paren) && Left.is(TT_AttributeParen)) 2260 return false; 2261 if (Left.is(tok::l_paren) && Left.Previous && 2262 (Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen))) 2263 return false; 2264 if (Right.is(TT_ImplicitStringLiteral)) 2265 return false; 2266 2267 if (Right.is(tok::r_paren) || Right.is(TT_TemplateCloser)) 2268 return false; 2269 if (Right.is(tok::r_square) && Right.MatchingParen && 2270 Right.MatchingParen->is(TT_LambdaLSquare)) 2271 return false; 2272 2273 // We only break before r_brace if there was a corresponding break before 2274 // the l_brace, which is tracked by BreakBeforeClosingBrace. 2275 if (Right.is(tok::r_brace)) 2276 return Right.MatchingParen && Right.MatchingParen->BlockKind == BK_Block; 2277 2278 // Allow breaking after a trailing annotation, e.g. after a method 2279 // declaration. 2280 if (Left.is(TT_TrailingAnnotation)) 2281 return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren, 2282 tok::less, tok::coloncolon); 2283 2284 if (Right.is(tok::kw___attribute)) 2285 return true; 2286 2287 if (Left.is(tok::identifier) && Right.is(tok::string_literal)) 2288 return true; 2289 2290 if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral)) 2291 return true; 2292 2293 if (Left.is(TT_CtorInitializerComma) && 2294 Style.BreakConstructorInitializersBeforeComma) 2295 return false; 2296 if (Right.is(TT_CtorInitializerComma) && 2297 Style.BreakConstructorInitializersBeforeComma) 2298 return true; 2299 if ((Left.is(tok::greater) && Right.is(tok::greater)) || 2300 (Left.is(tok::less) && Right.is(tok::less))) 2301 return false; 2302 if (Right.is(TT_BinaryOperator) && 2303 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None && 2304 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All || 2305 Right.getPrecedence() != prec::Assignment)) 2306 return true; 2307 if (Left.is(TT_ArrayInitializerLSquare)) 2308 return true; 2309 if (Right.is(tok::kw_typename) && Left.isNot(tok::kw_const)) 2310 return true; 2311 if ((Left.isBinaryOperator() || Left.is(TT_BinaryOperator)) && 2312 !Left.isOneOf(tok::arrowstar, tok::lessless) && 2313 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All && 2314 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None || 2315 Left.getPrecedence() == prec::Assignment)) 2316 return true; 2317 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace, 2318 tok::kw_class, tok::kw_struct) || 2319 Right.isMemberAccess() || 2320 Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless, 2321 tok::colon, tok::l_square, tok::at) || 2322 (Left.is(tok::r_paren) && 2323 Right.isOneOf(tok::identifier, tok::kw_const)) || 2324 (Left.is(tok::l_paren) && !Right.is(tok::r_paren)); 2325 } 2326 2327 void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) { 2328 llvm::errs() << "AnnotatedTokens:\n"; 2329 const FormatToken *Tok = Line.First; 2330 while (Tok) { 2331 llvm::errs() << " M=" << Tok->MustBreakBefore 2332 << " C=" << Tok->CanBreakBefore 2333 << " T=" << getTokenTypeName(Tok->Type) 2334 << " S=" << Tok->SpacesRequiredBefore 2335 << " B=" << Tok->BlockParameterCount 2336 << " P=" << Tok->SplitPenalty << " Name=" << Tok->Tok.getName() 2337 << " L=" << Tok->TotalLength << " PPK=" << Tok->PackingKind 2338 << " FakeLParens="; 2339 for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i) 2340 llvm::errs() << Tok->FakeLParens[i] << "/"; 2341 llvm::errs() << " FakeRParens=" << Tok->FakeRParens << "\n"; 2342 if (!Tok->Next) 2343 assert(Tok == Line.Last); 2344 Tok = Tok->Next; 2345 } 2346 llvm::errs() << "----\n"; 2347 } 2348 2349 } // namespace format 2350 } // namespace clang 2351