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