]> granicus.if.org Git - clang/commitdiff
[clang-format] Make formatReplacements() also sort #includes.
authorEric Liu <ioeric@google.com>
Wed, 18 May 2016 13:43:48 +0000 (13:43 +0000)
committerEric Liu <ioeric@google.com>
Wed, 18 May 2016 13:43:48 +0000 (13:43 +0000)
Summary: [clang-format] Make formatReplacements() also sort #includes.

Reviewers: bkramer, djasper

Subscribers: klimek, cfe-commits

Differential Revision: http://reviews.llvm.org/D20362

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@269924 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/Format.cpp
unittests/Format/FormatTest.cpp

index c31e618520aa7155faac052b5be051ebd3eadd43..a70a7ef2a3c607306657153a17626316b690f616 100644 (file)
@@ -2140,13 +2140,23 @@ tooling::Replacements formatReplacements(StringRef Code,
                                          const tooling::Replacements &Replaces,
                                          const FormatStyle &Style) {
   // We need to use lambda function here since there are two versions of
+  // `sortIncludes`.
+  auto SortIncludes = [](const FormatStyle &Style, StringRef Code,
+                         std::vector<tooling::Range> Ranges,
+                         StringRef FileName) -> tooling::Replacements {
+    return sortIncludes(Style, Code, Ranges, FileName);
+  };
+  tooling::Replacements SortedReplaces =
+      processReplacements(SortIncludes, Code, Replaces, Style);
+
+  // We need to use lambda function here since there are two versions of
   // `reformat`.
   auto Reformat = [](const FormatStyle &Style, StringRef Code,
                      std::vector<tooling::Range> Ranges,
                      StringRef FileName) -> tooling::Replacements {
     return reformat(Style, Code, Ranges, FileName);
   };
-  return processReplacements(Reformat, Code, Replaces, Style);
+  return processReplacements(Reformat, Code, SortedReplaces, Style);
 }
 
 tooling::Replacements
index 3c72a13bf105e1d1728c528bb6f2fce141e194cb..1b434eb8c94a2eb800f938d9fd9ddbc4fb898e8b 100644 (file)
@@ -11559,6 +11559,31 @@ TEST_F(ReplacementTest, FixOnlyAffectedCodeAfterReplacements) {
   EXPECT_EQ(Expected, applyAllReplacements(Code, FinalReplaces));
 }
 
+TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
+  std::string Code = "#include \"a.h\"\n"
+                     "#include \"c.h\"\n"
+                     "\n"
+                     "int main() {\n"
+                     "  return 0;\n"
+                     "}";
+  std::string Expected = "#include \"a.h\"\n"
+                         "#include \"b.h\"\n"
+                         "#include \"c.h\"\n"
+                         "\n"
+                         "int main() {\n"
+                         "  return 0;\n"
+                         "}";
+  FileID ID = Context.createInMemoryFile("fix.cpp", Code);
+  tooling::Replacements Replaces;
+  Replaces.insert(tooling::Replacement(
+      Context.Sources, Context.getLocation(ID, 1, 1), 0, "#include \"b.h\"\n"));
+
+  format::FormatStyle Style = format::getLLVMStyle();
+  Style.SortIncludes = true;
+  auto FinalReplaces = formatReplacements(Code, Replaces, Style);
+  EXPECT_EQ(Expected, applyAllReplacements(Code, FinalReplaces));
+}
+
 } // end namespace
 } // end namespace format
 } // end namespace clang