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 #include "ContinuationIndenter.h"
17 #include "TokenAnnotator.h"
18 #include "UnwrappedLineFormatter.h"
19 #include "UnwrappedLineParser.h"
20 #include "WhitespaceManager.h"
21 #include "clang/Basic/Diagnostic.h"
22 #include "clang/Basic/DiagnosticOptions.h"
23 #include "clang/Basic/SourceManager.h"
24 #include "clang/Format/Format.h"
25 #include "clang/Lex/Lexer.h"
26 #include "llvm/ADT/STLExtras.h"
27 #include "llvm/Support/Allocator.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/Path.h"
30 #include "llvm/Support/YAMLTraits.h"
31 #include <queue>
32 #include <string>
33 
34 #define DEBUG_TYPE "format-formatter"
35 
36 using clang::format::FormatStyle;
37 
38 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(std::string)
39 
40 namespace llvm {
41 namespace yaml {
42 template <> struct ScalarEnumerationTraits<FormatStyle::LanguageKind> {
43   static void enumeration(IO &IO, FormatStyle::LanguageKind &Value) {
44     IO.enumCase(Value, "Cpp", FormatStyle::LK_Cpp);
45     IO.enumCase(Value, "Java", FormatStyle::LK_Java);
46     IO.enumCase(Value, "JavaScript", FormatStyle::LK_JavaScript);
47     IO.enumCase(Value, "Proto", FormatStyle::LK_Proto);
48   }
49 };
50 
51 template <> struct ScalarEnumerationTraits<FormatStyle::LanguageStandard> {
52   static void enumeration(IO &IO, FormatStyle::LanguageStandard &Value) {
53     IO.enumCase(Value, "Cpp03", FormatStyle::LS_Cpp03);
54     IO.enumCase(Value, "C++03", FormatStyle::LS_Cpp03);
55     IO.enumCase(Value, "Cpp11", FormatStyle::LS_Cpp11);
56     IO.enumCase(Value, "C++11", FormatStyle::LS_Cpp11);
57     IO.enumCase(Value, "Auto", FormatStyle::LS_Auto);
58   }
59 };
60 
61 template <> struct ScalarEnumerationTraits<FormatStyle::UseTabStyle> {
62   static void enumeration(IO &IO, FormatStyle::UseTabStyle &Value) {
63     IO.enumCase(Value, "Never", FormatStyle::UT_Never);
64     IO.enumCase(Value, "false", FormatStyle::UT_Never);
65     IO.enumCase(Value, "Always", FormatStyle::UT_Always);
66     IO.enumCase(Value, "true", FormatStyle::UT_Always);
67     IO.enumCase(Value, "ForIndentation", FormatStyle::UT_ForIndentation);
68   }
69 };
70 
71 template <> struct ScalarEnumerationTraits<FormatStyle::ShortFunctionStyle> {
72   static void enumeration(IO &IO, FormatStyle::ShortFunctionStyle &Value) {
73     IO.enumCase(Value, "None", FormatStyle::SFS_None);
74     IO.enumCase(Value, "false", FormatStyle::SFS_None);
75     IO.enumCase(Value, "All", FormatStyle::SFS_All);
76     IO.enumCase(Value, "true", FormatStyle::SFS_All);
77     IO.enumCase(Value, "Inline", FormatStyle::SFS_Inline);
78     IO.enumCase(Value, "Empty", FormatStyle::SFS_Empty);
79   }
80 };
81 
82 template <> struct ScalarEnumerationTraits<FormatStyle::BinaryOperatorStyle> {
83   static void enumeration(IO &IO, FormatStyle::BinaryOperatorStyle &Value) {
84     IO.enumCase(Value, "All", FormatStyle::BOS_All);
85     IO.enumCase(Value, "true", FormatStyle::BOS_All);
86     IO.enumCase(Value, "None", FormatStyle::BOS_None);
87     IO.enumCase(Value, "false", FormatStyle::BOS_None);
88     IO.enumCase(Value, "NonAssignment", FormatStyle::BOS_NonAssignment);
89   }
90 };
91 
92 template <> struct ScalarEnumerationTraits<FormatStyle::BraceBreakingStyle> {
93   static void enumeration(IO &IO, FormatStyle::BraceBreakingStyle &Value) {
94     IO.enumCase(Value, "Attach", FormatStyle::BS_Attach);
95     IO.enumCase(Value, "Linux", FormatStyle::BS_Linux);
96     IO.enumCase(Value, "Stroustrup", FormatStyle::BS_Stroustrup);
97     IO.enumCase(Value, "Allman", FormatStyle::BS_Allman);
98     IO.enumCase(Value, "GNU", FormatStyle::BS_GNU);
99   }
100 };
101 
102 template <>
103 struct ScalarEnumerationTraits<FormatStyle::NamespaceIndentationKind> {
104   static void enumeration(IO &IO,
105                           FormatStyle::NamespaceIndentationKind &Value) {
106     IO.enumCase(Value, "None", FormatStyle::NI_None);
107     IO.enumCase(Value, "Inner", FormatStyle::NI_Inner);
108     IO.enumCase(Value, "All", FormatStyle::NI_All);
109   }
110 };
111 
112 template <>
113 struct ScalarEnumerationTraits<FormatStyle::PointerAlignmentStyle> {
114   static void enumeration(IO &IO,
115                           FormatStyle::PointerAlignmentStyle &Value) {
116     IO.enumCase(Value, "Middle", FormatStyle::PAS_Middle);
117     IO.enumCase(Value, "Left", FormatStyle::PAS_Left);
118     IO.enumCase(Value, "Right", FormatStyle::PAS_Right);
119 
120     // For backward compatibility.
121     IO.enumCase(Value, "true", FormatStyle::PAS_Left);
122     IO.enumCase(Value, "false", FormatStyle::PAS_Right);
123   }
124 };
125 
126 template <>
127 struct ScalarEnumerationTraits<FormatStyle::SpaceBeforeParensOptions> {
128   static void enumeration(IO &IO,
129                           FormatStyle::SpaceBeforeParensOptions &Value) {
130     IO.enumCase(Value, "Never", FormatStyle::SBPO_Never);
131     IO.enumCase(Value, "ControlStatements",
132                 FormatStyle::SBPO_ControlStatements);
133     IO.enumCase(Value, "Always", FormatStyle::SBPO_Always);
134 
135     // For backward compatibility.
136     IO.enumCase(Value, "false", FormatStyle::SBPO_Never);
137     IO.enumCase(Value, "true", FormatStyle::SBPO_ControlStatements);
138   }
139 };
140 
141 template <> struct MappingTraits<FormatStyle> {
142   static void mapping(IO &IO, FormatStyle &Style) {
143     // When reading, read the language first, we need it for getPredefinedStyle.
144     IO.mapOptional("Language", Style.Language);
145 
146     if (IO.outputting()) {
147       StringRef StylesArray[] = { "LLVM",    "Google", "Chromium",
148                                   "Mozilla", "WebKit", "GNU" };
149       ArrayRef<StringRef> Styles(StylesArray);
150       for (size_t i = 0, e = Styles.size(); i < e; ++i) {
151         StringRef StyleName(Styles[i]);
152         FormatStyle PredefinedStyle;
153         if (getPredefinedStyle(StyleName, Style.Language, &PredefinedStyle) &&
154             Style == PredefinedStyle) {
155           IO.mapOptional("# BasedOnStyle", StyleName);
156           break;
157         }
158       }
159     } else {
160       StringRef BasedOnStyle;
161       IO.mapOptional("BasedOnStyle", BasedOnStyle);
162       if (!BasedOnStyle.empty()) {
163         FormatStyle::LanguageKind OldLanguage = Style.Language;
164         FormatStyle::LanguageKind Language =
165             ((FormatStyle *)IO.getContext())->Language;
166         if (!getPredefinedStyle(BasedOnStyle, Language, &Style)) {
167           IO.setError(Twine("Unknown value for BasedOnStyle: ", BasedOnStyle));
168           return;
169         }
170         Style.Language = OldLanguage;
171       }
172     }
173 
174     IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset);
175     IO.mapOptional("AlignAfterOpenBracket", Style.AlignAfterOpenBracket);
176     IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft);
177     IO.mapOptional("AlignOperands", Style.AlignOperands);
178     IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments);
179     IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine",
180                    Style.AllowAllParametersOfDeclarationOnNextLine);
181     IO.mapOptional("AllowShortBlocksOnASingleLine",
182                    Style.AllowShortBlocksOnASingleLine);
183     IO.mapOptional("AllowShortCaseLabelsOnASingleLine",
184                    Style.AllowShortCaseLabelsOnASingleLine);
185     IO.mapOptional("AllowShortIfStatementsOnASingleLine",
186                    Style.AllowShortIfStatementsOnASingleLine);
187     IO.mapOptional("AllowShortLoopsOnASingleLine",
188                    Style.AllowShortLoopsOnASingleLine);
189     IO.mapOptional("AllowShortFunctionsOnASingleLine",
190                    Style.AllowShortFunctionsOnASingleLine);
191     IO.mapOptional("AlwaysBreakAfterDefinitionReturnType",
192                    Style.AlwaysBreakAfterDefinitionReturnType);
193     IO.mapOptional("AlwaysBreakTemplateDeclarations",
194                    Style.AlwaysBreakTemplateDeclarations);
195     IO.mapOptional("AlwaysBreakBeforeMultilineStrings",
196                    Style.AlwaysBreakBeforeMultilineStrings);
197     IO.mapOptional("BreakBeforeBinaryOperators",
198                    Style.BreakBeforeBinaryOperators);
199     IO.mapOptional("BreakBeforeTernaryOperators",
200                    Style.BreakBeforeTernaryOperators);
201     IO.mapOptional("BreakConstructorInitializersBeforeComma",
202                    Style.BreakConstructorInitializersBeforeComma);
203     IO.mapOptional("BinPackParameters", Style.BinPackParameters);
204     IO.mapOptional("BinPackArguments", Style.BinPackArguments);
205     IO.mapOptional("ColumnLimit", Style.ColumnLimit);
206     IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine",
207                    Style.ConstructorInitializerAllOnOneLineOrOnePerLine);
208     IO.mapOptional("ConstructorInitializerIndentWidth",
209                    Style.ConstructorInitializerIndentWidth);
210     IO.mapOptional("DerivePointerAlignment", Style.DerivePointerAlignment);
211     IO.mapOptional("ExperimentalAutoDetectBinPacking",
212                    Style.ExperimentalAutoDetectBinPacking);
213     IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels);
214     IO.mapOptional("IndentWrappedFunctionNames",
215                    Style.IndentWrappedFunctionNames);
216     IO.mapOptional("IndentFunctionDeclarationAfterType",
217                    Style.IndentWrappedFunctionNames);
218     IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep);
219     IO.mapOptional("KeepEmptyLinesAtTheStartOfBlocks",
220                    Style.KeepEmptyLinesAtTheStartOfBlocks);
221     IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation);
222     IO.mapOptional("ObjCBlockIndentWidth", Style.ObjCBlockIndentWidth);
223     IO.mapOptional("ObjCSpaceAfterProperty", Style.ObjCSpaceAfterProperty);
224     IO.mapOptional("ObjCSpaceBeforeProtocolList",
225                    Style.ObjCSpaceBeforeProtocolList);
226     IO.mapOptional("PenaltyBreakBeforeFirstCallParameter",
227                    Style.PenaltyBreakBeforeFirstCallParameter);
228     IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment);
229     IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString);
230     IO.mapOptional("PenaltyBreakFirstLessLess",
231                    Style.PenaltyBreakFirstLessLess);
232     IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter);
233     IO.mapOptional("PenaltyReturnTypeOnItsOwnLine",
234                    Style.PenaltyReturnTypeOnItsOwnLine);
235     IO.mapOptional("PointerAlignment", Style.PointerAlignment);
236     IO.mapOptional("SpacesBeforeTrailingComments",
237                    Style.SpacesBeforeTrailingComments);
238     IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle);
239     IO.mapOptional("Standard", Style.Standard);
240     IO.mapOptional("IndentWidth", Style.IndentWidth);
241     IO.mapOptional("TabWidth", Style.TabWidth);
242     IO.mapOptional("UseTab", Style.UseTab);
243     IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces);
244     IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses);
245     IO.mapOptional("SpacesInSquareBrackets", Style.SpacesInSquareBrackets);
246     IO.mapOptional("SpacesInAngles", Style.SpacesInAngles);
247     IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses);
248     IO.mapOptional("SpacesInCStyleCastParentheses",
249                    Style.SpacesInCStyleCastParentheses);
250     IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast);
251     IO.mapOptional("SpacesInContainerLiterals",
252                    Style.SpacesInContainerLiterals);
253     IO.mapOptional("SpaceBeforeAssignmentOperators",
254                    Style.SpaceBeforeAssignmentOperators);
255     IO.mapOptional("ContinuationIndentWidth", Style.ContinuationIndentWidth);
256     IO.mapOptional("CommentPragmas", Style.CommentPragmas);
257     IO.mapOptional("ForEachMacros", Style.ForEachMacros);
258 
259     // For backward compatibility.
260     if (!IO.outputting()) {
261       IO.mapOptional("SpaceAfterControlStatementKeyword",
262                      Style.SpaceBeforeParens);
263       IO.mapOptional("PointerBindsToType", Style.PointerAlignment);
264       IO.mapOptional("DerivePointerBinding", Style.DerivePointerAlignment);
265     }
266     IO.mapOptional("SpaceBeforeParens", Style.SpaceBeforeParens);
267     IO.mapOptional("DisableFormat", Style.DisableFormat);
268   }
269 };
270 
271 // Allows to read vector<FormatStyle> while keeping default values.
272 // IO.getContext() should contain a pointer to the FormatStyle structure, that
273 // will be used to get default values for missing keys.
274 // If the first element has no Language specified, it will be treated as the
275 // default one for the following elements.
276 template <> struct DocumentListTraits<std::vector<FormatStyle> > {
277   static size_t size(IO &IO, std::vector<FormatStyle> &Seq) {
278     return Seq.size();
279   }
280   static FormatStyle &element(IO &IO, std::vector<FormatStyle> &Seq,
281                               size_t Index) {
282     if (Index >= Seq.size()) {
283       assert(Index == Seq.size());
284       FormatStyle Template;
285       if (Seq.size() > 0 && Seq[0].Language == FormatStyle::LK_None) {
286         Template = Seq[0];
287       } else {
288         Template = *((const FormatStyle *)IO.getContext());
289         Template.Language = FormatStyle::LK_None;
290       }
291       Seq.resize(Index + 1, Template);
292     }
293     return Seq[Index];
294   }
295 };
296 }
297 }
298 
299 namespace clang {
300 namespace format {
301 
302 const std::error_category &getParseCategory() {
303   static ParseErrorCategory C;
304   return C;
305 }
306 std::error_code make_error_code(ParseError e) {
307   return std::error_code(static_cast<int>(e), getParseCategory());
308 }
309 
310 const char *ParseErrorCategory::name() const LLVM_NOEXCEPT {
311   return "clang-format.parse_error";
312 }
313 
314 std::string ParseErrorCategory::message(int EV) const {
315   switch (static_cast<ParseError>(EV)) {
316   case ParseError::Success:
317     return "Success";
318   case ParseError::Error:
319     return "Invalid argument";
320   case ParseError::Unsuitable:
321     return "Unsuitable";
322   }
323   llvm_unreachable("unexpected parse error");
324 }
325 
326 FormatStyle getLLVMStyle() {
327   FormatStyle LLVMStyle;
328   LLVMStyle.Language = FormatStyle::LK_Cpp;
329   LLVMStyle.AccessModifierOffset = -2;
330   LLVMStyle.AlignEscapedNewlinesLeft = false;
331   LLVMStyle.AlignAfterOpenBracket = true;
332   LLVMStyle.AlignOperands = true;
333   LLVMStyle.AlignTrailingComments = true;
334   LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
335   LLVMStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
336   LLVMStyle.AllowShortBlocksOnASingleLine = false;
337   LLVMStyle.AllowShortCaseLabelsOnASingleLine = false;
338   LLVMStyle.AllowShortIfStatementsOnASingleLine = false;
339   LLVMStyle.AllowShortLoopsOnASingleLine = false;
340   LLVMStyle.AlwaysBreakAfterDefinitionReturnType = false;
341   LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
342   LLVMStyle.AlwaysBreakTemplateDeclarations = false;
343   LLVMStyle.BinPackParameters = true;
344   LLVMStyle.BinPackArguments = true;
345   LLVMStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
346   LLVMStyle.BreakBeforeTernaryOperators = true;
347   LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
348   LLVMStyle.BreakConstructorInitializersBeforeComma = false;
349   LLVMStyle.ColumnLimit = 80;
350   LLVMStyle.CommentPragmas = "^ IWYU pragma:";
351   LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
352   LLVMStyle.ConstructorInitializerIndentWidth = 4;
353   LLVMStyle.ContinuationIndentWidth = 4;
354   LLVMStyle.Cpp11BracedListStyle = true;
355   LLVMStyle.DerivePointerAlignment = false;
356   LLVMStyle.ExperimentalAutoDetectBinPacking = false;
357   LLVMStyle.ForEachMacros.push_back("foreach");
358   LLVMStyle.ForEachMacros.push_back("Q_FOREACH");
359   LLVMStyle.ForEachMacros.push_back("BOOST_FOREACH");
360   LLVMStyle.IndentCaseLabels = false;
361   LLVMStyle.IndentWrappedFunctionNames = false;
362   LLVMStyle.IndentWidth = 2;
363   LLVMStyle.TabWidth = 8;
364   LLVMStyle.MaxEmptyLinesToKeep = 1;
365   LLVMStyle.KeepEmptyLinesAtTheStartOfBlocks = true;
366   LLVMStyle.NamespaceIndentation = FormatStyle::NI_None;
367   LLVMStyle.ObjCBlockIndentWidth = 2;
368   LLVMStyle.ObjCSpaceAfterProperty = false;
369   LLVMStyle.ObjCSpaceBeforeProtocolList = true;
370   LLVMStyle.PointerAlignment = FormatStyle::PAS_Right;
371   LLVMStyle.SpacesBeforeTrailingComments = 1;
372   LLVMStyle.Standard = FormatStyle::LS_Cpp11;
373   LLVMStyle.UseTab = FormatStyle::UT_Never;
374   LLVMStyle.SpacesInParentheses = false;
375   LLVMStyle.SpacesInSquareBrackets = false;
376   LLVMStyle.SpaceInEmptyParentheses = false;
377   LLVMStyle.SpacesInContainerLiterals = true;
378   LLVMStyle.SpacesInCStyleCastParentheses = false;
379   LLVMStyle.SpaceAfterCStyleCast = false;
380   LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements;
381   LLVMStyle.SpaceBeforeAssignmentOperators = true;
382   LLVMStyle.SpacesInAngles = false;
383 
384   LLVMStyle.PenaltyBreakComment = 300;
385   LLVMStyle.PenaltyBreakFirstLessLess = 120;
386   LLVMStyle.PenaltyBreakString = 1000;
387   LLVMStyle.PenaltyExcessCharacter = 1000000;
388   LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
389   LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19;
390 
391   LLVMStyle.DisableFormat = false;
392 
393   return LLVMStyle;
394 }
395 
396 FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
397   FormatStyle GoogleStyle = getLLVMStyle();
398   GoogleStyle.Language = Language;
399 
400   GoogleStyle.AccessModifierOffset = -1;
401   GoogleStyle.AlignEscapedNewlinesLeft = true;
402   GoogleStyle.AllowShortIfStatementsOnASingleLine = true;
403   GoogleStyle.AllowShortLoopsOnASingleLine = true;
404   GoogleStyle.AlwaysBreakBeforeMultilineStrings = true;
405   GoogleStyle.AlwaysBreakTemplateDeclarations = true;
406   GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
407   GoogleStyle.DerivePointerAlignment = true;
408   GoogleStyle.IndentCaseLabels = true;
409   GoogleStyle.KeepEmptyLinesAtTheStartOfBlocks = false;
410   GoogleStyle.ObjCSpaceAfterProperty = false;
411   GoogleStyle.ObjCSpaceBeforeProtocolList = false;
412   GoogleStyle.PointerAlignment = FormatStyle::PAS_Left;
413   GoogleStyle.SpacesBeforeTrailingComments = 2;
414   GoogleStyle.Standard = FormatStyle::LS_Auto;
415 
416   GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
417   GoogleStyle.PenaltyBreakBeforeFirstCallParameter = 1;
418 
419   if (Language == FormatStyle::LK_Java) {
420     GoogleStyle.AlignAfterOpenBracket = false;
421     GoogleStyle.AlignOperands = false;
422     GoogleStyle.AlignTrailingComments = false;
423     GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
424     GoogleStyle.AllowShortIfStatementsOnASingleLine = false;
425     GoogleStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
426     GoogleStyle.ColumnLimit = 100;
427     GoogleStyle.SpaceAfterCStyleCast = true;
428     GoogleStyle.SpacesBeforeTrailingComments = 1;
429   } else if (Language == FormatStyle::LK_JavaScript) {
430     GoogleStyle.BreakBeforeTernaryOperators = false;
431     GoogleStyle.MaxEmptyLinesToKeep = 3;
432     GoogleStyle.SpacesInContainerLiterals = false;
433     GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
434   } else if (Language == FormatStyle::LK_Proto) {
435     GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
436     GoogleStyle.SpacesInContainerLiterals = false;
437   }
438 
439   return GoogleStyle;
440 }
441 
442 FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) {
443   FormatStyle ChromiumStyle = getGoogleStyle(Language);
444   if (Language == FormatStyle::LK_Java) {
445     ChromiumStyle.AllowShortIfStatementsOnASingleLine = true;
446     ChromiumStyle.IndentWidth = 4;
447     ChromiumStyle.ContinuationIndentWidth = 8;
448   } else {
449     ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false;
450     ChromiumStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
451     ChromiumStyle.AllowShortIfStatementsOnASingleLine = false;
452     ChromiumStyle.AllowShortLoopsOnASingleLine = false;
453     ChromiumStyle.BinPackParameters = false;
454     ChromiumStyle.DerivePointerAlignment = false;
455   }
456   return ChromiumStyle;
457 }
458 
459 FormatStyle getMozillaStyle() {
460   FormatStyle MozillaStyle = getLLVMStyle();
461   MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false;
462   MozillaStyle.Cpp11BracedListStyle = false;
463   MozillaStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
464   MozillaStyle.DerivePointerAlignment = true;
465   MozillaStyle.IndentCaseLabels = true;
466   MozillaStyle.ObjCSpaceAfterProperty = true;
467   MozillaStyle.ObjCSpaceBeforeProtocolList = false;
468   MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200;
469   MozillaStyle.PointerAlignment = FormatStyle::PAS_Left;
470   MozillaStyle.Standard = FormatStyle::LS_Cpp03;
471   return MozillaStyle;
472 }
473 
474 FormatStyle getWebKitStyle() {
475   FormatStyle Style = getLLVMStyle();
476   Style.AccessModifierOffset = -4;
477   Style.AlignAfterOpenBracket = false;
478   Style.AlignOperands = false;
479   Style.AlignTrailingComments = false;
480   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
481   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
482   Style.BreakConstructorInitializersBeforeComma = true;
483   Style.Cpp11BracedListStyle = false;
484   Style.ColumnLimit = 0;
485   Style.IndentWidth = 4;
486   Style.NamespaceIndentation = FormatStyle::NI_Inner;
487   Style.ObjCBlockIndentWidth = 4;
488   Style.ObjCSpaceAfterProperty = true;
489   Style.PointerAlignment = FormatStyle::PAS_Left;
490   Style.Standard = FormatStyle::LS_Cpp03;
491   return Style;
492 }
493 
494 FormatStyle getGNUStyle() {
495   FormatStyle Style = getLLVMStyle();
496   Style.AlwaysBreakAfterDefinitionReturnType = true;
497   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
498   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
499   Style.BreakBeforeTernaryOperators = true;
500   Style.Cpp11BracedListStyle = false;
501   Style.ColumnLimit = 79;
502   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
503   Style.Standard = FormatStyle::LS_Cpp03;
504   return Style;
505 }
506 
507 FormatStyle getNoStyle() {
508   FormatStyle NoStyle = getLLVMStyle();
509   NoStyle.DisableFormat = true;
510   return NoStyle;
511 }
512 
513 bool getPredefinedStyle(StringRef Name, FormatStyle::LanguageKind Language,
514                         FormatStyle *Style) {
515   if (Name.equals_lower("llvm")) {
516     *Style = getLLVMStyle();
517   } else if (Name.equals_lower("chromium")) {
518     *Style = getChromiumStyle(Language);
519   } else if (Name.equals_lower("mozilla")) {
520     *Style = getMozillaStyle();
521   } else if (Name.equals_lower("google")) {
522     *Style = getGoogleStyle(Language);
523   } else if (Name.equals_lower("webkit")) {
524     *Style = getWebKitStyle();
525   } else if (Name.equals_lower("gnu")) {
526     *Style = getGNUStyle();
527   } else if (Name.equals_lower("none")) {
528     *Style = getNoStyle();
529   } else {
530     return false;
531   }
532 
533   Style->Language = Language;
534   return true;
535 }
536 
537 std::error_code parseConfiguration(StringRef Text, FormatStyle *Style) {
538   assert(Style);
539   FormatStyle::LanguageKind Language = Style->Language;
540   assert(Language != FormatStyle::LK_None);
541   if (Text.trim().empty())
542     return make_error_code(ParseError::Error);
543 
544   std::vector<FormatStyle> Styles;
545   llvm::yaml::Input Input(Text);
546   // DocumentListTraits<vector<FormatStyle>> uses the context to get default
547   // values for the fields, keys for which are missing from the configuration.
548   // Mapping also uses the context to get the language to find the correct
549   // base style.
550   Input.setContext(Style);
551   Input >> Styles;
552   if (Input.error())
553     return Input.error();
554 
555   for (unsigned i = 0; i < Styles.size(); ++i) {
556     // Ensures that only the first configuration can skip the Language option.
557     if (Styles[i].Language == FormatStyle::LK_None && i != 0)
558       return make_error_code(ParseError::Error);
559     // Ensure that each language is configured at most once.
560     for (unsigned j = 0; j < i; ++j) {
561       if (Styles[i].Language == Styles[j].Language) {
562         DEBUG(llvm::dbgs()
563               << "Duplicate languages in the config file on positions " << j
564               << " and " << i << "\n");
565         return make_error_code(ParseError::Error);
566       }
567     }
568   }
569   // Look for a suitable configuration starting from the end, so we can
570   // find the configuration for the specific language first, and the default
571   // configuration (which can only be at slot 0) after it.
572   for (int i = Styles.size() - 1; i >= 0; --i) {
573     if (Styles[i].Language == Language ||
574         Styles[i].Language == FormatStyle::LK_None) {
575       *Style = Styles[i];
576       Style->Language = Language;
577       return make_error_code(ParseError::Success);
578     }
579   }
580   return make_error_code(ParseError::Unsuitable);
581 }
582 
583 std::string configurationAsText(const FormatStyle &Style) {
584   std::string Text;
585   llvm::raw_string_ostream Stream(Text);
586   llvm::yaml::Output Output(Stream);
587   // We use the same mapping method for input and output, so we need a non-const
588   // reference here.
589   FormatStyle NonConstStyle = Style;
590   Output << NonConstStyle;
591   return Stream.str();
592 }
593 
594 namespace {
595 
596 class FormatTokenLexer {
597 public:
598   FormatTokenLexer(SourceManager &SourceMgr, FileID ID, FormatStyle &Style,
599                    encoding::Encoding Encoding)
600       : FormatTok(nullptr), IsFirstToken(true), GreaterStashed(false),
601         Column(0), TrailingWhitespace(0), SourceMgr(SourceMgr), ID(ID),
602         Style(Style), IdentTable(getFormattingLangOpts(Style)),
603         Keywords(IdentTable), Encoding(Encoding), FirstInLineIndex(0),
604         FormattingDisabled(false) {
605     Lex.reset(new Lexer(ID, SourceMgr.getBuffer(ID), SourceMgr,
606                         getFormattingLangOpts(Style)));
607     Lex->SetKeepWhitespaceMode(true);
608 
609     for (const std::string &ForEachMacro : Style.ForEachMacros)
610       ForEachMacros.push_back(&IdentTable.get(ForEachMacro));
611     std::sort(ForEachMacros.begin(), ForEachMacros.end());
612   }
613 
614   ArrayRef<FormatToken *> lex() {
615     assert(Tokens.empty());
616     assert(FirstInLineIndex == 0);
617     do {
618       Tokens.push_back(getNextToken());
619       tryMergePreviousTokens();
620       if (Tokens.back()->NewlinesBefore > 0)
621         FirstInLineIndex = Tokens.size() - 1;
622     } while (Tokens.back()->Tok.isNot(tok::eof));
623     return Tokens;
624   }
625 
626   const AdditionalKeywords &getKeywords() { return Keywords; }
627 
628 private:
629   void tryMergePreviousTokens() {
630     if (tryMerge_TMacro())
631       return;
632     if (tryMergeConflictMarkers())
633       return;
634 
635     if (Style.Language == FormatStyle::LK_JavaScript) {
636       if (tryMergeJSRegexLiteral())
637         return;
638       if (tryMergeEscapeSequence())
639         return;
640 
641       static tok::TokenKind JSIdentity[] = { tok::equalequal, tok::equal };
642       static tok::TokenKind JSNotIdentity[] = { tok::exclaimequal, tok::equal };
643       static tok::TokenKind JSShiftEqual[] = { tok::greater, tok::greater,
644                                                tok::greaterequal };
645       static tok::TokenKind JSRightArrow[] = { tok::equal, tok::greater };
646       // FIXME: We probably need to change token type to mimic operator with the
647       // correct priority.
648       if (tryMergeTokens(JSIdentity))
649         return;
650       if (tryMergeTokens(JSNotIdentity))
651         return;
652       if (tryMergeTokens(JSShiftEqual))
653         return;
654       if (tryMergeTokens(JSRightArrow))
655         return;
656     }
657   }
658 
659   bool tryMergeTokens(ArrayRef<tok::TokenKind> Kinds) {
660     if (Tokens.size() < Kinds.size())
661       return false;
662 
663     SmallVectorImpl<FormatToken *>::const_iterator First =
664         Tokens.end() - Kinds.size();
665     if (!First[0]->is(Kinds[0]))
666       return false;
667     unsigned AddLength = 0;
668     for (unsigned i = 1; i < Kinds.size(); ++i) {
669       if (!First[i]->is(Kinds[i]) || First[i]->WhitespaceRange.getBegin() !=
670                                          First[i]->WhitespaceRange.getEnd())
671         return false;
672       AddLength += First[i]->TokenText.size();
673     }
674     Tokens.resize(Tokens.size() - Kinds.size() + 1);
675     First[0]->TokenText = StringRef(First[0]->TokenText.data(),
676                                     First[0]->TokenText.size() + AddLength);
677     First[0]->ColumnWidth += AddLength;
678     return true;
679   }
680 
681   // Tries to merge an escape sequence, i.e. a "\\" and the following
682   // character. Use e.g. inside JavaScript regex literals.
683   bool tryMergeEscapeSequence() {
684     if (Tokens.size() < 2)
685       return false;
686     FormatToken *Previous = Tokens[Tokens.size() - 2];
687     if (Previous->isNot(tok::unknown) || Previous->TokenText != "\\")
688       return false;
689     ++Previous->ColumnWidth;
690     StringRef Text = Previous->TokenText;
691     Previous->TokenText = StringRef(Text.data(), Text.size() + 1);
692     resetLexer(SourceMgr.getFileOffset(Tokens.back()->Tok.getLocation()) + 1);
693     Tokens.resize(Tokens.size() - 1);
694     Column = Previous->OriginalColumn + Previous->ColumnWidth;
695     return true;
696   }
697 
698   // Try to determine whether the current token ends a JavaScript regex literal.
699   // We heuristically assume that this is a regex literal if we find two
700   // unescaped slashes on a line and the token before the first slash is one of
701   // "(;,{}![:?", a binary operator or 'return', as those cannot be followed by
702   // a division.
703   bool tryMergeJSRegexLiteral() {
704     if (Tokens.size() < 2)
705       return false;
706     // If a regex literal ends in "\//", this gets represented by an unknown
707     // token "\" and a comment.
708     bool MightEndWithEscapedSlash =
709         Tokens.back()->is(tok::comment) &&
710         Tokens.back()->TokenText.startswith("//") &&
711         Tokens[Tokens.size() - 2]->TokenText == "\\";
712     if (!MightEndWithEscapedSlash &&
713         (Tokens.back()->isNot(tok::slash) ||
714          (Tokens[Tokens.size() - 2]->is(tok::unknown) &&
715           Tokens[Tokens.size() - 2]->TokenText == "\\")))
716       return false;
717     unsigned TokenCount = 0;
718     unsigned LastColumn = Tokens.back()->OriginalColumn;
719     for (auto I = Tokens.rbegin() + 1, E = Tokens.rend(); I != E; ++I) {
720       ++TokenCount;
721       if (I[0]->is(tok::slash) && I + 1 != E &&
722           (I[1]->isOneOf(tok::l_paren, tok::semi, tok::l_brace, tok::r_brace,
723                          tok::exclaim, tok::l_square, tok::colon, tok::comma,
724                          tok::question, tok::kw_return) ||
725            I[1]->isBinaryOperator())) {
726         if (MightEndWithEscapedSlash) {
727           // This regex literal ends in '\//'. Skip past the '//' of the last
728           // token and re-start lexing from there.
729           SourceLocation Loc = Tokens.back()->Tok.getLocation();
730           resetLexer(SourceMgr.getFileOffset(Loc) + 2);
731         }
732         Tokens.resize(Tokens.size() - TokenCount);
733         Tokens.back()->Tok.setKind(tok::unknown);
734         Tokens.back()->Type = TT_RegexLiteral;
735         Tokens.back()->ColumnWidth += LastColumn - I[0]->OriginalColumn;
736         return true;
737       }
738 
739       // There can't be a newline inside a regex literal.
740       if (I[0]->NewlinesBefore > 0)
741         return false;
742     }
743     return false;
744   }
745 
746   bool tryMerge_TMacro() {
747     if (Tokens.size() < 4)
748       return false;
749     FormatToken *Last = Tokens.back();
750     if (!Last->is(tok::r_paren))
751       return false;
752 
753     FormatToken *String = Tokens[Tokens.size() - 2];
754     if (!String->is(tok::string_literal) || String->IsMultiline)
755       return false;
756 
757     if (!Tokens[Tokens.size() - 3]->is(tok::l_paren))
758       return false;
759 
760     FormatToken *Macro = Tokens[Tokens.size() - 4];
761     if (Macro->TokenText != "_T")
762       return false;
763 
764     const char *Start = Macro->TokenText.data();
765     const char *End = Last->TokenText.data() + Last->TokenText.size();
766     String->TokenText = StringRef(Start, End - Start);
767     String->IsFirst = Macro->IsFirst;
768     String->LastNewlineOffset = Macro->LastNewlineOffset;
769     String->WhitespaceRange = Macro->WhitespaceRange;
770     String->OriginalColumn = Macro->OriginalColumn;
771     String->ColumnWidth = encoding::columnWidthWithTabs(
772         String->TokenText, String->OriginalColumn, Style.TabWidth, Encoding);
773 
774     Tokens.pop_back();
775     Tokens.pop_back();
776     Tokens.pop_back();
777     Tokens.back() = String;
778     return true;
779   }
780 
781   bool tryMergeConflictMarkers() {
782     if (Tokens.back()->NewlinesBefore == 0 && Tokens.back()->isNot(tok::eof))
783       return false;
784 
785     // Conflict lines look like:
786     // <marker> <text from the vcs>
787     // For example:
788     // >>>>>>> /file/in/file/system at revision 1234
789     //
790     // We merge all tokens in a line that starts with a conflict marker
791     // into a single token with a special token type that the unwrapped line
792     // parser will use to correctly rebuild the underlying code.
793 
794     FileID ID;
795     // Get the position of the first token in the line.
796     unsigned FirstInLineOffset;
797     std::tie(ID, FirstInLineOffset) = SourceMgr.getDecomposedLoc(
798         Tokens[FirstInLineIndex]->getStartOfNonWhitespace());
799     StringRef Buffer = SourceMgr.getBuffer(ID)->getBuffer();
800     // Calculate the offset of the start of the current line.
801     auto LineOffset = Buffer.rfind('\n', FirstInLineOffset);
802     if (LineOffset == StringRef::npos) {
803       LineOffset = 0;
804     } else {
805       ++LineOffset;
806     }
807 
808     auto FirstSpace = Buffer.find_first_of(" \n", LineOffset);
809     StringRef LineStart;
810     if (FirstSpace == StringRef::npos) {
811       LineStart = Buffer.substr(LineOffset);
812     } else {
813       LineStart = Buffer.substr(LineOffset, FirstSpace - LineOffset);
814     }
815 
816     TokenType Type = TT_Unknown;
817     if (LineStart == "<<<<<<<" || LineStart == ">>>>") {
818       Type = TT_ConflictStart;
819     } else if (LineStart == "|||||||" || LineStart == "=======" ||
820                LineStart == "====") {
821       Type = TT_ConflictAlternative;
822     } else if (LineStart == ">>>>>>>" || LineStart == "<<<<") {
823       Type = TT_ConflictEnd;
824     }
825 
826     if (Type != TT_Unknown) {
827       FormatToken *Next = Tokens.back();
828 
829       Tokens.resize(FirstInLineIndex + 1);
830       // We do not need to build a complete token here, as we will skip it
831       // during parsing anyway (as we must not touch whitespace around conflict
832       // markers).
833       Tokens.back()->Type = Type;
834       Tokens.back()->Tok.setKind(tok::kw___unknown_anytype);
835 
836       Tokens.push_back(Next);
837       return true;
838     }
839 
840     return false;
841   }
842 
843   FormatToken *getNextToken() {
844     if (GreaterStashed) {
845       // Create a synthesized second '>' token.
846       // FIXME: Increment Column and set OriginalColumn.
847       Token Greater = FormatTok->Tok;
848       FormatTok = new (Allocator.Allocate()) FormatToken;
849       FormatTok->Tok = Greater;
850       SourceLocation GreaterLocation =
851           FormatTok->Tok.getLocation().getLocWithOffset(1);
852       FormatTok->WhitespaceRange =
853           SourceRange(GreaterLocation, GreaterLocation);
854       FormatTok->TokenText = ">";
855       FormatTok->ColumnWidth = 1;
856       GreaterStashed = false;
857       return FormatTok;
858     }
859 
860     FormatTok = new (Allocator.Allocate()) FormatToken;
861     readRawToken(*FormatTok);
862     SourceLocation WhitespaceStart =
863         FormatTok->Tok.getLocation().getLocWithOffset(-TrailingWhitespace);
864     FormatTok->IsFirst = IsFirstToken;
865     IsFirstToken = false;
866 
867     // Consume and record whitespace until we find a significant token.
868     unsigned WhitespaceLength = TrailingWhitespace;
869     while (FormatTok->Tok.is(tok::unknown)) {
870       for (int i = 0, e = FormatTok->TokenText.size(); i != e; ++i) {
871         switch (FormatTok->TokenText[i]) {
872         case '\n':
873           ++FormatTok->NewlinesBefore;
874           // FIXME: This is technically incorrect, as it could also
875           // be a literal backslash at the end of the line.
876           if (i == 0 || (FormatTok->TokenText[i - 1] != '\\' &&
877                          (FormatTok->TokenText[i - 1] != '\r' || i == 1 ||
878                           FormatTok->TokenText[i - 2] != '\\')))
879             FormatTok->HasUnescapedNewline = true;
880           FormatTok->LastNewlineOffset = WhitespaceLength + i + 1;
881           Column = 0;
882           break;
883         case '\r':
884         case '\f':
885         case '\v':
886           Column = 0;
887           break;
888         case ' ':
889           ++Column;
890           break;
891         case '\t':
892           Column += Style.TabWidth - Column % Style.TabWidth;
893           break;
894         case '\\':
895           if (i + 1 == e || (FormatTok->TokenText[i + 1] != '\r' &&
896                              FormatTok->TokenText[i + 1] != '\n'))
897             FormatTok->Type = TT_ImplicitStringLiteral;
898           break;
899         default:
900           FormatTok->Type = TT_ImplicitStringLiteral;
901           ++Column;
902           break;
903         }
904       }
905 
906       if (FormatTok->is(TT_ImplicitStringLiteral))
907         break;
908       WhitespaceLength += FormatTok->Tok.getLength();
909 
910       readRawToken(*FormatTok);
911     }
912 
913     // In case the token starts with escaped newlines, we want to
914     // take them into account as whitespace - this pattern is quite frequent
915     // in macro definitions.
916     // FIXME: Add a more explicit test.
917     while (FormatTok->TokenText.size() > 1 && FormatTok->TokenText[0] == '\\' &&
918            FormatTok->TokenText[1] == '\n') {
919       ++FormatTok->NewlinesBefore;
920       WhitespaceLength += 2;
921       Column = 0;
922       FormatTok->TokenText = FormatTok->TokenText.substr(2);
923     }
924 
925     FormatTok->WhitespaceRange = SourceRange(
926         WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength));
927 
928     FormatTok->OriginalColumn = Column;
929 
930     TrailingWhitespace = 0;
931     if (FormatTok->Tok.is(tok::comment)) {
932       // FIXME: Add the trimmed whitespace to Column.
933       StringRef UntrimmedText = FormatTok->TokenText;
934       FormatTok->TokenText = FormatTok->TokenText.rtrim(" \t\v\f");
935       TrailingWhitespace = UntrimmedText.size() - FormatTok->TokenText.size();
936     } else if (FormatTok->Tok.is(tok::raw_identifier)) {
937       IdentifierInfo &Info = IdentTable.get(FormatTok->TokenText);
938       FormatTok->Tok.setIdentifierInfo(&Info);
939       FormatTok->Tok.setKind(Info.getTokenID());
940       if (Style.Language == FormatStyle::LK_Java &&
941           FormatTok->isOneOf(tok::kw_struct, tok::kw_union, tok::kw_delete)) {
942         FormatTok->Tok.setKind(tok::identifier);
943         FormatTok->Tok.setIdentifierInfo(nullptr);
944       }
945     } else if (FormatTok->Tok.is(tok::greatergreater)) {
946       FormatTok->Tok.setKind(tok::greater);
947       FormatTok->TokenText = FormatTok->TokenText.substr(0, 1);
948       GreaterStashed = true;
949     }
950 
951     // Now FormatTok is the next non-whitespace token.
952 
953     StringRef Text = FormatTok->TokenText;
954     size_t FirstNewlinePos = Text.find('\n');
955     if (FirstNewlinePos == StringRef::npos) {
956       // FIXME: ColumnWidth actually depends on the start column, we need to
957       // take this into account when the token is moved.
958       FormatTok->ColumnWidth =
959           encoding::columnWidthWithTabs(Text, Column, Style.TabWidth, Encoding);
960       Column += FormatTok->ColumnWidth;
961     } else {
962       FormatTok->IsMultiline = true;
963       // FIXME: ColumnWidth actually depends on the start column, we need to
964       // take this into account when the token is moved.
965       FormatTok->ColumnWidth = encoding::columnWidthWithTabs(
966           Text.substr(0, FirstNewlinePos), Column, Style.TabWidth, Encoding);
967 
968       // The last line of the token always starts in column 0.
969       // Thus, the length can be precomputed even in the presence of tabs.
970       FormatTok->LastLineColumnWidth = encoding::columnWidthWithTabs(
971           Text.substr(Text.find_last_of('\n') + 1), 0, Style.TabWidth,
972           Encoding);
973       Column = FormatTok->LastLineColumnWidth;
974     }
975 
976     FormatTok->IsForEachMacro =
977         std::binary_search(ForEachMacros.begin(), ForEachMacros.end(),
978                            FormatTok->Tok.getIdentifierInfo());
979 
980     return FormatTok;
981   }
982 
983   FormatToken *FormatTok;
984   bool IsFirstToken;
985   bool GreaterStashed;
986   unsigned Column;
987   unsigned TrailingWhitespace;
988   std::unique_ptr<Lexer> Lex;
989   SourceManager &SourceMgr;
990   FileID ID;
991   FormatStyle &Style;
992   IdentifierTable IdentTable;
993   AdditionalKeywords Keywords;
994   encoding::Encoding Encoding;
995   llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
996   // Index (in 'Tokens') of the last token that starts a new line.
997   unsigned FirstInLineIndex;
998   SmallVector<FormatToken *, 16> Tokens;
999   SmallVector<IdentifierInfo *, 8> ForEachMacros;
1000 
1001   bool FormattingDisabled;
1002 
1003   void readRawToken(FormatToken &Tok) {
1004     Lex->LexFromRawLexer(Tok.Tok);
1005     Tok.TokenText = StringRef(SourceMgr.getCharacterData(Tok.Tok.getLocation()),
1006                               Tok.Tok.getLength());
1007     // For formatting, treat unterminated string literals like normal string
1008     // literals.
1009     if (Tok.is(tok::unknown)) {
1010       if (!Tok.TokenText.empty() && Tok.TokenText[0] == '"') {
1011         Tok.Tok.setKind(tok::string_literal);
1012         Tok.IsUnterminatedLiteral = true;
1013       } else if (Style.Language == FormatStyle::LK_JavaScript &&
1014                  Tok.TokenText == "''") {
1015         Tok.Tok.setKind(tok::char_constant);
1016       }
1017     }
1018 
1019     if (Tok.is(tok::comment) && (Tok.TokenText == "// clang-format on" ||
1020                                  Tok.TokenText == "/* clang-format on */")) {
1021       FormattingDisabled = false;
1022     }
1023 
1024     Tok.Finalized = FormattingDisabled;
1025 
1026     if (Tok.is(tok::comment) && (Tok.TokenText == "// clang-format off" ||
1027                                  Tok.TokenText == "/* clang-format off */")) {
1028       FormattingDisabled = true;
1029     }
1030   }
1031 
1032   void resetLexer(unsigned Offset) {
1033     StringRef Buffer = SourceMgr.getBufferData(ID);
1034     Lex.reset(new Lexer(SourceMgr.getLocForStartOfFile(ID),
1035                         getFormattingLangOpts(Style), Buffer.begin(),
1036                         Buffer.begin() + Offset, Buffer.end()));
1037     Lex->SetKeepWhitespaceMode(true);
1038   }
1039 };
1040 
1041 static StringRef getLanguageName(FormatStyle::LanguageKind Language) {
1042   switch (Language) {
1043   case FormatStyle::LK_Cpp:
1044     return "C++";
1045   case FormatStyle::LK_Java:
1046     return "Java";
1047   case FormatStyle::LK_JavaScript:
1048     return "JavaScript";
1049   case FormatStyle::LK_Proto:
1050     return "Proto";
1051   default:
1052     return "Unknown";
1053   }
1054 }
1055 
1056 class Formatter : public UnwrappedLineConsumer {
1057 public:
1058   Formatter(const FormatStyle &Style, SourceManager &SourceMgr, FileID ID,
1059             ArrayRef<CharSourceRange> Ranges)
1060       : Style(Style), ID(ID), SourceMgr(SourceMgr),
1061         Whitespaces(SourceMgr, Style,
1062                     inputUsesCRLF(SourceMgr.getBufferData(ID))),
1063         Ranges(Ranges.begin(), Ranges.end()), UnwrappedLines(1),
1064         Encoding(encoding::detectEncoding(SourceMgr.getBufferData(ID))) {
1065     DEBUG(llvm::dbgs() << "File encoding: "
1066                        << (Encoding == encoding::Encoding_UTF8 ? "UTF8"
1067                                                                : "unknown")
1068                        << "\n");
1069     DEBUG(llvm::dbgs() << "Language: " << getLanguageName(Style.Language)
1070                        << "\n");
1071   }
1072 
1073   tooling::Replacements format() {
1074     tooling::Replacements Result;
1075     FormatTokenLexer Tokens(SourceMgr, ID, Style, Encoding);
1076 
1077     UnwrappedLineParser Parser(Style, Tokens.getKeywords(), Tokens.lex(),
1078                                *this);
1079     bool StructuralError = Parser.parse();
1080     assert(UnwrappedLines.rbegin()->empty());
1081     for (unsigned Run = 0, RunE = UnwrappedLines.size(); Run + 1 != RunE;
1082          ++Run) {
1083       DEBUG(llvm::dbgs() << "Run " << Run << "...\n");
1084       SmallVector<AnnotatedLine *, 16> AnnotatedLines;
1085       for (unsigned i = 0, e = UnwrappedLines[Run].size(); i != e; ++i) {
1086         AnnotatedLines.push_back(new AnnotatedLine(UnwrappedLines[Run][i]));
1087       }
1088       tooling::Replacements RunResult =
1089           format(AnnotatedLines, StructuralError, Tokens);
1090       DEBUG({
1091         llvm::dbgs() << "Replacements for run " << Run << ":\n";
1092         for (tooling::Replacements::iterator I = RunResult.begin(),
1093                                              E = RunResult.end();
1094              I != E; ++I) {
1095           llvm::dbgs() << I->toString() << "\n";
1096         }
1097       });
1098       for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1099         delete AnnotatedLines[i];
1100       }
1101       Result.insert(RunResult.begin(), RunResult.end());
1102       Whitespaces.reset();
1103     }
1104     return Result;
1105   }
1106 
1107   tooling::Replacements format(SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
1108                                bool StructuralError, FormatTokenLexer &Tokens) {
1109     TokenAnnotator Annotator(Style, Tokens.getKeywords());
1110     for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1111       Annotator.annotate(*AnnotatedLines[i]);
1112     }
1113     deriveLocalStyle(AnnotatedLines);
1114     for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1115       Annotator.calculateFormattingInformation(*AnnotatedLines[i]);
1116     }
1117     computeAffectedLines(AnnotatedLines.begin(), AnnotatedLines.end());
1118 
1119     Annotator.setCommentLineLevels(AnnotatedLines);
1120     ContinuationIndenter Indenter(Style, Tokens.getKeywords(), SourceMgr,
1121                                   Whitespaces, Encoding,
1122                                   BinPackInconclusiveFunctions);
1123     UnwrappedLineFormatter Formatter(&Indenter, &Whitespaces, Style);
1124     Formatter.format(AnnotatedLines, /*DryRun=*/false);
1125     return Whitespaces.generateReplacements();
1126   }
1127 
1128 private:
1129   // Determines which lines are affected by the SourceRanges given as input.
1130   // Returns \c true if at least one line between I and E or one of their
1131   // children is affected.
1132   bool computeAffectedLines(SmallVectorImpl<AnnotatedLine *>::iterator I,
1133                             SmallVectorImpl<AnnotatedLine *>::iterator E) {
1134     bool SomeLineAffected = false;
1135     const AnnotatedLine *PreviousLine = nullptr;
1136     while (I != E) {
1137       AnnotatedLine *Line = *I;
1138       Line->LeadingEmptyLinesAffected = affectsLeadingEmptyLines(*Line->First);
1139 
1140       // If a line is part of a preprocessor directive, it needs to be formatted
1141       // if any token within the directive is affected.
1142       if (Line->InPPDirective) {
1143         FormatToken *Last = Line->Last;
1144         SmallVectorImpl<AnnotatedLine *>::iterator PPEnd = I + 1;
1145         while (PPEnd != E && !(*PPEnd)->First->HasUnescapedNewline) {
1146           Last = (*PPEnd)->Last;
1147           ++PPEnd;
1148         }
1149 
1150         if (affectsTokenRange(*Line->First, *Last,
1151                               /*IncludeLeadingNewlines=*/false)) {
1152           SomeLineAffected = true;
1153           markAllAsAffected(I, PPEnd);
1154         }
1155         I = PPEnd;
1156         continue;
1157       }
1158 
1159       if (nonPPLineAffected(Line, PreviousLine))
1160         SomeLineAffected = true;
1161 
1162       PreviousLine = Line;
1163       ++I;
1164     }
1165     return SomeLineAffected;
1166   }
1167 
1168   // Determines whether 'Line' is affected by the SourceRanges given as input.
1169   // Returns \c true if line or one if its children is affected.
1170   bool nonPPLineAffected(AnnotatedLine *Line,
1171                          const AnnotatedLine *PreviousLine) {
1172     bool SomeLineAffected = false;
1173     Line->ChildrenAffected =
1174         computeAffectedLines(Line->Children.begin(), Line->Children.end());
1175     if (Line->ChildrenAffected)
1176       SomeLineAffected = true;
1177 
1178     // Stores whether one of the line's tokens is directly affected.
1179     bool SomeTokenAffected = false;
1180     // Stores whether we need to look at the leading newlines of the next token
1181     // in order to determine whether it was affected.
1182     bool IncludeLeadingNewlines = false;
1183 
1184     // Stores whether the first child line of any of this line's tokens is
1185     // affected.
1186     bool SomeFirstChildAffected = false;
1187 
1188     for (FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) {
1189       // Determine whether 'Tok' was affected.
1190       if (affectsTokenRange(*Tok, *Tok, IncludeLeadingNewlines))
1191         SomeTokenAffected = true;
1192 
1193       // Determine whether the first child of 'Tok' was affected.
1194       if (!Tok->Children.empty() && Tok->Children.front()->Affected)
1195         SomeFirstChildAffected = true;
1196 
1197       IncludeLeadingNewlines = Tok->Children.empty();
1198     }
1199 
1200     // Was this line moved, i.e. has it previously been on the same line as an
1201     // affected line?
1202     bool LineMoved = PreviousLine && PreviousLine->Affected &&
1203                      Line->First->NewlinesBefore == 0;
1204 
1205     bool IsContinuedComment =
1206         Line->First->is(tok::comment) && Line->First->Next == nullptr &&
1207         Line->First->NewlinesBefore < 2 && PreviousLine &&
1208         PreviousLine->Affected && PreviousLine->Last->is(tok::comment);
1209 
1210     if (SomeTokenAffected || SomeFirstChildAffected || LineMoved ||
1211         IsContinuedComment) {
1212       Line->Affected = true;
1213       SomeLineAffected = true;
1214     }
1215     return SomeLineAffected;
1216   }
1217 
1218   // Marks all lines between I and E as well as all their children as affected.
1219   void markAllAsAffected(SmallVectorImpl<AnnotatedLine *>::iterator I,
1220                          SmallVectorImpl<AnnotatedLine *>::iterator E) {
1221     while (I != E) {
1222       (*I)->Affected = true;
1223       markAllAsAffected((*I)->Children.begin(), (*I)->Children.end());
1224       ++I;
1225     }
1226   }
1227 
1228   // Returns true if the range from 'First' to 'Last' intersects with one of the
1229   // input ranges.
1230   bool affectsTokenRange(const FormatToken &First, const FormatToken &Last,
1231                          bool IncludeLeadingNewlines) {
1232     SourceLocation Start = First.WhitespaceRange.getBegin();
1233     if (!IncludeLeadingNewlines)
1234       Start = Start.getLocWithOffset(First.LastNewlineOffset);
1235     SourceLocation End = Last.getStartOfNonWhitespace();
1236     End = End.getLocWithOffset(Last.TokenText.size());
1237     CharSourceRange Range = CharSourceRange::getCharRange(Start, End);
1238     return affectsCharSourceRange(Range);
1239   }
1240 
1241   // Returns true if one of the input ranges intersect the leading empty lines
1242   // before 'Tok'.
1243   bool affectsLeadingEmptyLines(const FormatToken &Tok) {
1244     CharSourceRange EmptyLineRange = CharSourceRange::getCharRange(
1245         Tok.WhitespaceRange.getBegin(),
1246         Tok.WhitespaceRange.getBegin().getLocWithOffset(Tok.LastNewlineOffset));
1247     return affectsCharSourceRange(EmptyLineRange);
1248   }
1249 
1250   // Returns true if 'Range' intersects with one of the input ranges.
1251   bool affectsCharSourceRange(const CharSourceRange &Range) {
1252     for (SmallVectorImpl<CharSourceRange>::const_iterator I = Ranges.begin(),
1253                                                           E = Ranges.end();
1254          I != E; ++I) {
1255       if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), I->getBegin()) &&
1256           !SourceMgr.isBeforeInTranslationUnit(I->getEnd(), Range.getBegin()))
1257         return true;
1258     }
1259     return false;
1260   }
1261 
1262   static bool inputUsesCRLF(StringRef Text) {
1263     return Text.count('\r') * 2 > Text.count('\n');
1264   }
1265 
1266   void
1267   deriveLocalStyle(const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
1268     unsigned CountBoundToVariable = 0;
1269     unsigned CountBoundToType = 0;
1270     bool HasCpp03IncompatibleFormat = false;
1271     bool HasBinPackedFunction = false;
1272     bool HasOnePerLineFunction = false;
1273     for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1274       if (!AnnotatedLines[i]->First->Next)
1275         continue;
1276       FormatToken *Tok = AnnotatedLines[i]->First->Next;
1277       while (Tok->Next) {
1278         if (Tok->is(TT_PointerOrReference)) {
1279           bool SpacesBefore =
1280               Tok->WhitespaceRange.getBegin() != Tok->WhitespaceRange.getEnd();
1281           bool SpacesAfter = Tok->Next->WhitespaceRange.getBegin() !=
1282                              Tok->Next->WhitespaceRange.getEnd();
1283           if (SpacesBefore && !SpacesAfter)
1284             ++CountBoundToVariable;
1285           else if (!SpacesBefore && SpacesAfter)
1286             ++CountBoundToType;
1287         }
1288 
1289         if (Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd()) {
1290           if (Tok->is(tok::coloncolon) && Tok->Previous->is(TT_TemplateOpener))
1291             HasCpp03IncompatibleFormat = true;
1292           if (Tok->is(TT_TemplateCloser) &&
1293               Tok->Previous->is(TT_TemplateCloser))
1294             HasCpp03IncompatibleFormat = true;
1295         }
1296 
1297         if (Tok->PackingKind == PPK_BinPacked)
1298           HasBinPackedFunction = true;
1299         if (Tok->PackingKind == PPK_OnePerLine)
1300           HasOnePerLineFunction = true;
1301 
1302         Tok = Tok->Next;
1303       }
1304     }
1305     if (Style.DerivePointerAlignment) {
1306       if (CountBoundToType > CountBoundToVariable)
1307         Style.PointerAlignment = FormatStyle::PAS_Left;
1308       else if (CountBoundToType < CountBoundToVariable)
1309         Style.PointerAlignment = FormatStyle::PAS_Right;
1310     }
1311     if (Style.Standard == FormatStyle::LS_Auto) {
1312       Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11
1313                                                   : FormatStyle::LS_Cpp03;
1314     }
1315     BinPackInconclusiveFunctions =
1316         HasBinPackedFunction || !HasOnePerLineFunction;
1317   }
1318 
1319   void consumeUnwrappedLine(const UnwrappedLine &TheLine) override {
1320     assert(!UnwrappedLines.empty());
1321     UnwrappedLines.back().push_back(TheLine);
1322   }
1323 
1324   void finishRun() override {
1325     UnwrappedLines.push_back(SmallVector<UnwrappedLine, 16>());
1326   }
1327 
1328   FormatStyle Style;
1329   FileID ID;
1330   SourceManager &SourceMgr;
1331   WhitespaceManager Whitespaces;
1332   SmallVector<CharSourceRange, 8> Ranges;
1333   SmallVector<SmallVector<UnwrappedLine, 16>, 2> UnwrappedLines;
1334 
1335   encoding::Encoding Encoding;
1336   bool BinPackInconclusiveFunctions;
1337 };
1338 
1339 } // end anonymous namespace
1340 
1341 tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
1342                                SourceManager &SourceMgr,
1343                                ArrayRef<CharSourceRange> Ranges) {
1344   if (Style.DisableFormat)
1345     return tooling::Replacements();
1346   return reformat(Style, SourceMgr,
1347                   SourceMgr.getFileID(Lex.getSourceLocation()), Ranges);
1348 }
1349 
1350 tooling::Replacements reformat(const FormatStyle &Style,
1351                                SourceManager &SourceMgr, FileID ID,
1352                                ArrayRef<CharSourceRange> Ranges) {
1353   if (Style.DisableFormat)
1354     return tooling::Replacements();
1355   Formatter formatter(Style, SourceMgr, ID, Ranges);
1356   return formatter.format();
1357 }
1358 
1359 tooling::Replacements reformat(const FormatStyle &Style, StringRef Code,
1360                                ArrayRef<tooling::Range> Ranges,
1361                                StringRef FileName) {
1362   if (Style.DisableFormat)
1363     return tooling::Replacements();
1364 
1365   FileManager Files((FileSystemOptions()));
1366   DiagnosticsEngine Diagnostics(
1367       IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
1368       new DiagnosticOptions);
1369   SourceManager SourceMgr(Diagnostics, Files);
1370   std::unique_ptr<llvm::MemoryBuffer> Buf =
1371       llvm::MemoryBuffer::getMemBuffer(Code, FileName);
1372   const clang::FileEntry *Entry =
1373       Files.getVirtualFile(FileName, Buf->getBufferSize(), 0);
1374   SourceMgr.overrideFileContents(Entry, std::move(Buf));
1375   FileID ID =
1376       SourceMgr.createFileID(Entry, SourceLocation(), clang::SrcMgr::C_User);
1377   SourceLocation StartOfFile = SourceMgr.getLocForStartOfFile(ID);
1378   std::vector<CharSourceRange> CharRanges;
1379   for (const tooling::Range &Range : Ranges) {
1380     SourceLocation Start = StartOfFile.getLocWithOffset(Range.getOffset());
1381     SourceLocation End = Start.getLocWithOffset(Range.getLength());
1382     CharRanges.push_back(CharSourceRange::getCharRange(Start, End));
1383   }
1384   return reformat(Style, SourceMgr, ID, CharRanges);
1385 }
1386 
1387 LangOptions getFormattingLangOpts(const FormatStyle &Style) {
1388   LangOptions LangOpts;
1389   LangOpts.CPlusPlus = 1;
1390   LangOpts.CPlusPlus11 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
1391   LangOpts.CPlusPlus14 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
1392   LangOpts.LineComment = 1;
1393   bool AlternativeOperators = Style.Language != FormatStyle::LK_JavaScript &&
1394                               Style.Language != FormatStyle::LK_Java;
1395   LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0;
1396   LangOpts.Bool = 1;
1397   LangOpts.ObjC1 = 1;
1398   LangOpts.ObjC2 = 1;
1399   return LangOpts;
1400 }
1401 
1402 const char *StyleOptionHelpDescription =
1403     "Coding style, currently supports:\n"
1404     "  LLVM, Google, Chromium, Mozilla, WebKit.\n"
1405     "Use -style=file to load style configuration from\n"
1406     ".clang-format file located in one of the parent\n"
1407     "directories of the source file (or current\n"
1408     "directory for stdin).\n"
1409     "Use -style=\"{key: value, ...}\" to set specific\n"
1410     "parameters, e.g.:\n"
1411     "  -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\"";
1412 
1413 static FormatStyle::LanguageKind getLanguageByFileName(StringRef FileName) {
1414   if (FileName.endswith(".java")) {
1415     return FormatStyle::LK_Java;
1416   } else if (FileName.endswith_lower(".js")) {
1417     return FormatStyle::LK_JavaScript;
1418   } else if (FileName.endswith_lower(".proto") ||
1419              FileName.endswith_lower(".protodevel")) {
1420     return FormatStyle::LK_Proto;
1421   }
1422   return FormatStyle::LK_Cpp;
1423 }
1424 
1425 FormatStyle getStyle(StringRef StyleName, StringRef FileName,
1426                      StringRef FallbackStyle) {
1427   FormatStyle Style = getLLVMStyle();
1428   Style.Language = getLanguageByFileName(FileName);
1429   if (!getPredefinedStyle(FallbackStyle, Style.Language, &Style)) {
1430     llvm::errs() << "Invalid fallback style \"" << FallbackStyle
1431                  << "\" using LLVM style\n";
1432     return Style;
1433   }
1434 
1435   if (StyleName.startswith("{")) {
1436     // Parse YAML/JSON style from the command line.
1437     if (std::error_code ec = parseConfiguration(StyleName, &Style)) {
1438       llvm::errs() << "Error parsing -style: " << ec.message() << ", using "
1439                    << FallbackStyle << " style\n";
1440     }
1441     return Style;
1442   }
1443 
1444   if (!StyleName.equals_lower("file")) {
1445     if (!getPredefinedStyle(StyleName, Style.Language, &Style))
1446       llvm::errs() << "Invalid value for -style, using " << FallbackStyle
1447                    << " style\n";
1448     return Style;
1449   }
1450 
1451   // Look for .clang-format/_clang-format file in the file's parent directories.
1452   SmallString<128> UnsuitableConfigFiles;
1453   SmallString<128> Path(FileName);
1454   llvm::sys::fs::make_absolute(Path);
1455   for (StringRef Directory = Path; !Directory.empty();
1456        Directory = llvm::sys::path::parent_path(Directory)) {
1457     if (!llvm::sys::fs::is_directory(Directory))
1458       continue;
1459     SmallString<128> ConfigFile(Directory);
1460 
1461     llvm::sys::path::append(ConfigFile, ".clang-format");
1462     DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
1463     bool IsFile = false;
1464     // Ignore errors from is_regular_file: we only need to know if we can read
1465     // the file or not.
1466     llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile);
1467 
1468     if (!IsFile) {
1469       // Try _clang-format too, since dotfiles are not commonly used on Windows.
1470       ConfigFile = Directory;
1471       llvm::sys::path::append(ConfigFile, "_clang-format");
1472       DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
1473       llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile);
1474     }
1475 
1476     if (IsFile) {
1477       llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
1478           llvm::MemoryBuffer::getFile(ConfigFile.c_str());
1479       if (std::error_code EC = Text.getError()) {
1480         llvm::errs() << EC.message() << "\n";
1481         break;
1482       }
1483       if (std::error_code ec =
1484               parseConfiguration(Text.get()->getBuffer(), &Style)) {
1485         if (ec == ParseError::Unsuitable) {
1486           if (!UnsuitableConfigFiles.empty())
1487             UnsuitableConfigFiles.append(", ");
1488           UnsuitableConfigFiles.append(ConfigFile);
1489           continue;
1490         }
1491         llvm::errs() << "Error reading " << ConfigFile << ": " << ec.message()
1492                      << "\n";
1493         break;
1494       }
1495       DEBUG(llvm::dbgs() << "Using configuration file " << ConfigFile << "\n");
1496       return Style;
1497     }
1498   }
1499   llvm::errs() << "Can't find usable .clang-format, using " << FallbackStyle
1500                << " style\n";
1501   if (!UnsuitableConfigFiles.empty()) {
1502     llvm::errs() << "Configuration file(s) do(es) not support "
1503                  << getLanguageName(Style.Language) << ": "
1504                  << UnsuitableConfigFiles << "\n";
1505   }
1506   return Style;
1507 }
1508 
1509 } // namespace format
1510 } // namespace clang
1511