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