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