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