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