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 "clang/Format/Format.h"
17 #include "AffectedRangeManager.h"
18 #include "ContinuationIndenter.h"
19 #include "FormatInternal.h"
20 #include "FormatTokenLexer.h"
21 #include "NamespaceEndCommentsFixer.h"
22 #include "SortJavaScriptImports.h"
23 #include "TokenAnalyzer.h"
24 #include "TokenAnnotator.h"
25 #include "UnwrappedLineFormatter.h"
26 #include "UnwrappedLineParser.h"
27 #include "UsingDeclarationsSorter.h"
28 #include "WhitespaceManager.h"
29 #include "clang/Basic/Diagnostic.h"
30 #include "clang/Basic/DiagnosticOptions.h"
31 #include "clang/Basic/SourceManager.h"
32 #include "clang/Basic/VirtualFileSystem.h"
33 #include "clang/Lex/Lexer.h"
34 #include "llvm/ADT/STLExtras.h"
35 #include "llvm/Support/Allocator.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/Path.h"
38 #include "llvm/Support/Regex.h"
39 #include "llvm/Support/YAMLTraits.h"
40 #include <algorithm>
41 #include <memory>
42 #include <string>
43 
44 #define DEBUG_TYPE "format-formatter"
45 
46 using clang::format::FormatStyle;
47 
48 LLVM_YAML_IS_SEQUENCE_VECTOR(clang::format::FormatStyle::IncludeCategory)
49 LLVM_YAML_IS_SEQUENCE_VECTOR(clang::format::FormatStyle::RawStringFormat)
50 
51 namespace llvm {
52 namespace yaml {
53 template <> struct ScalarEnumerationTraits<FormatStyle::LanguageKind> {
54   static void enumeration(IO &IO, FormatStyle::LanguageKind &Value) {
55     IO.enumCase(Value, "Cpp", FormatStyle::LK_Cpp);
56     IO.enumCase(Value, "Java", FormatStyle::LK_Java);
57     IO.enumCase(Value, "JavaScript", FormatStyle::LK_JavaScript);
58     IO.enumCase(Value, "ObjC", FormatStyle::LK_ObjC);
59     IO.enumCase(Value, "Proto", FormatStyle::LK_Proto);
60     IO.enumCase(Value, "TableGen", FormatStyle::LK_TableGen);
61     IO.enumCase(Value, "TextProto", FormatStyle::LK_TextProto);
62   }
63 };
64 
65 template <> struct ScalarEnumerationTraits<FormatStyle::LanguageStandard> {
66   static void enumeration(IO &IO, FormatStyle::LanguageStandard &Value) {
67     IO.enumCase(Value, "Cpp03", FormatStyle::LS_Cpp03);
68     IO.enumCase(Value, "C++03", FormatStyle::LS_Cpp03);
69     IO.enumCase(Value, "Cpp11", FormatStyle::LS_Cpp11);
70     IO.enumCase(Value, "C++11", FormatStyle::LS_Cpp11);
71     IO.enumCase(Value, "Auto", FormatStyle::LS_Auto);
72   }
73 };
74 
75 template <> struct ScalarEnumerationTraits<FormatStyle::UseTabStyle> {
76   static void enumeration(IO &IO, FormatStyle::UseTabStyle &Value) {
77     IO.enumCase(Value, "Never", FormatStyle::UT_Never);
78     IO.enumCase(Value, "false", FormatStyle::UT_Never);
79     IO.enumCase(Value, "Always", FormatStyle::UT_Always);
80     IO.enumCase(Value, "true", FormatStyle::UT_Always);
81     IO.enumCase(Value, "ForIndentation", FormatStyle::UT_ForIndentation);
82     IO.enumCase(Value, "ForContinuationAndIndentation",
83                 FormatStyle::UT_ForContinuationAndIndentation);
84   }
85 };
86 
87 template <> struct ScalarEnumerationTraits<FormatStyle::JavaScriptQuoteStyle> {
88   static void enumeration(IO &IO, FormatStyle::JavaScriptQuoteStyle &Value) {
89     IO.enumCase(Value, "Leave", FormatStyle::JSQS_Leave);
90     IO.enumCase(Value, "Single", FormatStyle::JSQS_Single);
91     IO.enumCase(Value, "Double", FormatStyle::JSQS_Double);
92   }
93 };
94 
95 template <> struct ScalarEnumerationTraits<FormatStyle::ShortFunctionStyle> {
96   static void enumeration(IO &IO, FormatStyle::ShortFunctionStyle &Value) {
97     IO.enumCase(Value, "None", FormatStyle::SFS_None);
98     IO.enumCase(Value, "false", FormatStyle::SFS_None);
99     IO.enumCase(Value, "All", FormatStyle::SFS_All);
100     IO.enumCase(Value, "true", FormatStyle::SFS_All);
101     IO.enumCase(Value, "Inline", FormatStyle::SFS_Inline);
102     IO.enumCase(Value, "InlineOnly", FormatStyle::SFS_InlineOnly);
103     IO.enumCase(Value, "Empty", FormatStyle::SFS_Empty);
104   }
105 };
106 
107 template <> struct ScalarEnumerationTraits<FormatStyle::BinaryOperatorStyle> {
108   static void enumeration(IO &IO, FormatStyle::BinaryOperatorStyle &Value) {
109     IO.enumCase(Value, "All", FormatStyle::BOS_All);
110     IO.enumCase(Value, "true", FormatStyle::BOS_All);
111     IO.enumCase(Value, "None", FormatStyle::BOS_None);
112     IO.enumCase(Value, "false", FormatStyle::BOS_None);
113     IO.enumCase(Value, "NonAssignment", FormatStyle::BOS_NonAssignment);
114   }
115 };
116 
117 template <> struct ScalarEnumerationTraits<FormatStyle::BraceBreakingStyle> {
118   static void enumeration(IO &IO, FormatStyle::BraceBreakingStyle &Value) {
119     IO.enumCase(Value, "Attach", FormatStyle::BS_Attach);
120     IO.enumCase(Value, "Linux", FormatStyle::BS_Linux);
121     IO.enumCase(Value, "Mozilla", FormatStyle::BS_Mozilla);
122     IO.enumCase(Value, "Stroustrup", FormatStyle::BS_Stroustrup);
123     IO.enumCase(Value, "Allman", FormatStyle::BS_Allman);
124     IO.enumCase(Value, "GNU", FormatStyle::BS_GNU);
125     IO.enumCase(Value, "WebKit", FormatStyle::BS_WebKit);
126     IO.enumCase(Value, "Custom", FormatStyle::BS_Custom);
127   }
128 };
129 
130 template <>
131 struct ScalarEnumerationTraits<FormatStyle::BreakConstructorInitializersStyle> {
132   static void
133   enumeration(IO &IO, FormatStyle::BreakConstructorInitializersStyle &Value) {
134     IO.enumCase(Value, "BeforeColon", FormatStyle::BCIS_BeforeColon);
135     IO.enumCase(Value, "BeforeComma", FormatStyle::BCIS_BeforeComma);
136     IO.enumCase(Value, "AfterColon", FormatStyle::BCIS_AfterColon);
137   }
138 };
139 
140 template <>
141 struct ScalarEnumerationTraits<FormatStyle::PPDirectiveIndentStyle> {
142   static void enumeration(IO &IO, FormatStyle::PPDirectiveIndentStyle &Value) {
143     IO.enumCase(Value, "None", FormatStyle::PPDIS_None);
144     IO.enumCase(Value, "AfterHash", FormatStyle::PPDIS_AfterHash);
145   }
146 };
147 
148 template <>
149 struct ScalarEnumerationTraits<FormatStyle::ReturnTypeBreakingStyle> {
150   static void enumeration(IO &IO, FormatStyle::ReturnTypeBreakingStyle &Value) {
151     IO.enumCase(Value, "None", FormatStyle::RTBS_None);
152     IO.enumCase(Value, "All", FormatStyle::RTBS_All);
153     IO.enumCase(Value, "TopLevel", FormatStyle::RTBS_TopLevel);
154     IO.enumCase(Value, "TopLevelDefinitions",
155                 FormatStyle::RTBS_TopLevelDefinitions);
156     IO.enumCase(Value, "AllDefinitions", FormatStyle::RTBS_AllDefinitions);
157   }
158 };
159 
160 template <>
161 struct ScalarEnumerationTraits<FormatStyle::DefinitionReturnTypeBreakingStyle> {
162   static void
163   enumeration(IO &IO, FormatStyle::DefinitionReturnTypeBreakingStyle &Value) {
164     IO.enumCase(Value, "None", FormatStyle::DRTBS_None);
165     IO.enumCase(Value, "All", FormatStyle::DRTBS_All);
166     IO.enumCase(Value, "TopLevel", FormatStyle::DRTBS_TopLevel);
167 
168     // For backward compatibility.
169     IO.enumCase(Value, "false", FormatStyle::DRTBS_None);
170     IO.enumCase(Value, "true", FormatStyle::DRTBS_All);
171   }
172 };
173 
174 template <>
175 struct ScalarEnumerationTraits<FormatStyle::NamespaceIndentationKind> {
176   static void enumeration(IO &IO,
177                           FormatStyle::NamespaceIndentationKind &Value) {
178     IO.enumCase(Value, "None", FormatStyle::NI_None);
179     IO.enumCase(Value, "Inner", FormatStyle::NI_Inner);
180     IO.enumCase(Value, "All", FormatStyle::NI_All);
181   }
182 };
183 
184 template <> struct ScalarEnumerationTraits<FormatStyle::BracketAlignmentStyle> {
185   static void enumeration(IO &IO, FormatStyle::BracketAlignmentStyle &Value) {
186     IO.enumCase(Value, "Align", FormatStyle::BAS_Align);
187     IO.enumCase(Value, "DontAlign", FormatStyle::BAS_DontAlign);
188     IO.enumCase(Value, "AlwaysBreak", FormatStyle::BAS_AlwaysBreak);
189 
190     // For backward compatibility.
191     IO.enumCase(Value, "true", FormatStyle::BAS_Align);
192     IO.enumCase(Value, "false", FormatStyle::BAS_DontAlign);
193   }
194 };
195 
196 template <>
197 struct ScalarEnumerationTraits<FormatStyle::EscapedNewlineAlignmentStyle> {
198   static void enumeration(IO &IO,
199                           FormatStyle::EscapedNewlineAlignmentStyle &Value) {
200     IO.enumCase(Value, "DontAlign", FormatStyle::ENAS_DontAlign);
201     IO.enumCase(Value, "Left", FormatStyle::ENAS_Left);
202     IO.enumCase(Value, "Right", FormatStyle::ENAS_Right);
203 
204     // For backward compatibility.
205     IO.enumCase(Value, "true", FormatStyle::ENAS_Left);
206     IO.enumCase(Value, "false", FormatStyle::ENAS_Right);
207   }
208 };
209 
210 template <> struct ScalarEnumerationTraits<FormatStyle::PointerAlignmentStyle> {
211   static void enumeration(IO &IO, FormatStyle::PointerAlignmentStyle &Value) {
212     IO.enumCase(Value, "Middle", FormatStyle::PAS_Middle);
213     IO.enumCase(Value, "Left", FormatStyle::PAS_Left);
214     IO.enumCase(Value, "Right", FormatStyle::PAS_Right);
215 
216     // For backward compatibility.
217     IO.enumCase(Value, "true", FormatStyle::PAS_Left);
218     IO.enumCase(Value, "false", FormatStyle::PAS_Right);
219   }
220 };
221 
222 template <>
223 struct ScalarEnumerationTraits<FormatStyle::SpaceBeforeParensOptions> {
224   static void enumeration(IO &IO,
225                           FormatStyle::SpaceBeforeParensOptions &Value) {
226     IO.enumCase(Value, "Never", FormatStyle::SBPO_Never);
227     IO.enumCase(Value, "ControlStatements",
228                 FormatStyle::SBPO_ControlStatements);
229     IO.enumCase(Value, "Always", FormatStyle::SBPO_Always);
230 
231     // For backward compatibility.
232     IO.enumCase(Value, "false", FormatStyle::SBPO_Never);
233     IO.enumCase(Value, "true", FormatStyle::SBPO_ControlStatements);
234   }
235 };
236 
237 template <> struct MappingTraits<FormatStyle> {
238   static void mapping(IO &IO, FormatStyle &Style) {
239     // When reading, read the language first, we need it for getPredefinedStyle.
240     IO.mapOptional("Language", Style.Language);
241 
242     if (IO.outputting()) {
243       StringRef StylesArray[] = {"LLVM",    "Google", "Chromium",
244                                  "Mozilla", "WebKit", "GNU"};
245       ArrayRef<StringRef> Styles(StylesArray);
246       for (size_t i = 0, e = Styles.size(); i < e; ++i) {
247         StringRef StyleName(Styles[i]);
248         FormatStyle PredefinedStyle;
249         if (getPredefinedStyle(StyleName, Style.Language, &PredefinedStyle) &&
250             Style == PredefinedStyle) {
251           IO.mapOptional("# BasedOnStyle", StyleName);
252           break;
253         }
254       }
255     } else {
256       StringRef BasedOnStyle;
257       IO.mapOptional("BasedOnStyle", BasedOnStyle);
258       if (!BasedOnStyle.empty()) {
259         FormatStyle::LanguageKind OldLanguage = Style.Language;
260         FormatStyle::LanguageKind Language =
261             ((FormatStyle *)IO.getContext())->Language;
262         if (!getPredefinedStyle(BasedOnStyle, Language, &Style)) {
263           IO.setError(Twine("Unknown value for BasedOnStyle: ", BasedOnStyle));
264           return;
265         }
266         Style.Language = OldLanguage;
267       }
268     }
269 
270     // For backward compatibility.
271     if (!IO.outputting()) {
272       IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlines);
273       IO.mapOptional("DerivePointerBinding", Style.DerivePointerAlignment);
274       IO.mapOptional("IndentFunctionDeclarationAfterType",
275                      Style.IndentWrappedFunctionNames);
276       IO.mapOptional("PointerBindsToType", Style.PointerAlignment);
277       IO.mapOptional("SpaceAfterControlStatementKeyword",
278                      Style.SpaceBeforeParens);
279     }
280 
281     IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset);
282     IO.mapOptional("AlignAfterOpenBracket", Style.AlignAfterOpenBracket);
283     IO.mapOptional("AlignConsecutiveAssignments",
284                    Style.AlignConsecutiveAssignments);
285     IO.mapOptional("AlignConsecutiveDeclarations",
286                    Style.AlignConsecutiveDeclarations);
287     IO.mapOptional("AlignEscapedNewlines", Style.AlignEscapedNewlines);
288     IO.mapOptional("AlignOperands", Style.AlignOperands);
289     IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments);
290     IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine",
291                    Style.AllowAllParametersOfDeclarationOnNextLine);
292     IO.mapOptional("AllowShortBlocksOnASingleLine",
293                    Style.AllowShortBlocksOnASingleLine);
294     IO.mapOptional("AllowShortCaseLabelsOnASingleLine",
295                    Style.AllowShortCaseLabelsOnASingleLine);
296     IO.mapOptional("AllowShortFunctionsOnASingleLine",
297                    Style.AllowShortFunctionsOnASingleLine);
298     IO.mapOptional("AllowShortIfStatementsOnASingleLine",
299                    Style.AllowShortIfStatementsOnASingleLine);
300     IO.mapOptional("AllowShortLoopsOnASingleLine",
301                    Style.AllowShortLoopsOnASingleLine);
302     IO.mapOptional("AlwaysBreakAfterDefinitionReturnType",
303                    Style.AlwaysBreakAfterDefinitionReturnType);
304     IO.mapOptional("AlwaysBreakAfterReturnType",
305                    Style.AlwaysBreakAfterReturnType);
306     // If AlwaysBreakAfterDefinitionReturnType was specified but
307     // AlwaysBreakAfterReturnType was not, initialize the latter from the
308     // former for backwards compatibility.
309     if (Style.AlwaysBreakAfterDefinitionReturnType != FormatStyle::DRTBS_None &&
310         Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_None) {
311       if (Style.AlwaysBreakAfterDefinitionReturnType == FormatStyle::DRTBS_All)
312         Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
313       else if (Style.AlwaysBreakAfterDefinitionReturnType ==
314                FormatStyle::DRTBS_TopLevel)
315         Style.AlwaysBreakAfterReturnType =
316             FormatStyle::RTBS_TopLevelDefinitions;
317     }
318 
319     IO.mapOptional("AlwaysBreakBeforeMultilineStrings",
320                    Style.AlwaysBreakBeforeMultilineStrings);
321     IO.mapOptional("AlwaysBreakTemplateDeclarations",
322                    Style.AlwaysBreakTemplateDeclarations);
323     IO.mapOptional("BinPackArguments", Style.BinPackArguments);
324     IO.mapOptional("BinPackParameters", Style.BinPackParameters);
325     IO.mapOptional("BraceWrapping", Style.BraceWrapping);
326     IO.mapOptional("BreakBeforeBinaryOperators",
327                    Style.BreakBeforeBinaryOperators);
328     IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces);
329     IO.mapOptional("BreakBeforeInheritanceComma",
330                    Style.BreakBeforeInheritanceComma);
331     IO.mapOptional("BreakBeforeTernaryOperators",
332                    Style.BreakBeforeTernaryOperators);
333 
334     bool BreakConstructorInitializersBeforeComma = false;
335     IO.mapOptional("BreakConstructorInitializersBeforeComma",
336                    BreakConstructorInitializersBeforeComma);
337     IO.mapOptional("BreakConstructorInitializers",
338                    Style.BreakConstructorInitializers);
339     // If BreakConstructorInitializersBeforeComma was specified but
340     // BreakConstructorInitializers was not, initialize the latter from the
341     // former for backwards compatibility.
342     if (BreakConstructorInitializersBeforeComma &&
343         Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon)
344       Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
345 
346     IO.mapOptional("BreakAfterJavaFieldAnnotations",
347                    Style.BreakAfterJavaFieldAnnotations);
348     IO.mapOptional("BreakStringLiterals", Style.BreakStringLiterals);
349     IO.mapOptional("ColumnLimit", Style.ColumnLimit);
350     IO.mapOptional("CommentPragmas", Style.CommentPragmas);
351     IO.mapOptional("CompactNamespaces", Style.CompactNamespaces);
352     IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine",
353                    Style.ConstructorInitializerAllOnOneLineOrOnePerLine);
354     IO.mapOptional("ConstructorInitializerIndentWidth",
355                    Style.ConstructorInitializerIndentWidth);
356     IO.mapOptional("ContinuationIndentWidth", Style.ContinuationIndentWidth);
357     IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle);
358     IO.mapOptional("DerivePointerAlignment", Style.DerivePointerAlignment);
359     IO.mapOptional("DisableFormat", Style.DisableFormat);
360     IO.mapOptional("ExperimentalAutoDetectBinPacking",
361                    Style.ExperimentalAutoDetectBinPacking);
362     IO.mapOptional("FixNamespaceComments", Style.FixNamespaceComments);
363     IO.mapOptional("ForEachMacros", Style.ForEachMacros);
364     IO.mapOptional("IncludeBlocks", Style.IncludeBlocks);
365     IO.mapOptional("IncludeCategories", Style.IncludeCategories);
366     IO.mapOptional("IncludeIsMainRegex", Style.IncludeIsMainRegex);
367     IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels);
368     IO.mapOptional("IndentPPDirectives", Style.IndentPPDirectives);
369     IO.mapOptional("IndentWidth", Style.IndentWidth);
370     IO.mapOptional("IndentWrappedFunctionNames",
371                    Style.IndentWrappedFunctionNames);
372     IO.mapOptional("JavaScriptQuotes", Style.JavaScriptQuotes);
373     IO.mapOptional("JavaScriptWrapImports", Style.JavaScriptWrapImports);
374     IO.mapOptional("KeepEmptyLinesAtTheStartOfBlocks",
375                    Style.KeepEmptyLinesAtTheStartOfBlocks);
376     IO.mapOptional("MacroBlockBegin", Style.MacroBlockBegin);
377     IO.mapOptional("MacroBlockEnd", Style.MacroBlockEnd);
378     IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep);
379     IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation);
380     IO.mapOptional("ObjCBlockIndentWidth", Style.ObjCBlockIndentWidth);
381     IO.mapOptional("ObjCSpaceAfterProperty", Style.ObjCSpaceAfterProperty);
382     IO.mapOptional("ObjCSpaceBeforeProtocolList",
383                    Style.ObjCSpaceBeforeProtocolList);
384     IO.mapOptional("PenaltyBreakAssignment", Style.PenaltyBreakAssignment);
385     IO.mapOptional("PenaltyBreakBeforeFirstCallParameter",
386                    Style.PenaltyBreakBeforeFirstCallParameter);
387     IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment);
388     IO.mapOptional("PenaltyBreakFirstLessLess",
389                    Style.PenaltyBreakFirstLessLess);
390     IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString);
391     IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter);
392     IO.mapOptional("PenaltyReturnTypeOnItsOwnLine",
393                    Style.PenaltyReturnTypeOnItsOwnLine);
394     IO.mapOptional("PointerAlignment", Style.PointerAlignment);
395     IO.mapOptional("RawStringFormats", Style.RawStringFormats);
396     IO.mapOptional("ReflowComments", Style.ReflowComments);
397     IO.mapOptional("SortIncludes", Style.SortIncludes);
398     IO.mapOptional("SortUsingDeclarations", Style.SortUsingDeclarations);
399     IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast);
400     IO.mapOptional("SpaceAfterTemplateKeyword",
401                    Style.SpaceAfterTemplateKeyword);
402     IO.mapOptional("SpaceBeforeAssignmentOperators",
403                    Style.SpaceBeforeAssignmentOperators);
404     IO.mapOptional("SpaceBeforeParens", Style.SpaceBeforeParens);
405     IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses);
406     IO.mapOptional("SpacesBeforeTrailingComments",
407                    Style.SpacesBeforeTrailingComments);
408     IO.mapOptional("SpacesInAngles", Style.SpacesInAngles);
409     IO.mapOptional("SpacesInContainerLiterals",
410                    Style.SpacesInContainerLiterals);
411     IO.mapOptional("SpacesInCStyleCastParentheses",
412                    Style.SpacesInCStyleCastParentheses);
413     IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses);
414     IO.mapOptional("SpacesInSquareBrackets", Style.SpacesInSquareBrackets);
415     IO.mapOptional("Standard", Style.Standard);
416     IO.mapOptional("TabWidth", Style.TabWidth);
417     IO.mapOptional("UseTab", Style.UseTab);
418   }
419 };
420 
421 template <> struct MappingTraits<FormatStyle::BraceWrappingFlags> {
422   static void mapping(IO &IO, FormatStyle::BraceWrappingFlags &Wrapping) {
423     IO.mapOptional("AfterClass", Wrapping.AfterClass);
424     IO.mapOptional("AfterControlStatement", Wrapping.AfterControlStatement);
425     IO.mapOptional("AfterEnum", Wrapping.AfterEnum);
426     IO.mapOptional("AfterFunction", Wrapping.AfterFunction);
427     IO.mapOptional("AfterNamespace", Wrapping.AfterNamespace);
428     IO.mapOptional("AfterObjCDeclaration", Wrapping.AfterObjCDeclaration);
429     IO.mapOptional("AfterStruct", Wrapping.AfterStruct);
430     IO.mapOptional("AfterUnion", Wrapping.AfterUnion);
431     IO.mapOptional("AfterExternBlock", Wrapping.AfterExternBlock);
432     IO.mapOptional("BeforeCatch", Wrapping.BeforeCatch);
433     IO.mapOptional("BeforeElse", Wrapping.BeforeElse);
434     IO.mapOptional("IndentBraces", Wrapping.IndentBraces);
435     IO.mapOptional("SplitEmptyFunction", Wrapping.SplitEmptyFunction);
436     IO.mapOptional("SplitEmptyRecord", Wrapping.SplitEmptyRecord);
437     IO.mapOptional("SplitEmptyNamespace", Wrapping.SplitEmptyNamespace);
438   }
439 };
440 
441 template <> struct MappingTraits<FormatStyle::IncludeCategory> {
442   static void mapping(IO &IO, FormatStyle::IncludeCategory &Category) {
443     IO.mapOptional("Regex", Category.Regex);
444     IO.mapOptional("Priority", Category.Priority);
445   }
446 };
447 
448 template <> struct ScalarEnumerationTraits<FormatStyle::IncludeBlocksStyle> {
449   static void enumeration(IO &IO, FormatStyle::IncludeBlocksStyle &Value) {
450     IO.enumCase(Value, "Preserve", FormatStyle::IBS_Preserve);
451     IO.enumCase(Value, "Merge", FormatStyle::IBS_Merge);
452     IO.enumCase(Value, "Regroup", FormatStyle::IBS_Regroup);
453   }
454 };
455 
456 template <> struct MappingTraits<FormatStyle::RawStringFormat> {
457   static void mapping(IO &IO, FormatStyle::RawStringFormat &Format) {
458     IO.mapOptional("Delimiter", Format.Delimiter);
459     IO.mapOptional("Language", Format.Language);
460     IO.mapOptional("BasedOnStyle", Format.BasedOnStyle);
461   }
462 };
463 
464 // Allows to read vector<FormatStyle> while keeping default values.
465 // IO.getContext() should contain a pointer to the FormatStyle structure, that
466 // will be used to get default values for missing keys.
467 // If the first element has no Language specified, it will be treated as the
468 // default one for the following elements.
469 template <> struct DocumentListTraits<std::vector<FormatStyle>> {
470   static size_t size(IO &IO, std::vector<FormatStyle> &Seq) {
471     return Seq.size();
472   }
473   static FormatStyle &element(IO &IO, std::vector<FormatStyle> &Seq,
474                               size_t Index) {
475     if (Index >= Seq.size()) {
476       assert(Index == Seq.size());
477       FormatStyle Template;
478       if (!Seq.empty() && Seq[0].Language == FormatStyle::LK_None) {
479         Template = Seq[0];
480       } else {
481         Template = *((const FormatStyle *)IO.getContext());
482         Template.Language = FormatStyle::LK_None;
483       }
484       Seq.resize(Index + 1, Template);
485     }
486     return Seq[Index];
487   }
488 };
489 } // namespace yaml
490 } // namespace llvm
491 
492 namespace clang {
493 namespace format {
494 
495 const std::error_category &getParseCategory() {
496   static ParseErrorCategory C;
497   return C;
498 }
499 std::error_code make_error_code(ParseError e) {
500   return std::error_code(static_cast<int>(e), getParseCategory());
501 }
502 
503 inline llvm::Error make_string_error(const llvm::Twine &Message) {
504   return llvm::make_error<llvm::StringError>(Message,
505                                              llvm::inconvertibleErrorCode());
506 }
507 
508 const char *ParseErrorCategory::name() const noexcept {
509   return "clang-format.parse_error";
510 }
511 
512 std::string ParseErrorCategory::message(int EV) const {
513   switch (static_cast<ParseError>(EV)) {
514   case ParseError::Success:
515     return "Success";
516   case ParseError::Error:
517     return "Invalid argument";
518   case ParseError::Unsuitable:
519     return "Unsuitable";
520   }
521   llvm_unreachable("unexpected parse error");
522 }
523 
524 static FormatStyle expandPresets(const FormatStyle &Style) {
525   if (Style.BreakBeforeBraces == FormatStyle::BS_Custom)
526     return Style;
527   FormatStyle Expanded = Style;
528   Expanded.BraceWrapping = {false, false, false, false, false,
529                             false, false, false, false, false,
530                             false, false, true,  true,  true};
531   switch (Style.BreakBeforeBraces) {
532   case FormatStyle::BS_Linux:
533     Expanded.BraceWrapping.AfterClass = true;
534     Expanded.BraceWrapping.AfterFunction = true;
535     Expanded.BraceWrapping.AfterNamespace = true;
536     break;
537   case FormatStyle::BS_Mozilla:
538     Expanded.BraceWrapping.AfterClass = true;
539     Expanded.BraceWrapping.AfterEnum = true;
540     Expanded.BraceWrapping.AfterFunction = true;
541     Expanded.BraceWrapping.AfterStruct = true;
542     Expanded.BraceWrapping.AfterUnion = true;
543     Expanded.BraceWrapping.AfterExternBlock = true;
544     Expanded.BraceWrapping.SplitEmptyFunction = true;
545     Expanded.BraceWrapping.SplitEmptyRecord = false;
546     break;
547   case FormatStyle::BS_Stroustrup:
548     Expanded.BraceWrapping.AfterFunction = true;
549     Expanded.BraceWrapping.BeforeCatch = true;
550     Expanded.BraceWrapping.BeforeElse = true;
551     break;
552   case FormatStyle::BS_Allman:
553     Expanded.BraceWrapping.AfterClass = true;
554     Expanded.BraceWrapping.AfterControlStatement = true;
555     Expanded.BraceWrapping.AfterEnum = true;
556     Expanded.BraceWrapping.AfterFunction = true;
557     Expanded.BraceWrapping.AfterNamespace = true;
558     Expanded.BraceWrapping.AfterObjCDeclaration = true;
559     Expanded.BraceWrapping.AfterStruct = true;
560     Expanded.BraceWrapping.AfterExternBlock = true;
561     Expanded.BraceWrapping.BeforeCatch = true;
562     Expanded.BraceWrapping.BeforeElse = true;
563     break;
564   case FormatStyle::BS_GNU:
565     Expanded.BraceWrapping = {true, true, true, true, true, true, true, true,
566                               true, true, true, true, true, true, true};
567     break;
568   case FormatStyle::BS_WebKit:
569     Expanded.BraceWrapping.AfterFunction = true;
570     break;
571   default:
572     break;
573   }
574   return Expanded;
575 }
576 
577 FormatStyle getLLVMStyle() {
578   FormatStyle LLVMStyle;
579   LLVMStyle.Language = FormatStyle::LK_Cpp;
580   LLVMStyle.AccessModifierOffset = -2;
581   LLVMStyle.AlignEscapedNewlines = FormatStyle::ENAS_Right;
582   LLVMStyle.AlignAfterOpenBracket = FormatStyle::BAS_Align;
583   LLVMStyle.AlignOperands = true;
584   LLVMStyle.AlignTrailingComments = true;
585   LLVMStyle.AlignConsecutiveAssignments = false;
586   LLVMStyle.AlignConsecutiveDeclarations = false;
587   LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
588   LLVMStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
589   LLVMStyle.AllowShortBlocksOnASingleLine = false;
590   LLVMStyle.AllowShortCaseLabelsOnASingleLine = false;
591   LLVMStyle.AllowShortIfStatementsOnASingleLine = false;
592   LLVMStyle.AllowShortLoopsOnASingleLine = false;
593   LLVMStyle.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
594   LLVMStyle.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_None;
595   LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
596   LLVMStyle.AlwaysBreakTemplateDeclarations = false;
597   LLVMStyle.BinPackArguments = true;
598   LLVMStyle.BinPackParameters = true;
599   LLVMStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
600   LLVMStyle.BreakBeforeTernaryOperators = true;
601   LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
602   LLVMStyle.BraceWrapping = {false, false, false, false, false,
603                              false, false, false, false, false,
604                              false, false, true,  true,  true};
605   LLVMStyle.BreakAfterJavaFieldAnnotations = false;
606   LLVMStyle.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
607   LLVMStyle.BreakBeforeInheritanceComma = false;
608   LLVMStyle.BreakStringLiterals = true;
609   LLVMStyle.ColumnLimit = 80;
610   LLVMStyle.CommentPragmas = "^ IWYU pragma:";
611   LLVMStyle.CompactNamespaces = false;
612   LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
613   LLVMStyle.ConstructorInitializerIndentWidth = 4;
614   LLVMStyle.ContinuationIndentWidth = 4;
615   LLVMStyle.Cpp11BracedListStyle = true;
616   LLVMStyle.DerivePointerAlignment = false;
617   LLVMStyle.ExperimentalAutoDetectBinPacking = false;
618   LLVMStyle.FixNamespaceComments = true;
619   LLVMStyle.ForEachMacros.push_back("foreach");
620   LLVMStyle.ForEachMacros.push_back("Q_FOREACH");
621   LLVMStyle.ForEachMacros.push_back("BOOST_FOREACH");
622   LLVMStyle.IncludeCategories = {{"^\"(llvm|llvm-c|clang|clang-c)/", 2},
623                                  {"^(<|\"(gtest|gmock|isl|json)/)", 3},
624                                  {".*", 1}};
625   LLVMStyle.IncludeIsMainRegex = "(Test)?$";
626   LLVMStyle.IncludeBlocks = FormatStyle::IBS_Preserve;
627   LLVMStyle.IndentCaseLabels = false;
628   LLVMStyle.IndentPPDirectives = FormatStyle::PPDIS_None;
629   LLVMStyle.IndentWrappedFunctionNames = false;
630   LLVMStyle.IndentWidth = 2;
631   LLVMStyle.JavaScriptQuotes = FormatStyle::JSQS_Leave;
632   LLVMStyle.JavaScriptWrapImports = true;
633   LLVMStyle.TabWidth = 8;
634   LLVMStyle.MaxEmptyLinesToKeep = 1;
635   LLVMStyle.KeepEmptyLinesAtTheStartOfBlocks = true;
636   LLVMStyle.NamespaceIndentation = FormatStyle::NI_None;
637   LLVMStyle.ObjCBlockIndentWidth = 2;
638   LLVMStyle.ObjCSpaceAfterProperty = false;
639   LLVMStyle.ObjCSpaceBeforeProtocolList = true;
640   LLVMStyle.PointerAlignment = FormatStyle::PAS_Right;
641   LLVMStyle.SpacesBeforeTrailingComments = 1;
642   LLVMStyle.Standard = FormatStyle::LS_Cpp11;
643   LLVMStyle.UseTab = FormatStyle::UT_Never;
644   LLVMStyle.RawStringFormats = {{"pb", FormatStyle::LK_TextProto, "google"}};
645   LLVMStyle.ReflowComments = true;
646   LLVMStyle.SpacesInParentheses = false;
647   LLVMStyle.SpacesInSquareBrackets = false;
648   LLVMStyle.SpaceInEmptyParentheses = false;
649   LLVMStyle.SpacesInContainerLiterals = true;
650   LLVMStyle.SpacesInCStyleCastParentheses = false;
651   LLVMStyle.SpaceAfterCStyleCast = false;
652   LLVMStyle.SpaceAfterTemplateKeyword = true;
653   LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements;
654   LLVMStyle.SpaceBeforeAssignmentOperators = true;
655   LLVMStyle.SpacesInAngles = false;
656 
657   LLVMStyle.PenaltyBreakAssignment = prec::Assignment;
658   LLVMStyle.PenaltyBreakComment = 300;
659   LLVMStyle.PenaltyBreakFirstLessLess = 120;
660   LLVMStyle.PenaltyBreakString = 1000;
661   LLVMStyle.PenaltyExcessCharacter = 1000000;
662   LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
663   LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19;
664 
665   LLVMStyle.DisableFormat = false;
666   LLVMStyle.SortIncludes = true;
667   LLVMStyle.SortUsingDeclarations = true;
668 
669   return LLVMStyle;
670 }
671 
672 FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
673   if (Language == FormatStyle::LK_TextProto) {
674     FormatStyle GoogleStyle = getGoogleStyle(FormatStyle::LK_Proto);
675     GoogleStyle.Language = FormatStyle::LK_TextProto;
676     return GoogleStyle;
677   }
678 
679   FormatStyle GoogleStyle = getLLVMStyle();
680   GoogleStyle.Language = Language;
681 
682   GoogleStyle.AccessModifierOffset = -1;
683   GoogleStyle.AlignEscapedNewlines = FormatStyle::ENAS_Left;
684   GoogleStyle.AllowShortIfStatementsOnASingleLine = true;
685   GoogleStyle.AllowShortLoopsOnASingleLine = true;
686   GoogleStyle.AlwaysBreakBeforeMultilineStrings = true;
687   GoogleStyle.AlwaysBreakTemplateDeclarations = true;
688   GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
689   GoogleStyle.DerivePointerAlignment = true;
690   GoogleStyle.IncludeCategories = {
691       {"^<ext/.*\\.h>", 2}, {"^<.*\\.h>", 1}, {"^<.*", 2}, {".*", 3}};
692   GoogleStyle.IncludeIsMainRegex = "([-_](test|unittest))?$";
693   GoogleStyle.IndentCaseLabels = true;
694   GoogleStyle.KeepEmptyLinesAtTheStartOfBlocks = false;
695   GoogleStyle.ObjCSpaceAfterProperty = false;
696   GoogleStyle.ObjCSpaceBeforeProtocolList = false;
697   GoogleStyle.PointerAlignment = FormatStyle::PAS_Left;
698   GoogleStyle.SpacesBeforeTrailingComments = 2;
699   GoogleStyle.Standard = FormatStyle::LS_Auto;
700 
701   GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
702   GoogleStyle.PenaltyBreakBeforeFirstCallParameter = 1;
703 
704   if (Language == FormatStyle::LK_Java) {
705     GoogleStyle.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
706     GoogleStyle.AlignOperands = false;
707     GoogleStyle.AlignTrailingComments = false;
708     GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
709     GoogleStyle.AllowShortIfStatementsOnASingleLine = false;
710     GoogleStyle.AlwaysBreakBeforeMultilineStrings = false;
711     GoogleStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
712     GoogleStyle.ColumnLimit = 100;
713     GoogleStyle.SpaceAfterCStyleCast = true;
714     GoogleStyle.SpacesBeforeTrailingComments = 1;
715   } else if (Language == FormatStyle::LK_JavaScript) {
716     GoogleStyle.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
717     GoogleStyle.AlignOperands = false;
718     GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
719     GoogleStyle.AlwaysBreakBeforeMultilineStrings = false;
720     GoogleStyle.BreakBeforeTernaryOperators = false;
721     // taze:, triple slash directives (`/// <...`), @tag followed by { for a lot
722     // of JSDoc tags, and @see, which is commonly followed by overlong URLs.
723     GoogleStyle.CommentPragmas =
724         "(taze:|^/[ \t]*<|(@[A-Za-z_0-9-]+[ \\t]*{)|@see)";
725     GoogleStyle.MaxEmptyLinesToKeep = 3;
726     GoogleStyle.NamespaceIndentation = FormatStyle::NI_All;
727     GoogleStyle.SpacesInContainerLiterals = false;
728     GoogleStyle.JavaScriptQuotes = FormatStyle::JSQS_Single;
729     GoogleStyle.JavaScriptWrapImports = false;
730   } else if (Language == FormatStyle::LK_Proto) {
731     GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
732     GoogleStyle.SpacesInContainerLiterals = false;
733   } else if (Language == FormatStyle::LK_ObjC) {
734     GoogleStyle.ColumnLimit = 100;
735     GoogleStyle.IndentWrappedFunctionNames = true;
736   }
737 
738   return GoogleStyle;
739 }
740 
741 FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) {
742   FormatStyle ChromiumStyle = getGoogleStyle(Language);
743   if (Language == FormatStyle::LK_Java) {
744     ChromiumStyle.AllowShortIfStatementsOnASingleLine = true;
745     ChromiumStyle.BreakAfterJavaFieldAnnotations = true;
746     ChromiumStyle.ContinuationIndentWidth = 8;
747     ChromiumStyle.IndentWidth = 4;
748   } else if (Language == FormatStyle::LK_JavaScript) {
749     ChromiumStyle.AllowShortIfStatementsOnASingleLine = false;
750     ChromiumStyle.AllowShortLoopsOnASingleLine = false;
751   } else {
752     ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false;
753     ChromiumStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
754     ChromiumStyle.AllowShortIfStatementsOnASingleLine = false;
755     ChromiumStyle.AllowShortLoopsOnASingleLine = false;
756     ChromiumStyle.BinPackParameters = false;
757     ChromiumStyle.DerivePointerAlignment = false;
758     if (Language == FormatStyle::LK_ObjC)
759       ChromiumStyle.ColumnLimit = 80;
760   }
761   return ChromiumStyle;
762 }
763 
764 FormatStyle getMozillaStyle() {
765   FormatStyle MozillaStyle = getLLVMStyle();
766   MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false;
767   MozillaStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
768   MozillaStyle.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
769   MozillaStyle.AlwaysBreakAfterDefinitionReturnType =
770       FormatStyle::DRTBS_TopLevel;
771   MozillaStyle.AlwaysBreakTemplateDeclarations = true;
772   MozillaStyle.BinPackParameters = false;
773   MozillaStyle.BinPackArguments = false;
774   MozillaStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
775   MozillaStyle.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
776   MozillaStyle.BreakBeforeInheritanceComma = true;
777   MozillaStyle.ConstructorInitializerIndentWidth = 2;
778   MozillaStyle.ContinuationIndentWidth = 2;
779   MozillaStyle.Cpp11BracedListStyle = false;
780   MozillaStyle.FixNamespaceComments = false;
781   MozillaStyle.IndentCaseLabels = true;
782   MozillaStyle.ObjCSpaceAfterProperty = true;
783   MozillaStyle.ObjCSpaceBeforeProtocolList = false;
784   MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200;
785   MozillaStyle.PointerAlignment = FormatStyle::PAS_Left;
786   MozillaStyle.SpaceAfterTemplateKeyword = false;
787   return MozillaStyle;
788 }
789 
790 FormatStyle getWebKitStyle() {
791   FormatStyle Style = getLLVMStyle();
792   Style.AccessModifierOffset = -4;
793   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
794   Style.AlignOperands = false;
795   Style.AlignTrailingComments = false;
796   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
797   Style.BreakBeforeBraces = FormatStyle::BS_WebKit;
798   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
799   Style.Cpp11BracedListStyle = false;
800   Style.ColumnLimit = 0;
801   Style.FixNamespaceComments = false;
802   Style.IndentWidth = 4;
803   Style.NamespaceIndentation = FormatStyle::NI_Inner;
804   Style.ObjCBlockIndentWidth = 4;
805   Style.ObjCSpaceAfterProperty = true;
806   Style.PointerAlignment = FormatStyle::PAS_Left;
807   return Style;
808 }
809 
810 FormatStyle getGNUStyle() {
811   FormatStyle Style = getLLVMStyle();
812   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
813   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
814   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
815   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
816   Style.BreakBeforeTernaryOperators = true;
817   Style.Cpp11BracedListStyle = false;
818   Style.ColumnLimit = 79;
819   Style.FixNamespaceComments = false;
820   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
821   Style.Standard = FormatStyle::LS_Cpp03;
822   return Style;
823 }
824 
825 FormatStyle getNoStyle() {
826   FormatStyle NoStyle = getLLVMStyle();
827   NoStyle.DisableFormat = true;
828   NoStyle.SortIncludes = false;
829   NoStyle.SortUsingDeclarations = false;
830   return NoStyle;
831 }
832 
833 bool getPredefinedStyle(StringRef Name, FormatStyle::LanguageKind Language,
834                         FormatStyle *Style) {
835   if (Name.equals_lower("llvm")) {
836     *Style = getLLVMStyle();
837   } else if (Name.equals_lower("chromium")) {
838     *Style = getChromiumStyle(Language);
839   } else if (Name.equals_lower("mozilla")) {
840     *Style = getMozillaStyle();
841   } else if (Name.equals_lower("google")) {
842     *Style = getGoogleStyle(Language);
843   } else if (Name.equals_lower("webkit")) {
844     *Style = getWebKitStyle();
845   } else if (Name.equals_lower("gnu")) {
846     *Style = getGNUStyle();
847   } else if (Name.equals_lower("none")) {
848     *Style = getNoStyle();
849   } else {
850     return false;
851   }
852 
853   Style->Language = Language;
854   return true;
855 }
856 
857 std::error_code parseConfiguration(StringRef Text, FormatStyle *Style) {
858   assert(Style);
859   FormatStyle::LanguageKind Language = Style->Language;
860   assert(Language != FormatStyle::LK_None);
861   if (Text.trim().empty())
862     return make_error_code(ParseError::Error);
863 
864   std::vector<FormatStyle> Styles;
865   llvm::yaml::Input Input(Text);
866   // DocumentListTraits<vector<FormatStyle>> uses the context to get default
867   // values for the fields, keys for which are missing from the configuration.
868   // Mapping also uses the context to get the language to find the correct
869   // base style.
870   Input.setContext(Style);
871   Input >> Styles;
872   if (Input.error())
873     return Input.error();
874 
875   for (unsigned i = 0; i < Styles.size(); ++i) {
876     // Ensures that only the first configuration can skip the Language option.
877     if (Styles[i].Language == FormatStyle::LK_None && i != 0)
878       return make_error_code(ParseError::Error);
879     // Ensure that each language is configured at most once.
880     for (unsigned j = 0; j < i; ++j) {
881       if (Styles[i].Language == Styles[j].Language) {
882         DEBUG(llvm::dbgs()
883               << "Duplicate languages in the config file on positions " << j
884               << " and " << i << "\n");
885         return make_error_code(ParseError::Error);
886       }
887     }
888   }
889   // Look for a suitable configuration starting from the end, so we can
890   // find the configuration for the specific language first, and the default
891   // configuration (which can only be at slot 0) after it.
892   for (int i = Styles.size() - 1; i >= 0; --i) {
893     if (Styles[i].Language == Language ||
894         Styles[i].Language == FormatStyle::LK_None) {
895       *Style = Styles[i];
896       Style->Language = Language;
897       return make_error_code(ParseError::Success);
898     }
899   }
900   return make_error_code(ParseError::Unsuitable);
901 }
902 
903 std::string configurationAsText(const FormatStyle &Style) {
904   std::string Text;
905   llvm::raw_string_ostream Stream(Text);
906   llvm::yaml::Output Output(Stream);
907   // We use the same mapping method for input and output, so we need a non-const
908   // reference here.
909   FormatStyle NonConstStyle = expandPresets(Style);
910   Output << NonConstStyle;
911   return Stream.str();
912 }
913 
914 namespace {
915 
916 class JavaScriptRequoter : public TokenAnalyzer {
917 public:
918   JavaScriptRequoter(const Environment &Env, const FormatStyle &Style)
919       : TokenAnalyzer(Env, Style) {}
920 
921   std::pair<tooling::Replacements, unsigned>
922   analyze(TokenAnnotator &Annotator,
923           SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
924           FormatTokenLexer &Tokens) override {
925     AffectedRangeMgr.computeAffectedLines(AnnotatedLines.begin(),
926                                           AnnotatedLines.end());
927     tooling::Replacements Result;
928     requoteJSStringLiteral(AnnotatedLines, Result);
929     return {Result, 0};
930   }
931 
932 private:
933   // Replaces double/single-quoted string literal as appropriate, re-escaping
934   // the contents in the process.
935   void requoteJSStringLiteral(SmallVectorImpl<AnnotatedLine *> &Lines,
936                               tooling::Replacements &Result) {
937     for (AnnotatedLine *Line : Lines) {
938       requoteJSStringLiteral(Line->Children, Result);
939       if (!Line->Affected)
940         continue;
941       for (FormatToken *FormatTok = Line->First; FormatTok;
942            FormatTok = FormatTok->Next) {
943         StringRef Input = FormatTok->TokenText;
944         if (FormatTok->Finalized || !FormatTok->isStringLiteral() ||
945             // NB: testing for not starting with a double quote to avoid
946             // breaking `template strings`.
947             (Style.JavaScriptQuotes == FormatStyle::JSQS_Single &&
948              !Input.startswith("\"")) ||
949             (Style.JavaScriptQuotes == FormatStyle::JSQS_Double &&
950              !Input.startswith("\'")))
951           continue;
952 
953         // Change start and end quote.
954         bool IsSingle = Style.JavaScriptQuotes == FormatStyle::JSQS_Single;
955         SourceLocation Start = FormatTok->Tok.getLocation();
956         auto Replace = [&](SourceLocation Start, unsigned Length,
957                            StringRef ReplacementText) {
958           auto Err = Result.add(tooling::Replacement(
959               Env.getSourceManager(), Start, Length, ReplacementText));
960           // FIXME: handle error. For now, print error message and skip the
961           // replacement for release version.
962           if (Err) {
963             llvm::errs() << llvm::toString(std::move(Err)) << "\n";
964             assert(false);
965           }
966         };
967         Replace(Start, 1, IsSingle ? "'" : "\"");
968         Replace(FormatTok->Tok.getEndLoc().getLocWithOffset(-1), 1,
969                 IsSingle ? "'" : "\"");
970 
971         // Escape internal quotes.
972         bool Escaped = false;
973         for (size_t i = 1; i < Input.size() - 1; i++) {
974           switch (Input[i]) {
975           case '\\':
976             if (!Escaped && i + 1 < Input.size() &&
977                 ((IsSingle && Input[i + 1] == '"') ||
978                  (!IsSingle && Input[i + 1] == '\''))) {
979               // Remove this \, it's escaping a " or ' that no longer needs
980               // escaping
981               Replace(Start.getLocWithOffset(i), 1, "");
982               continue;
983             }
984             Escaped = !Escaped;
985             break;
986           case '\"':
987           case '\'':
988             if (!Escaped && IsSingle == (Input[i] == '\'')) {
989               // Escape the quote.
990               Replace(Start.getLocWithOffset(i), 0, "\\");
991             }
992             Escaped = false;
993             break;
994           default:
995             Escaped = false;
996             break;
997           }
998         }
999       }
1000     }
1001   }
1002 };
1003 
1004 class Formatter : public TokenAnalyzer {
1005 public:
1006   Formatter(const Environment &Env, const FormatStyle &Style,
1007             FormattingAttemptStatus *Status)
1008       : TokenAnalyzer(Env, Style), Status(Status) {}
1009 
1010   std::pair<tooling::Replacements, unsigned>
1011   analyze(TokenAnnotator &Annotator,
1012           SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
1013           FormatTokenLexer &Tokens) override {
1014     tooling::Replacements Result;
1015     deriveLocalStyle(AnnotatedLines);
1016     AffectedRangeMgr.computeAffectedLines(AnnotatedLines.begin(),
1017                                           AnnotatedLines.end());
1018     for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1019       Annotator.calculateFormattingInformation(*AnnotatedLines[i]);
1020     }
1021     Annotator.setCommentLineLevels(AnnotatedLines);
1022 
1023     WhitespaceManager Whitespaces(
1024         Env.getSourceManager(), Style,
1025         inputUsesCRLF(Env.getSourceManager().getBufferData(Env.getFileID())));
1026     ContinuationIndenter Indenter(Style, Tokens.getKeywords(),
1027                                   Env.getSourceManager(), Whitespaces, Encoding,
1028                                   BinPackInconclusiveFunctions);
1029     unsigned Penalty =
1030         UnwrappedLineFormatter(&Indenter, &Whitespaces, Style,
1031                                Tokens.getKeywords(), Env.getSourceManager(),
1032                                Status)
1033             .format(AnnotatedLines, /*DryRun=*/false,
1034                     /*AdditionalIndent=*/0,
1035                     /*FixBadIndentation=*/false,
1036                     /*FirstStartColumn=*/Env.getFirstStartColumn(),
1037                     /*NextStartColumn=*/Env.getNextStartColumn(),
1038                     /*LastStartColumn=*/Env.getLastStartColumn());
1039     for (const auto &R : Whitespaces.generateReplacements())
1040       if (Result.add(R))
1041         return std::make_pair(Result, 0);
1042     return std::make_pair(Result, Penalty);
1043   }
1044 
1045 private:
1046   static bool inputUsesCRLF(StringRef Text) {
1047     return Text.count('\r') * 2 > Text.count('\n');
1048   }
1049 
1050   bool
1051   hasCpp03IncompatibleFormat(const SmallVectorImpl<AnnotatedLine *> &Lines) {
1052     for (const AnnotatedLine *Line : Lines) {
1053       if (hasCpp03IncompatibleFormat(Line->Children))
1054         return true;
1055       for (FormatToken *Tok = Line->First->Next; Tok; Tok = Tok->Next) {
1056         if (Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd()) {
1057           if (Tok->is(tok::coloncolon) && Tok->Previous->is(TT_TemplateOpener))
1058             return true;
1059           if (Tok->is(TT_TemplateCloser) &&
1060               Tok->Previous->is(TT_TemplateCloser))
1061             return true;
1062         }
1063       }
1064     }
1065     return false;
1066   }
1067 
1068   int countVariableAlignments(const SmallVectorImpl<AnnotatedLine *> &Lines) {
1069     int AlignmentDiff = 0;
1070     for (const AnnotatedLine *Line : Lines) {
1071       AlignmentDiff += countVariableAlignments(Line->Children);
1072       for (FormatToken *Tok = Line->First; Tok && Tok->Next; Tok = Tok->Next) {
1073         if (!Tok->is(TT_PointerOrReference))
1074           continue;
1075         bool SpaceBefore =
1076             Tok->WhitespaceRange.getBegin() != Tok->WhitespaceRange.getEnd();
1077         bool SpaceAfter = Tok->Next->WhitespaceRange.getBegin() !=
1078                           Tok->Next->WhitespaceRange.getEnd();
1079         if (SpaceBefore && !SpaceAfter)
1080           ++AlignmentDiff;
1081         if (!SpaceBefore && SpaceAfter)
1082           --AlignmentDiff;
1083       }
1084     }
1085     return AlignmentDiff;
1086   }
1087 
1088   void
1089   deriveLocalStyle(const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
1090     bool HasBinPackedFunction = false;
1091     bool HasOnePerLineFunction = false;
1092     for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1093       if (!AnnotatedLines[i]->First->Next)
1094         continue;
1095       FormatToken *Tok = AnnotatedLines[i]->First->Next;
1096       while (Tok->Next) {
1097         if (Tok->PackingKind == PPK_BinPacked)
1098           HasBinPackedFunction = true;
1099         if (Tok->PackingKind == PPK_OnePerLine)
1100           HasOnePerLineFunction = true;
1101 
1102         Tok = Tok->Next;
1103       }
1104     }
1105     if (Style.DerivePointerAlignment)
1106       Style.PointerAlignment = countVariableAlignments(AnnotatedLines) <= 0
1107                                    ? FormatStyle::PAS_Left
1108                                    : FormatStyle::PAS_Right;
1109     if (Style.Standard == FormatStyle::LS_Auto)
1110       Style.Standard = hasCpp03IncompatibleFormat(AnnotatedLines)
1111                            ? FormatStyle::LS_Cpp11
1112                            : FormatStyle::LS_Cpp03;
1113     BinPackInconclusiveFunctions =
1114         HasBinPackedFunction || !HasOnePerLineFunction;
1115   }
1116 
1117   bool BinPackInconclusiveFunctions;
1118   FormattingAttemptStatus *Status;
1119 };
1120 
1121 // This class clean up the erroneous/redundant code around the given ranges in
1122 // file.
1123 class Cleaner : public TokenAnalyzer {
1124 public:
1125   Cleaner(const Environment &Env, const FormatStyle &Style)
1126       : TokenAnalyzer(Env, Style),
1127         DeletedTokens(FormatTokenLess(Env.getSourceManager())) {}
1128 
1129   // FIXME: eliminate unused parameters.
1130   std::pair<tooling::Replacements, unsigned>
1131   analyze(TokenAnnotator &Annotator,
1132           SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
1133           FormatTokenLexer &Tokens) override {
1134     // FIXME: in the current implementation the granularity of affected range
1135     // is an annotated line. However, this is not sufficient. Furthermore,
1136     // redundant code introduced by replacements does not necessarily
1137     // intercept with ranges of replacements that result in the redundancy.
1138     // To determine if some redundant code is actually introduced by
1139     // replacements(e.g. deletions), we need to come up with a more
1140     // sophisticated way of computing affected ranges.
1141     AffectedRangeMgr.computeAffectedLines(AnnotatedLines.begin(),
1142                                           AnnotatedLines.end());
1143 
1144     checkEmptyNamespace(AnnotatedLines);
1145 
1146     for (auto &Line : AnnotatedLines) {
1147       if (Line->Affected) {
1148         cleanupRight(Line->First, tok::comma, tok::comma);
1149         cleanupRight(Line->First, TT_CtorInitializerColon, tok::comma);
1150         cleanupRight(Line->First, tok::l_paren, tok::comma);
1151         cleanupLeft(Line->First, tok::comma, tok::r_paren);
1152         cleanupLeft(Line->First, TT_CtorInitializerComma, tok::l_brace);
1153         cleanupLeft(Line->First, TT_CtorInitializerColon, tok::l_brace);
1154         cleanupLeft(Line->First, TT_CtorInitializerColon, tok::equal);
1155       }
1156     }
1157 
1158     return {generateFixes(), 0};
1159   }
1160 
1161 private:
1162   bool containsOnlyComments(const AnnotatedLine &Line) {
1163     for (FormatToken *Tok = Line.First; Tok != nullptr; Tok = Tok->Next) {
1164       if (Tok->isNot(tok::comment))
1165         return false;
1166     }
1167     return true;
1168   }
1169 
1170   // Iterate through all lines and remove any empty (nested) namespaces.
1171   void checkEmptyNamespace(SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
1172     std::set<unsigned> DeletedLines;
1173     for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1174       auto &Line = *AnnotatedLines[i];
1175       if (Line.startsWith(tok::kw_namespace) ||
1176           Line.startsWith(tok::kw_inline, tok::kw_namespace)) {
1177         checkEmptyNamespace(AnnotatedLines, i, i, DeletedLines);
1178       }
1179     }
1180 
1181     for (auto Line : DeletedLines) {
1182       FormatToken *Tok = AnnotatedLines[Line]->First;
1183       while (Tok) {
1184         deleteToken(Tok);
1185         Tok = Tok->Next;
1186       }
1187     }
1188   }
1189 
1190   // The function checks if the namespace, which starts from \p CurrentLine, and
1191   // its nested namespaces are empty and delete them if they are empty. It also
1192   // sets \p NewLine to the last line checked.
1193   // Returns true if the current namespace is empty.
1194   bool checkEmptyNamespace(SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
1195                            unsigned CurrentLine, unsigned &NewLine,
1196                            std::set<unsigned> &DeletedLines) {
1197     unsigned InitLine = CurrentLine, End = AnnotatedLines.size();
1198     if (Style.BraceWrapping.AfterNamespace) {
1199       // If the left brace is in a new line, we should consume it first so that
1200       // it does not make the namespace non-empty.
1201       // FIXME: error handling if there is no left brace.
1202       if (!AnnotatedLines[++CurrentLine]->startsWith(tok::l_brace)) {
1203         NewLine = CurrentLine;
1204         return false;
1205       }
1206     } else if (!AnnotatedLines[CurrentLine]->endsWith(tok::l_brace)) {
1207       return false;
1208     }
1209     while (++CurrentLine < End) {
1210       if (AnnotatedLines[CurrentLine]->startsWith(tok::r_brace))
1211         break;
1212 
1213       if (AnnotatedLines[CurrentLine]->startsWith(tok::kw_namespace) ||
1214           AnnotatedLines[CurrentLine]->startsWith(tok::kw_inline,
1215                                                   tok::kw_namespace)) {
1216         if (!checkEmptyNamespace(AnnotatedLines, CurrentLine, NewLine,
1217                                  DeletedLines))
1218           return false;
1219         CurrentLine = NewLine;
1220         continue;
1221       }
1222 
1223       if (containsOnlyComments(*AnnotatedLines[CurrentLine]))
1224         continue;
1225 
1226       // If there is anything other than comments or nested namespaces in the
1227       // current namespace, the namespace cannot be empty.
1228       NewLine = CurrentLine;
1229       return false;
1230     }
1231 
1232     NewLine = CurrentLine;
1233     if (CurrentLine >= End)
1234       return false;
1235 
1236     // Check if the empty namespace is actually affected by changed ranges.
1237     if (!AffectedRangeMgr.affectsCharSourceRange(CharSourceRange::getCharRange(
1238             AnnotatedLines[InitLine]->First->Tok.getLocation(),
1239             AnnotatedLines[CurrentLine]->Last->Tok.getEndLoc())))
1240       return false;
1241 
1242     for (unsigned i = InitLine; i <= CurrentLine; ++i) {
1243       DeletedLines.insert(i);
1244     }
1245 
1246     return true;
1247   }
1248 
1249   // Checks pairs {start, start->next},..., {end->previous, end} and deletes one
1250   // of the token in the pair if the left token has \p LK token kind and the
1251   // right token has \p RK token kind. If \p DeleteLeft is true, the left token
1252   // is deleted on match; otherwise, the right token is deleted.
1253   template <typename LeftKind, typename RightKind>
1254   void cleanupPair(FormatToken *Start, LeftKind LK, RightKind RK,
1255                    bool DeleteLeft) {
1256     auto NextNotDeleted = [this](const FormatToken &Tok) -> FormatToken * {
1257       for (auto *Res = Tok.Next; Res; Res = Res->Next)
1258         if (!Res->is(tok::comment) &&
1259             DeletedTokens.find(Res) == DeletedTokens.end())
1260           return Res;
1261       return nullptr;
1262     };
1263     for (auto *Left = Start; Left;) {
1264       auto *Right = NextNotDeleted(*Left);
1265       if (!Right)
1266         break;
1267       if (Left->is(LK) && Right->is(RK)) {
1268         deleteToken(DeleteLeft ? Left : Right);
1269         for (auto *Tok = Left->Next; Tok && Tok != Right; Tok = Tok->Next)
1270           deleteToken(Tok);
1271         // If the right token is deleted, we should keep the left token
1272         // unchanged and pair it with the new right token.
1273         if (!DeleteLeft)
1274           continue;
1275       }
1276       Left = Right;
1277     }
1278   }
1279 
1280   template <typename LeftKind, typename RightKind>
1281   void cleanupLeft(FormatToken *Start, LeftKind LK, RightKind RK) {
1282     cleanupPair(Start, LK, RK, /*DeleteLeft=*/true);
1283   }
1284 
1285   template <typename LeftKind, typename RightKind>
1286   void cleanupRight(FormatToken *Start, LeftKind LK, RightKind RK) {
1287     cleanupPair(Start, LK, RK, /*DeleteLeft=*/false);
1288   }
1289 
1290   // Delete the given token.
1291   inline void deleteToken(FormatToken *Tok) {
1292     if (Tok)
1293       DeletedTokens.insert(Tok);
1294   }
1295 
1296   tooling::Replacements generateFixes() {
1297     tooling::Replacements Fixes;
1298     std::vector<FormatToken *> Tokens;
1299     std::copy(DeletedTokens.begin(), DeletedTokens.end(),
1300               std::back_inserter(Tokens));
1301 
1302     // Merge multiple continuous token deletions into one big deletion so that
1303     // the number of replacements can be reduced. This makes computing affected
1304     // ranges more efficient when we run reformat on the changed code.
1305     unsigned Idx = 0;
1306     while (Idx < Tokens.size()) {
1307       unsigned St = Idx, End = Idx;
1308       while ((End + 1) < Tokens.size() &&
1309              Tokens[End]->Next == Tokens[End + 1]) {
1310         End++;
1311       }
1312       auto SR = CharSourceRange::getCharRange(Tokens[St]->Tok.getLocation(),
1313                                               Tokens[End]->Tok.getEndLoc());
1314       auto Err =
1315           Fixes.add(tooling::Replacement(Env.getSourceManager(), SR, ""));
1316       // FIXME: better error handling. for now just print error message and skip
1317       // for the release version.
1318       if (Err) {
1319         llvm::errs() << llvm::toString(std::move(Err)) << "\n";
1320         assert(false && "Fixes must not conflict!");
1321       }
1322       Idx = End + 1;
1323     }
1324 
1325     return Fixes;
1326   }
1327 
1328   // Class for less-than inequality comparason for the set `RedundantTokens`.
1329   // We store tokens in the order they appear in the translation unit so that
1330   // we do not need to sort them in `generateFixes()`.
1331   struct FormatTokenLess {
1332     FormatTokenLess(const SourceManager &SM) : SM(SM) {}
1333 
1334     bool operator()(const FormatToken *LHS, const FormatToken *RHS) const {
1335       return SM.isBeforeInTranslationUnit(LHS->Tok.getLocation(),
1336                                           RHS->Tok.getLocation());
1337     }
1338     const SourceManager &SM;
1339   };
1340 
1341   // Tokens to be deleted.
1342   std::set<FormatToken *, FormatTokenLess> DeletedTokens;
1343 };
1344 
1345 struct IncludeDirective {
1346   StringRef Filename;
1347   StringRef Text;
1348   unsigned Offset;
1349   int Category;
1350 };
1351 
1352 } // end anonymous namespace
1353 
1354 // Determines whether 'Ranges' intersects with ('Start', 'End').
1355 static bool affectsRange(ArrayRef<tooling::Range> Ranges, unsigned Start,
1356                          unsigned End) {
1357   for (auto Range : Ranges) {
1358     if (Range.getOffset() < End &&
1359         Range.getOffset() + Range.getLength() > Start)
1360       return true;
1361   }
1362   return false;
1363 }
1364 
1365 // Returns a pair (Index, OffsetToEOL) describing the position of the cursor
1366 // before sorting/deduplicating. Index is the index of the include under the
1367 // cursor in the original set of includes. If this include has duplicates, it is
1368 // the index of the first of the duplicates as the others are going to be
1369 // removed. OffsetToEOL describes the cursor's position relative to the end of
1370 // its current line.
1371 // If `Cursor` is not on any #include, `Index` will be UINT_MAX.
1372 static std::pair<unsigned, unsigned>
1373 FindCursorIndex(const SmallVectorImpl<IncludeDirective> &Includes,
1374                 const SmallVectorImpl<unsigned> &Indices, unsigned Cursor) {
1375   unsigned CursorIndex = UINT_MAX;
1376   unsigned OffsetToEOL = 0;
1377   for (int i = 0, e = Includes.size(); i != e; ++i) {
1378     unsigned Start = Includes[Indices[i]].Offset;
1379     unsigned End = Start + Includes[Indices[i]].Text.size();
1380     if (!(Cursor >= Start && Cursor < End))
1381       continue;
1382     CursorIndex = Indices[i];
1383     OffsetToEOL = End - Cursor;
1384     // Put the cursor on the only remaining #include among the duplicate
1385     // #includes.
1386     while (--i >= 0 && Includes[CursorIndex].Text == Includes[Indices[i]].Text)
1387       CursorIndex = i;
1388     break;
1389   }
1390   return std::make_pair(CursorIndex, OffsetToEOL);
1391 }
1392 
1393 // Sorts and deduplicate a block of includes given by 'Includes' alphabetically
1394 // adding the necessary replacement to 'Replaces'. 'Includes' must be in strict
1395 // source order.
1396 // #include directives with the same text will be deduplicated, and only the
1397 // first #include in the duplicate #includes remains. If the `Cursor` is
1398 // provided and put on a deleted #include, it will be moved to the remaining
1399 // #include in the duplicate #includes.
1400 static void sortCppIncludes(const FormatStyle &Style,
1401                             const SmallVectorImpl<IncludeDirective> &Includes,
1402                             ArrayRef<tooling::Range> Ranges, StringRef FileName,
1403                             tooling::Replacements &Replaces, unsigned *Cursor) {
1404   unsigned IncludesBeginOffset = Includes.front().Offset;
1405   unsigned IncludesEndOffset =
1406       Includes.back().Offset + Includes.back().Text.size();
1407   unsigned IncludesBlockSize = IncludesEndOffset - IncludesBeginOffset;
1408   if (!affectsRange(Ranges, IncludesBeginOffset, IncludesEndOffset))
1409     return;
1410   SmallVector<unsigned, 16> Indices;
1411   for (unsigned i = 0, e = Includes.size(); i != e; ++i)
1412     Indices.push_back(i);
1413   std::stable_sort(
1414       Indices.begin(), Indices.end(), [&](unsigned LHSI, unsigned RHSI) {
1415         return std::tie(Includes[LHSI].Category, Includes[LHSI].Filename) <
1416                std::tie(Includes[RHSI].Category, Includes[RHSI].Filename);
1417       });
1418   // The index of the include on which the cursor will be put after
1419   // sorting/deduplicating.
1420   unsigned CursorIndex;
1421   // The offset from cursor to the end of line.
1422   unsigned CursorToEOLOffset;
1423   if (Cursor)
1424     std::tie(CursorIndex, CursorToEOLOffset) =
1425         FindCursorIndex(Includes, Indices, *Cursor);
1426 
1427   // Deduplicate #includes.
1428   Indices.erase(std::unique(Indices.begin(), Indices.end(),
1429                             [&](unsigned LHSI, unsigned RHSI) {
1430                               return Includes[LHSI].Text == Includes[RHSI].Text;
1431                             }),
1432                 Indices.end());
1433 
1434   int CurrentCategory = Includes.front().Category;
1435 
1436   // If the #includes are out of order, we generate a single replacement fixing
1437   // the entire block. Otherwise, no replacement is generated.
1438   if (Indices.size() == Includes.size() &&
1439       std::is_sorted(Indices.begin(), Indices.end()) &&
1440       Style.IncludeBlocks == FormatStyle::IBS_Preserve)
1441     return;
1442 
1443   std::string result;
1444   for (unsigned Index : Indices) {
1445     if (!result.empty()) {
1446       result += "\n";
1447       if (Style.IncludeBlocks == FormatStyle::IBS_Regroup &&
1448           CurrentCategory != Includes[Index].Category)
1449         result += "\n";
1450     }
1451     result += Includes[Index].Text;
1452     if (Cursor && CursorIndex == Index)
1453       *Cursor = IncludesBeginOffset + result.size() - CursorToEOLOffset;
1454     CurrentCategory = Includes[Index].Category;
1455   }
1456 
1457   auto Err = Replaces.add(tooling::Replacement(
1458       FileName, Includes.front().Offset, IncludesBlockSize, result));
1459   // FIXME: better error handling. For now, just skip the replacement for the
1460   // release version.
1461   if (Err) {
1462     llvm::errs() << llvm::toString(std::move(Err)) << "\n";
1463     assert(false);
1464   }
1465 }
1466 
1467 namespace {
1468 
1469 // This class manages priorities of #include categories and calculates
1470 // priorities for headers.
1471 class IncludeCategoryManager {
1472 public:
1473   IncludeCategoryManager(const FormatStyle &Style, StringRef FileName)
1474       : Style(Style), FileName(FileName) {
1475     FileStem = llvm::sys::path::stem(FileName);
1476     for (const auto &Category : Style.IncludeCategories)
1477       CategoryRegexs.emplace_back(Category.Regex, llvm::Regex::IgnoreCase);
1478     IsMainFile = FileName.endswith(".c") || FileName.endswith(".cc") ||
1479                  FileName.endswith(".cpp") || FileName.endswith(".c++") ||
1480                  FileName.endswith(".cxx") || FileName.endswith(".m") ||
1481                  FileName.endswith(".mm");
1482   }
1483 
1484   // Returns the priority of the category which \p IncludeName belongs to.
1485   // If \p CheckMainHeader is true and \p IncludeName is a main header, returns
1486   // 0. Otherwise, returns the priority of the matching category or INT_MAX.
1487   int getIncludePriority(StringRef IncludeName, bool CheckMainHeader) {
1488     int Ret = INT_MAX;
1489     for (unsigned i = 0, e = CategoryRegexs.size(); i != e; ++i)
1490       if (CategoryRegexs[i].match(IncludeName)) {
1491         Ret = Style.IncludeCategories[i].Priority;
1492         break;
1493       }
1494     if (CheckMainHeader && IsMainFile && Ret > 0 && isMainHeader(IncludeName))
1495       Ret = 0;
1496     return Ret;
1497   }
1498 
1499 private:
1500   bool isMainHeader(StringRef IncludeName) const {
1501     if (!IncludeName.startswith("\""))
1502       return false;
1503     StringRef HeaderStem =
1504         llvm::sys::path::stem(IncludeName.drop_front(1).drop_back(1));
1505     if (FileStem.startswith(HeaderStem) ||
1506         FileStem.startswith_lower(HeaderStem)) {
1507       llvm::Regex MainIncludeRegex(
1508           (HeaderStem + Style.IncludeIsMainRegex).str(),
1509           llvm::Regex::IgnoreCase);
1510       if (MainIncludeRegex.match(FileStem))
1511         return true;
1512     }
1513     return false;
1514   }
1515 
1516   const FormatStyle &Style;
1517   bool IsMainFile;
1518   StringRef FileName;
1519   StringRef FileStem;
1520   SmallVector<llvm::Regex, 4> CategoryRegexs;
1521 };
1522 
1523 const char IncludeRegexPattern[] =
1524     R"(^[\t\ ]*#[\t\ ]*(import|include)[^"<]*(["<][^">]*[">]))";
1525 
1526 } // anonymous namespace
1527 
1528 tooling::Replacements sortCppIncludes(const FormatStyle &Style, StringRef Code,
1529                                       ArrayRef<tooling::Range> Ranges,
1530                                       StringRef FileName,
1531                                       tooling::Replacements &Replaces,
1532                                       unsigned *Cursor) {
1533   unsigned Prev = 0;
1534   unsigned SearchFrom = 0;
1535   llvm::Regex IncludeRegex(IncludeRegexPattern);
1536   SmallVector<StringRef, 4> Matches;
1537   SmallVector<IncludeDirective, 16> IncludesInBlock;
1538 
1539   // In compiled files, consider the first #include to be the main #include of
1540   // the file if it is not a system #include. This ensures that the header
1541   // doesn't have hidden dependencies
1542   // (http://llvm.org/docs/CodingStandards.html#include-style).
1543   //
1544   // FIXME: Do some sanity checking, e.g. edit distance of the base name, to fix
1545   // cases where the first #include is unlikely to be the main header.
1546   IncludeCategoryManager Categories(Style, FileName);
1547   bool FirstIncludeBlock = true;
1548   bool MainIncludeFound = false;
1549   bool FormattingOff = false;
1550 
1551   for (;;) {
1552     auto Pos = Code.find('\n', SearchFrom);
1553     StringRef Line =
1554         Code.substr(Prev, (Pos != StringRef::npos ? Pos : Code.size()) - Prev);
1555 
1556     StringRef Trimmed = Line.trim();
1557     if (Trimmed == "// clang-format off")
1558       FormattingOff = true;
1559     else if (Trimmed == "// clang-format on")
1560       FormattingOff = false;
1561 
1562     const bool EmptyLineSkipped =
1563         Trimmed.empty() && (Style.IncludeBlocks == FormatStyle::IBS_Merge ||
1564                             Style.IncludeBlocks == FormatStyle::IBS_Regroup);
1565 
1566     if (!FormattingOff && !Line.endswith("\\")) {
1567       if (IncludeRegex.match(Line, &Matches)) {
1568         StringRef IncludeName = Matches[2];
1569         int Category = Categories.getIncludePriority(
1570             IncludeName,
1571             /*CheckMainHeader=*/!MainIncludeFound && FirstIncludeBlock);
1572         if (Category == 0)
1573           MainIncludeFound = true;
1574         IncludesInBlock.push_back({IncludeName, Line, Prev, Category});
1575       } else if (!IncludesInBlock.empty() && !EmptyLineSkipped) {
1576         sortCppIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces,
1577                         Cursor);
1578         IncludesInBlock.clear();
1579         FirstIncludeBlock = false;
1580       }
1581       Prev = Pos + 1;
1582     }
1583     if (Pos == StringRef::npos || Pos + 1 == Code.size())
1584       break;
1585     SearchFrom = Pos + 1;
1586   }
1587   if (!IncludesInBlock.empty())
1588     sortCppIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces, Cursor);
1589   return Replaces;
1590 }
1591 
1592 bool isMpegTS(StringRef Code) {
1593   // MPEG transport streams use the ".ts" file extension. clang-format should
1594   // not attempt to format those. MPEG TS' frame format starts with 0x47 every
1595   // 189 bytes - detect that and return.
1596   return Code.size() > 188 && Code[0] == 0x47 && Code[188] == 0x47;
1597 }
1598 
1599 bool isLikelyXml(StringRef Code) { return Code.ltrim().startswith("<"); }
1600 
1601 tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code,
1602                                    ArrayRef<tooling::Range> Ranges,
1603                                    StringRef FileName, unsigned *Cursor) {
1604   tooling::Replacements Replaces;
1605   if (!Style.SortIncludes)
1606     return Replaces;
1607   if (isLikelyXml(Code))
1608     return Replaces;
1609   if (Style.Language == FormatStyle::LanguageKind::LK_JavaScript &&
1610       isMpegTS(Code))
1611     return Replaces;
1612   if (Style.Language == FormatStyle::LanguageKind::LK_JavaScript)
1613     return sortJavaScriptImports(Style, Code, Ranges, FileName);
1614   sortCppIncludes(Style, Code, Ranges, FileName, Replaces, Cursor);
1615   return Replaces;
1616 }
1617 
1618 template <typename T>
1619 static llvm::Expected<tooling::Replacements>
1620 processReplacements(T ProcessFunc, StringRef Code,
1621                     const tooling::Replacements &Replaces,
1622                     const FormatStyle &Style) {
1623   if (Replaces.empty())
1624     return tooling::Replacements();
1625 
1626   auto NewCode = applyAllReplacements(Code, Replaces);
1627   if (!NewCode)
1628     return NewCode.takeError();
1629   std::vector<tooling::Range> ChangedRanges = Replaces.getAffectedRanges();
1630   StringRef FileName = Replaces.begin()->getFilePath();
1631 
1632   tooling::Replacements FormatReplaces =
1633       ProcessFunc(Style, *NewCode, ChangedRanges, FileName);
1634 
1635   return Replaces.merge(FormatReplaces);
1636 }
1637 
1638 llvm::Expected<tooling::Replacements>
1639 formatReplacements(StringRef Code, const tooling::Replacements &Replaces,
1640                    const FormatStyle &Style) {
1641   // We need to use lambda function here since there are two versions of
1642   // `sortIncludes`.
1643   auto SortIncludes = [](const FormatStyle &Style, StringRef Code,
1644                          std::vector<tooling::Range> Ranges,
1645                          StringRef FileName) -> tooling::Replacements {
1646     return sortIncludes(Style, Code, Ranges, FileName);
1647   };
1648   auto SortedReplaces =
1649       processReplacements(SortIncludes, Code, Replaces, Style);
1650   if (!SortedReplaces)
1651     return SortedReplaces.takeError();
1652 
1653   // We need to use lambda function here since there are two versions of
1654   // `reformat`.
1655   auto Reformat = [](const FormatStyle &Style, StringRef Code,
1656                      std::vector<tooling::Range> Ranges,
1657                      StringRef FileName) -> tooling::Replacements {
1658     return reformat(Style, Code, Ranges, FileName);
1659   };
1660   return processReplacements(Reformat, Code, *SortedReplaces, Style);
1661 }
1662 
1663 namespace {
1664 
1665 inline bool isHeaderInsertion(const tooling::Replacement &Replace) {
1666   return Replace.getOffset() == UINT_MAX && Replace.getLength() == 0 &&
1667          llvm::Regex(IncludeRegexPattern).match(Replace.getReplacementText());
1668 }
1669 
1670 inline bool isHeaderDeletion(const tooling::Replacement &Replace) {
1671   return Replace.getOffset() == UINT_MAX && Replace.getLength() == 1;
1672 }
1673 
1674 // Returns the offset after skipping a sequence of tokens, matched by \p
1675 // GetOffsetAfterSequence, from the start of the code.
1676 // \p GetOffsetAfterSequence should be a function that matches a sequence of
1677 // tokens and returns an offset after the sequence.
1678 unsigned getOffsetAfterTokenSequence(
1679     StringRef FileName, StringRef Code, const FormatStyle &Style,
1680     llvm::function_ref<unsigned(const SourceManager &, Lexer &, Token &)>
1681         GetOffsetAfterSequence) {
1682   std::unique_ptr<Environment> Env =
1683       Environment::CreateVirtualEnvironment(Code, FileName, /*Ranges=*/{});
1684   const SourceManager &SourceMgr = Env->getSourceManager();
1685   Lexer Lex(Env->getFileID(), SourceMgr.getBuffer(Env->getFileID()), SourceMgr,
1686             getFormattingLangOpts(Style));
1687   Token Tok;
1688   // Get the first token.
1689   Lex.LexFromRawLexer(Tok);
1690   return GetOffsetAfterSequence(SourceMgr, Lex, Tok);
1691 }
1692 
1693 // Check if a sequence of tokens is like "#<Name> <raw_identifier>". If it is,
1694 // \p Tok will be the token after this directive; otherwise, it can be any token
1695 // after the given \p Tok (including \p Tok).
1696 bool checkAndConsumeDirectiveWithName(Lexer &Lex, StringRef Name, Token &Tok) {
1697   bool Matched = Tok.is(tok::hash) && !Lex.LexFromRawLexer(Tok) &&
1698                  Tok.is(tok::raw_identifier) &&
1699                  Tok.getRawIdentifier() == Name && !Lex.LexFromRawLexer(Tok) &&
1700                  Tok.is(tok::raw_identifier);
1701   if (Matched)
1702     Lex.LexFromRawLexer(Tok);
1703   return Matched;
1704 }
1705 
1706 void skipComments(Lexer &Lex, Token &Tok) {
1707   while (Tok.is(tok::comment))
1708     if (Lex.LexFromRawLexer(Tok))
1709       return;
1710 }
1711 
1712 // Returns the offset after header guard directives and any comments
1713 // before/after header guards. If no header guard presents in the code, this
1714 // will returns the offset after skipping all comments from the start of the
1715 // code.
1716 unsigned getOffsetAfterHeaderGuardsAndComments(StringRef FileName,
1717                                                StringRef Code,
1718                                                const FormatStyle &Style) {
1719   return getOffsetAfterTokenSequence(
1720       FileName, Code, Style,
1721       [](const SourceManager &SM, Lexer &Lex, Token Tok) {
1722         skipComments(Lex, Tok);
1723         unsigned InitialOffset = SM.getFileOffset(Tok.getLocation());
1724         if (checkAndConsumeDirectiveWithName(Lex, "ifndef", Tok)) {
1725           skipComments(Lex, Tok);
1726           if (checkAndConsumeDirectiveWithName(Lex, "define", Tok))
1727             return SM.getFileOffset(Tok.getLocation());
1728         }
1729         return InitialOffset;
1730       });
1731 }
1732 
1733 // Check if a sequence of tokens is like
1734 //    "#include ("header.h" | <header.h>)".
1735 // If it is, \p Tok will be the token after this directive; otherwise, it can be
1736 // any token after the given \p Tok (including \p Tok).
1737 bool checkAndConsumeInclusiveDirective(Lexer &Lex, Token &Tok) {
1738   auto Matched = [&]() {
1739     Lex.LexFromRawLexer(Tok);
1740     return true;
1741   };
1742   if (Tok.is(tok::hash) && !Lex.LexFromRawLexer(Tok) &&
1743       Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == "include") {
1744     if (Lex.LexFromRawLexer(Tok))
1745       return false;
1746     if (Tok.is(tok::string_literal))
1747       return Matched();
1748     if (Tok.is(tok::less)) {
1749       while (!Lex.LexFromRawLexer(Tok) && Tok.isNot(tok::greater)) {
1750       }
1751       if (Tok.is(tok::greater))
1752         return Matched();
1753     }
1754   }
1755   return false;
1756 }
1757 
1758 // Returns the offset of the last #include directive after which a new
1759 // #include can be inserted. This ignores #include's after the #include block(s)
1760 // in the beginning of a file to avoid inserting headers into code sections
1761 // where new #include's should not be added by default.
1762 // These code sections include:
1763 //      - raw string literals (containing #include).
1764 //      - #if blocks.
1765 //      - Special #include's among declarations (e.g. functions).
1766 //
1767 // If no #include after which a new #include can be inserted, this returns the
1768 // offset after skipping all comments from the start of the code.
1769 // Inserting after an #include is not allowed if it comes after code that is not
1770 // #include (e.g. pre-processing directive that is not #include, declarations).
1771 unsigned getMaxHeaderInsertionOffset(StringRef FileName, StringRef Code,
1772                                      const FormatStyle &Style) {
1773   return getOffsetAfterTokenSequence(
1774       FileName, Code, Style,
1775       [](const SourceManager &SM, Lexer &Lex, Token Tok) {
1776         skipComments(Lex, Tok);
1777         unsigned MaxOffset = SM.getFileOffset(Tok.getLocation());
1778         while (checkAndConsumeInclusiveDirective(Lex, Tok))
1779           MaxOffset = SM.getFileOffset(Tok.getLocation());
1780         return MaxOffset;
1781       });
1782 }
1783 
1784 bool isDeletedHeader(llvm::StringRef HeaderName,
1785                      const std::set<llvm::StringRef> &HeadersToDelete) {
1786   return HeadersToDelete.count(HeaderName) ||
1787          HeadersToDelete.count(HeaderName.trim("\"<>"));
1788 }
1789 
1790 // FIXME: insert empty lines between newly created blocks.
1791 tooling::Replacements
1792 fixCppIncludeInsertions(StringRef Code, const tooling::Replacements &Replaces,
1793                         const FormatStyle &Style) {
1794   if (!Style.isCpp())
1795     return Replaces;
1796 
1797   tooling::Replacements HeaderInsertions;
1798   std::set<llvm::StringRef> HeadersToDelete;
1799   tooling::Replacements Result;
1800   for (const auto &R : Replaces) {
1801     if (isHeaderInsertion(R)) {
1802       // Replacements from \p Replaces must be conflict-free already, so we can
1803       // simply consume the error.
1804       llvm::consumeError(HeaderInsertions.add(R));
1805     } else if (isHeaderDeletion(R)) {
1806       HeadersToDelete.insert(R.getReplacementText());
1807     } else if (R.getOffset() == UINT_MAX) {
1808       llvm::errs() << "Insertions other than header #include insertion are "
1809                       "not supported! "
1810                    << R.getReplacementText() << "\n";
1811     } else {
1812       llvm::consumeError(Result.add(R));
1813     }
1814   }
1815   if (HeaderInsertions.empty() && HeadersToDelete.empty())
1816     return Replaces;
1817 
1818   llvm::Regex IncludeRegex(IncludeRegexPattern);
1819   llvm::Regex DefineRegex(R"(^[\t\ ]*#[\t\ ]*define[\t\ ]*[^\\]*$)");
1820   SmallVector<StringRef, 4> Matches;
1821 
1822   StringRef FileName = Replaces.begin()->getFilePath();
1823   IncludeCategoryManager Categories(Style, FileName);
1824 
1825   // Record the offset of the end of the last include in each category.
1826   std::map<int, int> CategoryEndOffsets;
1827   // All possible priorities.
1828   // Add 0 for main header and INT_MAX for headers that are not in any category.
1829   std::set<int> Priorities = {0, INT_MAX};
1830   for (const auto &Category : Style.IncludeCategories)
1831     Priorities.insert(Category.Priority);
1832   int FirstIncludeOffset = -1;
1833   // All new headers should be inserted after this offset.
1834   unsigned MinInsertOffset =
1835       getOffsetAfterHeaderGuardsAndComments(FileName, Code, Style);
1836   StringRef TrimmedCode = Code.drop_front(MinInsertOffset);
1837   // Max insertion offset in the original code.
1838   unsigned MaxInsertOffset =
1839       MinInsertOffset +
1840       getMaxHeaderInsertionOffset(FileName, TrimmedCode, Style);
1841   SmallVector<StringRef, 32> Lines;
1842   TrimmedCode.split(Lines, '\n');
1843   unsigned Offset = MinInsertOffset;
1844   unsigned NextLineOffset;
1845   std::set<StringRef> ExistingIncludes;
1846   for (auto Line : Lines) {
1847     NextLineOffset = std::min(Code.size(), Offset + Line.size() + 1);
1848     if (IncludeRegex.match(Line, &Matches)) {
1849       // The header name with quotes or angle brackets.
1850       StringRef IncludeName = Matches[2];
1851       ExistingIncludes.insert(IncludeName);
1852       // Only record the offset of current #include if we can insert after it.
1853       if (Offset <= MaxInsertOffset) {
1854         int Category = Categories.getIncludePriority(
1855             IncludeName, /*CheckMainHeader=*/FirstIncludeOffset < 0);
1856         CategoryEndOffsets[Category] = NextLineOffset;
1857         if (FirstIncludeOffset < 0)
1858           FirstIncludeOffset = Offset;
1859       }
1860       if (isDeletedHeader(IncludeName, HeadersToDelete)) {
1861         // If this is the last line without trailing newline, we need to make
1862         // sure we don't delete across the file boundary.
1863         unsigned Length = std::min(Line.size() + 1, Code.size() - Offset);
1864         llvm::Error Err =
1865             Result.add(tooling::Replacement(FileName, Offset, Length, ""));
1866         if (Err) {
1867           // Ignore the deletion on conflict.
1868           llvm::errs() << "Failed to add header deletion replacement for "
1869                        << IncludeName << ": " << llvm::toString(std::move(Err))
1870                        << "\n";
1871         }
1872       }
1873     }
1874     Offset = NextLineOffset;
1875   }
1876 
1877   // Populate CategoryEndOfssets:
1878   // - Ensure that CategoryEndOffset[Highest] is always populated.
1879   // - If CategoryEndOffset[Priority] isn't set, use the next higher value that
1880   //   is set, up to CategoryEndOffset[Highest].
1881   auto Highest = Priorities.begin();
1882   if (CategoryEndOffsets.find(*Highest) == CategoryEndOffsets.end()) {
1883     if (FirstIncludeOffset >= 0)
1884       CategoryEndOffsets[*Highest] = FirstIncludeOffset;
1885     else
1886       CategoryEndOffsets[*Highest] = MinInsertOffset;
1887   }
1888   // By this point, CategoryEndOffset[Highest] is always set appropriately:
1889   //  - to an appropriate location before/after existing #includes, or
1890   //  - to right after the header guard, or
1891   //  - to the beginning of the file.
1892   for (auto I = ++Priorities.begin(), E = Priorities.end(); I != E; ++I)
1893     if (CategoryEndOffsets.find(*I) == CategoryEndOffsets.end())
1894       CategoryEndOffsets[*I] = CategoryEndOffsets[*std::prev(I)];
1895 
1896   bool NeedNewLineAtEnd = !Code.empty() && Code.back() != '\n';
1897   for (const auto &R : HeaderInsertions) {
1898     auto IncludeDirective = R.getReplacementText();
1899     bool Matched = IncludeRegex.match(IncludeDirective, &Matches);
1900     assert(Matched && "Header insertion replacement must have replacement text "
1901                       "'#include ...'");
1902     (void)Matched;
1903     auto IncludeName = Matches[2];
1904     if (ExistingIncludes.find(IncludeName) != ExistingIncludes.end()) {
1905       DEBUG(llvm::dbgs() << "Skip adding existing include : " << IncludeName
1906                          << "\n");
1907       continue;
1908     }
1909     int Category =
1910         Categories.getIncludePriority(IncludeName, /*CheckMainHeader=*/true);
1911     Offset = CategoryEndOffsets[Category];
1912     std::string NewInclude = !IncludeDirective.endswith("\n")
1913                                  ? (IncludeDirective + "\n").str()
1914                                  : IncludeDirective.str();
1915     // When inserting headers at end of the code, also append '\n' to the code
1916     // if it does not end with '\n'.
1917     if (NeedNewLineAtEnd && Offset == Code.size()) {
1918       NewInclude = "\n" + NewInclude;
1919       NeedNewLineAtEnd = false;
1920     }
1921     auto NewReplace = tooling::Replacement(FileName, Offset, 0, NewInclude);
1922     auto Err = Result.add(NewReplace);
1923     if (Err) {
1924       llvm::consumeError(std::move(Err));
1925       unsigned NewOffset = Result.getShiftedCodePosition(Offset);
1926       NewReplace = tooling::Replacement(FileName, NewOffset, 0, NewInclude);
1927       Result = Result.merge(tooling::Replacements(NewReplace));
1928     }
1929   }
1930   return Result;
1931 }
1932 
1933 } // anonymous namespace
1934 
1935 llvm::Expected<tooling::Replacements>
1936 cleanupAroundReplacements(StringRef Code, const tooling::Replacements &Replaces,
1937                           const FormatStyle &Style) {
1938   // We need to use lambda function here since there are two versions of
1939   // `cleanup`.
1940   auto Cleanup = [](const FormatStyle &Style, StringRef Code,
1941                     std::vector<tooling::Range> Ranges,
1942                     StringRef FileName) -> tooling::Replacements {
1943     return cleanup(Style, Code, Ranges, FileName);
1944   };
1945   // Make header insertion replacements insert new headers into correct blocks.
1946   tooling::Replacements NewReplaces =
1947       fixCppIncludeInsertions(Code, Replaces, Style);
1948   return processReplacements(Cleanup, Code, NewReplaces, Style);
1949 }
1950 
1951 namespace internal {
1952 std::pair<tooling::Replacements, unsigned>
1953 reformat(const FormatStyle &Style, StringRef Code,
1954          ArrayRef<tooling::Range> Ranges, unsigned FirstStartColumn,
1955          unsigned NextStartColumn, unsigned LastStartColumn, StringRef FileName,
1956          FormattingAttemptStatus *Status) {
1957   FormatStyle Expanded = expandPresets(Style);
1958   if (Expanded.DisableFormat)
1959     return {tooling::Replacements(), 0};
1960   if (isLikelyXml(Code))
1961     return {tooling::Replacements(), 0};
1962   if (Expanded.Language == FormatStyle::LK_JavaScript && isMpegTS(Code))
1963     return {tooling::Replacements(), 0};
1964 
1965   typedef std::function<std::pair<tooling::Replacements, unsigned>(
1966       const Environment &)>
1967       AnalyzerPass;
1968   SmallVector<AnalyzerPass, 4> Passes;
1969 
1970   if (Style.Language == FormatStyle::LK_Cpp) {
1971     if (Style.FixNamespaceComments)
1972       Passes.emplace_back([&](const Environment &Env) {
1973         return NamespaceEndCommentsFixer(Env, Expanded).process();
1974       });
1975 
1976     if (Style.SortUsingDeclarations)
1977       Passes.emplace_back([&](const Environment &Env) {
1978         return UsingDeclarationsSorter(Env, Expanded).process();
1979       });
1980   }
1981 
1982   if (Style.Language == FormatStyle::LK_JavaScript &&
1983       Style.JavaScriptQuotes != FormatStyle::JSQS_Leave)
1984     Passes.emplace_back([&](const Environment &Env) {
1985       return JavaScriptRequoter(Env, Expanded).process();
1986     });
1987 
1988   Passes.emplace_back([&](const Environment &Env) {
1989     return Formatter(Env, Expanded, Status).process();
1990   });
1991 
1992   std::unique_ptr<Environment> Env = Environment::CreateVirtualEnvironment(
1993       Code, FileName, Ranges, FirstStartColumn, NextStartColumn,
1994       LastStartColumn);
1995   llvm::Optional<std::string> CurrentCode = None;
1996   tooling::Replacements Fixes;
1997   unsigned Penalty = 0;
1998   for (size_t I = 0, E = Passes.size(); I < E; ++I) {
1999     std::pair<tooling::Replacements, unsigned> PassFixes = Passes[I](*Env);
2000     auto NewCode = applyAllReplacements(
2001         CurrentCode ? StringRef(*CurrentCode) : Code, PassFixes.first);
2002     if (NewCode) {
2003       Fixes = Fixes.merge(PassFixes.first);
2004       Penalty += PassFixes.second;
2005       if (I + 1 < E) {
2006         CurrentCode = std::move(*NewCode);
2007         Env = Environment::CreateVirtualEnvironment(
2008             *CurrentCode, FileName,
2009             tooling::calculateRangesAfterReplacements(Fixes, Ranges),
2010             FirstStartColumn, NextStartColumn, LastStartColumn);
2011       }
2012     }
2013   }
2014 
2015   return {Fixes, Penalty};
2016 }
2017 } // namespace internal
2018 
2019 tooling::Replacements reformat(const FormatStyle &Style, StringRef Code,
2020                                ArrayRef<tooling::Range> Ranges,
2021                                StringRef FileName,
2022                                FormattingAttemptStatus *Status) {
2023   return internal::reformat(Style, Code, Ranges,
2024                             /*FirstStartColumn=*/0,
2025                             /*NextStartColumn=*/0,
2026                             /*LastStartColumn=*/0, FileName, Status)
2027       .first;
2028 }
2029 
2030 tooling::Replacements cleanup(const FormatStyle &Style, StringRef Code,
2031                               ArrayRef<tooling::Range> Ranges,
2032                               StringRef FileName) {
2033   // cleanups only apply to C++ (they mostly concern ctor commas etc.)
2034   if (Style.Language != FormatStyle::LK_Cpp)
2035     return tooling::Replacements();
2036   std::unique_ptr<Environment> Env =
2037       Environment::CreateVirtualEnvironment(Code, FileName, Ranges);
2038   Cleaner Clean(*Env, Style);
2039   return Clean.process().first;
2040 }
2041 
2042 tooling::Replacements reformat(const FormatStyle &Style, StringRef Code,
2043                                ArrayRef<tooling::Range> Ranges,
2044                                StringRef FileName, bool *IncompleteFormat) {
2045   FormattingAttemptStatus Status;
2046   auto Result = reformat(Style, Code, Ranges, FileName, &Status);
2047   if (!Status.FormatComplete)
2048     *IncompleteFormat = true;
2049   return Result;
2050 }
2051 
2052 tooling::Replacements fixNamespaceEndComments(const FormatStyle &Style,
2053                                               StringRef Code,
2054                                               ArrayRef<tooling::Range> Ranges,
2055                                               StringRef FileName) {
2056   std::unique_ptr<Environment> Env =
2057       Environment::CreateVirtualEnvironment(Code, FileName, Ranges);
2058   NamespaceEndCommentsFixer Fix(*Env, Style);
2059   return Fix.process().first;
2060 }
2061 
2062 tooling::Replacements sortUsingDeclarations(const FormatStyle &Style,
2063                                             StringRef Code,
2064                                             ArrayRef<tooling::Range> Ranges,
2065                                             StringRef FileName) {
2066   std::unique_ptr<Environment> Env =
2067       Environment::CreateVirtualEnvironment(Code, FileName, Ranges);
2068   UsingDeclarationsSorter Sorter(*Env, Style);
2069   return Sorter.process().first;
2070 }
2071 
2072 LangOptions getFormattingLangOpts(const FormatStyle &Style) {
2073   LangOptions LangOpts;
2074   LangOpts.CPlusPlus = 1;
2075   LangOpts.CPlusPlus11 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
2076   LangOpts.CPlusPlus14 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
2077   LangOpts.CPlusPlus17 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
2078   LangOpts.CPlusPlus2a = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
2079   LangOpts.LineComment = 1;
2080   bool AlternativeOperators = Style.isCpp();
2081   LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0;
2082   LangOpts.Bool = 1;
2083   LangOpts.ObjC1 = 1;
2084   LangOpts.ObjC2 = 1;
2085   LangOpts.MicrosoftExt = 1;    // To get kw___try, kw___finally.
2086   LangOpts.DeclSpecKeyword = 1; // To get __declspec.
2087   return LangOpts;
2088 }
2089 
2090 const char *StyleOptionHelpDescription =
2091     "Coding style, currently supports:\n"
2092     "  LLVM, Google, Chromium, Mozilla, WebKit.\n"
2093     "Use -style=file to load style configuration from\n"
2094     ".clang-format file located in one of the parent\n"
2095     "directories of the source file (or current\n"
2096     "directory for stdin).\n"
2097     "Use -style=\"{key: value, ...}\" to set specific\n"
2098     "parameters, e.g.:\n"
2099     "  -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\"";
2100 
2101 static FormatStyle::LanguageKind getLanguageByFileName(StringRef FileName) {
2102   if (FileName.endswith(".java"))
2103     return FormatStyle::LK_Java;
2104   if (FileName.endswith_lower(".js") || FileName.endswith_lower(".ts"))
2105     return FormatStyle::LK_JavaScript; // JavaScript or TypeScript.
2106   if (FileName.endswith(".m") || FileName.endswith(".mm"))
2107     return FormatStyle::LK_ObjC;
2108   if (FileName.endswith_lower(".proto") ||
2109       FileName.endswith_lower(".protodevel"))
2110     return FormatStyle::LK_Proto;
2111   if (FileName.endswith_lower(".textpb") ||
2112       FileName.endswith_lower(".pb.txt") ||
2113       FileName.endswith_lower(".textproto") ||
2114       FileName.endswith_lower(".asciipb"))
2115     return FormatStyle::LK_TextProto;
2116   if (FileName.endswith_lower(".td"))
2117     return FormatStyle::LK_TableGen;
2118   return FormatStyle::LK_Cpp;
2119 }
2120 
2121 llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
2122                                      StringRef FallbackStyleName,
2123                                      StringRef Code, vfs::FileSystem *FS) {
2124   if (!FS) {
2125     FS = vfs::getRealFileSystem().get();
2126   }
2127   FormatStyle Style = getLLVMStyle();
2128   Style.Language = getLanguageByFileName(FileName);
2129 
2130   // This is a very crude detection of whether a header contains ObjC code that
2131   // should be improved over time and probably be done on tokens, not one the
2132   // bare content of the file.
2133   if (Style.Language == FormatStyle::LK_Cpp && FileName.endswith(".h") &&
2134       (Code.contains("\n- (") || Code.contains("\n+ (") ||
2135        Code.contains("\n@end\n") || Code.contains("\n@end ") ||
2136        Code.endswith("@end")))
2137     Style.Language = FormatStyle::LK_ObjC;
2138 
2139   FormatStyle FallbackStyle = getNoStyle();
2140   if (!getPredefinedStyle(FallbackStyleName, Style.Language, &FallbackStyle))
2141     return make_string_error("Invalid fallback style \"" + FallbackStyleName);
2142 
2143   if (StyleName.startswith("{")) {
2144     // Parse YAML/JSON style from the command line.
2145     if (std::error_code ec = parseConfiguration(StyleName, &Style))
2146       return make_string_error("Error parsing -style: " + ec.message());
2147     return Style;
2148   }
2149 
2150   if (!StyleName.equals_lower("file")) {
2151     if (!getPredefinedStyle(StyleName, Style.Language, &Style))
2152       return make_string_error("Invalid value for -style");
2153     return Style;
2154   }
2155 
2156   // Look for .clang-format/_clang-format file in the file's parent directories.
2157   SmallString<128> UnsuitableConfigFiles;
2158   SmallString<128> Path(FileName);
2159   if (std::error_code EC = FS->makeAbsolute(Path))
2160     return make_string_error(EC.message());
2161 
2162   for (StringRef Directory = Path; !Directory.empty();
2163        Directory = llvm::sys::path::parent_path(Directory)) {
2164 
2165     auto Status = FS->status(Directory);
2166     if (!Status ||
2167         Status->getType() != llvm::sys::fs::file_type::directory_file) {
2168       continue;
2169     }
2170 
2171     SmallString<128> ConfigFile(Directory);
2172 
2173     llvm::sys::path::append(ConfigFile, ".clang-format");
2174     DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
2175 
2176     Status = FS->status(ConfigFile.str());
2177     bool FoundConfigFile =
2178         Status && (Status->getType() == llvm::sys::fs::file_type::regular_file);
2179     if (!FoundConfigFile) {
2180       // Try _clang-format too, since dotfiles are not commonly used on Windows.
2181       ConfigFile = Directory;
2182       llvm::sys::path::append(ConfigFile, "_clang-format");
2183       DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
2184       Status = FS->status(ConfigFile.str());
2185       FoundConfigFile = Status && (Status->getType() ==
2186                                    llvm::sys::fs::file_type::regular_file);
2187     }
2188 
2189     if (FoundConfigFile) {
2190       llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
2191           FS->getBufferForFile(ConfigFile.str());
2192       if (std::error_code EC = Text.getError())
2193         return make_string_error(EC.message());
2194       if (std::error_code ec =
2195               parseConfiguration(Text.get()->getBuffer(), &Style)) {
2196         if (ec == ParseError::Unsuitable) {
2197           if (!UnsuitableConfigFiles.empty())
2198             UnsuitableConfigFiles.append(", ");
2199           UnsuitableConfigFiles.append(ConfigFile);
2200           continue;
2201         }
2202         return make_string_error("Error reading " + ConfigFile + ": " +
2203                                  ec.message());
2204       }
2205       DEBUG(llvm::dbgs() << "Using configuration file " << ConfigFile << "\n");
2206       return Style;
2207     }
2208   }
2209   if (!UnsuitableConfigFiles.empty())
2210     return make_string_error("Configuration file(s) do(es) not support " +
2211                              getLanguageName(Style.Language) + ": " +
2212                              UnsuitableConfigFiles);
2213   return FallbackStyle;
2214 }
2215 
2216 } // namespace format
2217 } // namespace clang
2218