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