Format.cpp revision 5ef8aacd5f25767fc7bd1ec47c2b5f5fd1ac38eb
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 "BreakableToken.h"
19#include "TokenAnnotator.h"
20#include "UnwrappedLineParser.h"
21#include "WhitespaceManager.h"
22#include "clang/Basic/Diagnostic.h"
23#include "clang/Basic/OperatorPrecedence.h"
24#include "clang/Basic/SourceManager.h"
25#include "clang/Format/Format.h"
26#include "clang/Lex/Lexer.h"
27#include "llvm/ADT/STLExtras.h"
28#include "llvm/Support/Allocator.h"
29#include "llvm/Support/Debug.h"
30#include "llvm/Support/YAMLTraits.h"
31#include <queue>
32#include <string>
33
34namespace llvm {
35namespace yaml {
36template <>
37struct ScalarEnumerationTraits<clang::format::FormatStyle::LanguageStandard> {
38  static void enumeration(IO &IO,
39                          clang::format::FormatStyle::LanguageStandard &Value) {
40    IO.enumCase(Value, "C++03", clang::format::FormatStyle::LS_Cpp03);
41    IO.enumCase(Value, "C++11", clang::format::FormatStyle::LS_Cpp11);
42    IO.enumCase(Value, "Auto", clang::format::FormatStyle::LS_Auto);
43  }
44};
45
46template <>
47struct ScalarEnumerationTraits<clang::format::FormatStyle::BraceBreakingStyle> {
48  static void
49  enumeration(IO &IO, clang::format::FormatStyle::BraceBreakingStyle &Value) {
50    IO.enumCase(Value, "Attach", clang::format::FormatStyle::BS_Attach);
51    IO.enumCase(Value, "Linux", clang::format::FormatStyle::BS_Linux);
52    IO.enumCase(Value, "Stroustrup", clang::format::FormatStyle::BS_Stroustrup);
53  }
54};
55
56template <> struct MappingTraits<clang::format::FormatStyle> {
57  static void mapping(llvm::yaml::IO &IO, clang::format::FormatStyle &Style) {
58    if (IO.outputting()) {
59      StringRef StylesArray[] = { "LLVM", "Google", "Chromium", "Mozilla" };
60      ArrayRef<StringRef> Styles(StylesArray);
61      for (size_t i = 0, e = Styles.size(); i < e; ++i) {
62        StringRef StyleName(Styles[i]);
63        clang::format::FormatStyle PredefinedStyle;
64        if (clang::format::getPredefinedStyle(StyleName, &PredefinedStyle) &&
65            Style == PredefinedStyle) {
66          IO.mapOptional("# BasedOnStyle", StyleName);
67          break;
68        }
69      }
70    } else {
71      StringRef BasedOnStyle;
72      IO.mapOptional("BasedOnStyle", BasedOnStyle);
73      if (!BasedOnStyle.empty())
74        if (!clang::format::getPredefinedStyle(BasedOnStyle, &Style)) {
75          IO.setError(Twine("Unknown value for BasedOnStyle: ", BasedOnStyle));
76          return;
77        }
78    }
79
80    IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset);
81    IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft);
82    IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine",
83                   Style.AllowAllParametersOfDeclarationOnNextLine);
84    IO.mapOptional("AllowShortIfStatementsOnASingleLine",
85                   Style.AllowShortIfStatementsOnASingleLine);
86    IO.mapOptional("AllowShortLoopsOnASingleLine",
87                   Style.AllowShortLoopsOnASingleLine);
88    IO.mapOptional("AlwaysBreakTemplateDeclarations",
89                   Style.AlwaysBreakTemplateDeclarations);
90    IO.mapOptional("BinPackParameters", Style.BinPackParameters);
91    IO.mapOptional("ColumnLimit", Style.ColumnLimit);
92    IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine",
93                   Style.ConstructorInitializerAllOnOneLineOrOnePerLine);
94    IO.mapOptional("DerivePointerBinding", Style.DerivePointerBinding);
95    IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels);
96    IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep);
97    IO.mapOptional("ObjCSpaceBeforeProtocolList",
98                   Style.ObjCSpaceBeforeProtocolList);
99    IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter);
100    IO.mapOptional("PenaltyReturnTypeOnItsOwnLine",
101                   Style.PenaltyReturnTypeOnItsOwnLine);
102    IO.mapOptional("PointerBindsToType", Style.PointerBindsToType);
103    IO.mapOptional("SpacesBeforeTrailingComments",
104                   Style.SpacesBeforeTrailingComments);
105    IO.mapOptional("SpacesInBracedLists", Style.SpacesInBracedLists);
106    IO.mapOptional("Standard", Style.Standard);
107    IO.mapOptional("IndentWidth", Style.IndentWidth);
108    IO.mapOptional("UseTab", Style.UseTab);
109    IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces);
110  }
111};
112}
113}
114
115namespace clang {
116namespace format {
117
118FormatStyle getLLVMStyle() {
119  FormatStyle LLVMStyle;
120  LLVMStyle.AccessModifierOffset = -2;
121  LLVMStyle.AlignEscapedNewlinesLeft = false;
122  LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
123  LLVMStyle.AllowShortIfStatementsOnASingleLine = false;
124  LLVMStyle.AllowShortLoopsOnASingleLine = false;
125  LLVMStyle.AlwaysBreakTemplateDeclarations = false;
126  LLVMStyle.BinPackParameters = true;
127  LLVMStyle.ColumnLimit = 80;
128  LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
129  LLVMStyle.DerivePointerBinding = false;
130  LLVMStyle.IndentCaseLabels = false;
131  LLVMStyle.MaxEmptyLinesToKeep = 1;
132  LLVMStyle.ObjCSpaceBeforeProtocolList = true;
133  LLVMStyle.PenaltyExcessCharacter = 1000000;
134  LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 75;
135  LLVMStyle.PointerBindsToType = false;
136  LLVMStyle.SpacesBeforeTrailingComments = 1;
137  LLVMStyle.SpacesInBracedLists = true;
138  LLVMStyle.Standard = FormatStyle::LS_Cpp03;
139  LLVMStyle.IndentWidth = 2;
140  LLVMStyle.UseTab = false;
141  LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
142  return LLVMStyle;
143}
144
145FormatStyle getGoogleStyle() {
146  FormatStyle GoogleStyle;
147  GoogleStyle.AccessModifierOffset = -1;
148  GoogleStyle.AlignEscapedNewlinesLeft = true;
149  GoogleStyle.AllowAllParametersOfDeclarationOnNextLine = true;
150  GoogleStyle.AllowShortIfStatementsOnASingleLine = true;
151  GoogleStyle.AllowShortLoopsOnASingleLine = true;
152  GoogleStyle.AlwaysBreakTemplateDeclarations = true;
153  GoogleStyle.BinPackParameters = true;
154  GoogleStyle.ColumnLimit = 80;
155  GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
156  GoogleStyle.DerivePointerBinding = true;
157  GoogleStyle.IndentCaseLabels = true;
158  GoogleStyle.MaxEmptyLinesToKeep = 1;
159  GoogleStyle.ObjCSpaceBeforeProtocolList = false;
160  GoogleStyle.PenaltyExcessCharacter = 1000000;
161  GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
162  GoogleStyle.PointerBindsToType = true;
163  GoogleStyle.SpacesBeforeTrailingComments = 2;
164  GoogleStyle.SpacesInBracedLists = false;
165  GoogleStyle.Standard = FormatStyle::LS_Auto;
166  GoogleStyle.IndentWidth = 2;
167  GoogleStyle.UseTab = false;
168  GoogleStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
169  return GoogleStyle;
170}
171
172FormatStyle getChromiumStyle() {
173  FormatStyle ChromiumStyle = getGoogleStyle();
174  ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false;
175  ChromiumStyle.AllowShortIfStatementsOnASingleLine = false;
176  ChromiumStyle.AllowShortLoopsOnASingleLine = false;
177  ChromiumStyle.BinPackParameters = false;
178  ChromiumStyle.Standard = FormatStyle::LS_Cpp03;
179  ChromiumStyle.DerivePointerBinding = false;
180  return ChromiumStyle;
181}
182
183FormatStyle getMozillaStyle() {
184  FormatStyle MozillaStyle = getLLVMStyle();
185  MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false;
186  MozillaStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
187  MozillaStyle.DerivePointerBinding = true;
188  MozillaStyle.IndentCaseLabels = true;
189  MozillaStyle.ObjCSpaceBeforeProtocolList = false;
190  MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200;
191  MozillaStyle.PointerBindsToType = true;
192  return MozillaStyle;
193}
194
195bool getPredefinedStyle(StringRef Name, FormatStyle *Style) {
196  if (Name.equals_lower("llvm"))
197    *Style = getLLVMStyle();
198  else if (Name.equals_lower("chromium"))
199    *Style = getChromiumStyle();
200  else if (Name.equals_lower("mozilla"))
201    *Style = getMozillaStyle();
202  else if (Name.equals_lower("google"))
203    *Style = getGoogleStyle();
204  else
205    return false;
206
207  return true;
208}
209
210llvm::error_code parseConfiguration(StringRef Text, FormatStyle *Style) {
211  if (Text.trim().empty())
212    return llvm::make_error_code(llvm::errc::invalid_argument);
213  llvm::yaml::Input Input(Text);
214  Input >> *Style;
215  return Input.error();
216}
217
218std::string configurationAsText(const FormatStyle &Style) {
219  std::string Text;
220  llvm::raw_string_ostream Stream(Text);
221  llvm::yaml::Output Output(Stream);
222  // We use the same mapping method for input and output, so we need a non-const
223  // reference here.
224  FormatStyle NonConstStyle = Style;
225  Output << NonConstStyle;
226  return Stream.str();
227}
228
229// Returns the length of everything up to the first possible line break after
230// the ), ], } or > matching \c Tok.
231static unsigned getLengthToMatchingParen(const FormatToken &Tok) {
232  if (Tok.MatchingParen == NULL)
233    return 0;
234  FormatToken *End = Tok.MatchingParen;
235  while (End->Next && !End->Next->CanBreakBefore) {
236    End = End->Next;
237  }
238  return End->TotalLength - Tok.TotalLength + 1;
239}
240
241class UnwrappedLineFormatter {
242public:
243  UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr,
244                         const AnnotatedLine &Line, unsigned FirstIndent,
245                         const FormatToken *RootToken,
246                         WhitespaceManager &Whitespaces)
247      : Style(Style), SourceMgr(SourceMgr), Line(Line),
248        FirstIndent(FirstIndent), RootToken(RootToken),
249        Whitespaces(Whitespaces), Count(0) {}
250
251  /// \brief Formats an \c UnwrappedLine.
252  void format(const AnnotatedLine *NextLine) {
253    // Initialize state dependent on indent.
254    LineState State;
255    State.Column = FirstIndent;
256    State.NextToken = RootToken;
257    State.Stack.push_back(
258        ParenState(FirstIndent, FirstIndent, /*AvoidBinPacking=*/ false,
259                   /*NoLineBreak=*/ false));
260    State.LineContainsContinuedForLoopSection = false;
261    State.ParenLevel = 0;
262    State.StartOfStringLiteral = 0;
263    State.StartOfLineLevel = State.ParenLevel;
264    State.LowestLevelOnLine = State.ParenLevel;
265    State.IgnoreStackForComparison = false;
266
267    // The first token has already been indented and thus consumed.
268    moveStateToNextToken(State, /*DryRun=*/ false);
269
270    // If everything fits on a single line, just put it there.
271    unsigned ColumnLimit = Style.ColumnLimit;
272    if (NextLine && NextLine->InPPDirective &&
273        !NextLine->First->HasUnescapedNewline)
274      ColumnLimit = getColumnLimit();
275    if (Line.Last->TotalLength <= ColumnLimit - FirstIndent) {
276      while (State.NextToken != NULL) {
277        addTokenToState(false, false, State);
278      }
279    }
280
281    // If the ObjC method declaration does not fit on a line, we should format
282    // it with one arg per line.
283    if (Line.Type == LT_ObjCMethodDecl)
284      State.Stack.back().BreakBeforeParameter = true;
285
286    // Find best solution in solution space.
287    analyzeSolutionSpace(State);
288  }
289
290private:
291  void DebugTokenState(const FormatToken &FormatTok) {
292    const Token &Tok = FormatTok.Tok;
293    llvm::dbgs() << StringRef(SourceMgr.getCharacterData(Tok.getLocation()),
294                              Tok.getLength());
295    llvm::dbgs();
296  }
297
298  struct ParenState {
299    ParenState(unsigned Indent, unsigned LastSpace, bool AvoidBinPacking,
300               bool NoLineBreak)
301        : Indent(Indent), LastSpace(LastSpace), FirstLessLess(0),
302          BreakBeforeClosingBrace(false), QuestionColumn(0),
303          AvoidBinPacking(AvoidBinPacking), BreakBeforeParameter(false),
304          NoLineBreak(NoLineBreak), ColonPos(0), StartOfFunctionCall(0),
305          NestedNameSpecifierContinuation(0), CallContinuation(0),
306          VariablePos(0), ForFakeParenthesis(false) {}
307
308    /// \brief The position to which a specific parenthesis level needs to be
309    /// indented.
310    unsigned Indent;
311
312    /// \brief The position of the last space on each level.
313    ///
314    /// Used e.g. to break like:
315    /// functionCall(Parameter, otherCall(
316    ///                             OtherParameter));
317    unsigned LastSpace;
318
319    /// \brief The position the first "<<" operator encountered on each level.
320    ///
321    /// Used to align "<<" operators. 0 if no such operator has been encountered
322    /// on a level.
323    unsigned FirstLessLess;
324
325    /// \brief Whether a newline needs to be inserted before the block's closing
326    /// brace.
327    ///
328    /// We only want to insert a newline before the closing brace if there also
329    /// was a newline after the beginning left brace.
330    bool BreakBeforeClosingBrace;
331
332    /// \brief The column of a \c ? in a conditional expression;
333    unsigned QuestionColumn;
334
335    /// \brief Avoid bin packing, i.e. multiple parameters/elements on multiple
336    /// lines, in this context.
337    bool AvoidBinPacking;
338
339    /// \brief Break after the next comma (or all the commas in this context if
340    /// \c AvoidBinPacking is \c true).
341    bool BreakBeforeParameter;
342
343    /// \brief Line breaking in this context would break a formatting rule.
344    bool NoLineBreak;
345
346    /// \brief The position of the colon in an ObjC method declaration/call.
347    unsigned ColonPos;
348
349    /// \brief The start of the most recent function in a builder-type call.
350    unsigned StartOfFunctionCall;
351
352    /// \brief If a nested name specifier was broken over multiple lines, this
353    /// contains the start column of the second line. Otherwise 0.
354    unsigned NestedNameSpecifierContinuation;
355
356    /// \brief If a call expression was broken over multiple lines, this
357    /// contains the start column of the second line. Otherwise 0.
358    unsigned CallContinuation;
359
360    /// \brief The column of the first variable name in a variable declaration.
361    ///
362    /// Used to align further variables if necessary.
363    unsigned VariablePos;
364
365    /// \brief \c true if this \c ParenState was created for a fake parenthesis.
366    ///
367    /// Does not need to be considered for memoization / the comparison function
368    /// as otherwise identical states will have the same fake/non-fake
369    /// \c ParenStates.
370    bool ForFakeParenthesis;
371
372    bool operator<(const ParenState &Other) const {
373      if (Indent != Other.Indent)
374        return Indent < Other.Indent;
375      if (LastSpace != Other.LastSpace)
376        return LastSpace < Other.LastSpace;
377      if (FirstLessLess != Other.FirstLessLess)
378        return FirstLessLess < Other.FirstLessLess;
379      if (BreakBeforeClosingBrace != Other.BreakBeforeClosingBrace)
380        return BreakBeforeClosingBrace;
381      if (QuestionColumn != Other.QuestionColumn)
382        return QuestionColumn < Other.QuestionColumn;
383      if (AvoidBinPacking != Other.AvoidBinPacking)
384        return AvoidBinPacking;
385      if (BreakBeforeParameter != Other.BreakBeforeParameter)
386        return BreakBeforeParameter;
387      if (NoLineBreak != Other.NoLineBreak)
388        return NoLineBreak;
389      if (ColonPos != Other.ColonPos)
390        return ColonPos < Other.ColonPos;
391      if (StartOfFunctionCall != Other.StartOfFunctionCall)
392        return StartOfFunctionCall < Other.StartOfFunctionCall;
393      if (CallContinuation != Other.CallContinuation)
394        return CallContinuation < Other.CallContinuation;
395      if (VariablePos != Other.VariablePos)
396        return VariablePos < Other.VariablePos;
397      return false;
398    }
399  };
400
401  /// \brief The current state when indenting a unwrapped line.
402  ///
403  /// As the indenting tries different combinations this is copied by value.
404  struct LineState {
405    /// \brief The number of used columns in the current line.
406    unsigned Column;
407
408    /// \brief The token that needs to be next formatted.
409    const FormatToken *NextToken;
410
411    /// \brief \c true if this line contains a continued for-loop section.
412    bool LineContainsContinuedForLoopSection;
413
414    /// \brief The level of nesting inside (), [], <> and {}.
415    unsigned ParenLevel;
416
417    /// \brief The \c ParenLevel at the start of this line.
418    unsigned StartOfLineLevel;
419
420    /// \brief The lowest \c ParenLevel on the current line.
421    unsigned LowestLevelOnLine;
422
423    /// \brief The start column of the string literal, if we're in a string
424    /// literal sequence, 0 otherwise.
425    unsigned StartOfStringLiteral;
426
427    /// \brief A stack keeping track of properties applying to parenthesis
428    /// levels.
429    std::vector<ParenState> Stack;
430
431    /// \brief Ignore the stack of \c ParenStates for state comparison.
432    ///
433    /// In long and deeply nested unwrapped lines, the current algorithm can
434    /// be insufficient for finding the best formatting with a reasonable amount
435    /// of time and memory. Setting this flag will effectively lead to the
436    /// algorithm not analyzing some combinations. However, these combinations
437    /// rarely contain the optimal solution: In short, accepting a higher
438    /// penalty early would need to lead to different values in the \c
439    /// ParenState stack (in an otherwise identical state) and these different
440    /// values would need to lead to a significant amount of avoided penalty
441    /// later.
442    ///
443    /// FIXME: Come up with a better algorithm instead.
444    bool IgnoreStackForComparison;
445
446    /// \brief Comparison operator to be able to used \c LineState in \c map.
447    bool operator<(const LineState &Other) const {
448      if (NextToken != Other.NextToken)
449        return NextToken < Other.NextToken;
450      if (Column != Other.Column)
451        return Column < Other.Column;
452      if (LineContainsContinuedForLoopSection !=
453          Other.LineContainsContinuedForLoopSection)
454        return LineContainsContinuedForLoopSection;
455      if (ParenLevel != Other.ParenLevel)
456        return ParenLevel < Other.ParenLevel;
457      if (StartOfLineLevel != Other.StartOfLineLevel)
458        return StartOfLineLevel < Other.StartOfLineLevel;
459      if (LowestLevelOnLine != Other.LowestLevelOnLine)
460        return LowestLevelOnLine < Other.LowestLevelOnLine;
461      if (StartOfStringLiteral != Other.StartOfStringLiteral)
462        return StartOfStringLiteral < Other.StartOfStringLiteral;
463      if (IgnoreStackForComparison || Other.IgnoreStackForComparison)
464        return false;
465      return Stack < Other.Stack;
466    }
467  };
468
469  /// \brief Appends the next token to \p State and updates information
470  /// necessary for indentation.
471  ///
472  /// Puts the token on the current line if \p Newline is \c true and adds a
473  /// line break and necessary indentation otherwise.
474  ///
475  /// If \p DryRun is \c false, also creates and stores the required
476  /// \c Replacement.
477  unsigned addTokenToState(bool Newline, bool DryRun, LineState &State) {
478    const FormatToken &Current = *State.NextToken;
479    const FormatToken &Previous = *State.NextToken->Previous;
480
481    if (State.Stack.size() == 0 || Current.Type == TT_ImplicitStringLiteral) {
482      // FIXME: Is this correct?
483      int WhitespaceLength = SourceMgr.getSpellingColumnNumber(
484                                 State.NextToken->WhitespaceRange.getEnd()) -
485                             SourceMgr.getSpellingColumnNumber(
486                                 State.NextToken->WhitespaceRange.getBegin());
487      State.Column += WhitespaceLength + State.NextToken->TokenLength;
488      State.NextToken = State.NextToken->Next;
489      return 0;
490    }
491
492    // If we are continuing an expression, we want to indent an extra 4 spaces.
493    unsigned ContinuationIndent =
494        std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) + 4;
495    if (Newline) {
496      if (Current.is(tok::r_brace)) {
497        State.Column = Line.Level * Style.IndentWidth;
498      } else if (Current.is(tok::string_literal) &&
499                 State.StartOfStringLiteral != 0) {
500        State.Column = State.StartOfStringLiteral;
501        State.Stack.back().BreakBeforeParameter = true;
502      } else if (Current.is(tok::lessless) &&
503                 State.Stack.back().FirstLessLess != 0) {
504        State.Column = State.Stack.back().FirstLessLess;
505      } else if (Current.isOneOf(tok::period, tok::arrow) &&
506                 Current.Type != TT_DesignatedInitializerPeriod) {
507        if (State.Stack.back().CallContinuation == 0) {
508          State.Column = ContinuationIndent;
509          State.Stack.back().CallContinuation = State.Column;
510        } else {
511          State.Column = State.Stack.back().CallContinuation;
512        }
513      } else if (Current.Type == TT_ConditionalExpr) {
514        State.Column = State.Stack.back().QuestionColumn;
515      } else if (Previous.is(tok::comma) &&
516                 State.Stack.back().VariablePos != 0) {
517        State.Column = State.Stack.back().VariablePos;
518      } else if (Previous.ClosesTemplateDeclaration ||
519                 (Current.Type == TT_StartOfName && State.ParenLevel == 0 &&
520                  Line.StartsDefinition)) {
521        State.Column = State.Stack.back().Indent;
522      } else if (Current.Type == TT_ObjCSelectorName) {
523        if (State.Stack.back().ColonPos > Current.TokenLength) {
524          State.Column = State.Stack.back().ColonPos - Current.TokenLength;
525        } else {
526          State.Column = State.Stack.back().Indent;
527          State.Stack.back().ColonPos = State.Column + Current.TokenLength;
528        }
529      } else if (Current.Type == TT_StartOfName ||
530                 Previous.isOneOf(tok::coloncolon, tok::equal) ||
531                 Previous.Type == TT_ObjCMethodExpr) {
532        State.Column = ContinuationIndent;
533      } else {
534        State.Column = State.Stack.back().Indent;
535        // Ensure that we fall back to indenting 4 spaces instead of just
536        // flushing continuations left.
537        if (State.Column == FirstIndent)
538          State.Column += 4;
539      }
540
541      if (Current.is(tok::question))
542        State.Stack.back().BreakBeforeParameter = true;
543      if ((Previous.isOneOf(tok::comma, tok::semi) &&
544           !State.Stack.back().AvoidBinPacking) ||
545          Previous.Type == TT_BinaryOperator)
546        State.Stack.back().BreakBeforeParameter = false;
547      if (Previous.Type == TT_TemplateCloser && State.ParenLevel == 0)
548        State.Stack.back().BreakBeforeParameter = false;
549
550      if (!DryRun) {
551        unsigned NewLines = 1;
552        if (Current.Type == TT_LineComment)
553          NewLines = std::max(
554              NewLines,
555              std::min(Current.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1));
556        Whitespaces.replaceWhitespace(Current, NewLines, State.Column,
557                                      State.Column, Line.InPPDirective);
558      }
559
560      State.Stack.back().LastSpace = State.Column;
561      if (Current.isOneOf(tok::arrow, tok::period) &&
562          Current.Type != TT_DesignatedInitializerPeriod)
563        State.Stack.back().LastSpace += Current.TokenLength;
564      State.StartOfLineLevel = State.ParenLevel;
565      State.LowestLevelOnLine = State.ParenLevel;
566
567      // Any break on this level means that the parent level has been broken
568      // and we need to avoid bin packing there.
569      for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) {
570        State.Stack[i].BreakBeforeParameter = true;
571      }
572      const FormatToken *TokenBefore = Current.getPreviousNoneComment();
573      if (TokenBefore && !TokenBefore->isOneOf(tok::comma, tok::semi) &&
574          TokenBefore->Type != TT_TemplateCloser &&
575          TokenBefore->Type != TT_BinaryOperator && !TokenBefore->opensScope())
576        State.Stack.back().BreakBeforeParameter = true;
577
578      // If we break after {, we should also break before the corresponding }.
579      if (Previous.is(tok::l_brace))
580        State.Stack.back().BreakBeforeClosingBrace = true;
581
582      if (State.Stack.back().AvoidBinPacking) {
583        // If we are breaking after '(', '{', '<', this is not bin packing
584        // unless AllowAllParametersOfDeclarationOnNextLine is false.
585        if (!(Previous.isOneOf(tok::l_paren, tok::l_brace) ||
586              Previous.Type == TT_BinaryOperator) ||
587            (!Style.AllowAllParametersOfDeclarationOnNextLine &&
588             Line.MustBeDeclaration))
589          State.Stack.back().BreakBeforeParameter = true;
590      }
591    } else {
592      if (Current.is(tok::equal) &&
593          (RootToken->is(tok::kw_for) || State.ParenLevel == 0) &&
594          State.Stack.back().VariablePos == 0) {
595        State.Stack.back().VariablePos = State.Column;
596        // Move over * and & if they are bound to the variable name.
597        const FormatToken *Tok = &Previous;
598        while (Tok && State.Stack.back().VariablePos >= Tok->TokenLength) {
599          State.Stack.back().VariablePos -= Tok->TokenLength;
600          if (Tok->SpacesRequiredBefore != 0)
601            break;
602          Tok = Tok->Previous;
603        }
604        if (Previous.PartOfMultiVariableDeclStmt)
605          State.Stack.back().LastSpace = State.Stack.back().VariablePos;
606      }
607
608      unsigned Spaces = State.NextToken->SpacesRequiredBefore;
609
610      if (!DryRun)
611        Whitespaces.replaceWhitespace(Current, 0, Spaces,
612                                      State.Column + Spaces);
613
614      if (Current.Type == TT_ObjCSelectorName &&
615          State.Stack.back().ColonPos == 0) {
616        if (State.Stack.back().Indent + Current.LongestObjCSelectorName >
617            State.Column + Spaces + Current.TokenLength)
618          State.Stack.back().ColonPos =
619              State.Stack.back().Indent + Current.LongestObjCSelectorName;
620        else
621          State.Stack.back().ColonPos =
622              State.Column + Spaces + Current.TokenLength;
623      }
624
625      if (Previous.opensScope() && Previous.Type != TT_ObjCMethodExpr &&
626          Current.Type != TT_LineComment)
627        State.Stack.back().Indent = State.Column + Spaces;
628      if (Previous.is(tok::comma) && !Current.isTrailingComment() &&
629          State.Stack.back().AvoidBinPacking)
630        State.Stack.back().NoLineBreak = true;
631
632      State.Column += Spaces;
633      if (Current.is(tok::l_paren) && Previous.isOneOf(tok::kw_if, tok::kw_for))
634        // Treat the condition inside an if as if it was a second function
635        // parameter, i.e. let nested calls have an indent of 4.
636        State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(".
637      else if (Previous.is(tok::comma))
638        State.Stack.back().LastSpace = State.Column;
639      else if ((Previous.Type == TT_BinaryOperator ||
640                Previous.Type == TT_ConditionalExpr ||
641                Previous.Type == TT_CtorInitializerColon) &&
642               !(Previous.getPrecedence() == prec::Assignment &&
643                 Current.FakeLParens.empty()))
644        // Always indent relative to the RHS of the expression unless this is a
645        // simple assignment without binary expression on the RHS.
646        State.Stack.back().LastSpace = State.Column;
647      else if (Previous.Type == TT_InheritanceColon)
648        State.Stack.back().Indent = State.Column;
649      else if (Previous.opensScope() && !Current.FakeLParens.empty())
650        // If this function has multiple parameters or a binary expression
651        // parameter, indent nested calls from the start of the first parameter.
652        State.Stack.back().LastSpace = State.Column;
653    }
654
655    return moveStateToNextToken(State, DryRun);
656  }
657
658  /// \brief Mark the next token as consumed in \p State and modify its stacks
659  /// accordingly.
660  unsigned moveStateToNextToken(LineState &State, bool DryRun) {
661    const FormatToken &Current = *State.NextToken;
662    assert(State.Stack.size());
663
664    if (Current.Type == TT_InheritanceColon)
665      State.Stack.back().AvoidBinPacking = true;
666    if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0)
667      State.Stack.back().FirstLessLess = State.Column;
668    if (Current.is(tok::question))
669      State.Stack.back().QuestionColumn = State.Column;
670    if (Current.isOneOf(tok::period, tok::arrow) &&
671        Line.Type == LT_BuilderTypeCall && State.ParenLevel == 0)
672      State.Stack.back().StartOfFunctionCall =
673          Current.LastInChainOfCalls ? 0 : State.Column + Current.TokenLength;
674    if (Current.Type == TT_CtorInitializerColon) {
675      // Indent 2 from the column, so:
676      // SomeClass::SomeClass()
677      //     : First(...), ...
678      //       Next(...)
679      //       ^ line up here.
680      State.Stack.back().Indent = State.Column + 2;
681      if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
682        State.Stack.back().AvoidBinPacking = true;
683      State.Stack.back().BreakBeforeParameter = false;
684    }
685
686    // If return returns a binary expression, align after it.
687    if (Current.is(tok::kw_return) && !Current.FakeLParens.empty())
688      State.Stack.back().LastSpace = State.Column + 7;
689
690    // In ObjC method declaration we align on the ":" of parameters, but we need
691    // to ensure that we indent parameters on subsequent lines by at least 4.
692    if (Current.Type == TT_ObjCMethodSpecifier)
693      State.Stack.back().Indent += 4;
694
695    // Insert scopes created by fake parenthesis.
696    const FormatToken *Previous = Current.getPreviousNoneComment();
697    // Don't add extra indentation for the first fake parenthesis after
698    // 'return', assignements or opening <({[. The indentation for these cases
699    // is special cased.
700    bool SkipFirstExtraIndent =
701        Current.is(tok::kw_return) ||
702        (Previous && (Previous->opensScope() ||
703                      Previous->getPrecedence() == prec::Assignment));
704    for (SmallVector<prec::Level, 4>::const_reverse_iterator
705             I = Current.FakeLParens.rbegin(),
706             E = Current.FakeLParens.rend();
707         I != E; ++I) {
708      ParenState NewParenState = State.Stack.back();
709      NewParenState.ForFakeParenthesis = true;
710      NewParenState.Indent =
711          std::max(std::max(State.Column, NewParenState.Indent),
712                   State.Stack.back().LastSpace);
713
714      // Always indent conditional expressions. Never indent expression where
715      // the 'operator' is ',', ';' or an assignment (i.e. *I <=
716      // prec::Assignment) as those have different indentation rules. Indent
717      // other expression, unless the indentation needs to be skipped.
718      if (*I == prec::Conditional ||
719          (!SkipFirstExtraIndent && *I > prec::Assignment))
720        NewParenState.Indent += 4;
721      if (Previous && !Previous->opensScope())
722        NewParenState.BreakBeforeParameter = false;
723      State.Stack.push_back(NewParenState);
724      SkipFirstExtraIndent = false;
725    }
726
727    // If we encounter an opening (, [, { or <, we add a level to our stacks to
728    // prepare for the following tokens.
729    if (Current.opensScope()) {
730      unsigned NewIndent;
731      unsigned LastSpace = State.Stack.back().LastSpace;
732      bool AvoidBinPacking;
733      if (Current.is(tok::l_brace)) {
734        NewIndent = Style.IndentWidth + LastSpace;
735        const FormatToken *NextNoComment = Current.getNextNoneComment();
736        AvoidBinPacking = NextNoComment &&
737                          NextNoComment->Type == TT_DesignatedInitializerPeriod;
738      } else {
739        NewIndent =
740            4 + std::max(LastSpace, State.Stack.back().StartOfFunctionCall);
741        AvoidBinPacking = !Style.BinPackParameters;
742      }
743
744      State.Stack.push_back(ParenState(NewIndent, LastSpace, AvoidBinPacking,
745                                       State.Stack.back().NoLineBreak));
746      ++State.ParenLevel;
747    }
748
749    // If this '[' opens an ObjC call, determine whether all parameters fit into
750    // one line and put one per line if they don't.
751    if (Current.is(tok::l_square) && Current.Type == TT_ObjCMethodExpr &&
752        Current.MatchingParen != NULL) {
753      if (getLengthToMatchingParen(Current) + State.Column > getColumnLimit())
754        State.Stack.back().BreakBeforeParameter = true;
755    }
756
757    // If we encounter a closing ), ], } or >, we can remove a level from our
758    // stacks.
759    if (Current.isOneOf(tok::r_paren, tok::r_square) ||
760        (Current.is(tok::r_brace) && State.NextToken != RootToken) ||
761        State.NextToken->Type == TT_TemplateCloser) {
762      State.Stack.pop_back();
763      --State.ParenLevel;
764    }
765    State.LowestLevelOnLine =
766        std::min(State.LowestLevelOnLine, State.ParenLevel);
767
768    // Remove scopes created by fake parenthesis.
769    for (unsigned i = 0, e = Current.FakeRParens; i != e; ++i) {
770      unsigned VariablePos = State.Stack.back().VariablePos;
771      State.Stack.pop_back();
772      State.Stack.back().VariablePos = VariablePos;
773    }
774
775    if (Current.is(tok::string_literal) && State.StartOfStringLiteral == 0) {
776      State.StartOfStringLiteral = State.Column;
777    } else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash,
778                                tok::string_literal)) {
779      State.StartOfStringLiteral = 0;
780    }
781
782    State.Column += Current.TokenLength;
783
784    State.NextToken = State.NextToken->Next;
785
786    return breakProtrudingToken(Current, State, DryRun);
787  }
788
789  /// \brief If the current token sticks out over the end of the line, break
790  /// it if possible.
791  ///
792  /// \returns An extra penalty if a token was broken, otherwise 0.
793  ///
794  /// Note that the penalty of the token protruding the allowed line length is
795  /// already handled in \c addNextStateToQueue; the returned penalty will only
796  /// cover the cost of the additional line breaks.
797  unsigned breakProtrudingToken(const FormatToken &Current, LineState &State,
798                                bool DryRun) {
799    unsigned UnbreakableTailLength = Current.UnbreakableTailLength;
800    llvm::OwningPtr<BreakableToken> Token;
801    unsigned StartColumn = State.Column - Current.TokenLength;
802    unsigned OriginalStartColumn =
803        SourceMgr.getSpellingColumnNumber(Current.getStartOfNonWhitespace()) -
804        1;
805
806    if (Current.is(tok::string_literal) &&
807        Current.Type != TT_ImplicitStringLiteral) {
808      // Only break up default narrow strings.
809      const char *LiteralData =
810          SourceMgr.getCharacterData(Current.getStartOfNonWhitespace());
811      if (!LiteralData || *LiteralData != '"')
812        return 0;
813
814      Token.reset(new BreakableStringLiteral(Current, StartColumn));
815    } else if (Current.Type == TT_BlockComment) {
816      BreakableBlockComment *BBC = new BreakableBlockComment(
817          Style, Current, StartColumn, OriginalStartColumn, !Current.Previous);
818      Token.reset(BBC);
819    } else if (Current.Type == TT_LineComment &&
820               (Current.Previous == NULL ||
821                Current.Previous->Type != TT_ImplicitStringLiteral)) {
822      Token.reset(new BreakableLineComment(Current, StartColumn));
823    } else {
824      return 0;
825    }
826    if (UnbreakableTailLength >= getColumnLimit())
827      return 0;
828    unsigned RemainingSpace = getColumnLimit() - UnbreakableTailLength;
829
830    bool BreakInserted = false;
831    unsigned Penalty = 0;
832    unsigned PositionAfterLastLineInToken = 0;
833    for (unsigned LineIndex = 0, EndIndex = Token->getLineCount();
834         LineIndex != EndIndex; ++LineIndex) {
835      if (!DryRun) {
836        Token->replaceWhitespaceBefore(LineIndex, Line.InPPDirective,
837                                       Whitespaces);
838      }
839      unsigned TailOffset = 0;
840      unsigned RemainingTokenLength =
841          Token->getLineLengthAfterSplit(LineIndex, TailOffset);
842      while (RemainingTokenLength > RemainingSpace) {
843        BreakableToken::Split Split =
844            Token->getSplit(LineIndex, TailOffset, getColumnLimit());
845        if (Split.first == StringRef::npos)
846          break;
847        assert(Split.first != 0);
848        unsigned NewRemainingTokenLength = Token->getLineLengthAfterSplit(
849            LineIndex, TailOffset + Split.first + Split.second);
850        assert(NewRemainingTokenLength < RemainingTokenLength);
851        if (!DryRun) {
852          Token->insertBreak(LineIndex, TailOffset, Split, Line.InPPDirective,
853                             Whitespaces);
854        }
855        TailOffset += Split.first + Split.second;
856        RemainingTokenLength = NewRemainingTokenLength;
857        Penalty += Style.PenaltyExcessCharacter;
858        BreakInserted = true;
859      }
860      PositionAfterLastLineInToken = RemainingTokenLength;
861    }
862
863    if (BreakInserted) {
864      State.Column = PositionAfterLastLineInToken;
865      for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
866        State.Stack[i].BreakBeforeParameter = true;
867      State.Stack.back().LastSpace = StartColumn;
868    }
869    return Penalty;
870  }
871
872  unsigned getColumnLimit() {
873    // In preprocessor directives reserve two chars for trailing " \"
874    return Style.ColumnLimit - (Line.InPPDirective ? 2 : 0);
875  }
876
877  /// \brief An edge in the solution space from \c Previous->State to \c State,
878  /// inserting a newline dependent on the \c NewLine.
879  struct StateNode {
880    StateNode(const LineState &State, bool NewLine, StateNode *Previous)
881        : State(State), NewLine(NewLine), Previous(Previous) {}
882    LineState State;
883    bool NewLine;
884    StateNode *Previous;
885  };
886
887  /// \brief A pair of <penalty, count> that is used to prioritize the BFS on.
888  ///
889  /// In case of equal penalties, we want to prefer states that were inserted
890  /// first. During state generation we make sure that we insert states first
891  /// that break the line as late as possible.
892  typedef std::pair<unsigned, unsigned> OrderedPenalty;
893
894  /// \brief An item in the prioritized BFS search queue. The \c StateNode's
895  /// \c State has the given \c OrderedPenalty.
896  typedef std::pair<OrderedPenalty, StateNode *> QueueItem;
897
898  /// \brief The BFS queue type.
899  typedef std::priority_queue<QueueItem, std::vector<QueueItem>,
900                              std::greater<QueueItem> > QueueType;
901
902  /// \brief Analyze the entire solution space starting from \p InitialState.
903  ///
904  /// This implements a variant of Dijkstra's algorithm on the graph that spans
905  /// the solution space (\c LineStates are the nodes). The algorithm tries to
906  /// find the shortest path (the one with lowest penalty) from \p InitialState
907  /// to a state where all tokens are placed.
908  void analyzeSolutionSpace(LineState &InitialState) {
909    std::set<LineState> Seen;
910
911    // Insert start element into queue.
912    StateNode *Node =
913        new (Allocator.Allocate()) StateNode(InitialState, false, NULL);
914    Queue.push(QueueItem(OrderedPenalty(0, Count), Node));
915    ++Count;
916
917    // While not empty, take first element and follow edges.
918    while (!Queue.empty()) {
919      unsigned Penalty = Queue.top().first.first;
920      StateNode *Node = Queue.top().second;
921      if (Node->State.NextToken == NULL) {
922        DEBUG(llvm::dbgs() << "\n---\nPenalty for line: " << Penalty << "\n");
923        break;
924      }
925      Queue.pop();
926
927      // Cut off the analysis of certain solutions if the analysis gets too
928      // complex. See description of IgnoreStackForComparison.
929      if (Count > 10000)
930        Node->State.IgnoreStackForComparison = true;
931
932      if (!Seen.insert(Node->State).second)
933        // State already examined with lower penalty.
934        continue;
935
936      addNextStateToQueue(Penalty, Node, /*NewLine=*/ false);
937      addNextStateToQueue(Penalty, Node, /*NewLine=*/ true);
938    }
939
940    if (Queue.empty())
941      // We were unable to find a solution, do nothing.
942      // FIXME: Add diagnostic?
943      return;
944
945    // Reconstruct the solution.
946    reconstructPath(InitialState, Queue.top().second);
947    DEBUG(llvm::dbgs() << "Total number of analyzed states: " << Count << "\n");
948    DEBUG(llvm::dbgs() << "---\n");
949  }
950
951  void reconstructPath(LineState &State, StateNode *Current) {
952    std::deque<StateNode *> Path;
953    // We do not need a break before the initial token.
954    while (Current->Previous) {
955      Path.push_front(Current);
956      Current = Current->Previous;
957    }
958    for (std::deque<StateNode *>::iterator I = Path.begin(), E = Path.end();
959         I != E; ++I) {
960      DEBUG({
961        if ((*I)->NewLine) {
962          llvm::dbgs() << "Penalty for splitting before "
963                       << (*I)->Previous->State.NextToken->Tok.getName() << ": "
964                       << (*I)->Previous->State.NextToken->SplitPenalty << "\n";
965        }
966      });
967      addTokenToState((*I)->NewLine, false, State);
968    }
969  }
970
971  /// \brief Add the following state to the analysis queue \c Queue.
972  ///
973  /// Assume the current state is \p PreviousNode and has been reached with a
974  /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true.
975  void addNextStateToQueue(unsigned Penalty, StateNode *PreviousNode,
976                           bool NewLine) {
977    if (NewLine && !canBreak(PreviousNode->State))
978      return;
979    if (!NewLine && mustBreak(PreviousNode->State))
980      return;
981    if (NewLine)
982      Penalty += PreviousNode->State.NextToken->SplitPenalty;
983
984    StateNode *Node = new (Allocator.Allocate())
985        StateNode(PreviousNode->State, NewLine, PreviousNode);
986    Penalty += addTokenToState(NewLine, true, Node->State);
987    if (Node->State.Column > getColumnLimit()) {
988      unsigned ExcessCharacters = Node->State.Column - getColumnLimit();
989      Penalty += Style.PenaltyExcessCharacter * ExcessCharacters;
990    }
991
992    Queue.push(QueueItem(OrderedPenalty(Penalty, Count), Node));
993    ++Count;
994  }
995
996  /// \brief Returns \c true, if a line break after \p State is allowed.
997  bool canBreak(const LineState &State) {
998    const FormatToken &Current = *State.NextToken;
999    const FormatToken &Previous = *Current.Previous;
1000    assert(&Previous == Current.Previous);
1001    if (!Current.CanBreakBefore &&
1002        !(Current.is(tok::r_brace) &&
1003          State.Stack.back().BreakBeforeClosingBrace))
1004      return false;
1005    // The opening "{" of a braced list has to be on the same line as the first
1006    // element if it is nested in another braced init list or function call.
1007    if (!Current.MustBreakBefore && Previous.is(tok::l_brace) &&
1008        Previous.Previous &&
1009        Previous.Previous->isOneOf(tok::l_brace, tok::l_paren, tok::comma))
1010      return false;
1011    // This prevents breaks like:
1012    //   ...
1013    //   SomeParameter, OtherParameter).DoSomething(
1014    //   ...
1015    // As they hide "DoSomething" and are generally bad for readability.
1016    if (Previous.opensScope() &&
1017        State.LowestLevelOnLine < State.StartOfLineLevel)
1018      return false;
1019    return !State.Stack.back().NoLineBreak;
1020  }
1021
1022  /// \brief Returns \c true, if a line break after \p State is mandatory.
1023  bool mustBreak(const LineState &State) {
1024    const FormatToken &Current = *State.NextToken;
1025    const FormatToken &Previous = *Current.Previous;
1026    if (Current.MustBreakBefore || Current.Type == TT_InlineASMColon)
1027      return true;
1028    if (Current.is(tok::r_brace) && State.Stack.back().BreakBeforeClosingBrace)
1029      return true;
1030    if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection)
1031      return true;
1032    if ((Previous.isOneOf(tok::comma, tok::semi) || Current.is(tok::question) ||
1033         Current.Type == TT_ConditionalExpr) &&
1034        State.Stack.back().BreakBeforeParameter &&
1035        !Current.isTrailingComment() &&
1036        !Current.isOneOf(tok::r_paren, tok::r_brace))
1037      return true;
1038
1039    // If we need to break somewhere inside the LHS of a binary expression, we
1040    // should also break after the operator.
1041    if (Previous.Type == TT_BinaryOperator &&
1042        Current.Type != TT_BinaryOperator && // Special case for ">>".
1043        !Current.isTrailingComment() &&
1044        !Previous.isOneOf(tok::lessless, tok::question) &&
1045        Previous.getPrecedence() != prec::Assignment &&
1046        State.Stack.back().BreakBeforeParameter)
1047      return true;
1048
1049    // FIXME: Comparing LongestObjCSelectorName to 0 is a hacky way of finding
1050    // out whether it is the first parameter. Clean this up.
1051    if (Current.Type == TT_ObjCSelectorName &&
1052        Current.LongestObjCSelectorName == 0 &&
1053        State.Stack.back().BreakBeforeParameter)
1054      return true;
1055    if ((Current.Type == TT_CtorInitializerColon ||
1056         (Previous.ClosesTemplateDeclaration && State.ParenLevel == 0)))
1057      return true;
1058
1059    if (Current.Type == TT_StartOfName && Line.MightBeFunctionDecl &&
1060        State.Stack.back().BreakBeforeParameter && State.ParenLevel == 0)
1061      return true;
1062    return false;
1063  }
1064
1065  // Returns the total number of columns required for the remaining tokens.
1066  unsigned getRemainingLength(const LineState &State) {
1067    if (State.NextToken && State.NextToken->Previous)
1068      return Line.Last->TotalLength - State.NextToken->Previous->TotalLength;
1069    return 0;
1070  }
1071
1072  FormatStyle Style;
1073  SourceManager &SourceMgr;
1074  const AnnotatedLine &Line;
1075  const unsigned FirstIndent;
1076  const FormatToken *RootToken;
1077  WhitespaceManager &Whitespaces;
1078
1079  llvm::SpecificBumpPtrAllocator<StateNode> Allocator;
1080  QueueType Queue;
1081  // Increasing count of \c StateNode items we have created. This is used
1082  // to create a deterministic order independent of the container.
1083  unsigned Count;
1084};
1085
1086class FormatTokenLexer {
1087public:
1088  FormatTokenLexer(Lexer &Lex, SourceManager &SourceMgr)
1089      : FormatTok(NULL), GreaterStashed(false), TrailingWhitespace(0), Lex(Lex),
1090        SourceMgr(SourceMgr), IdentTable(Lex.getLangOpts()) {
1091    Lex.SetKeepWhitespaceMode(true);
1092  }
1093
1094  ArrayRef<FormatToken *> lex() {
1095    assert(Tokens.empty());
1096    do {
1097      Tokens.push_back(getNextToken());
1098    } while (Tokens.back()->Tok.isNot(tok::eof));
1099    return Tokens;
1100  }
1101
1102  IdentifierTable &getIdentTable() { return IdentTable; }
1103
1104private:
1105  FormatToken *getNextToken() {
1106    if (GreaterStashed) {
1107      // Create a synthesized second '>' token.
1108      Token Greater = FormatTok->Tok;
1109      FormatTok = new (Allocator.Allocate()) FormatToken;
1110      FormatTok->Tok = Greater;
1111      SourceLocation GreaterLocation =
1112          FormatTok->Tok.getLocation().getLocWithOffset(1);
1113      FormatTok->WhitespaceRange =
1114          SourceRange(GreaterLocation, GreaterLocation);
1115      FormatTok->TokenLength = 1;
1116      GreaterStashed = false;
1117      return FormatTok;
1118    }
1119
1120    FormatTok = new (Allocator.Allocate()) FormatToken;
1121    Lex.LexFromRawLexer(FormatTok->Tok);
1122    StringRef Text = rawTokenText(FormatTok->Tok);
1123    SourceLocation WhitespaceStart =
1124        FormatTok->Tok.getLocation().getLocWithOffset(-TrailingWhitespace);
1125    if (SourceMgr.getFileOffset(WhitespaceStart) == 0)
1126      FormatTok->IsFirst = true;
1127
1128    // Consume and record whitespace until we find a significant token.
1129    unsigned WhitespaceLength = TrailingWhitespace;
1130    while (FormatTok->Tok.is(tok::unknown)) {
1131      unsigned Newlines = Text.count('\n');
1132      if (Newlines > 0)
1133        FormatTok->LastNewlineOffset = WhitespaceLength + Text.rfind('\n') + 1;
1134      unsigned EscapedNewlines = Text.count("\\\n");
1135      FormatTok->NewlinesBefore += Newlines;
1136      FormatTok->HasUnescapedNewline |= EscapedNewlines != Newlines;
1137      WhitespaceLength += FormatTok->Tok.getLength();
1138
1139      if (FormatTok->Tok.is(tok::eof)) {
1140        FormatTok->WhitespaceRange =
1141            SourceRange(WhitespaceStart,
1142                        WhitespaceStart.getLocWithOffset(WhitespaceLength));
1143        return FormatTok;
1144      }
1145      Lex.LexFromRawLexer(FormatTok->Tok);
1146      Text = rawTokenText(FormatTok->Tok);
1147    }
1148
1149    // Now FormatTok is the next non-whitespace token.
1150    FormatTok->TokenLength = Text.size();
1151
1152    TrailingWhitespace = 0;
1153    if (FormatTok->Tok.is(tok::comment)) {
1154      TrailingWhitespace = Text.size() - Text.rtrim().size();
1155      FormatTok->TokenLength -= TrailingWhitespace;
1156    }
1157
1158    // In case the token starts with escaped newlines, we want to
1159    // take them into account as whitespace - this pattern is quite frequent
1160    // in macro definitions.
1161    // FIXME: What do we want to do with other escaped spaces, and escaped
1162    // spaces or newlines in the middle of tokens?
1163    // FIXME: Add a more explicit test.
1164    unsigned i = 0;
1165    while (i + 1 < Text.size() && Text[i] == '\\' && Text[i + 1] == '\n') {
1166      // FIXME: ++FormatTok->NewlinesBefore is missing...
1167      WhitespaceLength += 2;
1168      FormatTok->TokenLength -= 2;
1169      i += 2;
1170    }
1171
1172    if (FormatTok->Tok.is(tok::raw_identifier)) {
1173      IdentifierInfo &Info = IdentTable.get(Text);
1174      FormatTok->Tok.setIdentifierInfo(&Info);
1175      FormatTok->Tok.setKind(Info.getTokenID());
1176    }
1177
1178    if (FormatTok->Tok.is(tok::greatergreater)) {
1179      FormatTok->Tok.setKind(tok::greater);
1180      FormatTok->TokenLength = 1;
1181      GreaterStashed = true;
1182    }
1183
1184    FormatTok->WhitespaceRange = SourceRange(
1185        WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength));
1186    FormatTok->TokenText = StringRef(
1187        SourceMgr.getCharacterData(FormatTok->getStartOfNonWhitespace()),
1188        FormatTok->TokenLength);
1189    return FormatTok;
1190  }
1191
1192  FormatToken *FormatTok;
1193  bool GreaterStashed;
1194  unsigned TrailingWhitespace;
1195  Lexer &Lex;
1196  SourceManager &SourceMgr;
1197  IdentifierTable IdentTable;
1198  llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
1199  SmallVector<FormatToken *, 16> Tokens;
1200
1201  /// Returns the text of \c FormatTok.
1202  StringRef rawTokenText(Token &Tok) {
1203    return StringRef(SourceMgr.getCharacterData(Tok.getLocation()),
1204                     Tok.getLength());
1205  }
1206};
1207
1208class Formatter : public UnwrappedLineConsumer {
1209public:
1210  Formatter(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr,
1211            const std::vector<CharSourceRange> &Ranges)
1212      : Style(Style), Lex(Lex), SourceMgr(SourceMgr),
1213        Whitespaces(SourceMgr, Style), Ranges(Ranges) {}
1214
1215  virtual ~Formatter() {}
1216
1217  tooling::Replacements format() {
1218    FormatTokenLexer Tokens(Lex, SourceMgr);
1219
1220    UnwrappedLineParser Parser(Style, Tokens.lex(), *this);
1221    bool StructuralError = Parser.parse();
1222    TokenAnnotator Annotator(Style, SourceMgr, Lex,
1223                             Tokens.getIdentTable().get("in"));
1224    for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1225      Annotator.annotate(AnnotatedLines[i]);
1226    }
1227    deriveLocalStyle();
1228    for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1229      Annotator.calculateFormattingInformation(AnnotatedLines[i]);
1230    }
1231
1232    // Adapt level to the next line if this is a comment.
1233    // FIXME: Can/should this be done in the UnwrappedLineParser?
1234    const AnnotatedLine *NextNoneCommentLine = NULL;
1235    for (unsigned i = AnnotatedLines.size() - 1; i > 0; --i) {
1236      if (NextNoneCommentLine && AnnotatedLines[i].First->is(tok::comment) &&
1237          !AnnotatedLines[i].First->Next)
1238        AnnotatedLines[i].Level = NextNoneCommentLine->Level;
1239      else
1240        NextNoneCommentLine =
1241            AnnotatedLines[i].First->isNot(tok::r_brace) ? &AnnotatedLines[i]
1242                                                         : NULL;
1243    }
1244
1245    std::vector<int> IndentForLevel;
1246    bool PreviousLineWasTouched = false;
1247    const FormatToken *PreviousLineLastToken = 0;
1248    bool FormatPPDirective = false;
1249    for (std::vector<AnnotatedLine>::iterator I = AnnotatedLines.begin(),
1250                                              E = AnnotatedLines.end();
1251         I != E; ++I) {
1252      const AnnotatedLine &TheLine = *I;
1253      const FormatToken *FirstTok = TheLine.First;
1254      int Offset = getIndentOffset(*TheLine.First);
1255
1256      // Check whether this line is part of a formatted preprocessor directive.
1257      if (FirstTok->HasUnescapedNewline)
1258        FormatPPDirective = false;
1259      if (!FormatPPDirective && TheLine.InPPDirective &&
1260          (touchesLine(TheLine) || touchesPPDirective(I + 1, E)))
1261        FormatPPDirective = true;
1262
1263      // Determine indent and try to merge multiple unwrapped lines.
1264      while (IndentForLevel.size() <= TheLine.Level)
1265        IndentForLevel.push_back(-1);
1266      IndentForLevel.resize(TheLine.Level + 1);
1267      unsigned Indent = getIndent(IndentForLevel, TheLine.Level);
1268      if (static_cast<int>(Indent) + Offset >= 0)
1269        Indent += Offset;
1270      tryFitMultipleLinesInOne(Indent, I, E);
1271
1272      bool WasMoved = PreviousLineWasTouched && FirstTok->NewlinesBefore == 0;
1273      if (TheLine.First->is(tok::eof)) {
1274        if (PreviousLineWasTouched) {
1275          unsigned NewLines = std::min(FirstTok->NewlinesBefore, 1u);
1276          Whitespaces.replaceWhitespace(*TheLine.First, NewLines, /*Indent*/ 0,
1277                                        /*TargetColumn*/ 0);
1278        }
1279      } else if (TheLine.Type != LT_Invalid &&
1280                 (WasMoved || FormatPPDirective || touchesLine(TheLine))) {
1281        unsigned LevelIndent = getIndent(IndentForLevel, TheLine.Level);
1282        if (FirstTok->WhitespaceRange.isValid() &&
1283            // Insert a break even if there is a structural error in case where
1284            // we break apart a line consisting of multiple unwrapped lines.
1285            (FirstTok->NewlinesBefore == 0 || !StructuralError)) {
1286          formatFirstToken(*TheLine.First, PreviousLineLastToken, Indent,
1287                           TheLine.InPPDirective);
1288        } else {
1289          Indent = LevelIndent =
1290              SourceMgr.getSpellingColumnNumber(FirstTok->Tok.getLocation()) -
1291              1;
1292        }
1293        UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine, Indent,
1294                                         TheLine.First, Whitespaces);
1295        Formatter.format(I + 1 != E ? &*(I + 1) : NULL);
1296        IndentForLevel[TheLine.Level] = LevelIndent;
1297        PreviousLineWasTouched = true;
1298      } else {
1299        // Format the first token if necessary, and notify the WhitespaceManager
1300        // about the unchanged whitespace.
1301        for (const FormatToken *Tok = TheLine.First; Tok != NULL;
1302             Tok = Tok->Next) {
1303          if (Tok == TheLine.First &&
1304              (Tok->NewlinesBefore > 0 || Tok->IsFirst)) {
1305            unsigned LevelIndent =
1306                SourceMgr.getSpellingColumnNumber(Tok->Tok.getLocation()) - 1;
1307            // Remove trailing whitespace of the previous line if it was
1308            // touched.
1309            if (PreviousLineWasTouched || touchesEmptyLineBefore(TheLine)) {
1310              formatFirstToken(*Tok, PreviousLineLastToken, LevelIndent,
1311                               TheLine.InPPDirective);
1312            } else {
1313              Whitespaces.addUntouchableToken(*Tok, TheLine.InPPDirective);
1314            }
1315
1316            if (static_cast<int>(LevelIndent) - Offset >= 0)
1317              LevelIndent -= Offset;
1318            if (Tok->isNot(tok::comment))
1319              IndentForLevel[TheLine.Level] = LevelIndent;
1320          } else {
1321            Whitespaces.addUntouchableToken(*Tok, TheLine.InPPDirective);
1322          }
1323        }
1324        // If we did not reformat this unwrapped line, the column at the end of
1325        // the last token is unchanged - thus, we can calculate the end of the
1326        // last token.
1327        PreviousLineWasTouched = false;
1328      }
1329      PreviousLineLastToken = I->Last;
1330    }
1331    return Whitespaces.generateReplacements();
1332  }
1333
1334private:
1335  void deriveLocalStyle() {
1336    unsigned CountBoundToVariable = 0;
1337    unsigned CountBoundToType = 0;
1338    bool HasCpp03IncompatibleFormat = false;
1339    for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1340      if (!AnnotatedLines[i].First->Next)
1341        continue;
1342      FormatToken *Tok = AnnotatedLines[i].First->Next;
1343      while (Tok->Next) {
1344        if (Tok->Type == TT_PointerOrReference) {
1345          bool SpacesBefore =
1346              Tok->WhitespaceRange.getBegin() != Tok->WhitespaceRange.getEnd();
1347          bool SpacesAfter = Tok->Next->WhitespaceRange.getBegin() !=
1348                             Tok->Next->WhitespaceRange.getEnd();
1349          if (SpacesBefore && !SpacesAfter)
1350            ++CountBoundToVariable;
1351          else if (!SpacesBefore && SpacesAfter)
1352            ++CountBoundToType;
1353        }
1354
1355        if (Tok->Type == TT_TemplateCloser &&
1356            Tok->Previous->Type == TT_TemplateCloser &&
1357            Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd())
1358          HasCpp03IncompatibleFormat = true;
1359        Tok = Tok->Next;
1360      }
1361    }
1362    if (Style.DerivePointerBinding) {
1363      if (CountBoundToType > CountBoundToVariable)
1364        Style.PointerBindsToType = true;
1365      else if (CountBoundToType < CountBoundToVariable)
1366        Style.PointerBindsToType = false;
1367    }
1368    if (Style.Standard == FormatStyle::LS_Auto) {
1369      Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11
1370                                                  : FormatStyle::LS_Cpp03;
1371    }
1372  }
1373
1374  /// \brief Get the indent of \p Level from \p IndentForLevel.
1375  ///
1376  /// \p IndentForLevel must contain the indent for the level \c l
1377  /// at \p IndentForLevel[l], or a value < 0 if the indent for
1378  /// that level is unknown.
1379  unsigned getIndent(const std::vector<int> IndentForLevel, unsigned Level) {
1380    if (IndentForLevel[Level] != -1)
1381      return IndentForLevel[Level];
1382    if (Level == 0)
1383      return 0;
1384    return getIndent(IndentForLevel, Level - 1) + Style.IndentWidth;
1385  }
1386
1387  /// \brief Get the offset of the line relatively to the level.
1388  ///
1389  /// For example, 'public:' labels in classes are offset by 1 or 2
1390  /// characters to the left from their level.
1391  int getIndentOffset(const FormatToken &RootToken) {
1392    if (RootToken.isAccessSpecifier(false) || RootToken.isObjCAccessSpecifier())
1393      return Style.AccessModifierOffset;
1394    return 0;
1395  }
1396
1397  /// \brief Tries to merge lines into one.
1398  ///
1399  /// This will change \c Line and \c AnnotatedLine to contain the merged line,
1400  /// if possible; note that \c I will be incremented when lines are merged.
1401  void tryFitMultipleLinesInOne(unsigned Indent,
1402                                std::vector<AnnotatedLine>::iterator &I,
1403                                std::vector<AnnotatedLine>::iterator E) {
1404    // We can never merge stuff if there are trailing line comments.
1405    if (I->Last->Type == TT_LineComment)
1406      return;
1407
1408    unsigned Limit = Style.ColumnLimit - Indent;
1409    // If we already exceed the column limit, we set 'Limit' to 0. The different
1410    // tryMerge..() functions can then decide whether to still do merging.
1411    Limit = I->Last->TotalLength > Limit ? 0 : Limit - I->Last->TotalLength;
1412
1413    if (I + 1 == E || (I + 1)->Type == LT_Invalid)
1414      return;
1415
1416    if (I->Last->is(tok::l_brace)) {
1417      tryMergeSimpleBlock(I, E, Limit);
1418    } else if (Style.AllowShortIfStatementsOnASingleLine &&
1419               I->First->is(tok::kw_if)) {
1420      tryMergeSimpleControlStatement(I, E, Limit);
1421    } else if (Style.AllowShortLoopsOnASingleLine &&
1422               I->First->isOneOf(tok::kw_for, tok::kw_while)) {
1423      tryMergeSimpleControlStatement(I, E, Limit);
1424    } else if (I->InPPDirective &&
1425               (I->First->HasUnescapedNewline || I->First->IsFirst)) {
1426      tryMergeSimplePPDirective(I, E, Limit);
1427    }
1428  }
1429
1430  void tryMergeSimplePPDirective(std::vector<AnnotatedLine>::iterator &I,
1431                                 std::vector<AnnotatedLine>::iterator E,
1432                                 unsigned Limit) {
1433    if (Limit == 0)
1434      return;
1435    AnnotatedLine &Line = *I;
1436    if (!(I + 1)->InPPDirective || (I + 1)->First->HasUnescapedNewline)
1437      return;
1438    if (I + 2 != E && (I + 2)->InPPDirective &&
1439        !(I + 2)->First->HasUnescapedNewline)
1440      return;
1441    if (1 + (I + 1)->Last->TotalLength > Limit)
1442      return;
1443    join(Line, *(++I));
1444  }
1445
1446  void tryMergeSimpleControlStatement(std::vector<AnnotatedLine>::iterator &I,
1447                                      std::vector<AnnotatedLine>::iterator E,
1448                                      unsigned Limit) {
1449    if (Limit == 0)
1450      return;
1451    if ((I + 1)->InPPDirective != I->InPPDirective ||
1452        ((I + 1)->InPPDirective && (I + 1)->First->HasUnescapedNewline))
1453      return;
1454    AnnotatedLine &Line = *I;
1455    if (Line.Last->isNot(tok::r_paren))
1456      return;
1457    if (1 + (I + 1)->Last->TotalLength > Limit)
1458      return;
1459    if ((I + 1)->First->isOneOf(tok::semi, tok::kw_if, tok::kw_for,
1460                                tok::kw_while) ||
1461        (I + 1)->First->Type == TT_LineComment)
1462      return;
1463    // Only inline simple if's (no nested if or else).
1464    if (I + 2 != E && Line.First->is(tok::kw_if) &&
1465        (I + 2)->First->is(tok::kw_else))
1466      return;
1467    join(Line, *(++I));
1468  }
1469
1470  void tryMergeSimpleBlock(std::vector<AnnotatedLine>::iterator &I,
1471                           std::vector<AnnotatedLine>::iterator E,
1472                           unsigned Limit) {
1473    // No merging if the brace already is on the next line.
1474    if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
1475      return;
1476
1477    // First, check that the current line allows merging. This is the case if
1478    // we're not in a control flow statement and the last token is an opening
1479    // brace.
1480    AnnotatedLine &Line = *I;
1481    if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::r_brace,
1482                            tok::kw_else, tok::kw_try, tok::kw_catch,
1483                            tok::kw_for,
1484                            // This gets rid of all ObjC @ keywords and methods.
1485                            tok::at, tok::minus, tok::plus))
1486      return;
1487
1488    FormatToken *Tok = (I + 1)->First;
1489    if (Tok->is(tok::r_brace) && !Tok->MustBreakBefore &&
1490        (Tok->getNextNoneComment() == NULL ||
1491         Tok->getNextNoneComment()->is(tok::semi))) {
1492      // We merge empty blocks even if the line exceeds the column limit.
1493      Tok->SpacesRequiredBefore = 0;
1494      Tok->CanBreakBefore = true;
1495      join(Line, *(I + 1));
1496      I += 1;
1497    } else if (Limit != 0 && Line.First->isNot(tok::kw_namespace)) {
1498      // Check that we still have three lines and they fit into the limit.
1499      if (I + 2 == E || (I + 2)->Type == LT_Invalid ||
1500          !nextTwoLinesFitInto(I, Limit))
1501        return;
1502
1503      // Second, check that the next line does not contain any braces - if it
1504      // does, readability declines when putting it into a single line.
1505      if ((I + 1)->Last->Type == TT_LineComment || Tok->MustBreakBefore)
1506        return;
1507      do {
1508        if (Tok->isOneOf(tok::l_brace, tok::r_brace))
1509          return;
1510        Tok = Tok->Next;
1511      } while (Tok != NULL);
1512
1513      // Last, check that the third line contains a single closing brace.
1514      Tok = (I + 2)->First;
1515      if (Tok->getNextNoneComment() != NULL || Tok->isNot(tok::r_brace) ||
1516          Tok->MustBreakBefore)
1517        return;
1518
1519      join(Line, *(I + 1));
1520      join(Line, *(I + 2));
1521      I += 2;
1522    }
1523  }
1524
1525  bool nextTwoLinesFitInto(std::vector<AnnotatedLine>::iterator I,
1526                           unsigned Limit) {
1527    return 1 + (I + 1)->Last->TotalLength + 1 + (I + 2)->Last->TotalLength <=
1528           Limit;
1529  }
1530
1531  void join(AnnotatedLine &A, const AnnotatedLine &B) {
1532    assert(!A.Last->Next);
1533    assert(!B.First->Previous);
1534    A.Last->Next = B.First;
1535    B.First->Previous = A.Last;
1536    unsigned LengthA = A.Last->TotalLength + B.First->SpacesRequiredBefore;
1537    for (FormatToken *Tok = B.First; Tok; Tok = Tok->Next) {
1538      Tok->TotalLength += LengthA;
1539      A.Last = Tok;
1540    }
1541  }
1542
1543  bool touchesRanges(const CharSourceRange &Range) {
1544    for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
1545      if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),
1546                                               Ranges[i].getBegin()) &&
1547          !SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(),
1548                                               Range.getBegin()))
1549        return true;
1550    }
1551    return false;
1552  }
1553
1554  bool touchesLine(const AnnotatedLine &TheLine) {
1555    const FormatToken *First = TheLine.First;
1556    const FormatToken *Last = TheLine.Last;
1557    CharSourceRange LineRange = CharSourceRange::getCharRange(
1558        First->WhitespaceRange.getBegin().getLocWithOffset(
1559            First->LastNewlineOffset),
1560        Last->Tok.getLocation().getLocWithOffset(Last->TokenLength - 1));
1561    return touchesRanges(LineRange);
1562  }
1563
1564  bool touchesPPDirective(std::vector<AnnotatedLine>::iterator I,
1565                          std::vector<AnnotatedLine>::iterator E) {
1566    for (; I != E; ++I) {
1567      if (I->First->HasUnescapedNewline)
1568        return false;
1569      if (touchesLine(*I))
1570        return true;
1571    }
1572    return false;
1573  }
1574
1575  bool touchesEmptyLineBefore(const AnnotatedLine &TheLine) {
1576    const FormatToken *First = TheLine.First;
1577    CharSourceRange LineRange = CharSourceRange::getCharRange(
1578        First->WhitespaceRange.getBegin(),
1579        First->WhitespaceRange.getBegin().getLocWithOffset(
1580            First->LastNewlineOffset));
1581    return touchesRanges(LineRange);
1582  }
1583
1584  virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) {
1585    AnnotatedLines.push_back(AnnotatedLine(TheLine));
1586  }
1587
1588  /// \brief Add a new line and the required indent before the first Token
1589  /// of the \c UnwrappedLine if there was no structural parsing error.
1590  /// Returns the indent level of the \c UnwrappedLine.
1591  void formatFirstToken(const FormatToken &RootToken,
1592                        const FormatToken *PreviousToken, unsigned Indent,
1593                        bool InPPDirective) {
1594    unsigned Newlines =
1595        std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);
1596    if (Newlines == 0 && !RootToken.IsFirst)
1597      Newlines = 1;
1598
1599    // Insert extra new line before access specifiers.
1600    if (PreviousToken && PreviousToken->isOneOf(tok::semi, tok::r_brace) &&
1601        RootToken.isAccessSpecifier() && RootToken.NewlinesBefore == 1)
1602      ++Newlines;
1603
1604    Whitespaces.replaceWhitespace(
1605        RootToken, Newlines, Indent, Indent,
1606        InPPDirective && !RootToken.HasUnescapedNewline);
1607  }
1608
1609  FormatStyle Style;
1610  Lexer &Lex;
1611  SourceManager &SourceMgr;
1612  WhitespaceManager Whitespaces;
1613  std::vector<CharSourceRange> Ranges;
1614  std::vector<AnnotatedLine> AnnotatedLines;
1615};
1616
1617tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
1618                               SourceManager &SourceMgr,
1619                               std::vector<CharSourceRange> Ranges) {
1620  Formatter formatter(Style, Lex, SourceMgr, Ranges);
1621  return formatter.format();
1622}
1623
1624tooling::Replacements reformat(const FormatStyle &Style, StringRef Code,
1625                               std::vector<tooling::Range> Ranges,
1626                               StringRef FileName) {
1627  FileManager Files((FileSystemOptions()));
1628  DiagnosticsEngine Diagnostics(
1629      IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
1630      new DiagnosticOptions);
1631  SourceManager SourceMgr(Diagnostics, Files);
1632  llvm::MemoryBuffer *Buf = llvm::MemoryBuffer::getMemBuffer(Code, FileName);
1633  const clang::FileEntry *Entry =
1634      Files.getVirtualFile(FileName, Buf->getBufferSize(), 0);
1635  SourceMgr.overrideFileContents(Entry, Buf);
1636  FileID ID =
1637      SourceMgr.createFileID(Entry, SourceLocation(), clang::SrcMgr::C_User);
1638  Lexer Lex(ID, SourceMgr.getBuffer(ID), SourceMgr, getFormattingLangOpts());
1639  SourceLocation StartOfFile = SourceMgr.getLocForStartOfFile(ID);
1640  std::vector<CharSourceRange> CharRanges;
1641  for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
1642    SourceLocation Start = StartOfFile.getLocWithOffset(Ranges[i].getOffset());
1643    SourceLocation End = Start.getLocWithOffset(Ranges[i].getLength());
1644    CharRanges.push_back(CharSourceRange::getCharRange(Start, End));
1645  }
1646  return reformat(Style, Lex, SourceMgr, CharRanges);
1647}
1648
1649LangOptions getFormattingLangOpts() {
1650  LangOptions LangOpts;
1651  LangOpts.CPlusPlus = 1;
1652  LangOpts.CPlusPlus11 = 1;
1653  LangOpts.LineComment = 1;
1654  LangOpts.Bool = 1;
1655  LangOpts.ObjC1 = 1;
1656  LangOpts.ObjC2 = 1;
1657  return LangOpts;
1658}
1659
1660} // namespace format
1661} // namespace clang
1662