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