Format.cpp revision 7df56bfcf7186f73c99564cd54216f07a8db7352
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 "ContinuationIndenter.h"
19#include "TokenAnnotator.h"
20#include "UnwrappedLineParser.h"
21#include "WhitespaceManager.h"
22#include "clang/Basic/Diagnostic.h"
23#include "clang/Basic/SourceManager.h"
24#include "clang/Format/Format.h"
25#include "clang/Lex/Lexer.h"
26#include "llvm/ADT/STLExtras.h"
27#include "llvm/Support/Allocator.h"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/YAMLTraits.h"
30#include <queue>
31#include <string>
32
33namespace llvm {
34namespace yaml {
35template <>
36struct ScalarEnumerationTraits<clang::format::FormatStyle::LanguageStandard> {
37  static void enumeration(IO &IO,
38                          clang::format::FormatStyle::LanguageStandard &Value) {
39    IO.enumCase(Value, "C++03", clang::format::FormatStyle::LS_Cpp03);
40    IO.enumCase(Value, "C++11", clang::format::FormatStyle::LS_Cpp11);
41    IO.enumCase(Value, "Auto", clang::format::FormatStyle::LS_Auto);
42  }
43};
44
45template <>
46struct ScalarEnumerationTraits<clang::format::FormatStyle::BraceBreakingStyle> {
47  static void
48  enumeration(IO &IO, clang::format::FormatStyle::BraceBreakingStyle &Value) {
49    IO.enumCase(Value, "Attach", clang::format::FormatStyle::BS_Attach);
50    IO.enumCase(Value, "Linux", clang::format::FormatStyle::BS_Linux);
51    IO.enumCase(Value, "Stroustrup", clang::format::FormatStyle::BS_Stroustrup);
52    IO.enumCase(Value, "Allman", clang::format::FormatStyle::BS_Allman);
53  }
54};
55
56template <>
57struct ScalarEnumerationTraits<
58    clang::format::FormatStyle::NamespaceIndentationKind> {
59  static void
60  enumeration(IO &IO,
61              clang::format::FormatStyle::NamespaceIndentationKind &Value) {
62    IO.enumCase(Value, "None", clang::format::FormatStyle::NI_None);
63    IO.enumCase(Value, "Inner", clang::format::FormatStyle::NI_Inner);
64    IO.enumCase(Value, "All", clang::format::FormatStyle::NI_All);
65  }
66};
67
68template <> struct MappingTraits<clang::format::FormatStyle> {
69  static void mapping(llvm::yaml::IO &IO, clang::format::FormatStyle &Style) {
70    if (IO.outputting()) {
71      StringRef StylesArray[] = { "LLVM", "Google", "Chromium", "Mozilla" };
72      ArrayRef<StringRef> Styles(StylesArray);
73      for (size_t i = 0, e = Styles.size(); i < e; ++i) {
74        StringRef StyleName(Styles[i]);
75        clang::format::FormatStyle PredefinedStyle;
76        if (clang::format::getPredefinedStyle(StyleName, &PredefinedStyle) &&
77            Style == PredefinedStyle) {
78          IO.mapOptional("# BasedOnStyle", StyleName);
79          break;
80        }
81      }
82    } else {
83      StringRef BasedOnStyle;
84      IO.mapOptional("BasedOnStyle", BasedOnStyle);
85      if (!BasedOnStyle.empty())
86        if (!clang::format::getPredefinedStyle(BasedOnStyle, &Style)) {
87          IO.setError(Twine("Unknown value for BasedOnStyle: ", BasedOnStyle));
88          return;
89        }
90    }
91
92    IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset);
93    IO.mapOptional("ConstructorInitializerIndentWidth",
94                   Style.ConstructorInitializerIndentWidth);
95    IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft);
96    IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments);
97    IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine",
98                   Style.AllowAllParametersOfDeclarationOnNextLine);
99    IO.mapOptional("AllowShortIfStatementsOnASingleLine",
100                   Style.AllowShortIfStatementsOnASingleLine);
101    IO.mapOptional("AllowShortLoopsOnASingleLine",
102                   Style.AllowShortLoopsOnASingleLine);
103    IO.mapOptional("AlwaysBreakTemplateDeclarations",
104                   Style.AlwaysBreakTemplateDeclarations);
105    IO.mapOptional("AlwaysBreakBeforeMultilineStrings",
106                   Style.AlwaysBreakBeforeMultilineStrings);
107    IO.mapOptional("BreakBeforeBinaryOperators",
108                   Style.BreakBeforeBinaryOperators);
109    IO.mapOptional("BreakConstructorInitializersBeforeComma",
110                   Style.BreakConstructorInitializersBeforeComma);
111    IO.mapOptional("BinPackParameters", Style.BinPackParameters);
112    IO.mapOptional("ColumnLimit", Style.ColumnLimit);
113    IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine",
114                   Style.ConstructorInitializerAllOnOneLineOrOnePerLine);
115    IO.mapOptional("DerivePointerBinding", Style.DerivePointerBinding);
116    IO.mapOptional("ExperimentalAutoDetectBinPacking",
117                   Style.ExperimentalAutoDetectBinPacking);
118    IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels);
119    IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep);
120    IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation);
121    IO.mapOptional("ObjCSpaceBeforeProtocolList",
122                   Style.ObjCSpaceBeforeProtocolList);
123    IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment);
124    IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString);
125    IO.mapOptional("PenaltyBreakFirstLessLess",
126                   Style.PenaltyBreakFirstLessLess);
127    IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter);
128    IO.mapOptional("PenaltyReturnTypeOnItsOwnLine",
129                   Style.PenaltyReturnTypeOnItsOwnLine);
130    IO.mapOptional("PointerBindsToType", Style.PointerBindsToType);
131    IO.mapOptional("SpacesBeforeTrailingComments",
132                   Style.SpacesBeforeTrailingComments);
133    IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle);
134    IO.mapOptional("Standard", Style.Standard);
135    IO.mapOptional("IndentWidth", Style.IndentWidth);
136    IO.mapOptional("UseTab", Style.UseTab);
137    IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces);
138    IO.mapOptional("IndentFunctionDeclarationAfterType",
139                   Style.IndentFunctionDeclarationAfterType);
140    IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses);
141    IO.mapOptional("SpaceInEmptyParentheses",
142                   Style.SpaceInEmptyParentheses);
143    IO.mapOptional("SpacesInCStyleCastParentheses",
144                   Style.SpacesInCStyleCastParentheses);
145    IO.mapOptional("SpaceAfterControlStatementKeyword",
146                   Style.SpaceAfterControlStatementKeyword);
147  }
148};
149}
150}
151
152namespace clang {
153namespace format {
154
155void setDefaultPenalties(FormatStyle &Style) {
156  Style.PenaltyBreakComment = 45;
157  Style.PenaltyBreakFirstLessLess = 120;
158  Style.PenaltyBreakString = 1000;
159  Style.PenaltyExcessCharacter = 1000000;
160}
161
162FormatStyle getLLVMStyle() {
163  FormatStyle LLVMStyle;
164  LLVMStyle.AccessModifierOffset = -2;
165  LLVMStyle.AlignEscapedNewlinesLeft = false;
166  LLVMStyle.AlignTrailingComments = true;
167  LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
168  LLVMStyle.AllowShortIfStatementsOnASingleLine = false;
169  LLVMStyle.AllowShortLoopsOnASingleLine = false;
170  LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
171  LLVMStyle.AlwaysBreakTemplateDeclarations = false;
172  LLVMStyle.BinPackParameters = true;
173  LLVMStyle.BreakBeforeBinaryOperators = false;
174  LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
175  LLVMStyle.BreakConstructorInitializersBeforeComma = false;
176  LLVMStyle.ColumnLimit = 80;
177  LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
178  LLVMStyle.ConstructorInitializerIndentWidth = 4;
179  LLVMStyle.Cpp11BracedListStyle = false;
180  LLVMStyle.DerivePointerBinding = false;
181  LLVMStyle.ExperimentalAutoDetectBinPacking = false;
182  LLVMStyle.IndentCaseLabels = false;
183  LLVMStyle.IndentFunctionDeclarationAfterType = false;
184  LLVMStyle.IndentWidth = 2;
185  LLVMStyle.MaxEmptyLinesToKeep = 1;
186  LLVMStyle.NamespaceIndentation = FormatStyle::NI_None;
187  LLVMStyle.ObjCSpaceBeforeProtocolList = true;
188  LLVMStyle.PointerBindsToType = false;
189  LLVMStyle.SpacesBeforeTrailingComments = 1;
190  LLVMStyle.Standard = FormatStyle::LS_Cpp03;
191  LLVMStyle.UseTab = false;
192  LLVMStyle.SpacesInParentheses = false;
193  LLVMStyle.SpaceInEmptyParentheses = false;
194  LLVMStyle.SpacesInCStyleCastParentheses = false;
195  LLVMStyle.SpaceAfterControlStatementKeyword = true;
196
197  setDefaultPenalties(LLVMStyle);
198  LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
199
200  return LLVMStyle;
201}
202
203FormatStyle getGoogleStyle() {
204  FormatStyle GoogleStyle;
205  GoogleStyle.AccessModifierOffset = -1;
206  GoogleStyle.AlignEscapedNewlinesLeft = true;
207  GoogleStyle.AlignTrailingComments = true;
208  GoogleStyle.AllowAllParametersOfDeclarationOnNextLine = true;
209  GoogleStyle.AllowShortIfStatementsOnASingleLine = true;
210  GoogleStyle.AllowShortLoopsOnASingleLine = true;
211  GoogleStyle.AlwaysBreakBeforeMultilineStrings = true;
212  GoogleStyle.AlwaysBreakTemplateDeclarations = true;
213  GoogleStyle.BinPackParameters = true;
214  GoogleStyle.BreakBeforeBinaryOperators = false;
215  GoogleStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
216  GoogleStyle.BreakConstructorInitializersBeforeComma = false;
217  GoogleStyle.ColumnLimit = 80;
218  GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
219  GoogleStyle.ConstructorInitializerIndentWidth = 4;
220  GoogleStyle.Cpp11BracedListStyle = true;
221  GoogleStyle.DerivePointerBinding = true;
222  GoogleStyle.ExperimentalAutoDetectBinPacking = false;
223  GoogleStyle.IndentCaseLabels = true;
224  GoogleStyle.IndentFunctionDeclarationAfterType = true;
225  GoogleStyle.IndentWidth = 2;
226  GoogleStyle.MaxEmptyLinesToKeep = 1;
227  GoogleStyle.NamespaceIndentation = FormatStyle::NI_None;
228  GoogleStyle.ObjCSpaceBeforeProtocolList = false;
229  GoogleStyle.PointerBindsToType = true;
230  GoogleStyle.SpacesBeforeTrailingComments = 2;
231  GoogleStyle.Standard = FormatStyle::LS_Auto;
232  GoogleStyle.UseTab = false;
233  GoogleStyle.SpacesInParentheses = false;
234  GoogleStyle.SpaceInEmptyParentheses = false;
235  GoogleStyle.SpacesInCStyleCastParentheses = false;
236  GoogleStyle.SpaceAfterControlStatementKeyword = true;
237
238  setDefaultPenalties(GoogleStyle);
239  GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
240
241  return GoogleStyle;
242}
243
244FormatStyle getChromiumStyle() {
245  FormatStyle ChromiumStyle = getGoogleStyle();
246  ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false;
247  ChromiumStyle.AllowShortIfStatementsOnASingleLine = false;
248  ChromiumStyle.AllowShortLoopsOnASingleLine = false;
249  ChromiumStyle.BinPackParameters = false;
250  ChromiumStyle.DerivePointerBinding = false;
251  ChromiumStyle.Standard = FormatStyle::LS_Cpp03;
252  return ChromiumStyle;
253}
254
255FormatStyle getMozillaStyle() {
256  FormatStyle MozillaStyle = getLLVMStyle();
257  MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false;
258  MozillaStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
259  MozillaStyle.DerivePointerBinding = true;
260  MozillaStyle.IndentCaseLabels = true;
261  MozillaStyle.ObjCSpaceBeforeProtocolList = false;
262  MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200;
263  MozillaStyle.PointerBindsToType = true;
264  return MozillaStyle;
265}
266
267FormatStyle getWebKitStyle() {
268  FormatStyle Style = getLLVMStyle();
269  Style.AccessModifierOffset = -4;
270  Style.AlignTrailingComments = false;
271  Style.BreakBeforeBinaryOperators = true;
272  Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
273  Style.BreakConstructorInitializersBeforeComma = true;
274  Style.ColumnLimit = 0;
275  Style.IndentWidth = 4;
276  Style.NamespaceIndentation = FormatStyle::NI_Inner;
277  Style.PointerBindsToType = true;
278  return Style;
279}
280
281bool getPredefinedStyle(StringRef Name, FormatStyle *Style) {
282  if (Name.equals_lower("llvm"))
283    *Style = getLLVMStyle();
284  else if (Name.equals_lower("chromium"))
285    *Style = getChromiumStyle();
286  else if (Name.equals_lower("mozilla"))
287    *Style = getMozillaStyle();
288  else if (Name.equals_lower("google"))
289    *Style = getGoogleStyle();
290  else if (Name.equals_lower("webkit"))
291    *Style = getWebKitStyle();
292  else
293    return false;
294
295  return true;
296}
297
298llvm::error_code parseConfiguration(StringRef Text, FormatStyle *Style) {
299  if (Text.trim().empty())
300    return llvm::make_error_code(llvm::errc::invalid_argument);
301  llvm::yaml::Input Input(Text);
302  Input >> *Style;
303  return Input.error();
304}
305
306std::string configurationAsText(const FormatStyle &Style) {
307  std::string Text;
308  llvm::raw_string_ostream Stream(Text);
309  llvm::yaml::Output Output(Stream);
310  // We use the same mapping method for input and output, so we need a non-const
311  // reference here.
312  FormatStyle NonConstStyle = Style;
313  Output << NonConstStyle;
314  return Stream.str();
315}
316
317namespace {
318
319class NoColumnLimitFormatter {
320public:
321  NoColumnLimitFormatter(ContinuationIndenter *Indenter)
322      : Indenter(Indenter) {}
323
324  /// \brief Formats the line starting at \p State, simply keeping all of the
325  /// input's line breaking decisions.
326  void format() {
327    LineState State = Indenter->getInitialState();
328    while (State.NextToken != NULL) {
329      bool Newline =
330          Indenter->mustBreak(State) ||
331          (Indenter->canBreak(State) && State.NextToken->NewlinesBefore > 0);
332      Indenter->addTokenToState(State, Newline, /*DryRun=*/false);
333    }
334  }
335private:
336  ContinuationIndenter *Indenter;
337};
338
339class UnwrappedLineFormatter {
340public:
341  UnwrappedLineFormatter(ContinuationIndenter *Indenter,
342                         const FormatStyle &Style, const AnnotatedLine &Line)
343      : Indenter(Indenter), Style(Style), Line(Line), Count(0) {}
344
345  /// \brief Formats an \c UnwrappedLine.
346  void format() {
347    LineState State = Indenter->getInitialState();
348
349    // If the ObjC method declaration does not fit on a line, we should format
350    // it with one arg per line.
351    if (Line.Type == LT_ObjCMethodDecl)
352      State.Stack.back().BreakBeforeParameter = true;
353
354    // Find best solution in solution space.
355    analyzeSolutionSpace(State);
356  }
357
358private:
359  /// \brief An edge in the solution space from \c Previous->State to \c State,
360  /// inserting a newline dependent on the \c NewLine.
361  struct StateNode {
362    StateNode(const LineState &State, bool NewLine, StateNode *Previous)
363        : State(State), NewLine(NewLine), Previous(Previous) {}
364    LineState State;
365    bool NewLine;
366    StateNode *Previous;
367  };
368
369  /// \brief A pair of <penalty, count> that is used to prioritize the BFS on.
370  ///
371  /// In case of equal penalties, we want to prefer states that were inserted
372  /// first. During state generation we make sure that we insert states first
373  /// that break the line as late as possible.
374  typedef std::pair<unsigned, unsigned> OrderedPenalty;
375
376  /// \brief An item in the prioritized BFS search queue. The \c StateNode's
377  /// \c State has the given \c OrderedPenalty.
378  typedef std::pair<OrderedPenalty, StateNode *> QueueItem;
379
380  /// \brief The BFS queue type.
381  typedef std::priority_queue<QueueItem, std::vector<QueueItem>,
382                              std::greater<QueueItem> > QueueType;
383
384  /// \brief Analyze the entire solution space starting from \p InitialState.
385  ///
386  /// This implements a variant of Dijkstra's algorithm on the graph that spans
387  /// the solution space (\c LineStates are the nodes). The algorithm tries to
388  /// find the shortest path (the one with lowest penalty) from \p InitialState
389  /// to a state where all tokens are placed.
390  void analyzeSolutionSpace(LineState &InitialState) {
391    std::set<LineState> Seen;
392
393    // Insert start element into queue.
394    StateNode *Node =
395        new (Allocator.Allocate()) StateNode(InitialState, false, NULL);
396    Queue.push(QueueItem(OrderedPenalty(0, Count), Node));
397    ++Count;
398
399    // While not empty, take first element and follow edges.
400    while (!Queue.empty()) {
401      unsigned Penalty = Queue.top().first.first;
402      StateNode *Node = Queue.top().second;
403      if (Node->State.NextToken == NULL) {
404        DEBUG(llvm::dbgs() << "\n---\nPenalty for line: " << Penalty << "\n");
405        break;
406      }
407      Queue.pop();
408
409      // Cut off the analysis of certain solutions if the analysis gets too
410      // complex. See description of IgnoreStackForComparison.
411      if (Count > 10000)
412        Node->State.IgnoreStackForComparison = true;
413
414      if (!Seen.insert(Node->State).second)
415        // State already examined with lower penalty.
416        continue;
417
418      addNextStateToQueue(Penalty, Node, /*NewLine=*/false);
419      addNextStateToQueue(Penalty, Node, /*NewLine=*/true);
420    }
421
422    if (Queue.empty())
423      // We were unable to find a solution, do nothing.
424      // FIXME: Add diagnostic?
425      return;
426
427    // Reconstruct the solution.
428    reconstructPath(InitialState, Queue.top().second);
429    DEBUG(llvm::dbgs() << "Total number of analyzed states: " << Count << "\n");
430    DEBUG(llvm::dbgs() << "---\n");
431  }
432
433  void reconstructPath(LineState &State, StateNode *Current) {
434    std::deque<StateNode *> Path;
435    // We do not need a break before the initial token.
436    while (Current->Previous) {
437      Path.push_front(Current);
438      Current = Current->Previous;
439    }
440    for (std::deque<StateNode *>::iterator I = Path.begin(), E = Path.end();
441         I != E; ++I) {
442      DEBUG({
443        if ((*I)->NewLine) {
444          llvm::dbgs() << "Penalty for splitting before "
445                       << (*I)->Previous->State.NextToken->Tok.getName() << ": "
446                       << (*I)->Previous->State.NextToken->SplitPenalty << "\n";
447        }
448      });
449      Indenter->addTokenToState(State, (*I)->NewLine, false);
450    }
451  }
452
453  /// \brief Add the following state to the analysis queue \c Queue.
454  ///
455  /// Assume the current state is \p PreviousNode and has been reached with a
456  /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true.
457  void addNextStateToQueue(unsigned Penalty, StateNode *PreviousNode,
458                           bool NewLine) {
459    if (NewLine && !Indenter->canBreak(PreviousNode->State))
460      return;
461    if (!NewLine && Indenter->mustBreak(PreviousNode->State))
462      return;
463    if (NewLine) {
464      if (!PreviousNode->State.Stack.back().ContainsLineBreak)
465        Penalty += 15;
466      Penalty += PreviousNode->State.NextToken->SplitPenalty;
467    }
468
469    StateNode *Node = new (Allocator.Allocate())
470        StateNode(PreviousNode->State, NewLine, PreviousNode);
471    Penalty += Indenter->addTokenToState(Node->State, NewLine, true);
472    if (Node->State.Column > Indenter->getColumnLimit()) {
473      unsigned ExcessCharacters =
474          Node->State.Column - Indenter->getColumnLimit();
475      Penalty += Style.PenaltyExcessCharacter * ExcessCharacters;
476    }
477
478    Queue.push(QueueItem(OrderedPenalty(Penalty, Count), Node));
479    ++Count;
480  }
481
482  ContinuationIndenter *Indenter;
483  FormatStyle Style;
484  const AnnotatedLine &Line;
485
486  llvm::SpecificBumpPtrAllocator<StateNode> Allocator;
487  QueueType Queue;
488  // Increasing count of \c StateNode items we have created. This is used
489  // to create a deterministic order independent of the container.
490  unsigned Count;
491};
492
493class FormatTokenLexer {
494public:
495  FormatTokenLexer(Lexer &Lex, SourceManager &SourceMgr,
496                   encoding::Encoding Encoding)
497      : FormatTok(NULL), GreaterStashed(false), TrailingWhitespace(0), Lex(Lex),
498        SourceMgr(SourceMgr), IdentTable(getFormattingLangOpts()),
499        Encoding(Encoding) {
500    Lex.SetKeepWhitespaceMode(true);
501  }
502
503  ArrayRef<FormatToken *> lex() {
504    assert(Tokens.empty());
505    do {
506      Tokens.push_back(getNextToken());
507    } while (Tokens.back()->Tok.isNot(tok::eof));
508    return Tokens;
509  }
510
511  IdentifierTable &getIdentTable() { return IdentTable; }
512
513private:
514  FormatToken *getNextToken() {
515    if (GreaterStashed) {
516      // Create a synthesized second '>' token.
517      Token Greater = FormatTok->Tok;
518      FormatTok = new (Allocator.Allocate()) FormatToken;
519      FormatTok->Tok = Greater;
520      SourceLocation GreaterLocation =
521          FormatTok->Tok.getLocation().getLocWithOffset(1);
522      FormatTok->WhitespaceRange =
523          SourceRange(GreaterLocation, GreaterLocation);
524      FormatTok->TokenText = ">";
525      FormatTok->CodePointCount = 1;
526      GreaterStashed = false;
527      return FormatTok;
528    }
529
530    FormatTok = new (Allocator.Allocate()) FormatToken;
531    readRawToken(*FormatTok);
532    SourceLocation WhitespaceStart =
533        FormatTok->Tok.getLocation().getLocWithOffset(-TrailingWhitespace);
534    if (SourceMgr.getFileOffset(WhitespaceStart) == 0)
535      FormatTok->IsFirst = true;
536
537    // Consume and record whitespace until we find a significant token.
538    unsigned WhitespaceLength = TrailingWhitespace;
539    while (FormatTok->Tok.is(tok::unknown)) {
540      unsigned Newlines = FormatTok->TokenText.count('\n');
541      if (Newlines > 0)
542        FormatTok->LastNewlineOffset =
543            WhitespaceLength + FormatTok->TokenText.rfind('\n') + 1;
544      FormatTok->NewlinesBefore += Newlines;
545      unsigned EscapedNewlines = FormatTok->TokenText.count("\\\n");
546      FormatTok->HasUnescapedNewline |= EscapedNewlines != Newlines;
547      WhitespaceLength += FormatTok->Tok.getLength();
548
549      readRawToken(*FormatTok);
550    }
551
552    // In case the token starts with escaped newlines, we want to
553    // take them into account as whitespace - this pattern is quite frequent
554    // in macro definitions.
555    // FIXME: What do we want to do with other escaped spaces, and escaped
556    // spaces or newlines in the middle of tokens?
557    // FIXME: Add a more explicit test.
558    while (FormatTok->TokenText.size() > 1 && FormatTok->TokenText[0] == '\\' &&
559           FormatTok->TokenText[1] == '\n') {
560      // FIXME: ++FormatTok->NewlinesBefore is missing...
561      WhitespaceLength += 2;
562      FormatTok->TokenText = FormatTok->TokenText.substr(2);
563    }
564
565    TrailingWhitespace = 0;
566    if (FormatTok->Tok.is(tok::comment)) {
567      StringRef UntrimmedText = FormatTok->TokenText;
568      FormatTok->TokenText = FormatTok->TokenText.rtrim();
569      TrailingWhitespace = UntrimmedText.size() - FormatTok->TokenText.size();
570    } else if (FormatTok->Tok.is(tok::raw_identifier)) {
571      IdentifierInfo &Info = IdentTable.get(FormatTok->TokenText);
572      FormatTok->Tok.setIdentifierInfo(&Info);
573      FormatTok->Tok.setKind(Info.getTokenID());
574    } else if (FormatTok->Tok.is(tok::greatergreater)) {
575      FormatTok->Tok.setKind(tok::greater);
576      FormatTok->TokenText = FormatTok->TokenText.substr(0, 1);
577      GreaterStashed = true;
578    }
579
580    // Now FormatTok is the next non-whitespace token.
581    FormatTok->CodePointCount =
582        encoding::getCodePointCount(FormatTok->TokenText, Encoding);
583
584    FormatTok->WhitespaceRange = SourceRange(
585        WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength));
586    return FormatTok;
587  }
588
589  FormatToken *FormatTok;
590  bool GreaterStashed;
591  unsigned TrailingWhitespace;
592  Lexer &Lex;
593  SourceManager &SourceMgr;
594  IdentifierTable IdentTable;
595  encoding::Encoding Encoding;
596  llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
597  SmallVector<FormatToken *, 16> Tokens;
598
599  void readRawToken(FormatToken &Tok) {
600    Lex.LexFromRawLexer(Tok.Tok);
601    Tok.TokenText = StringRef(SourceMgr.getCharacterData(Tok.Tok.getLocation()),
602                              Tok.Tok.getLength());
603
604    // For formatting, treat unterminated string literals like normal string
605    // literals.
606    if (Tok.is(tok::unknown) && !Tok.TokenText.empty() &&
607        Tok.TokenText[0] == '"') {
608      Tok.Tok.setKind(tok::string_literal);
609      Tok.IsUnterminatedLiteral = true;
610    }
611  }
612};
613
614class Formatter : public UnwrappedLineConsumer {
615public:
616  Formatter(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr,
617            const std::vector<CharSourceRange> &Ranges)
618      : Style(Style), Lex(Lex), SourceMgr(SourceMgr),
619        Whitespaces(SourceMgr, Style), Ranges(Ranges),
620        Encoding(encoding::detectEncoding(Lex.getBuffer())) {
621    DEBUG(llvm::dbgs() << "File encoding: "
622                       << (Encoding == encoding::Encoding_UTF8 ? "UTF8"
623                                                               : "unknown")
624                       << "\n");
625  }
626
627  virtual ~Formatter() {}
628
629  tooling::Replacements format() {
630    FormatTokenLexer Tokens(Lex, SourceMgr, Encoding);
631
632    UnwrappedLineParser Parser(Style, Tokens.lex(), *this);
633    bool StructuralError = Parser.parse();
634    TokenAnnotator Annotator(Style, Tokens.getIdentTable().get("in"));
635    for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
636      Annotator.annotate(AnnotatedLines[i]);
637    }
638    deriveLocalStyle();
639    for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
640      Annotator.calculateFormattingInformation(AnnotatedLines[i]);
641    }
642
643    // Adapt level to the next line if this is a comment.
644    // FIXME: Can/should this be done in the UnwrappedLineParser?
645    const AnnotatedLine *NextNonCommentLine = NULL;
646    for (unsigned i = AnnotatedLines.size() - 1; i > 0; --i) {
647      if (NextNonCommentLine && AnnotatedLines[i].First->is(tok::comment) &&
648          !AnnotatedLines[i].First->Next)
649        AnnotatedLines[i].Level = NextNonCommentLine->Level;
650      else
651        NextNonCommentLine = AnnotatedLines[i].First->isNot(tok::r_brace)
652                                 ? &AnnotatedLines[i]
653                                 : NULL;
654    }
655
656    std::vector<int> IndentForLevel;
657    bool PreviousLineWasTouched = false;
658    const FormatToken *PreviousLineLastToken = 0;
659    bool FormatPPDirective = false;
660    for (std::vector<AnnotatedLine>::iterator I = AnnotatedLines.begin(),
661                                              E = AnnotatedLines.end();
662         I != E; ++I) {
663      const AnnotatedLine &TheLine = *I;
664      const FormatToken *FirstTok = TheLine.First;
665      int Offset = getIndentOffset(*TheLine.First);
666
667      // Check whether this line is part of a formatted preprocessor directive.
668      if (FirstTok->HasUnescapedNewline)
669        FormatPPDirective = false;
670      if (!FormatPPDirective && TheLine.InPPDirective &&
671          (touchesLine(TheLine) || touchesPPDirective(I + 1, E)))
672        FormatPPDirective = true;
673
674      // Determine indent and try to merge multiple unwrapped lines.
675      while (IndentForLevel.size() <= TheLine.Level)
676        IndentForLevel.push_back(-1);
677      IndentForLevel.resize(TheLine.Level + 1);
678      unsigned Indent = getIndent(IndentForLevel, TheLine.Level);
679      if (static_cast<int>(Indent) + Offset >= 0)
680        Indent += Offset;
681      tryFitMultipleLinesInOne(Indent, I, E);
682
683      bool WasMoved = PreviousLineWasTouched && FirstTok->NewlinesBefore == 0;
684      if (TheLine.First->is(tok::eof)) {
685        if (PreviousLineWasTouched) {
686          unsigned NewLines = std::min(FirstTok->NewlinesBefore, 1u);
687          Whitespaces.replaceWhitespace(*TheLine.First, NewLines, /*Indent*/ 0,
688                                        /*TargetColumn*/ 0);
689        }
690      } else if (TheLine.Type != LT_Invalid &&
691                 (WasMoved || FormatPPDirective || touchesLine(TheLine))) {
692        unsigned LevelIndent = getIndent(IndentForLevel, TheLine.Level);
693        if (FirstTok->WhitespaceRange.isValid() &&
694            // Insert a break even if there is a structural error in case where
695            // we break apart a line consisting of multiple unwrapped lines.
696            (FirstTok->NewlinesBefore == 0 || !StructuralError)) {
697          formatFirstToken(*TheLine.First, PreviousLineLastToken, Indent,
698                           TheLine.InPPDirective);
699        } else {
700          Indent = LevelIndent =
701              SourceMgr.getSpellingColumnNumber(FirstTok->Tok.getLocation()) -
702              1;
703        }
704        ContinuationIndenter Indenter(Style, SourceMgr, TheLine, Indent,
705                                      Whitespaces, Encoding,
706                                      BinPackInconclusiveFunctions);
707
708        // If everything fits on a single line, just put it there.
709        unsigned ColumnLimit = Style.ColumnLimit;
710        if ((I + 1) != E && (I + 1)->InPPDirective &&
711            !(I + 1)->First->HasUnescapedNewline)
712          ColumnLimit = Indenter.getColumnLimit();
713
714        if (I->Last->TotalLength + Indent <= ColumnLimit) {
715          LineState State = Indenter.getInitialState();
716          while (State.NextToken != NULL)
717            Indenter.addTokenToState(State, false, false);
718        } else if (Style.ColumnLimit == 0) {
719          NoColumnLimitFormatter Formatter(&Indenter);
720          Formatter.format();
721        } else {
722          UnwrappedLineFormatter Formatter(&Indenter, Style, TheLine);
723          Formatter.format();
724        }
725
726        IndentForLevel[TheLine.Level] = LevelIndent;
727        PreviousLineWasTouched = true;
728      } else {
729        // Format the first token if necessary, and notify the WhitespaceManager
730        // about the unchanged whitespace.
731        for (const FormatToken *Tok = TheLine.First; Tok != NULL;
732             Tok = Tok->Next) {
733          if (Tok == TheLine.First &&
734              (Tok->NewlinesBefore > 0 || Tok->IsFirst)) {
735            unsigned LevelIndent =
736                SourceMgr.getSpellingColumnNumber(Tok->Tok.getLocation()) - 1;
737            // Remove trailing whitespace of the previous line if it was
738            // touched.
739            if (PreviousLineWasTouched || touchesEmptyLineBefore(TheLine)) {
740              formatFirstToken(*Tok, PreviousLineLastToken, LevelIndent,
741                               TheLine.InPPDirective);
742            } else {
743              Whitespaces.addUntouchableToken(*Tok, TheLine.InPPDirective);
744            }
745
746            if (static_cast<int>(LevelIndent) - Offset >= 0)
747              LevelIndent -= Offset;
748            if (Tok->isNot(tok::comment))
749              IndentForLevel[TheLine.Level] = LevelIndent;
750          } else {
751            Whitespaces.addUntouchableToken(*Tok, TheLine.InPPDirective);
752          }
753        }
754        // If we did not reformat this unwrapped line, the column at the end of
755        // the last token is unchanged - thus, we can calculate the end of the
756        // last token.
757        PreviousLineWasTouched = false;
758      }
759      PreviousLineLastToken = I->Last;
760    }
761    return Whitespaces.generateReplacements();
762  }
763
764private:
765  void deriveLocalStyle() {
766    unsigned CountBoundToVariable = 0;
767    unsigned CountBoundToType = 0;
768    bool HasCpp03IncompatibleFormat = false;
769    bool HasBinPackedFunction = false;
770    bool HasOnePerLineFunction = false;
771    for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
772      if (!AnnotatedLines[i].First->Next)
773        continue;
774      FormatToken *Tok = AnnotatedLines[i].First->Next;
775      while (Tok->Next) {
776        if (Tok->Type == TT_PointerOrReference) {
777          bool SpacesBefore =
778              Tok->WhitespaceRange.getBegin() != Tok->WhitespaceRange.getEnd();
779          bool SpacesAfter = Tok->Next->WhitespaceRange.getBegin() !=
780                             Tok->Next->WhitespaceRange.getEnd();
781          if (SpacesBefore && !SpacesAfter)
782            ++CountBoundToVariable;
783          else if (!SpacesBefore && SpacesAfter)
784            ++CountBoundToType;
785        }
786
787        if (Tok->Type == TT_TemplateCloser &&
788            Tok->Previous->Type == TT_TemplateCloser &&
789            Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd())
790          HasCpp03IncompatibleFormat = true;
791
792        if (Tok->PackingKind == PPK_BinPacked)
793          HasBinPackedFunction = true;
794        if (Tok->PackingKind == PPK_OnePerLine)
795          HasOnePerLineFunction = true;
796
797        Tok = Tok->Next;
798      }
799    }
800    if (Style.DerivePointerBinding) {
801      if (CountBoundToType > CountBoundToVariable)
802        Style.PointerBindsToType = true;
803      else if (CountBoundToType < CountBoundToVariable)
804        Style.PointerBindsToType = false;
805    }
806    if (Style.Standard == FormatStyle::LS_Auto) {
807      Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11
808                                                  : FormatStyle::LS_Cpp03;
809    }
810    BinPackInconclusiveFunctions =
811        HasBinPackedFunction || !HasOnePerLineFunction;
812  }
813
814  /// \brief Get the indent of \p Level from \p IndentForLevel.
815  ///
816  /// \p IndentForLevel must contain the indent for the level \c l
817  /// at \p IndentForLevel[l], or a value < 0 if the indent for
818  /// that level is unknown.
819  unsigned getIndent(const std::vector<int> IndentForLevel, unsigned Level) {
820    if (IndentForLevel[Level] != -1)
821      return IndentForLevel[Level];
822    if (Level == 0)
823      return 0;
824    return getIndent(IndentForLevel, Level - 1) + Style.IndentWidth;
825  }
826
827  /// \brief Get the offset of the line relatively to the level.
828  ///
829  /// For example, 'public:' labels in classes are offset by 1 or 2
830  /// characters to the left from their level.
831  int getIndentOffset(const FormatToken &RootToken) {
832    if (RootToken.isAccessSpecifier(false) || RootToken.isObjCAccessSpecifier())
833      return Style.AccessModifierOffset;
834    return 0;
835  }
836
837  /// \brief Tries to merge lines into one.
838  ///
839  /// This will change \c Line and \c AnnotatedLine to contain the merged line,
840  /// if possible; note that \c I will be incremented when lines are merged.
841  void tryFitMultipleLinesInOne(unsigned Indent,
842                                std::vector<AnnotatedLine>::iterator &I,
843                                std::vector<AnnotatedLine>::iterator E) {
844    // We can never merge stuff if there are trailing line comments.
845    if (I->Last->Type == TT_LineComment)
846      return;
847
848    if (Indent > Style.ColumnLimit)
849      return;
850
851    unsigned Limit = Style.ColumnLimit - Indent;
852    // If we already exceed the column limit, we set 'Limit' to 0. The different
853    // tryMerge..() functions can then decide whether to still do merging.
854    Limit = I->Last->TotalLength > Limit ? 0 : Limit - I->Last->TotalLength;
855
856    if (I + 1 == E || (I + 1)->Type == LT_Invalid)
857      return;
858
859    if (I->Last->is(tok::l_brace)) {
860      tryMergeSimpleBlock(I, E, Limit);
861    } else if (Style.AllowShortIfStatementsOnASingleLine &&
862               I->First->is(tok::kw_if)) {
863      tryMergeSimpleControlStatement(I, E, Limit);
864    } else if (Style.AllowShortLoopsOnASingleLine &&
865               I->First->isOneOf(tok::kw_for, tok::kw_while)) {
866      tryMergeSimpleControlStatement(I, E, Limit);
867    } else if (I->InPPDirective &&
868               (I->First->HasUnescapedNewline || I->First->IsFirst)) {
869      tryMergeSimplePPDirective(I, E, Limit);
870    }
871  }
872
873  void tryMergeSimplePPDirective(std::vector<AnnotatedLine>::iterator &I,
874                                 std::vector<AnnotatedLine>::iterator E,
875                                 unsigned Limit) {
876    if (Limit == 0)
877      return;
878    AnnotatedLine &Line = *I;
879    if (!(I + 1)->InPPDirective || (I + 1)->First->HasUnescapedNewline)
880      return;
881    if (I + 2 != E && (I + 2)->InPPDirective &&
882        !(I + 2)->First->HasUnescapedNewline)
883      return;
884    if (1 + (I + 1)->Last->TotalLength > Limit)
885      return;
886    join(Line, *(++I));
887  }
888
889  void tryMergeSimpleControlStatement(std::vector<AnnotatedLine>::iterator &I,
890                                      std::vector<AnnotatedLine>::iterator E,
891                                      unsigned Limit) {
892    if (Limit == 0)
893      return;
894    if (Style.BreakBeforeBraces == FormatStyle::BS_Allman &&
895        (I + 1)->First->is(tok::l_brace))
896      return;
897    if ((I + 1)->InPPDirective != I->InPPDirective ||
898        ((I + 1)->InPPDirective && (I + 1)->First->HasUnescapedNewline))
899      return;
900    AnnotatedLine &Line = *I;
901    if (Line.Last->isNot(tok::r_paren))
902      return;
903    if (1 + (I + 1)->Last->TotalLength > Limit)
904      return;
905    if ((I + 1)->First->isOneOf(tok::semi, tok::kw_if, tok::kw_for,
906                                tok::kw_while) ||
907        (I + 1)->First->Type == TT_LineComment)
908      return;
909    // Only inline simple if's (no nested if or else).
910    if (I + 2 != E && Line.First->is(tok::kw_if) &&
911        (I + 2)->First->is(tok::kw_else))
912      return;
913    join(Line, *(++I));
914  }
915
916  void tryMergeSimpleBlock(std::vector<AnnotatedLine>::iterator &I,
917                           std::vector<AnnotatedLine>::iterator E,
918                           unsigned Limit) {
919    // No merging if the brace already is on the next line.
920    if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
921      return;
922
923    // First, check that the current line allows merging. This is the case if
924    // we're not in a control flow statement and the last token is an opening
925    // brace.
926    AnnotatedLine &Line = *I;
927    if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::r_brace,
928                            tok::kw_else, tok::kw_try, tok::kw_catch,
929                            tok::kw_for,
930                            // This gets rid of all ObjC @ keywords and methods.
931                            tok::at, tok::minus, tok::plus))
932      return;
933
934    FormatToken *Tok = (I + 1)->First;
935    if (Tok->is(tok::r_brace) && !Tok->MustBreakBefore &&
936        (Tok->getNextNonComment() == NULL ||
937         Tok->getNextNonComment()->is(tok::semi))) {
938      // We merge empty blocks even if the line exceeds the column limit.
939      Tok->SpacesRequiredBefore = 0;
940      Tok->CanBreakBefore = true;
941      join(Line, *(I + 1));
942      I += 1;
943    } else if (Limit != 0 && Line.First->isNot(tok::kw_namespace)) {
944      // Check that we still have three lines and they fit into the limit.
945      if (I + 2 == E || (I + 2)->Type == LT_Invalid ||
946          !nextTwoLinesFitInto(I, Limit))
947        return;
948
949      // Second, check that the next line does not contain any braces - if it
950      // does, readability declines when putting it into a single line.
951      if ((I + 1)->Last->Type == TT_LineComment || Tok->MustBreakBefore)
952        return;
953      do {
954        if (Tok->isOneOf(tok::l_brace, tok::r_brace))
955          return;
956        Tok = Tok->Next;
957      } while (Tok != NULL);
958
959      // Last, check that the third line contains a single closing brace.
960      Tok = (I + 2)->First;
961      if (Tok->getNextNonComment() != NULL || Tok->isNot(tok::r_brace) ||
962          Tok->MustBreakBefore)
963        return;
964
965      join(Line, *(I + 1));
966      join(Line, *(I + 2));
967      I += 2;
968    }
969  }
970
971  bool nextTwoLinesFitInto(std::vector<AnnotatedLine>::iterator I,
972                           unsigned Limit) {
973    return 1 + (I + 1)->Last->TotalLength + 1 + (I + 2)->Last->TotalLength <=
974           Limit;
975  }
976
977  void join(AnnotatedLine &A, const AnnotatedLine &B) {
978    assert(!A.Last->Next);
979    assert(!B.First->Previous);
980    A.Last->Next = B.First;
981    B.First->Previous = A.Last;
982    unsigned LengthA = A.Last->TotalLength + B.First->SpacesRequiredBefore;
983    for (FormatToken *Tok = B.First; Tok; Tok = Tok->Next) {
984      Tok->TotalLength += LengthA;
985      A.Last = Tok;
986    }
987  }
988
989  bool touchesRanges(const CharSourceRange &Range) {
990    for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
991      if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),
992                                               Ranges[i].getBegin()) &&
993          !SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(),
994                                               Range.getBegin()))
995        return true;
996    }
997    return false;
998  }
999
1000  bool touchesLine(const AnnotatedLine &TheLine) {
1001    const FormatToken *First = TheLine.First;
1002    const FormatToken *Last = TheLine.Last;
1003    CharSourceRange LineRange = CharSourceRange::getCharRange(
1004        First->WhitespaceRange.getBegin().getLocWithOffset(
1005            First->LastNewlineOffset),
1006        Last->Tok.getLocation().getLocWithOffset(Last->TokenText.size() - 1));
1007    return touchesRanges(LineRange);
1008  }
1009
1010  bool touchesPPDirective(std::vector<AnnotatedLine>::iterator I,
1011                          std::vector<AnnotatedLine>::iterator E) {
1012    for (; I != E; ++I) {
1013      if (I->First->HasUnescapedNewline)
1014        return false;
1015      if (touchesLine(*I))
1016        return true;
1017    }
1018    return false;
1019  }
1020
1021  bool touchesEmptyLineBefore(const AnnotatedLine &TheLine) {
1022    const FormatToken *First = TheLine.First;
1023    CharSourceRange LineRange = CharSourceRange::getCharRange(
1024        First->WhitespaceRange.getBegin(),
1025        First->WhitespaceRange.getBegin().getLocWithOffset(
1026            First->LastNewlineOffset));
1027    return touchesRanges(LineRange);
1028  }
1029
1030  virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) {
1031    AnnotatedLines.push_back(AnnotatedLine(TheLine));
1032  }
1033
1034  /// \brief Add a new line and the required indent before the first Token
1035  /// of the \c UnwrappedLine if there was no structural parsing error.
1036  /// Returns the indent level of the \c UnwrappedLine.
1037  void formatFirstToken(const FormatToken &RootToken,
1038                        const FormatToken *PreviousToken, unsigned Indent,
1039                        bool InPPDirective) {
1040    unsigned Newlines =
1041        std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);
1042    // Remove empty lines before "}" where applicable.
1043    if (RootToken.is(tok::r_brace) &&
1044        (!RootToken.Next ||
1045         (RootToken.Next->is(tok::semi) && !RootToken.Next->Next)))
1046      Newlines = std::min(Newlines, 1u);
1047    if (Newlines == 0 && !RootToken.IsFirst)
1048      Newlines = 1;
1049
1050    // Insert extra new line before access specifiers.
1051    if (PreviousToken && PreviousToken->isOneOf(tok::semi, tok::r_brace) &&
1052        RootToken.isAccessSpecifier() && RootToken.NewlinesBefore == 1)
1053      ++Newlines;
1054
1055    Whitespaces.replaceWhitespace(
1056        RootToken, Newlines, Indent, Indent,
1057        InPPDirective && !RootToken.HasUnescapedNewline);
1058  }
1059
1060  FormatStyle Style;
1061  Lexer &Lex;
1062  SourceManager &SourceMgr;
1063  WhitespaceManager Whitespaces;
1064  std::vector<CharSourceRange> Ranges;
1065  std::vector<AnnotatedLine> AnnotatedLines;
1066
1067  encoding::Encoding Encoding;
1068  bool BinPackInconclusiveFunctions;
1069};
1070
1071} // end anonymous namespace
1072
1073tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
1074                               SourceManager &SourceMgr,
1075                               std::vector<CharSourceRange> Ranges) {
1076  Formatter formatter(Style, Lex, SourceMgr, Ranges);
1077  return formatter.format();
1078}
1079
1080tooling::Replacements reformat(const FormatStyle &Style, StringRef Code,
1081                               std::vector<tooling::Range> Ranges,
1082                               StringRef FileName) {
1083  FileManager Files((FileSystemOptions()));
1084  DiagnosticsEngine Diagnostics(
1085      IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
1086      new DiagnosticOptions);
1087  SourceManager SourceMgr(Diagnostics, Files);
1088  llvm::MemoryBuffer *Buf = llvm::MemoryBuffer::getMemBuffer(Code, FileName);
1089  const clang::FileEntry *Entry =
1090      Files.getVirtualFile(FileName, Buf->getBufferSize(), 0);
1091  SourceMgr.overrideFileContents(Entry, Buf);
1092  FileID ID =
1093      SourceMgr.createFileID(Entry, SourceLocation(), clang::SrcMgr::C_User);
1094  Lexer Lex(ID, SourceMgr.getBuffer(ID), SourceMgr,
1095            getFormattingLangOpts(Style.Standard));
1096  SourceLocation StartOfFile = SourceMgr.getLocForStartOfFile(ID);
1097  std::vector<CharSourceRange> CharRanges;
1098  for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
1099    SourceLocation Start = StartOfFile.getLocWithOffset(Ranges[i].getOffset());
1100    SourceLocation End = Start.getLocWithOffset(Ranges[i].getLength());
1101    CharRanges.push_back(CharSourceRange::getCharRange(Start, End));
1102  }
1103  return reformat(Style, Lex, SourceMgr, CharRanges);
1104}
1105
1106LangOptions getFormattingLangOpts(FormatStyle::LanguageStandard Standard) {
1107  LangOptions LangOpts;
1108  LangOpts.CPlusPlus = 1;
1109  LangOpts.CPlusPlus11 = Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
1110  LangOpts.LineComment = 1;
1111  LangOpts.Bool = 1;
1112  LangOpts.ObjC1 = 1;
1113  LangOpts.ObjC2 = 1;
1114  return LangOpts;
1115}
1116
1117} // namespace format
1118} // namespace clang
1119