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