1 //===--- Format.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 functions declared in Format.h. This will be 12 /// split into separate files as we go. 13 /// 14 //===----------------------------------------------------------------------===// 15 16 #define DEBUG_TYPE "format-formatter" 17 18 #include "TokenAnnotator.h" 19 #include "UnwrappedLineParser.h" 20 #include "clang/Basic/Diagnostic.h" 21 #include "clang/Basic/OperatorPrecedence.h" 22 #include "clang/Basic/SourceManager.h" 23 #include "clang/Format/Format.h" 24 #include "clang/Frontend/TextDiagnosticPrinter.h" 25 #include "clang/Lex/Lexer.h" 26 #include "llvm/Support/Debug.h" 27 #include <string> 28 29 namespace clang { 30 namespace format { 31 32 FormatStyle getLLVMStyle() { 33 FormatStyle LLVMStyle; 34 LLVMStyle.ColumnLimit = 80; 35 LLVMStyle.MaxEmptyLinesToKeep = 1; 36 LLVMStyle.PointerBindsToType = false; 37 LLVMStyle.DerivePointerBinding = false; 38 LLVMStyle.AccessModifierOffset = -2; 39 LLVMStyle.Standard = FormatStyle::LS_Cpp03; 40 LLVMStyle.IndentCaseLabels = false; 41 LLVMStyle.SpacesBeforeTrailingComments = 1; 42 LLVMStyle.BinPackParameters = true; 43 LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true; 44 LLVMStyle.AllowReturnTypeOnItsOwnLine = true; 45 LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false; 46 LLVMStyle.AllowShortIfStatementsOnASingleLine = false; 47 LLVMStyle.ObjCSpaceBeforeProtocolList = true; 48 LLVMStyle.PenaltyExcessCharacter = 1000000; 49 return LLVMStyle; 50 } 51 52 FormatStyle getGoogleStyle() { 53 FormatStyle GoogleStyle; 54 GoogleStyle.ColumnLimit = 80; 55 GoogleStyle.MaxEmptyLinesToKeep = 1; 56 GoogleStyle.PointerBindsToType = true; 57 GoogleStyle.DerivePointerBinding = true; 58 GoogleStyle.AccessModifierOffset = -1; 59 GoogleStyle.Standard = FormatStyle::LS_Auto; 60 GoogleStyle.IndentCaseLabels = true; 61 GoogleStyle.SpacesBeforeTrailingComments = 2; 62 GoogleStyle.BinPackParameters = false; 63 GoogleStyle.AllowAllParametersOfDeclarationOnNextLine = true; 64 GoogleStyle.AllowReturnTypeOnItsOwnLine = false; 65 GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 66 GoogleStyle.AllowShortIfStatementsOnASingleLine = false; 67 GoogleStyle.ObjCSpaceBeforeProtocolList = false; 68 GoogleStyle.PenaltyExcessCharacter = 1000000; 69 return GoogleStyle; 70 } 71 72 FormatStyle getChromiumStyle() { 73 FormatStyle ChromiumStyle = getGoogleStyle(); 74 ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false; 75 ChromiumStyle.Standard = FormatStyle::LS_Cpp03; 76 ChromiumStyle.DerivePointerBinding = false; 77 return ChromiumStyle; 78 } 79 80 static bool isTrailingComment(const AnnotatedToken &Tok) { 81 return Tok.is(tok::comment) && 82 (Tok.Children.empty() || Tok.Children[0].MustBreakBefore); 83 } 84 85 // Returns the length of everything up to the first possible line break after 86 // the ), ], } or > matching \c Tok. 87 static unsigned getLengthToMatchingParen(const AnnotatedToken &Tok) { 88 if (Tok.MatchingParen == NULL) 89 return 0; 90 AnnotatedToken *End = Tok.MatchingParen; 91 while (!End->Children.empty() && !End->Children[0].CanBreakBefore) { 92 End = &End->Children[0]; 93 } 94 return End->TotalLength - Tok.TotalLength + 1; 95 } 96 97 /// \brief Manages the whitespaces around tokens and their replacements. 98 /// 99 /// This includes special handling for certain constructs, e.g. the alignment of 100 /// trailing line comments. 101 class WhitespaceManager { 102 public: 103 WhitespaceManager(SourceManager &SourceMgr) : SourceMgr(SourceMgr) {} 104 105 /// \brief Replaces the whitespace in front of \p Tok. Only call once for 106 /// each \c AnnotatedToken. 107 void replaceWhitespace(const AnnotatedToken &Tok, unsigned NewLines, 108 unsigned Spaces, unsigned WhitespaceStartColumn, 109 const FormatStyle &Style) { 110 // 2+ newlines mean an empty line separating logic scopes. 111 if (NewLines >= 2) 112 alignComments(); 113 114 // Align line comments if they are trailing or if they continue other 115 // trailing comments. 116 if (isTrailingComment(Tok) && (Tok.Parent != NULL || !Comments.empty())) { 117 if (Style.ColumnLimit >= 118 Spaces + WhitespaceStartColumn + Tok.FormatTok.TokenLength) { 119 Comments.push_back(StoredComment()); 120 Comments.back().Tok = Tok.FormatTok; 121 Comments.back().Spaces = Spaces; 122 Comments.back().NewLines = NewLines; 123 if (NewLines == 0) 124 Comments.back().MinColumn = WhitespaceStartColumn + Spaces; 125 else 126 Comments.back().MinColumn = Spaces; 127 Comments.back().MaxColumn = 128 Style.ColumnLimit - Spaces - Tok.FormatTok.TokenLength; 129 return; 130 } 131 } 132 133 // If this line does not have a trailing comment, align the stored comments. 134 if (Tok.Children.empty() && !isTrailingComment(Tok)) 135 alignComments(); 136 storeReplacement(Tok.FormatTok, 137 std::string(NewLines, '\n') + std::string(Spaces, ' ')); 138 } 139 140 /// \brief Like \c replaceWhitespace, but additionally adds right-aligned 141 /// backslashes to escape newlines inside a preprocessor directive. 142 /// 143 /// This function and \c replaceWhitespace have the same behavior if 144 /// \c Newlines == 0. 145 void replacePPWhitespace(const AnnotatedToken &Tok, unsigned NewLines, 146 unsigned Spaces, unsigned WhitespaceStartColumn, 147 const FormatStyle &Style) { 148 std::string NewLineText; 149 if (NewLines > 0) { 150 unsigned Offset = 151 std::min<int>(Style.ColumnLimit - 1, WhitespaceStartColumn); 152 for (unsigned i = 0; i < NewLines; ++i) { 153 NewLineText += std::string(Style.ColumnLimit - Offset - 1, ' '); 154 NewLineText += "\\\n"; 155 Offset = 0; 156 } 157 } 158 storeReplacement(Tok.FormatTok, NewLineText + std::string(Spaces, ' ')); 159 } 160 161 /// \brief Returns all the \c Replacements created during formatting. 162 const tooling::Replacements &generateReplacements() { 163 alignComments(); 164 return Replaces; 165 } 166 167 private: 168 /// \brief Structure to store a comment for later layout and alignment. 169 struct StoredComment { 170 FormatToken Tok; 171 unsigned MinColumn; 172 unsigned MaxColumn; 173 unsigned NewLines; 174 unsigned Spaces; 175 }; 176 SmallVector<StoredComment, 16> Comments; 177 typedef SmallVector<StoredComment, 16>::iterator comment_iterator; 178 179 /// \brief Try to align all stashed comments. 180 void alignComments() { 181 unsigned MinColumn = 0; 182 unsigned MaxColumn = UINT_MAX; 183 comment_iterator Start = Comments.begin(); 184 for (comment_iterator I = Comments.begin(), E = Comments.end(); I != E; 185 ++I) { 186 if (I->MinColumn > MaxColumn || I->MaxColumn < MinColumn) { 187 alignComments(Start, I, MinColumn); 188 MinColumn = I->MinColumn; 189 MaxColumn = I->MaxColumn; 190 Start = I; 191 } else { 192 MinColumn = std::max(MinColumn, I->MinColumn); 193 MaxColumn = std::min(MaxColumn, I->MaxColumn); 194 } 195 } 196 alignComments(Start, Comments.end(), MinColumn); 197 Comments.clear(); 198 } 199 200 /// \brief Put all the comments between \p I and \p E into \p Column. 201 void alignComments(comment_iterator I, comment_iterator E, unsigned Column) { 202 while (I != E) { 203 unsigned Spaces = I->Spaces + Column - I->MinColumn; 204 storeReplacement(I->Tok, std::string(I->NewLines, '\n') + 205 std::string(Spaces, ' ')); 206 ++I; 207 } 208 } 209 210 /// \brief Stores \p Text as the replacement for the whitespace in front of 211 /// \p Tok. 212 void storeReplacement(const FormatToken &Tok, const std::string Text) { 213 // Don't create a replacement, if it does not change anything. 214 if (StringRef(SourceMgr.getCharacterData(Tok.WhiteSpaceStart), 215 Tok.WhiteSpaceLength) == Text) 216 return; 217 218 Replaces.insert(tooling::Replacement(SourceMgr, Tok.WhiteSpaceStart, 219 Tok.WhiteSpaceLength, Text)); 220 } 221 222 SourceManager &SourceMgr; 223 tooling::Replacements Replaces; 224 }; 225 226 class UnwrappedLineFormatter { 227 public: 228 UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr, 229 const AnnotatedLine &Line, unsigned FirstIndent, 230 const AnnotatedToken &RootToken, 231 WhitespaceManager &Whitespaces, bool StructuralError) 232 : Style(Style), SourceMgr(SourceMgr), Line(Line), 233 FirstIndent(FirstIndent), RootToken(RootToken), 234 Whitespaces(Whitespaces) { 235 } 236 237 /// \brief Formats an \c UnwrappedLine. 238 /// 239 /// \returns The column after the last token in the last line of the 240 /// \c UnwrappedLine. 241 unsigned format() { 242 // Initialize state dependent on indent. 243 LineState State; 244 State.Column = FirstIndent; 245 State.NextToken = &RootToken; 246 State.Stack.push_back( 247 ParenState(FirstIndent + 4, FirstIndent, !Style.BinPackParameters, 248 /*HasMultiParameterLine=*/ false)); 249 State.VariablePos = 0; 250 State.LineContainsContinuedForLoopSection = false; 251 State.ParenLevel = 0; 252 253 DEBUG({ 254 DebugTokenState(*State.NextToken); 255 }); 256 257 // The first token has already been indented and thus consumed. 258 moveStateToNextToken(State); 259 260 // If everything fits on a single line, just put it there. 261 if (Line.Last->TotalLength <= getColumnLimit() - FirstIndent) { 262 while (State.NextToken != NULL) { 263 addTokenToState(false, false, State); 264 } 265 return State.Column; 266 } 267 268 // If the ObjC method declaration does not fit on a line, we should format 269 // it with one arg per line. 270 if (Line.Type == LT_ObjCMethodDecl) 271 State.Stack.back().BreakBeforeParameter = true; 272 273 // Find best solution in solution space. 274 return analyzeSolutionSpace(State); 275 } 276 277 private: 278 void DebugTokenState(const AnnotatedToken &AnnotatedTok) { 279 const Token &Tok = AnnotatedTok.FormatTok.Tok; 280 llvm::errs() << StringRef(SourceMgr.getCharacterData(Tok.getLocation()), 281 Tok.getLength()); 282 llvm::errs(); 283 } 284 285 struct ParenState { 286 ParenState(unsigned Indent, unsigned LastSpace, bool AvoidBinPacking, 287 bool HasMultiParameterLine) 288 : Indent(Indent), LastSpace(LastSpace), FirstLessLess(0), 289 BreakBeforeClosingBrace(false), QuestionColumn(0), 290 AvoidBinPacking(AvoidBinPacking), BreakBeforeParameter(false), 291 HasMultiParameterLine(HasMultiParameterLine), ColonPos(0) { 292 } 293 294 /// \brief The position to which a specific parenthesis level needs to be 295 /// indented. 296 unsigned Indent; 297 298 /// \brief The position of the last space on each level. 299 /// 300 /// Used e.g. to break like: 301 /// functionCall(Parameter, otherCall( 302 /// OtherParameter)); 303 unsigned LastSpace; 304 305 /// \brief The position the first "<<" operator encountered on each level. 306 /// 307 /// Used to align "<<" operators. 0 if no such operator has been encountered 308 /// on a level. 309 unsigned FirstLessLess; 310 311 /// \brief Whether a newline needs to be inserted before the block's closing 312 /// brace. 313 /// 314 /// We only want to insert a newline before the closing brace if there also 315 /// was a newline after the beginning left brace. 316 bool BreakBeforeClosingBrace; 317 318 /// \brief The column of a \c ? in a conditional expression; 319 unsigned QuestionColumn; 320 321 /// \brief Avoid bin packing, i.e. multiple parameters/elements on multiple 322 /// lines, in this context. 323 bool AvoidBinPacking; 324 325 /// \brief Break after the next comma (or all the commas in this context if 326 /// \c AvoidBinPacking is \c true). 327 bool BreakBeforeParameter; 328 329 /// \brief This context already has a line with more than one parameter. 330 bool HasMultiParameterLine; 331 332 /// \brief The position of the colon in an ObjC method declaration/call. 333 unsigned ColonPos; 334 335 bool operator<(const ParenState &Other) const { 336 if (Indent != Other.Indent) 337 return Indent < Other.Indent; 338 if (LastSpace != Other.LastSpace) 339 return LastSpace < Other.LastSpace; 340 if (FirstLessLess != Other.FirstLessLess) 341 return FirstLessLess < Other.FirstLessLess; 342 if (BreakBeforeClosingBrace != Other.BreakBeforeClosingBrace) 343 return BreakBeforeClosingBrace; 344 if (QuestionColumn != Other.QuestionColumn) 345 return QuestionColumn < Other.QuestionColumn; 346 if (AvoidBinPacking != Other.AvoidBinPacking) 347 return AvoidBinPacking; 348 if (BreakBeforeParameter != Other.BreakBeforeParameter) 349 return BreakBeforeParameter; 350 if (HasMultiParameterLine != Other.HasMultiParameterLine) 351 return HasMultiParameterLine; 352 if (ColonPos != Other.ColonPos) 353 return ColonPos < Other.ColonPos; 354 return false; 355 } 356 }; 357 358 /// \brief The current state when indenting a unwrapped line. 359 /// 360 /// As the indenting tries different combinations this is copied by value. 361 struct LineState { 362 /// \brief The number of used columns in the current line. 363 unsigned Column; 364 365 /// \brief The token that needs to be next formatted. 366 const AnnotatedToken *NextToken; 367 368 /// \brief The column of the first variable name in a variable declaration. 369 /// 370 /// Used to align further variables if necessary. 371 unsigned VariablePos; 372 373 /// \brief \c true if this line contains a continued for-loop section. 374 bool LineContainsContinuedForLoopSection; 375 376 /// \brief The level of nesting inside (), [], <> and {}. 377 unsigned ParenLevel; 378 379 /// \brief A stack keeping track of properties applying to parenthesis 380 /// levels. 381 std::vector<ParenState> Stack; 382 383 /// \brief Comparison operator to be able to used \c LineState in \c map. 384 bool operator<(const LineState &Other) const { 385 if (Other.NextToken != NextToken) 386 return Other.NextToken > NextToken; 387 if (Other.Column != Column) 388 return Other.Column > Column; 389 if (Other.VariablePos != VariablePos) 390 return Other.VariablePos < VariablePos; 391 if (Other.LineContainsContinuedForLoopSection != 392 LineContainsContinuedForLoopSection) 393 return LineContainsContinuedForLoopSection; 394 if (Other.ParenLevel != ParenLevel) 395 return Other.ParenLevel < ParenLevel; 396 return Other.Stack < Stack; 397 } 398 }; 399 400 /// \brief Appends the next token to \p State and updates information 401 /// necessary for indentation. 402 /// 403 /// Puts the token on the current line if \p Newline is \c true and adds a 404 /// line break and necessary indentation otherwise. 405 /// 406 /// If \p DryRun is \c false, also creates and stores the required 407 /// \c Replacement. 408 void addTokenToState(bool Newline, bool DryRun, LineState &State) { 409 const AnnotatedToken &Current = *State.NextToken; 410 const AnnotatedToken &Previous = *State.NextToken->Parent; 411 assert(State.Stack.size()); 412 413 if (Current.Type == TT_ImplicitStringLiteral) { 414 State.Column += State.NextToken->FormatTok.WhiteSpaceLength + 415 State.NextToken->FormatTok.TokenLength; 416 if (State.NextToken->Children.empty()) 417 State.NextToken = NULL; 418 else 419 State.NextToken = &State.NextToken->Children[0]; 420 return; 421 } 422 423 if (Newline) { 424 unsigned WhitespaceStartColumn = State.Column; 425 if (Current.is(tok::r_brace)) { 426 State.Column = Line.Level * 2; 427 } else if (Current.is(tok::string_literal) && 428 Previous.is(tok::string_literal)) { 429 State.Column = State.Column - Previous.FormatTok.TokenLength; 430 } else if (Current.is(tok::lessless) && 431 State.Stack.back().FirstLessLess != 0) { 432 State.Column = State.Stack.back().FirstLessLess; 433 } else if (State.ParenLevel != 0 && 434 (Previous.is(tok::equal) || Previous.is(tok::coloncolon) || 435 Current.is(tok::period) || Current.is(tok::arrow) || 436 Current.is(tok::question))) { 437 // Indent and extra 4 spaces after if we know the current expression is 438 // continued. Don't do that on the top level, as we already indent 4 439 // there. 440 State.Column = std::max(State.Stack.back().LastSpace, 441 State.Stack.back().Indent) + 4; 442 } else if (Current.Type == TT_ConditionalExpr) { 443 State.Column = State.Stack.back().QuestionColumn; 444 } else if (Previous.is(tok::comma) && State.VariablePos != 0 && 445 ((RootToken.is(tok::kw_for) && State.ParenLevel == 1) || 446 State.ParenLevel == 0)) { 447 State.Column = State.VariablePos; 448 } else if (State.NextToken->Parent->ClosesTemplateDeclaration || 449 Current.Type == TT_StartOfName) { 450 State.Column = State.Stack.back().Indent - 4; 451 } else if (Current.Type == TT_ObjCSelectorName) { 452 if (State.Stack.back().ColonPos > Current.FormatTok.TokenLength) { 453 State.Column = 454 State.Stack.back().ColonPos - Current.FormatTok.TokenLength; 455 } else { 456 State.Column = State.Stack.back().Indent; 457 State.Stack.back().ColonPos = 458 State.Column + Current.FormatTok.TokenLength; 459 } 460 } else if (Previous.Type == TT_ObjCMethodExpr) { 461 State.Column = State.Stack.back().Indent + 4; 462 } else { 463 State.Column = State.Stack.back().Indent; 464 } 465 466 if (Previous.is(tok::comma) && !State.Stack.back().AvoidBinPacking) 467 State.Stack.back().BreakBeforeParameter = false; 468 469 if (RootToken.is(tok::kw_for)) 470 State.LineContainsContinuedForLoopSection = Previous.isNot(tok::semi); 471 472 if (!DryRun) { 473 if (!Line.InPPDirective) 474 Whitespaces.replaceWhitespace(Current, 1, State.Column, 475 WhitespaceStartColumn, Style); 476 else 477 Whitespaces.replacePPWhitespace(Current, 1, State.Column, 478 WhitespaceStartColumn, Style); 479 } 480 481 State.Stack.back().LastSpace = State.Column; 482 if (Current.is(tok::colon) && Current.Type != TT_ConditionalExpr) 483 State.Stack.back().Indent += 2; 484 } else { 485 if (Current.is(tok::equal) && 486 (RootToken.is(tok::kw_for) || State.ParenLevel == 0)) 487 State.VariablePos = State.Column - Previous.FormatTok.TokenLength; 488 489 unsigned Spaces = State.NextToken->SpacesRequiredBefore; 490 491 if (!DryRun) 492 Whitespaces.replaceWhitespace(Current, 0, Spaces, State.Column, Style); 493 494 if (Current.Type == TT_ObjCSelectorName && 495 State.Stack.back().ColonPos == 0) { 496 if (State.Stack.back().Indent + Current.LongestObjCSelectorName > 497 State.Column + Spaces + Current.FormatTok.TokenLength) 498 State.Stack.back().ColonPos = 499 State.Stack.back().Indent + Current.LongestObjCSelectorName; 500 else 501 State.Stack.back().ColonPos = 502 State.Column + Spaces + Current.FormatTok.TokenLength; 503 } 504 505 if (Current.Type != TT_LineComment && 506 (Previous.is(tok::l_paren) || Previous.is(tok::l_brace) || 507 State.NextToken->Parent->Type == TT_TemplateOpener)) 508 State.Stack.back().Indent = State.Column + Spaces; 509 if (Previous.is(tok::comma) && !isTrailingComment(Current)) 510 State.Stack.back().HasMultiParameterLine = true; 511 512 State.Column += Spaces; 513 if (Current.is(tok::l_paren) && Previous.is(tok::kw_if)) 514 // Treat the condition inside an if as if it was a second function 515 // parameter, i.e. let nested calls have an indent of 4. 516 State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(". 517 else if (Previous.is(tok::comma) && State.ParenLevel != 0) 518 // Top-level spaces are exempt as that mostly leads to better results. 519 State.Stack.back().LastSpace = State.Column; 520 else if ((Previous.Type == TT_BinaryOperator || 521 Previous.Type == TT_ConditionalExpr || 522 Previous.Type == TT_CtorInitializerColon) && 523 getPrecedence(Previous) != prec::Assignment) 524 State.Stack.back().LastSpace = State.Column; 525 else if (Previous.ParameterCount > 1 && 526 (Previous.is(tok::l_paren) || Previous.is(tok::l_square) || 527 Previous.is(tok::l_brace) || 528 Previous.Type == TT_TemplateOpener)) 529 // If this function has multiple parameters, indent nested calls from 530 // the start of the first parameter. 531 State.Stack.back().LastSpace = State.Column; 532 } 533 534 // If we break after an {, we should also break before the corresponding }. 535 if (Newline && Previous.is(tok::l_brace)) 536 State.Stack.back().BreakBeforeClosingBrace = true; 537 538 if (State.Stack.back().AvoidBinPacking && Newline && 539 (Line.First.isNot(tok::kw_for) || State.ParenLevel != 1)) { 540 // If we are breaking after '(', '{', '<', this is not bin packing unless 541 // AllowAllParametersOfDeclarationOnNextLine is false. 542 if ((Previous.isNot(tok::l_paren) && Previous.isNot(tok::l_brace) && 543 Previous.Type != TT_TemplateOpener) || 544 (!Style.AllowAllParametersOfDeclarationOnNextLine && 545 Line.MustBeDeclaration)) 546 State.Stack.back().BreakBeforeParameter = true; 547 548 // Any break on this level means that the parent level has been broken 549 // and we need to avoid bin packing there. 550 for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) { 551 if (Line.First.isNot(tok::kw_for) || i != 1) 552 State.Stack[i].BreakBeforeParameter = true; 553 } 554 } 555 556 moveStateToNextToken(State); 557 } 558 559 /// \brief Mark the next token as consumed in \p State and modify its stacks 560 /// accordingly. 561 void moveStateToNextToken(LineState &State) { 562 const AnnotatedToken &Current = *State.NextToken; 563 assert(State.Stack.size()); 564 565 if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0) 566 State.Stack.back().FirstLessLess = State.Column; 567 if (Current.is(tok::question)) 568 State.Stack.back().QuestionColumn = State.Column; 569 if (Current.is(tok::l_brace) && Current.MatchingParen != NULL && 570 !Current.MatchingParen->MustBreakBefore) { 571 if (getLengthToMatchingParen(Current) + State.Column > getColumnLimit()) 572 State.Stack.back().BreakBeforeParameter = true; 573 } 574 575 // Insert scopes created by fake parenthesis. 576 for (unsigned i = 0, e = Current.FakeLParens; i != e; ++i) { 577 ParenState NewParenState = State.Stack.back(); 578 NewParenState.Indent = std::max(State.Column, State.Stack.back().Indent); 579 State.Stack.push_back(NewParenState); 580 } 581 582 // If we encounter an opening (, [, { or <, we add a level to our stacks to 583 // prepare for the following tokens. 584 if (Current.is(tok::l_paren) || Current.is(tok::l_square) || 585 Current.is(tok::l_brace) || 586 State.NextToken->Type == TT_TemplateOpener) { 587 unsigned NewIndent; 588 bool AvoidBinPacking; 589 if (Current.is(tok::l_brace)) { 590 NewIndent = 2 + State.Stack.back().LastSpace; 591 AvoidBinPacking = false; 592 } else { 593 NewIndent = 4 + State.Stack.back().LastSpace; 594 AvoidBinPacking = !Style.BinPackParameters; 595 } 596 State.Stack.push_back( 597 ParenState(NewIndent, State.Stack.back().LastSpace, AvoidBinPacking, 598 State.Stack.back().HasMultiParameterLine)); 599 ++State.ParenLevel; 600 } 601 602 // If this '[' opens an ObjC call, determine whether all parameters fit into 603 // one line and put one per line if they don't. 604 if (Current.is(tok::l_square) && Current.Type == TT_ObjCMethodExpr && 605 Current.MatchingParen != NULL) { 606 if (getLengthToMatchingParen(Current) + State.Column > getColumnLimit()) 607 State.Stack.back().BreakBeforeParameter = true; 608 } 609 610 // If we encounter a closing ), ], } or >, we can remove a level from our 611 // stacks. 612 if (Current.is(tok::r_paren) || Current.is(tok::r_square) || 613 (Current.is(tok::r_brace) && State.NextToken != &RootToken) || 614 State.NextToken->Type == TT_TemplateCloser) { 615 State.Stack.pop_back(); 616 --State.ParenLevel; 617 } 618 619 // Remove scopes created by fake parenthesis. 620 for (unsigned i = 0, e = Current.FakeRParens; i != e; ++i) { 621 State.Stack.pop_back(); 622 } 623 624 if (State.NextToken->Children.empty()) 625 State.NextToken = NULL; 626 else 627 State.NextToken = &State.NextToken->Children[0]; 628 629 State.Column += Current.FormatTok.TokenLength; 630 } 631 632 unsigned getColumnLimit() { 633 return Style.ColumnLimit - (Line.InPPDirective ? 1 : 0); 634 } 635 636 /// \brief An edge in the solution space starting from the \c LineState and 637 /// inserting a newline dependent on the \c bool. 638 typedef std::pair<bool, const LineState *> Edge; 639 640 /// \brief An item in the prioritized BFS search queue. The \c LineState was 641 /// reached using the \c Edge. 642 typedef std::pair<LineState, Edge> QueueItem; 643 644 /// \brief Analyze the entire solution space starting from \p InitialState. 645 /// 646 /// This implements a variant of Dijkstra's algorithm on the graph that spans 647 /// the solution space (\c LineStates are the nodes). The algorithm tries to 648 /// find the shortest path (the one with lowest penalty) from \p InitialState 649 /// to a state where all tokens are placed. 650 unsigned analyzeSolutionSpace(const LineState &InitialState) { 651 // Insert start element into queue. 652 std::multimap<unsigned, QueueItem> Queue; 653 Queue.insert(std::pair<unsigned, QueueItem>( 654 0, QueueItem(InitialState, Edge(false, (const LineState *)0)))); 655 std::map<LineState, Edge> Seen; 656 657 // While not empty, take first element and follow edges. 658 while (!Queue.empty()) { 659 unsigned Penalty = Queue.begin()->first; 660 QueueItem Item = Queue.begin()->second; 661 if (Item.first.NextToken == NULL) { 662 DEBUG(llvm::errs() << "\n---\nPenalty for line: " << Penalty << "\n"); 663 break; 664 } 665 Queue.erase(Queue.begin()); 666 667 if (Seen.find(Item.first) != Seen.end()) 668 continue; // State already examined with lower penalty. 669 670 const LineState &SavedState = Seen.insert(std::pair<LineState, Edge>( 671 Item.first, 672 Edge(Item.second.first, Item.second.second))).first->first; 673 674 addNextStateToQueue(SavedState, Penalty, /*NewLine=*/ false, Queue); 675 addNextStateToQueue(SavedState, Penalty, /*NewLine=*/ true, Queue); 676 } 677 678 if (Queue.empty()) 679 // We were unable to find a solution, do nothing. 680 // FIXME: Add diagnostic? 681 return 0; 682 683 // Reconstruct the solution. 684 // FIXME: Add debugging output. 685 Edge *CurrentEdge = &Queue.begin()->second.second; 686 while (CurrentEdge->second != NULL) { 687 LineState CurrentState = *CurrentEdge->second; 688 if (CurrentEdge->first) { 689 DEBUG(llvm::errs() << "Penalty for splitting before " 690 << CurrentState.NextToken->FormatTok.Tok.getName() 691 << ": " << CurrentState.NextToken->SplitPenalty 692 << "\n"); 693 } 694 addTokenToState(CurrentEdge->first, false, CurrentState); 695 CurrentEdge = &Seen[*CurrentEdge->second]; 696 } 697 DEBUG(llvm::errs() << "---\n"); 698 699 // Return the column after the last token of the solution. 700 return Queue.begin()->second.first.Column; 701 } 702 703 /// \brief Add the following state to the analysis queue \p Queue. 704 /// 705 /// Assume the current state is \p OldState and has been reached with a 706 /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true. 707 void addNextStateToQueue(const LineState &OldState, unsigned Penalty, 708 bool NewLine, 709 std::multimap<unsigned, QueueItem> &Queue) { 710 if (NewLine && !canBreak(OldState)) 711 return; 712 if (!NewLine && mustBreak(OldState)) 713 return; 714 LineState State(OldState); 715 if (NewLine) 716 Penalty += State.NextToken->SplitPenalty; 717 addTokenToState(NewLine, true, State); 718 if (State.Column > getColumnLimit()) { 719 unsigned ExcessCharacters = State.Column - getColumnLimit(); 720 Penalty += Style.PenaltyExcessCharacter * ExcessCharacters; 721 } 722 Queue.insert(std::pair<unsigned, QueueItem>( 723 Penalty, QueueItem(State, Edge(NewLine, &OldState)))); 724 } 725 726 /// \brief Returns \c true, if a line break after \p State is allowed. 727 bool canBreak(const LineState &State) { 728 if (!State.NextToken->CanBreakBefore && 729 !(State.NextToken->is(tok::r_brace) && 730 State.Stack.back().BreakBeforeClosingBrace)) 731 return false; 732 // Trying to insert a parameter on a new line if there are already more than 733 // one parameter on the current line is bin packing. 734 if (State.Stack.back().HasMultiParameterLine && 735 State.Stack.back().AvoidBinPacking) 736 return false; 737 return true; 738 } 739 740 /// \brief Returns \c true, if a line break after \p State is mandatory. 741 bool mustBreak(const LineState &State) { 742 if (State.NextToken->MustBreakBefore) 743 return true; 744 if (State.NextToken->is(tok::r_brace) && 745 State.Stack.back().BreakBeforeClosingBrace) 746 return true; 747 if (State.NextToken->Parent->is(tok::semi) && 748 State.LineContainsContinuedForLoopSection) 749 return true; 750 if (State.NextToken->Parent->is(tok::comma) && 751 State.Stack.back().BreakBeforeParameter && 752 !isTrailingComment(*State.NextToken)) 753 return true; 754 // FIXME: Comparing LongestObjCSelectorName to 0 is a hacky way of finding 755 // out whether it is the first parameter. Clean this up. 756 if (State.NextToken->Type == TT_ObjCSelectorName && 757 State.NextToken->LongestObjCSelectorName == 0 && 758 State.Stack.back().BreakBeforeParameter) 759 return true; 760 if ((State.NextToken->Type == TT_CtorInitializerColon || 761 (State.NextToken->Parent->ClosesTemplateDeclaration && 762 State.ParenLevel == 0))) 763 return true; 764 return false; 765 } 766 767 FormatStyle Style; 768 SourceManager &SourceMgr; 769 const AnnotatedLine &Line; 770 const unsigned FirstIndent; 771 const AnnotatedToken &RootToken; 772 WhitespaceManager &Whitespaces; 773 }; 774 775 class LexerBasedFormatTokenSource : public FormatTokenSource { 776 public: 777 LexerBasedFormatTokenSource(Lexer &Lex, SourceManager &SourceMgr) 778 : GreaterStashed(false), Lex(Lex), SourceMgr(SourceMgr), 779 IdentTable(Lex.getLangOpts()) { 780 Lex.SetKeepWhitespaceMode(true); 781 } 782 783 virtual FormatToken getNextToken() { 784 if (GreaterStashed) { 785 FormatTok.NewlinesBefore = 0; 786 FormatTok.WhiteSpaceStart = 787 FormatTok.Tok.getLocation().getLocWithOffset(1); 788 FormatTok.WhiteSpaceLength = 0; 789 GreaterStashed = false; 790 return FormatTok; 791 } 792 793 FormatTok = FormatToken(); 794 Lex.LexFromRawLexer(FormatTok.Tok); 795 StringRef Text = rawTokenText(FormatTok.Tok); 796 FormatTok.WhiteSpaceStart = FormatTok.Tok.getLocation(); 797 if (SourceMgr.getFileOffset(FormatTok.WhiteSpaceStart) == 0) 798 FormatTok.IsFirst = true; 799 800 // Consume and record whitespace until we find a significant token. 801 while (FormatTok.Tok.is(tok::unknown)) { 802 unsigned Newlines = Text.count('\n'); 803 unsigned EscapedNewlines = Text.count("\\\n"); 804 FormatTok.NewlinesBefore += Newlines; 805 FormatTok.HasUnescapedNewline |= EscapedNewlines != Newlines; 806 FormatTok.WhiteSpaceLength += FormatTok.Tok.getLength(); 807 808 if (FormatTok.Tok.is(tok::eof)) 809 return FormatTok; 810 Lex.LexFromRawLexer(FormatTok.Tok); 811 Text = rawTokenText(FormatTok.Tok); 812 } 813 814 // Now FormatTok is the next non-whitespace token. 815 FormatTok.TokenLength = Text.size(); 816 817 // In case the token starts with escaped newlines, we want to 818 // take them into account as whitespace - this pattern is quite frequent 819 // in macro definitions. 820 // FIXME: What do we want to do with other escaped spaces, and escaped 821 // spaces or newlines in the middle of tokens? 822 // FIXME: Add a more explicit test. 823 unsigned i = 0; 824 while (i + 1 < Text.size() && Text[i] == '\\' && Text[i + 1] == '\n') { 825 // FIXME: ++FormatTok.NewlinesBefore is missing... 826 FormatTok.WhiteSpaceLength += 2; 827 FormatTok.TokenLength -= 2; 828 i += 2; 829 } 830 831 if (FormatTok.Tok.is(tok::raw_identifier)) { 832 IdentifierInfo &Info = IdentTable.get(Text); 833 FormatTok.Tok.setIdentifierInfo(&Info); 834 FormatTok.Tok.setKind(Info.getTokenID()); 835 } 836 837 if (FormatTok.Tok.is(tok::greatergreater)) { 838 FormatTok.Tok.setKind(tok::greater); 839 GreaterStashed = true; 840 } 841 842 return FormatTok; 843 } 844 845 IdentifierTable &getIdentTable() { return IdentTable; } 846 847 private: 848 FormatToken FormatTok; 849 bool GreaterStashed; 850 Lexer &Lex; 851 SourceManager &SourceMgr; 852 IdentifierTable IdentTable; 853 854 /// Returns the text of \c FormatTok. 855 StringRef rawTokenText(Token &Tok) { 856 return StringRef(SourceMgr.getCharacterData(Tok.getLocation()), 857 Tok.getLength()); 858 } 859 }; 860 861 class Formatter : public UnwrappedLineConsumer { 862 public: 863 Formatter(DiagnosticsEngine &Diag, const FormatStyle &Style, Lexer &Lex, 864 SourceManager &SourceMgr, 865 const std::vector<CharSourceRange> &Ranges) 866 : Diag(Diag), Style(Style), Lex(Lex), SourceMgr(SourceMgr), 867 Whitespaces(SourceMgr), Ranges(Ranges) { 868 } 869 870 virtual ~Formatter() {} 871 872 void deriveLocalStyle() { 873 unsigned CountBoundToVariable = 0; 874 unsigned CountBoundToType = 0; 875 bool HasCpp03IncompatibleFormat = false; 876 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { 877 if (AnnotatedLines[i].First.Children.empty()) 878 continue; 879 AnnotatedToken *Tok = &AnnotatedLines[i].First.Children[0]; 880 while (!Tok->Children.empty()) { 881 if (Tok->Type == TT_PointerOrReference) { 882 bool SpacesBefore = Tok->FormatTok.WhiteSpaceLength > 0; 883 bool SpacesAfter = Tok->Children[0].FormatTok.WhiteSpaceLength > 0; 884 if (SpacesBefore && !SpacesAfter) 885 ++CountBoundToVariable; 886 else if (!SpacesBefore && SpacesAfter) 887 ++CountBoundToType; 888 } 889 890 if (Tok->Type == TT_TemplateCloser && 891 Tok->Parent->Type == TT_TemplateCloser && 892 Tok->FormatTok.WhiteSpaceLength == 0) 893 HasCpp03IncompatibleFormat = true; 894 Tok = &Tok->Children[0]; 895 } 896 } 897 if (Style.DerivePointerBinding) { 898 if (CountBoundToType > CountBoundToVariable) 899 Style.PointerBindsToType = true; 900 else if (CountBoundToType < CountBoundToVariable) 901 Style.PointerBindsToType = false; 902 } 903 if (Style.Standard == FormatStyle::LS_Auto) { 904 Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11 905 : FormatStyle::LS_Cpp03; 906 } 907 } 908 909 tooling::Replacements format() { 910 LexerBasedFormatTokenSource Tokens(Lex, SourceMgr); 911 UnwrappedLineParser Parser(Diag, Style, Tokens, *this); 912 StructuralError = Parser.parse(); 913 unsigned PreviousEndOfLineColumn = 0; 914 TokenAnnotator Annotator(Style, SourceMgr, Lex, 915 Tokens.getIdentTable().get("in")); 916 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { 917 Annotator.annotate(AnnotatedLines[i]); 918 } 919 deriveLocalStyle(); 920 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { 921 Annotator.calculateFormattingInformation(AnnotatedLines[i]); 922 } 923 std::vector<int> IndentForLevel; 924 for (std::vector<AnnotatedLine>::iterator I = AnnotatedLines.begin(), 925 E = AnnotatedLines.end(); 926 I != E; ++I) { 927 const AnnotatedLine &TheLine = *I; 928 int Offset = GetIndentOffset(TheLine.First); 929 while (IndentForLevel.size() <= TheLine.Level) 930 IndentForLevel.push_back(-1); 931 IndentForLevel.resize(TheLine.Level + 1); 932 if (touchesRanges(TheLine) && TheLine.Type != LT_Invalid) { 933 unsigned LevelIndent = GetIndent(IndentForLevel, TheLine.Level); 934 unsigned Indent = LevelIndent; 935 if (static_cast<int>(Indent) + Offset >= 0) 936 Indent += Offset; 937 if (!TheLine.First.FormatTok.WhiteSpaceStart.isValid() || 938 StructuralError) { 939 Indent = LevelIndent = SourceMgr.getSpellingColumnNumber( 940 TheLine.First.FormatTok.Tok.getLocation()) - 1; 941 } else { 942 formatFirstToken(TheLine.First, Indent, TheLine.InPPDirective, 943 PreviousEndOfLineColumn); 944 } 945 tryFitMultipleLinesInOne(Indent, I, E); 946 UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine, Indent, 947 TheLine.First, Whitespaces, 948 StructuralError); 949 PreviousEndOfLineColumn = Formatter.format(); 950 IndentForLevel[TheLine.Level] = LevelIndent; 951 } else { 952 // If we did not reformat this unwrapped line, the column at the end of 953 // the last token is unchanged - thus, we can calculate the end of the 954 // last token. 955 PreviousEndOfLineColumn = 956 SourceMgr.getSpellingColumnNumber( 957 TheLine.Last->FormatTok.Tok.getLocation()) + 958 Lex.MeasureTokenLength(TheLine.Last->FormatTok.Tok.getLocation(), 959 SourceMgr, Lex.getLangOpts()) - 1; 960 if (TheLine.First.FormatTok.NewlinesBefore > 0 || 961 TheLine.First.FormatTok.IsFirst) { 962 unsigned Indent = SourceMgr.getSpellingColumnNumber( 963 TheLine.First.FormatTok.Tok.getLocation()) - 1; 964 unsigned LevelIndent = Indent; 965 if (static_cast<int>(LevelIndent) - Offset >= 0) 966 LevelIndent -= Offset; 967 IndentForLevel[TheLine.Level] = LevelIndent; 968 } 969 } 970 } 971 return Whitespaces.generateReplacements(); 972 } 973 974 private: 975 /// \brief Get the indent of \p Level from \p IndentForLevel. 976 /// 977 /// \p IndentForLevel must contain the indent for the level \c l 978 /// at \p IndentForLevel[l], or a value < 0 if the indent for 979 /// that level is unknown. 980 unsigned GetIndent(const std::vector<int> IndentForLevel, 981 unsigned Level) { 982 if (IndentForLevel[Level] != -1) 983 return IndentForLevel[Level]; 984 if (Level == 0) 985 return 0; 986 return GetIndent(IndentForLevel, Level - 1) + 2; 987 } 988 989 /// \brief Get the offset of the line relatively to the level. 990 /// 991 /// For example, 'public:' labels in classes are offset by 1 or 2 992 /// characters to the left from their level. 993 int GetIndentOffset(const AnnotatedToken &RootToken) { 994 bool IsAccessModifier = false; 995 if (RootToken.is(tok::kw_public) || RootToken.is(tok::kw_protected) || 996 RootToken.is(tok::kw_private)) 997 IsAccessModifier = true; 998 else if (RootToken.is(tok::at) && !RootToken.Children.empty() && 999 (RootToken.Children[0].isObjCAtKeyword(tok::objc_public) || 1000 RootToken.Children[0].isObjCAtKeyword(tok::objc_protected) || 1001 RootToken.Children[0].isObjCAtKeyword(tok::objc_package) || 1002 RootToken.Children[0].isObjCAtKeyword(tok::objc_private))) 1003 IsAccessModifier = true; 1004 1005 if (IsAccessModifier) 1006 return Style.AccessModifierOffset; 1007 return 0; 1008 } 1009 1010 /// \brief Tries to merge lines into one. 1011 /// 1012 /// This will change \c Line and \c AnnotatedLine to contain the merged line, 1013 /// if possible; note that \c I will be incremented when lines are merged. 1014 /// 1015 /// Returns whether the resulting \c Line can fit in a single line. 1016 void tryFitMultipleLinesInOne(unsigned Indent, 1017 std::vector<AnnotatedLine>::iterator &I, 1018 std::vector<AnnotatedLine>::iterator E) { 1019 unsigned Limit = Style.ColumnLimit - (I->InPPDirective ? 1 : 0) - Indent; 1020 1021 // We can never merge stuff if there are trailing line comments. 1022 if (I->Last->Type == TT_LineComment) 1023 return; 1024 1025 // Check whether the UnwrappedLine can be put onto a single line. If 1026 // so, this is bound to be the optimal solution (by definition) and we 1027 // don't need to analyze the entire solution space. 1028 if (I->Last->TotalLength > Limit) 1029 return; 1030 Limit -= I->Last->TotalLength; 1031 1032 if (I + 1 == E || (I + 1)->Type == LT_Invalid) 1033 return; 1034 1035 if (I->Last->is(tok::l_brace)) { 1036 tryMergeSimpleBlock(I, E, Limit); 1037 } else if (I->First.is(tok::kw_if)) { 1038 tryMergeSimpleIf(I, E, Limit); 1039 } else if (I->InPPDirective && (I->First.FormatTok.HasUnescapedNewline || 1040 I->First.FormatTok.IsFirst)) { 1041 tryMergeSimplePPDirective(I, E, Limit); 1042 } 1043 return; 1044 } 1045 1046 void tryMergeSimplePPDirective(std::vector<AnnotatedLine>::iterator &I, 1047 std::vector<AnnotatedLine>::iterator E, 1048 unsigned Limit) { 1049 AnnotatedLine &Line = *I; 1050 if (!(I + 1)->InPPDirective || (I + 1)->First.FormatTok.HasUnescapedNewline) 1051 return; 1052 if (I + 2 != E && (I + 2)->InPPDirective && 1053 !(I + 2)->First.FormatTok.HasUnescapedNewline) 1054 return; 1055 if (1 + (I + 1)->Last->TotalLength > Limit) 1056 return; 1057 join(Line, *(++I)); 1058 } 1059 1060 void tryMergeSimpleIf(std::vector<AnnotatedLine>::iterator &I, 1061 std::vector<AnnotatedLine>::iterator E, 1062 unsigned Limit) { 1063 if (!Style.AllowShortIfStatementsOnASingleLine) 1064 return; 1065 if ((I + 1)->InPPDirective != I->InPPDirective || 1066 ((I + 1)->InPPDirective && 1067 (I + 1)->First.FormatTok.HasUnescapedNewline)) 1068 return; 1069 AnnotatedLine &Line = *I; 1070 if (Line.Last->isNot(tok::r_paren)) 1071 return; 1072 if (1 + (I + 1)->Last->TotalLength > Limit) 1073 return; 1074 if ((I + 1)->First.is(tok::kw_if) || (I + 1)->First.Type == TT_LineComment) 1075 return; 1076 // Only inline simple if's (no nested if or else). 1077 if (I + 2 != E && (I + 2)->First.is(tok::kw_else)) 1078 return; 1079 join(Line, *(++I)); 1080 } 1081 1082 void tryMergeSimpleBlock(std::vector<AnnotatedLine>::iterator &I, 1083 std::vector<AnnotatedLine>::iterator E, 1084 unsigned Limit) { 1085 // First, check that the current line allows merging. This is the case if 1086 // we're not in a control flow statement and the last token is an opening 1087 // brace. 1088 AnnotatedLine &Line = *I; 1089 bool AllowedTokens = 1090 Line.First.isNot(tok::kw_if) && Line.First.isNot(tok::kw_while) && 1091 Line.First.isNot(tok::kw_do) && Line.First.isNot(tok::r_brace) && 1092 Line.First.isNot(tok::kw_else) && Line.First.isNot(tok::kw_try) && 1093 Line.First.isNot(tok::kw_catch) && Line.First.isNot(tok::kw_for) && 1094 // This gets rid of all ObjC @ keywords and methods. 1095 Line.First.isNot(tok::at) && Line.First.isNot(tok::minus) && 1096 Line.First.isNot(tok::plus); 1097 if (!AllowedTokens) 1098 return; 1099 1100 AnnotatedToken *Tok = &(I + 1)->First; 1101 if (Tok->Children.empty() && Tok->is(tok::r_brace) && 1102 !Tok->MustBreakBefore && Tok->TotalLength <= Limit) { 1103 Tok->SpacesRequiredBefore = 0; 1104 join(Line, *(I + 1)); 1105 I += 1; 1106 } else { 1107 // Check that we still have three lines and they fit into the limit. 1108 if (I + 2 == E || (I + 2)->Type == LT_Invalid || 1109 !nextTwoLinesFitInto(I, Limit)) 1110 return; 1111 1112 // Second, check that the next line does not contain any braces - if it 1113 // does, readability declines when putting it into a single line. 1114 if ((I + 1)->Last->Type == TT_LineComment || Tok->MustBreakBefore) 1115 return; 1116 do { 1117 if (Tok->is(tok::l_brace) || Tok->is(tok::r_brace)) 1118 return; 1119 Tok = Tok->Children.empty() ? NULL : &Tok->Children.back(); 1120 } while (Tok != NULL); 1121 1122 // Last, check that the third line contains a single closing brace. 1123 Tok = &(I + 2)->First; 1124 if (!Tok->Children.empty() || Tok->isNot(tok::r_brace) || 1125 Tok->MustBreakBefore) 1126 return; 1127 1128 join(Line, *(I + 1)); 1129 join(Line, *(I + 2)); 1130 I += 2; 1131 } 1132 } 1133 1134 bool nextTwoLinesFitInto(std::vector<AnnotatedLine>::iterator I, 1135 unsigned Limit) { 1136 return 1 + (I + 1)->Last->TotalLength + 1 + (I + 2)->Last->TotalLength <= 1137 Limit; 1138 } 1139 1140 void join(AnnotatedLine &A, const AnnotatedLine &B) { 1141 A.Last->Children.push_back(B.First); 1142 while (!A.Last->Children.empty()) { 1143 A.Last->Children[0].Parent = A.Last; 1144 A.Last = &A.Last->Children[0]; 1145 } 1146 } 1147 1148 bool touchesRanges(const AnnotatedLine &TheLine) { 1149 const FormatToken *First = &TheLine.First.FormatTok; 1150 const FormatToken *Last = &TheLine.Last->FormatTok; 1151 CharSourceRange LineRange = CharSourceRange::getTokenRange( 1152 First->Tok.getLocation(), Last->Tok.getLocation()); 1153 for (unsigned i = 0, e = Ranges.size(); i != e; ++i) { 1154 if (!SourceMgr.isBeforeInTranslationUnit(LineRange.getEnd(), 1155 Ranges[i].getBegin()) && 1156 !SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(), 1157 LineRange.getBegin())) 1158 return true; 1159 } 1160 return false; 1161 } 1162 1163 virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) { 1164 AnnotatedLines.push_back(AnnotatedLine(TheLine)); 1165 } 1166 1167 /// \brief Add a new line and the required indent before the first Token 1168 /// of the \c UnwrappedLine if there was no structural parsing error. 1169 /// Returns the indent level of the \c UnwrappedLine. 1170 void formatFirstToken(const AnnotatedToken &RootToken, unsigned Indent, 1171 bool InPPDirective, unsigned PreviousEndOfLineColumn) { 1172 const FormatToken &Tok = RootToken.FormatTok; 1173 1174 unsigned Newlines = 1175 std::min(Tok.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1); 1176 if (Newlines == 0 && !Tok.IsFirst) 1177 Newlines = 1; 1178 1179 if (!InPPDirective || Tok.HasUnescapedNewline) { 1180 Whitespaces.replaceWhitespace(RootToken, Newlines, Indent, 0, Style); 1181 } else { 1182 Whitespaces.replacePPWhitespace(RootToken, Newlines, Indent, 1183 PreviousEndOfLineColumn, Style); 1184 } 1185 } 1186 1187 DiagnosticsEngine &Diag; 1188 FormatStyle Style; 1189 Lexer &Lex; 1190 SourceManager &SourceMgr; 1191 WhitespaceManager Whitespaces; 1192 std::vector<CharSourceRange> Ranges; 1193 std::vector<AnnotatedLine> AnnotatedLines; 1194 bool StructuralError; 1195 }; 1196 1197 tooling::Replacements 1198 reformat(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr, 1199 std::vector<CharSourceRange> Ranges, DiagnosticConsumer *DiagClient) { 1200 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); 1201 OwningPtr<DiagnosticConsumer> DiagPrinter; 1202 if (DiagClient == 0) { 1203 DiagPrinter.reset(new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts)); 1204 DiagPrinter->BeginSourceFile(Lex.getLangOpts(), Lex.getPP()); 1205 DiagClient = DiagPrinter.get(); 1206 } 1207 DiagnosticsEngine Diagnostics( 1208 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts, 1209 DiagClient, false); 1210 Diagnostics.setSourceManager(&SourceMgr); 1211 Formatter formatter(Diagnostics, Style, Lex, SourceMgr, Ranges); 1212 return formatter.format(); 1213 } 1214 1215 LangOptions getFormattingLangOpts() { 1216 LangOptions LangOpts; 1217 LangOpts.CPlusPlus = 1; 1218 LangOpts.CPlusPlus11 = 1; 1219 LangOpts.Bool = 1; 1220 LangOpts.ObjC1 = 1; 1221 LangOpts.ObjC2 = 1; 1222 return LangOpts; 1223 } 1224 1225 } // namespace format 1226 } // namespace clang 1227