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