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