1 //===--- TokenAnnotator.cpp - Format C++ code -----------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// 10 /// \file 11 /// \brief This file implements a token annotator, i.e. creates 12 /// \c AnnotatedTokens out of \c FormatTokens with required extra information. 13 /// 14 //===----------------------------------------------------------------------===// 15 16 #include "TokenAnnotator.h" 17 #include "clang/Basic/SourceManager.h" 18 #include "llvm/Support/Debug.h" 19 20 #define DEBUG_TYPE "format-token-annotator" 21 22 namespace clang { 23 namespace format { 24 25 namespace { 26 27 /// \brief A parser that gathers additional information about tokens. 28 /// 29 /// The \c TokenAnnotator tries to match parenthesis and square brakets and 30 /// store a parenthesis levels. It also tries to resolve matching "<" and ">" 31 /// into template parameter lists. 32 class AnnotatingParser { 33 public: 34 AnnotatingParser(const FormatStyle &Style, AnnotatedLine &Line, 35 IdentifierInfo &Ident_in) 36 : Style(Style), Line(Line), CurrentToken(Line.First), 37 KeywordVirtualFound(false), AutoFound(false), Ident_in(Ident_in) { 38 Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false)); 39 resetTokenMetadata(CurrentToken); 40 } 41 42 private: 43 bool parseAngle() { 44 if (CurrentToken == NULL) 45 return false; 46 ScopedContextCreator ContextCreator(*this, tok::less, 10); 47 FormatToken *Left = CurrentToken->Previous; 48 Contexts.back().IsExpression = false; 49 // If there's a template keyword before the opening angle bracket, this is a 50 // template parameter, not an argument. 51 Contexts.back().InTemplateArgument = 52 Left->Previous != NULL && Left->Previous->Tok.isNot(tok::kw_template); 53 54 while (CurrentToken != NULL) { 55 if (CurrentToken->is(tok::greater)) { 56 Left->MatchingParen = CurrentToken; 57 CurrentToken->MatchingParen = Left; 58 CurrentToken->Type = TT_TemplateCloser; 59 next(); 60 return true; 61 } 62 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace, 63 tok::question, tok::colon)) 64 return false; 65 // If a && or || is found and interpreted as a binary operator, this set 66 // of angles is likely part of something like "a < b && c > d". If the 67 // angles are inside an expression, the ||/&& might also be a binary 68 // operator that was misinterpreted because we are parsing template 69 // parameters. 70 // FIXME: This is getting out of hand, write a decent parser. 71 if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) && 72 ((CurrentToken->Previous->Type == TT_BinaryOperator && 73 // Toplevel bool expressions do not make lots of sense; 74 // If we're on the top level, it contains only the base context and 75 // the context for the current opening angle bracket. 76 Contexts.size() > 2) || 77 Contexts[Contexts.size() - 2].IsExpression) && 78 Line.First->isNot(tok::kw_template)) 79 return false; 80 updateParameterCount(Left, CurrentToken); 81 if (!consumeToken()) 82 return false; 83 } 84 return false; 85 } 86 87 bool parseParens(bool LookForDecls = false) { 88 if (CurrentToken == NULL) 89 return false; 90 ScopedContextCreator ContextCreator(*this, tok::l_paren, 1); 91 92 // FIXME: This is a bit of a hack. Do better. 93 Contexts.back().ColonIsForRangeExpr = 94 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr; 95 96 bool StartsObjCMethodExpr = false; 97 FormatToken *Left = CurrentToken->Previous; 98 if (CurrentToken->is(tok::caret)) { 99 // (^ can start a block type. 100 Left->Type = TT_ObjCBlockLParen; 101 } else if (FormatToken *MaybeSel = Left->Previous) { 102 // @selector( starts a selector. 103 if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && MaybeSel->Previous && 104 MaybeSel->Previous->is(tok::at)) { 105 StartsObjCMethodExpr = true; 106 } 107 } 108 109 if (Left->Previous && 110 (Left->Previous->isOneOf(tok::kw_static_assert, tok::kw_if, 111 tok::kw_while, tok::l_paren, tok::comma) || 112 Left->Previous->Type == TT_BinaryOperator)) { 113 // static_assert, if and while usually contain expressions. 114 Contexts.back().IsExpression = true; 115 } else if (Line.InPPDirective && 116 (!Left->Previous || 117 (Left->Previous->isNot(tok::identifier) && 118 Left->Previous->Type != TT_OverloadedOperator))) { 119 Contexts.back().IsExpression = true; 120 } else if (Left->Previous && Left->Previous->is(tok::r_square) && 121 Left->Previous->MatchingParen && 122 Left->Previous->MatchingParen->Type == TT_LambdaLSquare) { 123 // This is a parameter list of a lambda expression. 124 Contexts.back().IsExpression = false; 125 } else if (Contexts[Contexts.size() - 2].CaretFound) { 126 // This is the parameter list of an ObjC block. 127 Contexts.back().IsExpression = false; 128 } else if (Left->Previous && Left->Previous->is(tok::kw___attribute)) { 129 Left->Type = TT_AttributeParen; 130 } else if (Left->Previous && Left->Previous->IsForEachMacro) { 131 // The first argument to a foreach macro is a declaration. 132 Contexts.back().IsForEachMacro = true; 133 Contexts.back().IsExpression = false; 134 } 135 136 if (StartsObjCMethodExpr) { 137 Contexts.back().ColonIsObjCMethodExpr = true; 138 Left->Type = TT_ObjCMethodExpr; 139 } 140 141 bool MightBeFunctionType = CurrentToken->is(tok::star); 142 bool HasMultipleLines = false; 143 bool HasMultipleParametersOnALine = false; 144 while (CurrentToken != NULL) { 145 // LookForDecls is set when "if (" has been seen. Check for 146 // 'identifier' '*' 'identifier' followed by not '=' -- this 147 // '*' has to be a binary operator but determineStarAmpUsage() will 148 // categorize it as an unary operator, so set the right type here. 149 if (LookForDecls && CurrentToken->Next) { 150 FormatToken *Prev = CurrentToken->getPreviousNonComment(); 151 if (Prev) { 152 FormatToken *PrevPrev = Prev->getPreviousNonComment(); 153 FormatToken *Next = CurrentToken->Next; 154 if (PrevPrev && PrevPrev->is(tok::identifier) && 155 Prev->isOneOf(tok::star, tok::amp, tok::ampamp) && 156 CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) { 157 Prev->Type = TT_BinaryOperator; 158 LookForDecls = false; 159 } 160 } 161 } 162 163 if (CurrentToken->Previous->Type == TT_PointerOrReference && 164 CurrentToken->Previous->Previous->isOneOf(tok::l_paren, 165 tok::coloncolon)) 166 MightBeFunctionType = true; 167 if (CurrentToken->is(tok::r_paren)) { 168 if (MightBeFunctionType && CurrentToken->Next && 169 (CurrentToken->Next->is(tok::l_paren) || 170 (CurrentToken->Next->is(tok::l_square) && 171 !Contexts.back().IsExpression))) 172 Left->Type = TT_FunctionTypeLParen; 173 Left->MatchingParen = CurrentToken; 174 CurrentToken->MatchingParen = Left; 175 176 if (StartsObjCMethodExpr) { 177 CurrentToken->Type = TT_ObjCMethodExpr; 178 if (Contexts.back().FirstObjCSelectorName != NULL) { 179 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 180 Contexts.back().LongestObjCSelectorName; 181 } 182 } 183 184 if (Left->Type == TT_AttributeParen) 185 CurrentToken->Type = TT_AttributeParen; 186 187 if (!HasMultipleLines) 188 Left->PackingKind = PPK_Inconclusive; 189 else if (HasMultipleParametersOnALine) 190 Left->PackingKind = PPK_BinPacked; 191 else 192 Left->PackingKind = PPK_OnePerLine; 193 194 next(); 195 return true; 196 } 197 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace)) 198 return false; 199 else if (CurrentToken->is(tok::l_brace)) 200 Left->Type = TT_Unknown; // Not TT_ObjCBlockLParen 201 updateParameterCount(Left, CurrentToken); 202 if (CurrentToken->is(tok::comma) && CurrentToken->Next && 203 !CurrentToken->Next->HasUnescapedNewline && 204 !CurrentToken->Next->isTrailingComment()) 205 HasMultipleParametersOnALine = true; 206 if (CurrentToken->isOneOf(tok::kw_const, tok::kw_auto) || 207 CurrentToken->isSimpleTypeSpecifier()) 208 Contexts.back().IsExpression = false; 209 if (!consumeToken()) 210 return false; 211 if (CurrentToken && CurrentToken->HasUnescapedNewline) 212 HasMultipleLines = true; 213 } 214 return false; 215 } 216 217 bool parseSquare() { 218 if (!CurrentToken) 219 return false; 220 221 // A '[' could be an index subscript (after an identifier or after 222 // ')' or ']'), it could be the start of an Objective-C method 223 // expression, or it could the the start of an Objective-C array literal. 224 FormatToken *Left = CurrentToken->Previous; 225 FormatToken *Parent = Left->getPreviousNonComment(); 226 bool StartsObjCMethodExpr = 227 Contexts.back().CanBeExpression && Left->Type != TT_LambdaLSquare && 228 (!Parent || Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren, 229 tok::kw_return, tok::kw_throw) || 230 Parent->isUnaryOperator() || Parent->Type == TT_ObjCForIn || 231 Parent->Type == TT_CastRParen || 232 getBinOpPrecedence(Parent->Tok.getKind(), true, true) > prec::Unknown); 233 ScopedContextCreator ContextCreator(*this, tok::l_square, 10); 234 Contexts.back().IsExpression = true; 235 bool ColonFound = false; 236 237 if (StartsObjCMethodExpr) { 238 Contexts.back().ColonIsObjCMethodExpr = true; 239 Left->Type = TT_ObjCMethodExpr; 240 } else if (Parent && Parent->is(tok::at)) { 241 Left->Type = TT_ArrayInitializerLSquare; 242 } else if (Left->Type == TT_Unknown) { 243 Left->Type = TT_ArraySubscriptLSquare; 244 } 245 246 while (CurrentToken != NULL) { 247 if (CurrentToken->is(tok::r_square)) { 248 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_paren) && 249 Left->Type == TT_ObjCMethodExpr) { 250 // An ObjC method call is rarely followed by an open parenthesis. 251 // FIXME: Do we incorrectly label ":" with this? 252 StartsObjCMethodExpr = false; 253 Left->Type = TT_Unknown; 254 } 255 if (StartsObjCMethodExpr && CurrentToken->Previous != Left) { 256 CurrentToken->Type = TT_ObjCMethodExpr; 257 // determineStarAmpUsage() thinks that '*' '[' is allocating an 258 // array of pointers, but if '[' starts a selector then '*' is a 259 // binary operator. 260 if (Parent != NULL && Parent->Type == TT_PointerOrReference) 261 Parent->Type = TT_BinaryOperator; 262 } 263 Left->MatchingParen = CurrentToken; 264 CurrentToken->MatchingParen = Left; 265 if (Contexts.back().FirstObjCSelectorName != NULL) { 266 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 267 Contexts.back().LongestObjCSelectorName; 268 if (Contexts.back().NumBlockParameters > 1) 269 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0; 270 } 271 next(); 272 return true; 273 } 274 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace)) 275 return false; 276 if (CurrentToken->is(tok::colon)) 277 ColonFound = true; 278 if (CurrentToken->is(tok::comma) && 279 Style.Language != FormatStyle::LK_Proto && 280 (Left->Type == TT_ArraySubscriptLSquare || 281 (Left->Type == TT_ObjCMethodExpr && !ColonFound))) 282 Left->Type = TT_ArrayInitializerLSquare; 283 updateParameterCount(Left, CurrentToken); 284 if (!consumeToken()) 285 return false; 286 } 287 return false; 288 } 289 290 bool parseBrace() { 291 if (CurrentToken != NULL) { 292 FormatToken *Left = CurrentToken->Previous; 293 294 if (Contexts.back().CaretFound) 295 Left->Type = TT_ObjCBlockLBrace; 296 Contexts.back().CaretFound = false; 297 298 ScopedContextCreator ContextCreator(*this, tok::l_brace, 1); 299 Contexts.back().ColonIsDictLiteral = true; 300 301 while (CurrentToken != NULL) { 302 if (CurrentToken->is(tok::r_brace)) { 303 Left->MatchingParen = CurrentToken; 304 CurrentToken->MatchingParen = Left; 305 next(); 306 return true; 307 } 308 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square)) 309 return false; 310 updateParameterCount(Left, CurrentToken); 311 if (CurrentToken->is(tok::colon) && 312 Style.Language != FormatStyle::LK_Proto) 313 Left->Type = TT_DictLiteral; 314 if (!consumeToken()) 315 return false; 316 } 317 } 318 // No closing "}" found, this probably starts a definition. 319 Line.StartsDefinition = true; 320 return true; 321 } 322 323 void updateParameterCount(FormatToken *Left, FormatToken *Current) { 324 if (Current->is(tok::comma)) { 325 ++Left->ParameterCount; 326 if (!Left->Role) 327 Left->Role.reset(new CommaSeparatedList(Style)); 328 Left->Role->CommaFound(Current); 329 } else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) { 330 Left->ParameterCount = 1; 331 } 332 } 333 334 bool parseConditional() { 335 while (CurrentToken != NULL) { 336 if (CurrentToken->is(tok::colon)) { 337 CurrentToken->Type = TT_ConditionalExpr; 338 next(); 339 return true; 340 } 341 if (!consumeToken()) 342 return false; 343 } 344 return false; 345 } 346 347 bool parseTemplateDeclaration() { 348 if (CurrentToken != NULL && CurrentToken->is(tok::less)) { 349 CurrentToken->Type = TT_TemplateOpener; 350 next(); 351 if (!parseAngle()) 352 return false; 353 if (CurrentToken != NULL) 354 CurrentToken->Previous->ClosesTemplateDeclaration = true; 355 return true; 356 } 357 return false; 358 } 359 360 bool consumeToken() { 361 FormatToken *Tok = CurrentToken; 362 next(); 363 switch (Tok->Tok.getKind()) { 364 case tok::plus: 365 case tok::minus: 366 if (Tok->Previous == NULL && Line.MustBeDeclaration) 367 Tok->Type = TT_ObjCMethodSpecifier; 368 break; 369 case tok::colon: 370 if (Tok->Previous == NULL) 371 return false; 372 // Colons from ?: are handled in parseConditional(). 373 if (Tok->Previous->is(tok::r_paren) && Contexts.size() == 1) { 374 Tok->Type = TT_CtorInitializerColon; 375 } else if (Contexts.back().ColonIsDictLiteral) { 376 Tok->Type = TT_DictLiteral; 377 } else if (Contexts.back().ColonIsObjCMethodExpr || 378 Line.First->Type == TT_ObjCMethodSpecifier) { 379 Tok->Type = TT_ObjCMethodExpr; 380 Tok->Previous->Type = TT_ObjCSelectorName; 381 if (Tok->Previous->ColumnWidth > 382 Contexts.back().LongestObjCSelectorName) { 383 Contexts.back().LongestObjCSelectorName = Tok->Previous->ColumnWidth; 384 } 385 if (Contexts.back().FirstObjCSelectorName == NULL) 386 Contexts.back().FirstObjCSelectorName = Tok->Previous; 387 } else if (Contexts.back().ColonIsForRangeExpr) { 388 Tok->Type = TT_RangeBasedForLoopColon; 389 } else if (CurrentToken != NULL && 390 CurrentToken->is(tok::numeric_constant)) { 391 Tok->Type = TT_BitFieldColon; 392 } else if (Contexts.size() == 1 && Line.First->isNot(tok::kw_enum)) { 393 Tok->Type = TT_InheritanceColon; 394 } else if (Contexts.back().ContextKind == tok::l_paren) { 395 Tok->Type = TT_InlineASMColon; 396 } 397 break; 398 case tok::kw_if: 399 case tok::kw_while: 400 if (CurrentToken != NULL && CurrentToken->is(tok::l_paren)) { 401 next(); 402 if (!parseParens(/*LookForDecls=*/true)) 403 return false; 404 } 405 break; 406 case tok::kw_for: 407 Contexts.back().ColonIsForRangeExpr = true; 408 next(); 409 if (!parseParens()) 410 return false; 411 break; 412 case tok::l_paren: 413 if (!parseParens()) 414 return false; 415 if (Line.MustBeDeclaration && Contexts.size() == 1 && 416 !Contexts.back().IsExpression && Line.First->Type != TT_ObjCProperty) 417 Line.MightBeFunctionDecl = true; 418 break; 419 case tok::l_square: 420 if (!parseSquare()) 421 return false; 422 break; 423 case tok::l_brace: 424 if (!parseBrace()) 425 return false; 426 break; 427 case tok::less: 428 if (Tok->Previous && !Tok->Previous->Tok.isLiteral() && parseAngle()) 429 Tok->Type = TT_TemplateOpener; 430 else { 431 Tok->Type = TT_BinaryOperator; 432 CurrentToken = Tok; 433 next(); 434 } 435 break; 436 case tok::r_paren: 437 case tok::r_square: 438 return false; 439 case tok::r_brace: 440 // Lines can start with '}'. 441 if (Tok->Previous != NULL) 442 return false; 443 break; 444 case tok::greater: 445 Tok->Type = TT_BinaryOperator; 446 break; 447 case tok::kw_operator: 448 while (CurrentToken && 449 !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) { 450 if (CurrentToken->isOneOf(tok::star, tok::amp)) 451 CurrentToken->Type = TT_PointerOrReference; 452 consumeToken(); 453 if (CurrentToken && CurrentToken->Previous->Type == TT_BinaryOperator) 454 CurrentToken->Previous->Type = TT_OverloadedOperator; 455 } 456 if (CurrentToken) { 457 CurrentToken->Type = TT_OverloadedOperatorLParen; 458 if (CurrentToken->Previous->Type == TT_BinaryOperator) 459 CurrentToken->Previous->Type = TT_OverloadedOperator; 460 } 461 break; 462 case tok::question: 463 parseConditional(); 464 break; 465 case tok::kw_template: 466 parseTemplateDeclaration(); 467 break; 468 case tok::identifier: 469 if (Line.First->is(tok::kw_for) && 470 Tok->Tok.getIdentifierInfo() == &Ident_in) 471 Tok->Type = TT_ObjCForIn; 472 break; 473 case tok::comma: 474 if (Contexts.back().FirstStartOfName) 475 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true; 476 if (Contexts.back().InCtorInitializer) 477 Tok->Type = TT_CtorInitializerComma; 478 if (Contexts.back().IsForEachMacro) 479 Contexts.back().IsExpression = true; 480 break; 481 default: 482 break; 483 } 484 return true; 485 } 486 487 void parseIncludeDirective() { 488 next(); 489 if (CurrentToken != NULL && CurrentToken->is(tok::less)) { 490 next(); 491 while (CurrentToken != NULL) { 492 if (CurrentToken->isNot(tok::comment) || CurrentToken->Next) 493 CurrentToken->Type = TT_ImplicitStringLiteral; 494 next(); 495 } 496 } else { 497 while (CurrentToken != NULL) { 498 if (CurrentToken->is(tok::string_literal)) 499 // Mark these string literals as "implicit" literals, too, so that 500 // they are not split or line-wrapped. 501 CurrentToken->Type = TT_ImplicitStringLiteral; 502 next(); 503 } 504 } 505 } 506 507 void parseWarningOrError() { 508 next(); 509 // We still want to format the whitespace left of the first token of the 510 // warning or error. 511 next(); 512 while (CurrentToken != NULL) { 513 CurrentToken->Type = TT_ImplicitStringLiteral; 514 next(); 515 } 516 } 517 518 void parsePragma() { 519 next(); // Consume "pragma". 520 if (CurrentToken && CurrentToken->TokenText == "mark") { 521 next(); // Consume "mark". 522 next(); // Consume first token (so we fix leading whitespace). 523 while (CurrentToken != NULL) { 524 CurrentToken->Type = TT_ImplicitStringLiteral; 525 next(); 526 } 527 } 528 } 529 530 void parsePreprocessorDirective() { 531 next(); 532 if (CurrentToken == NULL) 533 return; 534 if (CurrentToken->Tok.is(tok::numeric_constant)) { 535 CurrentToken->SpacesRequiredBefore = 1; 536 return; 537 } 538 // Hashes in the middle of a line can lead to any strange token 539 // sequence. 540 if (CurrentToken->Tok.getIdentifierInfo() == NULL) 541 return; 542 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) { 543 case tok::pp_include: 544 case tok::pp_import: 545 parseIncludeDirective(); 546 break; 547 case tok::pp_error: 548 case tok::pp_warning: 549 parseWarningOrError(); 550 break; 551 case tok::pp_pragma: 552 parsePragma(); 553 break; 554 case tok::pp_if: 555 case tok::pp_elif: 556 Contexts.back().IsExpression = true; 557 parseLine(); 558 break; 559 default: 560 break; 561 } 562 while (CurrentToken != NULL) 563 next(); 564 } 565 566 public: 567 LineType parseLine() { 568 if (CurrentToken->is(tok::hash)) { 569 parsePreprocessorDirective(); 570 return LT_PreprocessorDirective; 571 } 572 573 // Directly allow to 'import <string-literal>' to support protocol buffer 574 // definitions (code.google.com/p/protobuf) or missing "#" (either way we 575 // should not break the line). 576 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo(); 577 if (Info && Info->getPPKeywordID() == tok::pp_import && 578 CurrentToken->Next && CurrentToken->Next->is(tok::string_literal)) 579 parseIncludeDirective(); 580 581 while (CurrentToken != NULL) { 582 if (CurrentToken->is(tok::kw_virtual)) 583 KeywordVirtualFound = true; 584 if (!consumeToken()) 585 return LT_Invalid; 586 } 587 if (KeywordVirtualFound) 588 return LT_VirtualFunctionDecl; 589 590 if (Line.First->Type == TT_ObjCMethodSpecifier) { 591 if (Contexts.back().FirstObjCSelectorName != NULL) 592 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 593 Contexts.back().LongestObjCSelectorName; 594 return LT_ObjCMethodDecl; 595 } 596 597 return LT_Other; 598 } 599 600 private: 601 void resetTokenMetadata(FormatToken *Token) { 602 if (Token == nullptr) return; 603 604 // Reset token type in case we have already looked at it and then 605 // recovered from an error (e.g. failure to find the matching >). 606 if (CurrentToken->Type != TT_LambdaLSquare && 607 CurrentToken->Type != TT_FunctionLBrace && 608 CurrentToken->Type != TT_ImplicitStringLiteral && 609 CurrentToken->Type != TT_TrailingReturnArrow) 610 CurrentToken->Type = TT_Unknown; 611 if (CurrentToken->Role) 612 CurrentToken->Role.reset(NULL); 613 CurrentToken->FakeLParens.clear(); 614 CurrentToken->FakeRParens = 0; 615 } 616 617 void next() { 618 if (CurrentToken != NULL) { 619 determineTokenType(*CurrentToken); 620 CurrentToken->BindingStrength = Contexts.back().BindingStrength; 621 CurrentToken->NestingLevel = Contexts.size() - 1; 622 } 623 624 if (CurrentToken != NULL) 625 CurrentToken = CurrentToken->Next; 626 627 resetTokenMetadata(CurrentToken); 628 } 629 630 /// \brief A struct to hold information valid in a specific context, e.g. 631 /// a pair of parenthesis. 632 struct Context { 633 Context(tok::TokenKind ContextKind, unsigned BindingStrength, 634 bool IsExpression) 635 : ContextKind(ContextKind), BindingStrength(BindingStrength), 636 LongestObjCSelectorName(0), NumBlockParameters(0), 637 ColonIsForRangeExpr(false), ColonIsDictLiteral(false), 638 ColonIsObjCMethodExpr(false), FirstObjCSelectorName(NULL), 639 FirstStartOfName(NULL), IsExpression(IsExpression), 640 CanBeExpression(true), InTemplateArgument(false), 641 InCtorInitializer(false), CaretFound(false), IsForEachMacro(false) {} 642 643 tok::TokenKind ContextKind; 644 unsigned BindingStrength; 645 unsigned LongestObjCSelectorName; 646 unsigned NumBlockParameters; 647 bool ColonIsForRangeExpr; 648 bool ColonIsDictLiteral; 649 bool ColonIsObjCMethodExpr; 650 FormatToken *FirstObjCSelectorName; 651 FormatToken *FirstStartOfName; 652 bool IsExpression; 653 bool CanBeExpression; 654 bool InTemplateArgument; 655 bool InCtorInitializer; 656 bool CaretFound; 657 bool IsForEachMacro; 658 }; 659 660 /// \brief Puts a new \c Context onto the stack \c Contexts for the lifetime 661 /// of each instance. 662 struct ScopedContextCreator { 663 AnnotatingParser &P; 664 665 ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind, 666 unsigned Increase) 667 : P(P) { 668 P.Contexts.push_back(Context(ContextKind, 669 P.Contexts.back().BindingStrength + Increase, 670 P.Contexts.back().IsExpression)); 671 } 672 673 ~ScopedContextCreator() { P.Contexts.pop_back(); } 674 }; 675 676 void determineTokenType(FormatToken &Current) { 677 if (Current.getPrecedence() == prec::Assignment && 678 !Line.First->isOneOf(tok::kw_template, tok::kw_using) && 679 (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) { 680 Contexts.back().IsExpression = true; 681 for (FormatToken *Previous = Current.Previous; 682 Previous && !Previous->isOneOf(tok::comma, tok::semi); 683 Previous = Previous->Previous) { 684 if (Previous->is(tok::r_square)) 685 Previous = Previous->MatchingParen; 686 if (Previous->Type == TT_BinaryOperator && 687 Previous->isOneOf(tok::star, tok::amp)) { 688 Previous->Type = TT_PointerOrReference; 689 } 690 } 691 } else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) { 692 Contexts.back().IsExpression = true; 693 } else if (Current.is(tok::l_paren) && !Line.MustBeDeclaration && 694 !Line.InPPDirective) { 695 bool ParametersOfFunctionType = 696 Current.Previous && Current.Previous->is(tok::r_paren) && 697 Current.Previous->MatchingParen && 698 Current.Previous->MatchingParen->Type == TT_FunctionTypeLParen; 699 bool IsForOrCatch = Current.Previous && 700 Current.Previous->isOneOf(tok::kw_for, tok::kw_catch); 701 Contexts.back().IsExpression = !ParametersOfFunctionType && !IsForOrCatch; 702 } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) { 703 for (FormatToken *Previous = Current.Previous; 704 Previous && Previous->isOneOf(tok::star, tok::amp); 705 Previous = Previous->Previous) 706 Previous->Type = TT_PointerOrReference; 707 } else if (Current.Previous && 708 Current.Previous->Type == TT_CtorInitializerColon) { 709 Contexts.back().IsExpression = true; 710 Contexts.back().InCtorInitializer = true; 711 } else if (Current.is(tok::kw_new)) { 712 Contexts.back().CanBeExpression = false; 713 } else if (Current.is(tok::semi) || Current.is(tok::exclaim)) { 714 // This should be the condition or increment in a for-loop. 715 Contexts.back().IsExpression = true; 716 } 717 718 if (Current.Type == TT_Unknown) { 719 // Line.MightBeFunctionDecl can only be true after the parentheses of a 720 // function declaration have been found. In this case, 'Current' is a 721 // trailing token of this declaration and thus cannot be a name. 722 if (isStartOfName(Current) && !Line.MightBeFunctionDecl) { 723 Contexts.back().FirstStartOfName = &Current; 724 Current.Type = TT_StartOfName; 725 } else if (Current.is(tok::kw_auto)) { 726 AutoFound = true; 727 } else if (Current.is(tok::arrow) && AutoFound && 728 Line.MustBeDeclaration) { 729 Current.Type = TT_TrailingReturnArrow; 730 } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) { 731 Current.Type = 732 determineStarAmpUsage(Current, Contexts.back().CanBeExpression && 733 Contexts.back().IsExpression, 734 Contexts.back().InTemplateArgument); 735 } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) { 736 Current.Type = determinePlusMinusCaretUsage(Current); 737 if (Current.Type == TT_UnaryOperator && Current.is(tok::caret)) { 738 ++Contexts.back().NumBlockParameters; 739 Contexts.back().CaretFound = true; 740 } 741 } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) { 742 Current.Type = determineIncrementUsage(Current); 743 } else if (Current.is(tok::exclaim)) { 744 Current.Type = TT_UnaryOperator; 745 } else if (Current.is(tok::question)) { 746 Current.Type = TT_ConditionalExpr; 747 } else if (Current.isBinaryOperator() && 748 (!Current.Previous || 749 Current.Previous->isNot(tok::l_square))) { 750 Current.Type = TT_BinaryOperator; 751 } else if (Current.is(tok::comment)) { 752 if (Current.TokenText.startswith("//")) 753 Current.Type = TT_LineComment; 754 else 755 Current.Type = TT_BlockComment; 756 } else if (Current.is(tok::r_paren)) { 757 FormatToken *LeftOfParens = NULL; 758 if (Current.MatchingParen) 759 LeftOfParens = Current.MatchingParen->getPreviousNonComment(); 760 bool IsCast = false; 761 bool ParensAreEmpty = Current.Previous == Current.MatchingParen; 762 bool ParensAreType = !Current.Previous || 763 Current.Previous->Type == TT_PointerOrReference || 764 Current.Previous->Type == TT_TemplateCloser || 765 Current.Previous->isSimpleTypeSpecifier(); 766 bool ParensCouldEndDecl = 767 Current.Next && 768 Current.Next->isOneOf(tok::equal, tok::semi, tok::l_brace); 769 bool IsSizeOfOrAlignOf = 770 LeftOfParens && 771 LeftOfParens->isOneOf(tok::kw_sizeof, tok::kw_alignof); 772 if (ParensAreType && !ParensCouldEndDecl && !IsSizeOfOrAlignOf && 773 ((Contexts.size() > 1 && 774 Contexts[Contexts.size() - 2].IsExpression) || 775 (Current.Next && Current.Next->isBinaryOperator()))) 776 IsCast = true; 777 if (Current.Next && Current.Next->isNot(tok::string_literal) && 778 (Current.Next->Tok.isLiteral() || 779 Current.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof))) 780 IsCast = true; 781 // If there is an identifier after the (), it is likely a cast, unless 782 // there is also an identifier before the (). 783 if (LeftOfParens && (LeftOfParens->Tok.getIdentifierInfo() == NULL || 784 LeftOfParens->is(tok::kw_return)) && 785 LeftOfParens->Type != TT_OverloadedOperator && 786 LeftOfParens->isNot(tok::at) && 787 LeftOfParens->Type != TT_TemplateCloser && Current.Next && 788 Current.Next->is(tok::identifier)) 789 IsCast = true; 790 if (IsCast && !ParensAreEmpty) 791 Current.Type = TT_CastRParen; 792 } else if (Current.is(tok::at) && Current.Next) { 793 switch (Current.Next->Tok.getObjCKeywordID()) { 794 case tok::objc_interface: 795 case tok::objc_implementation: 796 case tok::objc_protocol: 797 Current.Type = TT_ObjCDecl; 798 break; 799 case tok::objc_property: 800 Current.Type = TT_ObjCProperty; 801 break; 802 default: 803 break; 804 } 805 } else if (Current.is(tok::period)) { 806 FormatToken *PreviousNoComment = Current.getPreviousNonComment(); 807 if (PreviousNoComment && 808 PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) 809 Current.Type = TT_DesignatedInitializerPeriod; 810 } else if (Current.isOneOf(tok::identifier, tok::kw_const) && 811 Current.Previous && Current.Previous->isNot(tok::equal) && 812 Line.MightBeFunctionDecl && Contexts.size() == 1) { 813 // Line.MightBeFunctionDecl can only be true after the parentheses of a 814 // function declaration have been found. 815 Current.Type = TT_TrailingAnnotation; 816 } 817 } 818 } 819 820 /// \brief Take a guess at whether \p Tok starts a name of a function or 821 /// variable declaration. 822 /// 823 /// This is a heuristic based on whether \p Tok is an identifier following 824 /// something that is likely a type. 825 bool isStartOfName(const FormatToken &Tok) { 826 if (Tok.isNot(tok::identifier) || Tok.Previous == NULL) 827 return false; 828 829 // Skip "const" as it does not have an influence on whether this is a name. 830 FormatToken *PreviousNotConst = Tok.Previous; 831 while (PreviousNotConst != NULL && PreviousNotConst->is(tok::kw_const)) 832 PreviousNotConst = PreviousNotConst->Previous; 833 834 if (PreviousNotConst == NULL) 835 return false; 836 837 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) && 838 PreviousNotConst->Previous && 839 PreviousNotConst->Previous->is(tok::hash); 840 841 if (PreviousNotConst->Type == TT_TemplateCloser) 842 return PreviousNotConst && PreviousNotConst->MatchingParen && 843 PreviousNotConst->MatchingParen->Previous && 844 PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template); 845 846 return (!IsPPKeyword && PreviousNotConst->is(tok::identifier)) || 847 PreviousNotConst->Type == TT_PointerOrReference || 848 PreviousNotConst->isSimpleTypeSpecifier(); 849 } 850 851 /// \brief Return the type of the given token assuming it is * or &. 852 TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression, 853 bool InTemplateArgument) { 854 const FormatToken *PrevToken = Tok.getPreviousNonComment(); 855 if (PrevToken == NULL) 856 return TT_UnaryOperator; 857 858 const FormatToken *NextToken = Tok.getNextNonComment(); 859 if (NextToken == NULL) 860 return TT_Unknown; 861 862 if (PrevToken->is(tok::coloncolon) || 863 (PrevToken->is(tok::l_paren) && !IsExpression)) 864 return TT_PointerOrReference; 865 866 if (PrevToken->isOneOf(tok::l_paren, tok::l_square, tok::l_brace, 867 tok::comma, tok::semi, tok::kw_return, tok::colon, 868 tok::equal, tok::kw_delete, tok::kw_sizeof) || 869 PrevToken->Type == TT_BinaryOperator || 870 PrevToken->Type == TT_ConditionalExpr || 871 PrevToken->Type == TT_UnaryOperator || PrevToken->Type == TT_CastRParen) 872 return TT_UnaryOperator; 873 874 if (NextToken->is(tok::l_square)) 875 return TT_PointerOrReference; 876 877 if (PrevToken->is(tok::r_paren) && PrevToken->MatchingParen && 878 PrevToken->MatchingParen->Previous && 879 PrevToken->MatchingParen->Previous->is(tok::kw_typeof)) 880 return TT_PointerOrReference; 881 882 if (PrevToken->Tok.isLiteral() || 883 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true, 884 tok::kw_false) || 885 NextToken->Tok.isLiteral() || 886 NextToken->isOneOf(tok::kw_true, tok::kw_false) || 887 NextToken->isUnaryOperator() || 888 // If we know we're in a template argument, there are no named 889 // declarations. Thus, having an identifier on the right-hand side 890 // indicates a binary operator. 891 (InTemplateArgument && NextToken->Tok.isAnyIdentifier())) 892 return TT_BinaryOperator; 893 894 // It is very unlikely that we are going to find a pointer or reference type 895 // definition on the RHS of an assignment. 896 if (IsExpression) 897 return TT_BinaryOperator; 898 899 return TT_PointerOrReference; 900 } 901 902 TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) { 903 const FormatToken *PrevToken = Tok.getPreviousNonComment(); 904 if (PrevToken == NULL || PrevToken->Type == TT_CastRParen) 905 return TT_UnaryOperator; 906 907 // Use heuristics to recognize unary operators. 908 if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square, 909 tok::question, tok::colon, tok::kw_return, 910 tok::kw_case, tok::at, tok::l_brace)) 911 return TT_UnaryOperator; 912 913 // There can't be two consecutive binary operators. 914 if (PrevToken->Type == TT_BinaryOperator) 915 return TT_UnaryOperator; 916 917 // Fall back to marking the token as binary operator. 918 return TT_BinaryOperator; 919 } 920 921 /// \brief Determine whether ++/-- are pre- or post-increments/-decrements. 922 TokenType determineIncrementUsage(const FormatToken &Tok) { 923 const FormatToken *PrevToken = Tok.getPreviousNonComment(); 924 if (PrevToken == NULL || PrevToken->Type == TT_CastRParen) 925 return TT_UnaryOperator; 926 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier)) 927 return TT_TrailingUnaryOperator; 928 929 return TT_UnaryOperator; 930 } 931 932 933 SmallVector<Context, 8> Contexts; 934 935 const FormatStyle &Style; 936 AnnotatedLine &Line; 937 FormatToken *CurrentToken; 938 bool KeywordVirtualFound; 939 bool AutoFound; 940 IdentifierInfo &Ident_in; 941 }; 942 943 static int PrecedenceUnaryOperator = prec::PointerToMember + 1; 944 static int PrecedenceArrowAndPeriod = prec::PointerToMember + 2; 945 946 /// \brief Parses binary expressions by inserting fake parenthesis based on 947 /// operator precedence. 948 class ExpressionParser { 949 public: 950 ExpressionParser(AnnotatedLine &Line) : Current(Line.First) { 951 // Skip leading "}", e.g. in "} else if (...) {". 952 if (Current->is(tok::r_brace)) 953 next(); 954 } 955 956 /// \brief Parse expressions with the given operatore precedence. 957 void parse(int Precedence = 0) { 958 // Skip 'return' and ObjC selector colons as they are not part of a binary 959 // expression. 960 while (Current && 961 (Current->is(tok::kw_return) || 962 (Current->is(tok::colon) && Current->Type == TT_ObjCMethodExpr))) 963 next(); 964 965 if (Current == NULL || Precedence > PrecedenceArrowAndPeriod) 966 return; 967 968 // Conditional expressions need to be parsed separately for proper nesting. 969 if (Precedence == prec::Conditional) { 970 parseConditionalExpr(); 971 return; 972 } 973 974 // Parse unary operators, which all have a higher precedence than binary 975 // operators. 976 if (Precedence == PrecedenceUnaryOperator) { 977 parseUnaryOperator(); 978 return; 979 } 980 981 FormatToken *Start = Current; 982 FormatToken *LatestOperator = NULL; 983 unsigned OperatorIndex = 0; 984 985 while (Current) { 986 // Consume operators with higher precedence. 987 parse(Precedence + 1); 988 989 int CurrentPrecedence = getCurrentPrecedence(); 990 991 if (Current && Current->Type == TT_ObjCSelectorName && 992 Precedence == CurrentPrecedence) { 993 if (LatestOperator) 994 addFakeParenthesis(Start, prec::Level(Precedence)); 995 Start = Current; 996 } 997 998 // At the end of the line or when an operator with higher precedence is 999 // found, insert fake parenthesis and return. 1000 if (Current == NULL || Current->closesScope() || 1001 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence)) { 1002 if (LatestOperator) { 1003 LatestOperator->LastOperator = true; 1004 if (Precedence == PrecedenceArrowAndPeriod) { 1005 // Call expressions don't have a binary operator precedence. 1006 addFakeParenthesis(Start, prec::Unknown); 1007 } else { 1008 addFakeParenthesis(Start, prec::Level(Precedence)); 1009 } 1010 } 1011 return; 1012 } 1013 1014 // Consume scopes: (), [], <> and {} 1015 if (Current->opensScope()) { 1016 while (Current && !Current->closesScope()) { 1017 next(); 1018 parse(); 1019 } 1020 next(); 1021 } else { 1022 // Operator found. 1023 if (CurrentPrecedence == Precedence) { 1024 LatestOperator = Current; 1025 Current->OperatorIndex = OperatorIndex; 1026 ++OperatorIndex; 1027 } 1028 1029 next(); 1030 } 1031 } 1032 } 1033 1034 private: 1035 /// \brief Gets the precedence (+1) of the given token for binary operators 1036 /// and other tokens that we treat like binary operators. 1037 int getCurrentPrecedence() { 1038 if (Current) { 1039 if (Current->Type == TT_ConditionalExpr) 1040 return prec::Conditional; 1041 else if (Current->is(tok::semi) || Current->Type == TT_InlineASMColon || 1042 Current->Type == TT_ObjCSelectorName) 1043 return 0; 1044 else if (Current->Type == TT_RangeBasedForLoopColon) 1045 return prec::Comma; 1046 else if (Current->Type == TT_BinaryOperator || Current->is(tok::comma)) 1047 return Current->getPrecedence(); 1048 else if (Current->isOneOf(tok::period, tok::arrow)) 1049 return PrecedenceArrowAndPeriod; 1050 } 1051 return -1; 1052 } 1053 1054 void addFakeParenthesis(FormatToken *Start, prec::Level Precedence) { 1055 Start->FakeLParens.push_back(Precedence); 1056 if (Precedence > prec::Unknown) 1057 Start->StartsBinaryExpression = true; 1058 if (Current) { 1059 ++Current->Previous->FakeRParens; 1060 if (Precedence > prec::Unknown) 1061 Current->Previous->EndsBinaryExpression = true; 1062 } 1063 } 1064 1065 /// \brief Parse unary operator expressions and surround them with fake 1066 /// parentheses if appropriate. 1067 void parseUnaryOperator() { 1068 if (Current == NULL || Current->Type != TT_UnaryOperator) { 1069 parse(PrecedenceArrowAndPeriod); 1070 return; 1071 } 1072 1073 FormatToken *Start = Current; 1074 next(); 1075 parseUnaryOperator(); 1076 1077 // The actual precedence doesn't matter. 1078 addFakeParenthesis(Start, prec::Unknown); 1079 } 1080 1081 void parseConditionalExpr() { 1082 FormatToken *Start = Current; 1083 parse(prec::LogicalOr); 1084 if (!Current || !Current->is(tok::question)) 1085 return; 1086 next(); 1087 parse(prec::LogicalOr); 1088 if (!Current || Current->Type != TT_ConditionalExpr) 1089 return; 1090 next(); 1091 parseConditionalExpr(); 1092 addFakeParenthesis(Start, prec::Conditional); 1093 } 1094 1095 void next() { 1096 if (Current) 1097 Current = Current->Next; 1098 while (Current && Current->isTrailingComment()) 1099 Current = Current->Next; 1100 } 1101 1102 FormatToken *Current; 1103 }; 1104 1105 } // end anonymous namespace 1106 1107 void 1108 TokenAnnotator::setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines) { 1109 const AnnotatedLine *NextNonCommentLine = NULL; 1110 for (SmallVectorImpl<AnnotatedLine *>::reverse_iterator I = Lines.rbegin(), 1111 E = Lines.rend(); 1112 I != E; ++I) { 1113 if (NextNonCommentLine && (*I)->First->is(tok::comment) && 1114 (*I)->First->Next == NULL) 1115 (*I)->Level = NextNonCommentLine->Level; 1116 else 1117 NextNonCommentLine = (*I)->First->isNot(tok::r_brace) ? (*I) : NULL; 1118 1119 setCommentLineLevels((*I)->Children); 1120 } 1121 } 1122 1123 void TokenAnnotator::annotate(AnnotatedLine &Line) { 1124 for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(), 1125 E = Line.Children.end(); 1126 I != E; ++I) { 1127 annotate(**I); 1128 } 1129 AnnotatingParser Parser(Style, Line, Ident_in); 1130 Line.Type = Parser.parseLine(); 1131 if (Line.Type == LT_Invalid) 1132 return; 1133 1134 ExpressionParser ExprParser(Line); 1135 ExprParser.parse(); 1136 1137 if (Line.First->Type == TT_ObjCMethodSpecifier) 1138 Line.Type = LT_ObjCMethodDecl; 1139 else if (Line.First->Type == TT_ObjCDecl) 1140 Line.Type = LT_ObjCDecl; 1141 else if (Line.First->Type == TT_ObjCProperty) 1142 Line.Type = LT_ObjCProperty; 1143 1144 Line.First->SpacesRequiredBefore = 1; 1145 Line.First->CanBreakBefore = Line.First->MustBreakBefore; 1146 } 1147 1148 void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) { 1149 Line.First->TotalLength = 1150 Line.First->IsMultiline ? Style.ColumnLimit : Line.First->ColumnWidth; 1151 if (!Line.First->Next) 1152 return; 1153 FormatToken *Current = Line.First->Next; 1154 bool InFunctionDecl = Line.MightBeFunctionDecl; 1155 while (Current != NULL) { 1156 if (Current->Type == TT_LineComment) { 1157 if (Current->Previous->BlockKind == BK_BracedInit && 1158 Current->Previous->opensScope()) 1159 Current->SpacesRequiredBefore = Style.Cpp11BracedListStyle ? 0 : 1; 1160 else 1161 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments; 1162 1163 // If we find a trailing comment, iterate backwards to determine whether 1164 // it seems to relate to a specific parameter. If so, break before that 1165 // parameter to avoid changing the comment's meaning. E.g. don't move 'b' 1166 // to the previous line in: 1167 // SomeFunction(a, 1168 // b, // comment 1169 // c); 1170 if (!Current->HasUnescapedNewline) { 1171 for (FormatToken *Parameter = Current->Previous; Parameter; 1172 Parameter = Parameter->Previous) { 1173 if (Parameter->isOneOf(tok::comment, tok::r_brace)) 1174 break; 1175 if (Parameter->Previous && Parameter->Previous->is(tok::comma)) { 1176 if (Parameter->Previous->Type != TT_CtorInitializerComma && 1177 Parameter->HasUnescapedNewline) 1178 Parameter->MustBreakBefore = true; 1179 break; 1180 } 1181 } 1182 } 1183 } else if (Current->SpacesRequiredBefore == 0 && 1184 spaceRequiredBefore(Line, *Current)) { 1185 Current->SpacesRequiredBefore = 1; 1186 } 1187 1188 Current->MustBreakBefore = 1189 Current->MustBreakBefore || mustBreakBefore(Line, *Current); 1190 1191 Current->CanBreakBefore = 1192 Current->MustBreakBefore || canBreakBefore(Line, *Current); 1193 if (Current->MustBreakBefore || !Current->Children.empty() || 1194 Current->IsMultiline) 1195 Current->TotalLength = Current->Previous->TotalLength + Style.ColumnLimit; 1196 else 1197 Current->TotalLength = Current->Previous->TotalLength + 1198 Current->ColumnWidth + 1199 Current->SpacesRequiredBefore; 1200 1201 if (Current->Type == TT_CtorInitializerColon) 1202 InFunctionDecl = false; 1203 1204 // FIXME: Only calculate this if CanBreakBefore is true once static 1205 // initializers etc. are sorted out. 1206 // FIXME: Move magic numbers to a better place. 1207 Current->SplitPenalty = 20 * Current->BindingStrength + 1208 splitPenalty(Line, *Current, InFunctionDecl); 1209 1210 Current = Current->Next; 1211 } 1212 1213 calculateUnbreakableTailLengths(Line); 1214 for (Current = Line.First; Current != NULL; Current = Current->Next) { 1215 if (Current->Role) 1216 Current->Role->precomputeFormattingInfos(Current); 1217 } 1218 1219 DEBUG({ printDebugInfo(Line); }); 1220 1221 for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(), 1222 E = Line.Children.end(); 1223 I != E; ++I) { 1224 calculateFormattingInformation(**I); 1225 } 1226 } 1227 1228 void TokenAnnotator::calculateUnbreakableTailLengths(AnnotatedLine &Line) { 1229 unsigned UnbreakableTailLength = 0; 1230 FormatToken *Current = Line.Last; 1231 while (Current != NULL) { 1232 Current->UnbreakableTailLength = UnbreakableTailLength; 1233 if (Current->CanBreakBefore || 1234 Current->isOneOf(tok::comment, tok::string_literal)) { 1235 UnbreakableTailLength = 0; 1236 } else { 1237 UnbreakableTailLength += 1238 Current->ColumnWidth + Current->SpacesRequiredBefore; 1239 } 1240 Current = Current->Previous; 1241 } 1242 } 1243 1244 unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line, 1245 const FormatToken &Tok, 1246 bool InFunctionDecl) { 1247 const FormatToken &Left = *Tok.Previous; 1248 const FormatToken &Right = Tok; 1249 1250 if (Left.is(tok::semi)) 1251 return 0; 1252 if (Left.is(tok::comma) || (Right.is(tok::identifier) && Right.Next && 1253 Right.Next->Type == TT_DictLiteral)) 1254 return 1; 1255 if (Right.is(tok::l_square)) { 1256 if (Style.Language == FormatStyle::LK_Proto) 1257 return 1; 1258 if (Right.Type != TT_ObjCMethodExpr) 1259 return 250; 1260 } 1261 if (Right.Type == TT_StartOfName || Right.is(tok::kw_operator)) { 1262 if (Line.First->is(tok::kw_for) && Right.PartOfMultiVariableDeclStmt) 1263 return 3; 1264 if (Left.Type == TT_StartOfName) 1265 return 20; 1266 if (InFunctionDecl && Right.NestingLevel == 0) 1267 return Style.PenaltyReturnTypeOnItsOwnLine; 1268 return 200; 1269 } 1270 if (Left.is(tok::equal) && Right.is(tok::l_brace)) 1271 return 150; 1272 if (Left.Type == TT_CastRParen) 1273 return 100; 1274 if (Left.is(tok::coloncolon) || 1275 (Right.is(tok::period) && Style.Language == FormatStyle::LK_Proto)) 1276 return 500; 1277 if (Left.isOneOf(tok::kw_class, tok::kw_struct)) 1278 return 5000; 1279 1280 if (Left.Type == TT_RangeBasedForLoopColon || 1281 Left.Type == TT_InheritanceColon) 1282 return 2; 1283 1284 if (Right.isMemberAccess()) { 1285 if (Left.is(tok::r_paren) && Left.MatchingParen && 1286 Left.MatchingParen->ParameterCount > 0) 1287 return 20; // Should be smaller than breaking at a nested comma. 1288 return 150; 1289 } 1290 1291 if (Right.Type == TT_TrailingAnnotation && Right.Next && 1292 Right.Next->isNot(tok::l_paren)) { 1293 // Generally, breaking before a trailing annotation is bad unless it is 1294 // function-like. It seems to be especially preferable to keep standard 1295 // annotations (i.e. "const", "final" and "override") on the same line. 1296 // Use a slightly higher penalty after ")" so that annotations like 1297 // "const override" are kept together. 1298 bool is_short_annotation = Right.TokenText.size() < 10; 1299 return (Left.is(tok::r_paren) ? 100 : 120) + 1300 (is_short_annotation ? 50 : 0); 1301 } 1302 1303 // In for-loops, prefer breaking at ',' and ';'. 1304 if (Line.First->is(tok::kw_for) && Left.is(tok::equal)) 1305 return 4; 1306 1307 // In Objective-C method expressions, prefer breaking before "param:" over 1308 // breaking after it. 1309 if (Right.Type == TT_ObjCSelectorName) 1310 return 0; 1311 if (Left.is(tok::colon) && Left.Type == TT_ObjCMethodExpr) 1312 return Line.MightBeFunctionDecl ? 50 : 500; 1313 1314 if (Left.is(tok::l_paren) && InFunctionDecl) 1315 return 100; 1316 if (Left.is(tok::equal) && InFunctionDecl) 1317 return 110; 1318 if (Left.opensScope()) 1319 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter 1320 : 19; 1321 1322 if (Right.is(tok::lessless)) { 1323 if (Left.is(tok::string_literal)) { 1324 StringRef Content = Left.TokenText; 1325 if (Content.startswith("\"")) 1326 Content = Content.drop_front(1); 1327 if (Content.endswith("\"")) 1328 Content = Content.drop_back(1); 1329 Content = Content.trim(); 1330 if (Content.size() > 1 && 1331 (Content.back() == ':' || Content.back() == '=')) 1332 return 25; 1333 } 1334 return 1; // Breaking at a << is really cheap. 1335 } 1336 if (Left.Type == TT_ConditionalExpr) 1337 return prec::Conditional; 1338 prec::Level Level = Left.getPrecedence(); 1339 1340 if (Level != prec::Unknown) 1341 return Level; 1342 1343 return 3; 1344 } 1345 1346 bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, 1347 const FormatToken &Left, 1348 const FormatToken &Right) { 1349 if (Style.Language == FormatStyle::LK_Proto) { 1350 if (Right.is(tok::l_paren) && 1351 (Left.TokenText == "returns" || Left.TokenText == "option")) 1352 return true; 1353 } 1354 if (Style.ObjCSpaceAfterProperty && Line.Type == LT_ObjCProperty && 1355 Left.Tok.getObjCKeywordID() == tok::objc_property) 1356 return true; 1357 if (Right.is(tok::hashhash)) 1358 return Left.is(tok::hash); 1359 if (Left.isOneOf(tok::hashhash, tok::hash)) 1360 return Right.is(tok::hash); 1361 if (Left.is(tok::l_paren) && Right.is(tok::r_paren)) 1362 return Style.SpaceInEmptyParentheses; 1363 if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) 1364 return (Right.Type == TT_CastRParen || 1365 (Left.MatchingParen && Left.MatchingParen->Type == TT_CastRParen)) 1366 ? Style.SpacesInCStyleCastParentheses 1367 : Style.SpacesInParentheses; 1368 if (Style.SpacesInAngles && 1369 ((Left.Type == TT_TemplateOpener) != (Right.Type == TT_TemplateCloser))) 1370 return true; 1371 if (Right.isOneOf(tok::semi, tok::comma)) 1372 return false; 1373 if (Right.is(tok::less) && 1374 (Left.is(tok::kw_template) || 1375 (Line.Type == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList))) 1376 return true; 1377 if (Left.is(tok::arrow) || Right.is(tok::arrow)) 1378 return false; 1379 if (Left.isOneOf(tok::exclaim, tok::tilde)) 1380 return false; 1381 if (Left.is(tok::at) && 1382 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant, 1383 tok::numeric_constant, tok::l_paren, tok::l_brace, 1384 tok::kw_true, tok::kw_false)) 1385 return false; 1386 if (Left.is(tok::coloncolon)) 1387 return false; 1388 if (Right.is(tok::coloncolon) && Left.isNot(tok::l_brace)) 1389 return (Left.is(tok::less) && Style.Standard == FormatStyle::LS_Cpp03) || 1390 !Left.isOneOf(tok::identifier, tok::greater, tok::l_paren, 1391 tok::r_paren, tok::less); 1392 if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less)) 1393 return false; 1394 if (Right.is(tok::ellipsis)) 1395 return Left.Tok.isLiteral(); 1396 if (Left.is(tok::l_square) && Right.is(tok::amp)) 1397 return false; 1398 if (Right.Type == TT_PointerOrReference) 1399 return Left.Tok.isLiteral() || 1400 ((Left.Type != TT_PointerOrReference) && Left.isNot(tok::l_paren) && 1401 !Style.PointerBindsToType); 1402 if (Right.Type == TT_FunctionTypeLParen && Left.isNot(tok::l_paren) && 1403 (Left.Type != TT_PointerOrReference || Style.PointerBindsToType)) 1404 return true; 1405 if (Left.Type == TT_PointerOrReference) 1406 return Right.Tok.isLiteral() || Right.Type == TT_BlockComment || 1407 ((Right.Type != TT_PointerOrReference) && 1408 Right.isNot(tok::l_paren) && Style.PointerBindsToType && 1409 Left.Previous && 1410 !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon)); 1411 if (Right.is(tok::star) && Left.is(tok::l_paren)) 1412 return false; 1413 if (Left.is(tok::l_square)) 1414 return Left.Type == TT_ArrayInitializerLSquare && 1415 Style.SpacesInContainerLiterals && Right.isNot(tok::r_square); 1416 if (Right.is(tok::r_square)) 1417 return Right.MatchingParen && Style.SpacesInContainerLiterals && 1418 Right.MatchingParen->Type == TT_ArrayInitializerLSquare; 1419 if (Right.is(tok::l_square) && Right.Type != TT_ObjCMethodExpr && 1420 Right.Type != TT_LambdaLSquare && Left.isNot(tok::numeric_constant)) 1421 return false; 1422 if (Left.is(tok::colon)) 1423 return Left.Type != TT_ObjCMethodExpr; 1424 if (Right.is(tok::l_paren)) { 1425 if (Left.is(tok::r_paren) && Left.Type == TT_AttributeParen) 1426 return true; 1427 return Line.Type == LT_ObjCDecl || 1428 Left.isOneOf(tok::kw_return, tok::kw_new, tok::kw_delete, 1429 tok::semi) || 1430 (Style.SpaceBeforeParens != FormatStyle::SBPO_Never && 1431 (Left.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, 1432 tok::kw_switch, tok::kw_catch) || 1433 Left.IsForEachMacro)) || 1434 (Style.SpaceBeforeParens == FormatStyle::SBPO_Always && 1435 Left.isOneOf(tok::identifier, tok::kw___attribute) && 1436 Line.Type != LT_PreprocessorDirective); 1437 } 1438 if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword) 1439 return false; 1440 if (Left.is(tok::l_brace) && Right.is(tok::r_brace)) 1441 return !Left.Children.empty(); // No spaces in "{}". 1442 if ((Left.is(tok::l_brace) && Left.BlockKind != BK_Block) || 1443 (Right.is(tok::r_brace) && Right.MatchingParen && 1444 Right.MatchingParen->BlockKind != BK_Block)) 1445 return !Style.Cpp11BracedListStyle; 1446 if (Left.Type == TT_BlockComment && Left.TokenText.endswith("=*/")) 1447 return false; 1448 if (Right.Type == TT_UnaryOperator) 1449 return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) && 1450 (Left.isNot(tok::colon) || Left.Type != TT_ObjCMethodExpr); 1451 if (Left.isOneOf(tok::identifier, tok::greater, tok::r_square) && 1452 Right.is(tok::l_brace) && Right.getNextNonComment() && 1453 Right.BlockKind != BK_Block) 1454 return false; 1455 if (Left.is(tok::period) || Right.is(tok::period)) 1456 return false; 1457 if (Right.is(tok::hash) && Left.is(tok::identifier) && Left.TokenText == "L") 1458 return false; 1459 return true; 1460 } 1461 1462 bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, 1463 const FormatToken &Tok) { 1464 if (Tok.Tok.getIdentifierInfo() && Tok.Previous->Tok.getIdentifierInfo()) 1465 return true; // Never ever merge two identifiers. 1466 if (Tok.Previous->Type == TT_ImplicitStringLiteral) 1467 return Tok.WhitespaceRange.getBegin() != Tok.WhitespaceRange.getEnd(); 1468 if (Line.Type == LT_ObjCMethodDecl) { 1469 if (Tok.Previous->Type == TT_ObjCMethodSpecifier) 1470 return true; 1471 if (Tok.Previous->is(tok::r_paren) && Tok.is(tok::identifier)) 1472 // Don't space between ')' and <id> 1473 return false; 1474 } 1475 if (Line.Type == LT_ObjCProperty && 1476 (Tok.is(tok::equal) || Tok.Previous->is(tok::equal))) 1477 return false; 1478 1479 if (Tok.Type == TT_TrailingReturnArrow || 1480 Tok.Previous->Type == TT_TrailingReturnArrow) 1481 return true; 1482 if (Tok.Previous->is(tok::comma)) 1483 return true; 1484 if (Tok.is(tok::comma)) 1485 return false; 1486 if (Tok.Type == TT_CtorInitializerColon || Tok.Type == TT_ObjCBlockLParen) 1487 return true; 1488 if (Tok.Previous->Tok.is(tok::kw_operator)) 1489 return Tok.is(tok::coloncolon); 1490 if (Tok.Type == TT_OverloadedOperatorLParen) 1491 return false; 1492 if (Tok.is(tok::colon)) 1493 return !Line.First->isOneOf(tok::kw_case, tok::kw_default) && 1494 Tok.getNextNonComment() != NULL && Tok.Type != TT_ObjCMethodExpr && 1495 !Tok.Previous->is(tok::question) && 1496 (Tok.Type != TT_DictLiteral || Style.SpacesInContainerLiterals); 1497 if (Tok.Previous->Type == TT_UnaryOperator || 1498 Tok.Previous->Type == TT_CastRParen) 1499 return Tok.Type == TT_BinaryOperator; 1500 if (Tok.Previous->is(tok::greater) && Tok.is(tok::greater)) { 1501 return Tok.Type == TT_TemplateCloser && 1502 Tok.Previous->Type == TT_TemplateCloser && 1503 (Style.Standard != FormatStyle::LS_Cpp11 || Style.SpacesInAngles); 1504 } 1505 if (Tok.isOneOf(tok::arrowstar, tok::periodstar) || 1506 Tok.Previous->isOneOf(tok::arrowstar, tok::periodstar)) 1507 return false; 1508 if (!Style.SpaceBeforeAssignmentOperators && 1509 Tok.getPrecedence() == prec::Assignment) 1510 return false; 1511 if ((Tok.Type == TT_BinaryOperator && !Tok.Previous->is(tok::l_paren)) || 1512 Tok.Previous->Type == TT_BinaryOperator || 1513 Tok.Previous->Type == TT_ConditionalExpr) 1514 return true; 1515 if (Tok.Previous->Type == TT_TemplateCloser && Tok.is(tok::l_paren)) 1516 return false; 1517 if (Tok.is(tok::less) && Tok.Previous->isNot(tok::l_paren) && 1518 Line.First->is(tok::hash)) 1519 return true; 1520 if (Tok.Type == TT_TrailingUnaryOperator) 1521 return false; 1522 return spaceRequiredBetween(Line, *Tok.Previous, Tok); 1523 } 1524 1525 bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, 1526 const FormatToken &Right) { 1527 const FormatToken &Left = *Right.Previous; 1528 if (Right.is(tok::comment)) { 1529 return Right.Previous->BlockKind != BK_BracedInit && 1530 Right.Previous->Type != TT_CtorInitializerColon && 1531 (Right.NewlinesBefore > 0 && Right.HasUnescapedNewline); 1532 } else if (Right.Previous->isTrailingComment() || 1533 (Right.isStringLiteral() && Right.Previous->isStringLiteral())) { 1534 return true; 1535 } else if (Right.Previous->IsUnterminatedLiteral) { 1536 return true; 1537 } else if (Right.is(tok::lessless) && Right.Next && 1538 Right.Previous->is(tok::string_literal) && 1539 Right.Next->is(tok::string_literal)) { 1540 return true; 1541 } else if (Right.Previous->ClosesTemplateDeclaration && 1542 Right.Previous->MatchingParen && 1543 Right.Previous->MatchingParen->NestingLevel == 0 && 1544 Style.AlwaysBreakTemplateDeclarations) { 1545 return true; 1546 } else if ((Right.Type == TT_CtorInitializerComma || 1547 Right.Type == TT_CtorInitializerColon) && 1548 Style.BreakConstructorInitializersBeforeComma && 1549 !Style.ConstructorInitializerAllOnOneLineOrOnePerLine) { 1550 return true; 1551 } else if (Right.is(tok::l_brace) && (Right.BlockKind == BK_Block)) { 1552 return Style.BreakBeforeBraces == FormatStyle::BS_Allman || 1553 Style.BreakBeforeBraces == FormatStyle::BS_GNU; 1554 } else if (Right.is(tok::string_literal) && 1555 Right.TokenText.startswith("R\"")) { 1556 // Raw string literals are special wrt. line breaks. The author has made a 1557 // deliberate choice and might have aligned the contents of the string 1558 // literal accordingly. Thus, we try keep existing line breaks. 1559 return Right.NewlinesBefore > 0; 1560 } else if (Right.Previous->is(tok::l_brace) && Right.NestingLevel == 1 && 1561 Style.Language == FormatStyle::LK_Proto) { 1562 // Don't enums onto single lines in protocol buffers. 1563 return true; 1564 } 1565 1566 // If the last token before a '}' is a comma or a comment, the intention is to 1567 // insert a line break after it in order to make shuffling around entries 1568 // easier. 1569 const FormatToken *BeforeClosingBrace = nullptr; 1570 if (Left.is(tok::l_brace) && Left.MatchingParen) 1571 BeforeClosingBrace = Left.MatchingParen->Previous; 1572 else if (Right.is(tok::r_brace)) 1573 BeforeClosingBrace = Right.Previous; 1574 if (BeforeClosingBrace && 1575 BeforeClosingBrace->isOneOf(tok::comma, tok::comment)) 1576 return true; 1577 1578 return false; 1579 } 1580 1581 bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, 1582 const FormatToken &Right) { 1583 const FormatToken &Left = *Right.Previous; 1584 if (Left.is(tok::at)) 1585 return false; 1586 if (Right.Type == TT_StartOfName || Right.is(tok::kw_operator)) 1587 return true; 1588 if (Right.isTrailingComment()) 1589 // We rely on MustBreakBefore being set correctly here as we should not 1590 // change the "binding" behavior of a comment. 1591 // The first comment in a braced lists is always interpreted as belonging to 1592 // the first list element. Otherwise, it should be placed outside of the 1593 // list. 1594 return Left.BlockKind == BK_BracedInit; 1595 if (Left.is(tok::question) && Right.is(tok::colon)) 1596 return false; 1597 if (Right.Type == TT_ConditionalExpr || Right.is(tok::question)) 1598 return Style.BreakBeforeTernaryOperators; 1599 if (Left.Type == TT_ConditionalExpr || Left.is(tok::question)) 1600 return !Style.BreakBeforeTernaryOperators; 1601 if (Right.is(tok::colon) && 1602 (Right.Type == TT_DictLiteral || Right.Type == TT_ObjCMethodExpr)) 1603 return false; 1604 if (Right.Type == TT_InheritanceColon) 1605 return true; 1606 if (Left.is(tok::colon) && 1607 (Left.Type == TT_DictLiteral || Left.Type == TT_ObjCMethodExpr)) 1608 return true; 1609 if (Right.Type == TT_ObjCSelectorName) 1610 return true; 1611 if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty) 1612 return true; 1613 if (Left.ClosesTemplateDeclaration) 1614 return true; 1615 if (Right.Type == TT_RangeBasedForLoopColon || 1616 Right.Type == TT_OverloadedOperatorLParen || 1617 Right.Type == TT_OverloadedOperator) 1618 return false; 1619 if (Left.Type == TT_RangeBasedForLoopColon) 1620 return true; 1621 if (Right.Type == TT_RangeBasedForLoopColon) 1622 return false; 1623 if (Left.Type == TT_PointerOrReference || Left.Type == TT_TemplateCloser || 1624 Left.Type == TT_UnaryOperator || Left.is(tok::kw_operator)) 1625 return false; 1626 if (Left.is(tok::equal) && Line.Type == LT_VirtualFunctionDecl) 1627 return false; 1628 if (Left.is(tok::l_paren) && Left.Type == TT_AttributeParen) 1629 return false; 1630 if (Left.is(tok::l_paren) && Left.Previous && 1631 (Left.Previous->Type == TT_BinaryOperator || 1632 Left.Previous->Type == TT_CastRParen)) 1633 return false; 1634 if (Right.Type == TT_ImplicitStringLiteral) 1635 return false; 1636 1637 if (Right.is(tok::r_paren) || Right.Type == TT_TemplateCloser) 1638 return false; 1639 1640 // We only break before r_brace if there was a corresponding break before 1641 // the l_brace, which is tracked by BreakBeforeClosingBrace. 1642 if (Right.is(tok::r_brace)) 1643 return Right.MatchingParen && Right.MatchingParen->BlockKind == BK_Block; 1644 1645 // Allow breaking after a trailing annotation, e.g. after a method 1646 // declaration. 1647 if (Left.Type == TT_TrailingAnnotation) 1648 return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren, 1649 tok::less, tok::coloncolon); 1650 1651 if (Right.is(tok::kw___attribute)) 1652 return true; 1653 1654 if (Left.is(tok::identifier) && Right.is(tok::string_literal)) 1655 return true; 1656 1657 if (Right.is(tok::identifier) && Right.Next && 1658 Right.Next->Type == TT_DictLiteral) 1659 return true; 1660 1661 if (Left.Type == TT_CtorInitializerComma && 1662 Style.BreakConstructorInitializersBeforeComma) 1663 return false; 1664 if (Right.Type == TT_CtorInitializerComma && 1665 Style.BreakConstructorInitializersBeforeComma) 1666 return true; 1667 if (Right.Type == TT_BinaryOperator && Style.BreakBeforeBinaryOperators) 1668 return true; 1669 if (Left.is(tok::greater) && Right.is(tok::greater) && 1670 Left.Type != TT_TemplateCloser) 1671 return false; 1672 if (Left.Type == TT_ArrayInitializerLSquare) 1673 return true; 1674 return (Left.isBinaryOperator() && Left.isNot(tok::lessless) && 1675 !Style.BreakBeforeBinaryOperators) || 1676 Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace, 1677 tok::kw_class, tok::kw_struct) || 1678 Right.isOneOf(tok::lessless, tok::arrow, tok::period, tok::colon, 1679 tok::l_square, tok::at) || 1680 (Left.is(tok::r_paren) && 1681 Right.isOneOf(tok::identifier, tok::kw_const)) || 1682 (Left.is(tok::l_paren) && !Right.is(tok::r_paren)); 1683 } 1684 1685 void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) { 1686 llvm::errs() << "AnnotatedTokens:\n"; 1687 const FormatToken *Tok = Line.First; 1688 while (Tok) { 1689 llvm::errs() << " M=" << Tok->MustBreakBefore 1690 << " C=" << Tok->CanBreakBefore << " T=" << Tok->Type 1691 << " S=" << Tok->SpacesRequiredBefore 1692 << " P=" << Tok->SplitPenalty << " Name=" << Tok->Tok.getName() 1693 << " L=" << Tok->TotalLength << " PPK=" << Tok->PackingKind 1694 << " FakeLParens="; 1695 for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i) 1696 llvm::errs() << Tok->FakeLParens[i] << "/"; 1697 llvm::errs() << " FakeRParens=" << Tok->FakeRParens << "\n"; 1698 if (Tok->Next == NULL) 1699 assert(Tok == Line.Last); 1700 Tok = Tok->Next; 1701 } 1702 llvm::errs() << "----\n"; 1703 } 1704 1705 } // namespace format 1706 } // namespace clang 1707