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