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