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