@@ -379,6 +379,7 @@ template <> struct MappingTraits<FormatStyle> {
379
379
IO.mapOptional (" PointerAlignment" , Style .PointerAlignment );
380
380
IO.mapOptional (" ReflowComments" , Style .ReflowComments );
381
381
IO.mapOptional (" SortIncludes" , Style .SortIncludes );
382
+ IO.mapOptional (" SortUsingDeclarations" , Style .SortUsingDeclarations );
382
383
IO.mapOptional (" SpaceAfterCStyleCast" , Style .SpaceAfterCStyleCast );
383
384
IO.mapOptional (" SpaceAfterTemplateKeyword" , Style .SpaceAfterTemplateKeyword );
384
385
IO.mapOptional (" SpaceBeforeAssignmentOperators" ,
@@ -619,6 +620,7 @@ FormatStyle getLLVMStyle() {
619
620
620
621
LLVMStyle.DisableFormat = false ;
621
622
LLVMStyle.SortIncludes = true ;
623
+ LLVMStyle.SortUsingDeclarations = true ;
622
624
623
625
return LLVMStyle;
624
626
}
@@ -773,6 +775,7 @@ FormatStyle getNoStyle() {
773
775
FormatStyle NoStyle = getLLVMStyle ();
774
776
NoStyle.DisableFormat = true ;
775
777
NoStyle.SortIncludes = false ;
778
+ NoStyle.SortUsingDeclarations = false ;
776
779
return NoStyle;
777
780
}
778
781
@@ -1879,38 +1882,53 @@ tooling::Replacements reformat(const FormatStyle &Style, StringRef Code,
1879
1882
return tooling::Replacements ();
1880
1883
if (Expanded.Language == FormatStyle::LK_JavaScript && isMpegTS (Code))
1881
1884
return tooling::Replacements ();
1882
- auto Env = Environment::CreateVirtualEnvironment (Code, FileName, Ranges);
1883
-
1884
- auto reformatAfterApplying = [&] (TokenAnalyzer& Fixer) {
1885
- tooling::Replacements Fixes = Fixer.process ();
1886
- if (!Fixes.empty ()) {
1887
- auto NewCode = applyAllReplacements (Code, Fixes);
1888
- if (NewCode) {
1889
- auto NewEnv = Environment::CreateVirtualEnvironment (
1890
- *NewCode, FileName,
1891
- tooling::calculateRangesAfterReplacements (Fixes, Ranges));
1892
- Formatter Format (*NewEnv, Expanded, Status);
1893
- return Fixes.merge (Format.process ());
1894
- }
1895
- }
1896
- Formatter Format (*Env, Expanded, Status);
1897
- return Format.process ();
1898
- };
1899
1885
1900
- if (Style .Language == FormatStyle::LK_Cpp &&
1901
- Style .FixNamespaceComments ) {
1902
- NamespaceEndCommentsFixer CommentsFixer (*Env, Expanded);
1903
- return reformatAfterApplying (CommentsFixer);
1886
+ typedef std::function<tooling::Replacements (const Environment &)>
1887
+ AnalyzerPass;
1888
+ SmallVector<AnalyzerPass, 4 > Passes;
1889
+
1890
+ if (Style .Language == FormatStyle::LK_Cpp) {
1891
+ if (Style .FixNamespaceComments )
1892
+ Passes.emplace_back ([&](const Environment &Env) {
1893
+ return NamespaceEndCommentsFixer (Env, Expanded).process ();
1894
+ });
1895
+
1896
+ if (Style .SortUsingDeclarations )
1897
+ Passes.emplace_back ([&](const Environment &Env) {
1898
+ return UsingDeclarationsSorter (Env, Expanded).process ();
1899
+ });
1904
1900
}
1905
1901
1906
1902
if (Style .Language == FormatStyle::LK_JavaScript &&
1907
- Style .JavaScriptQuotes != FormatStyle::JSQS_Leave) {
1908
- JavaScriptRequoter Requoter (*Env, Expanded);
1909
- return reformatAfterApplying (Requoter);
1903
+ Style .JavaScriptQuotes != FormatStyle::JSQS_Leave)
1904
+ Passes.emplace_back ([&](const Environment &Env) {
1905
+ return JavaScriptRequoter (Env, Expanded).process ();
1906
+ });
1907
+
1908
+ Passes.emplace_back ([&](const Environment &Env) {
1909
+ return Formatter (Env, Expanded, Status).process ();
1910
+ });
1911
+
1912
+ std::unique_ptr<Environment> Env =
1913
+ Environment::CreateVirtualEnvironment (Code, FileName, Ranges);
1914
+ llvm::Optional<std::string> CurrentCode = None;
1915
+ tooling::Replacements Fixes;
1916
+ for (size_t I = 0 , E = Passes.size (); I < E; ++I) {
1917
+ tooling::Replacements PassFixes = Passes[I](*Env);
1918
+ auto NewCode = applyAllReplacements (
1919
+ CurrentCode ? StringRef (*CurrentCode) : Code, PassFixes);
1920
+ if (NewCode) {
1921
+ Fixes = Fixes.merge (PassFixes);
1922
+ if (I + 1 < E) {
1923
+ CurrentCode = std::move (*NewCode);
1924
+ Env = Environment::CreateVirtualEnvironment (
1925
+ *CurrentCode, FileName,
1926
+ tooling::calculateRangesAfterReplacements (Fixes, Ranges));
1927
+ }
1928
+ }
1910
1929
}
1911
1930
1912
- Formatter Format (*Env, Expanded, Status);
1913
- return Format.process ();
1931
+ return Fixes;
1914
1932
}
1915
1933
1916
1934
tooling::Replacements cleanup (const FormatStyle &Style , StringRef Code,
0 commit comments