djasper updated the summary for this revision.
djasper updated this revision to Diff 34137.
djasper added a comment.

Now properly implemented merging of two replacement sets that are meant to be 
applied in sequence. I think this functionality will also be highly useful, 
e.g. when formatting the result of a clang-tidy fix or many other such things.


http://reviews.llvm.org/D11240

Files:
  include/clang/Format/Format.h
  include/clang/Tooling/Core/Replacement.h
  lib/Format/Format.cpp
  lib/Format/FormatToken.cpp
  lib/Tooling/Core/Replacement.cpp
  tools/clang-format/CMakeLists.txt
  tools/clang-format/ClangFormat.cpp
  tools/clang-format/clang-format.py
  unittests/Format/CMakeLists.txt
  unittests/Format/SortIncludesTest.cpp
  unittests/Tooling/RefactoringTest.cpp

Index: unittests/Tooling/RefactoringTest.cpp
===================================================================
--- unittests/Tooling/RefactoringTest.cpp
+++ unittests/Tooling/RefactoringTest.cpp
@@ -489,5 +489,98 @@
   }
 }
 
+class MergeReplacementsTest : public ::testing::Test {
+protected:
+  void mergeAndTestRewrite(StringRef Code, StringRef Intermediate,
+                           StringRef Result, const Replacements &First,
+                           const Replacements &Second) {
+    // These are mainly to verify the test itself and make it easier to read.
+    std::string AfterFirst = applyAllReplacements(Code, First);
+    std::string InSequenceRewrite = applyAllReplacements(AfterFirst, Second);
+    EXPECT_EQ(Intermediate, AfterFirst);
+    EXPECT_EQ(Result, InSequenceRewrite);
+
+    tooling::Replacements Merged = mergeReplacements(First, Second);
+    std::string MergedRewrite = applyAllReplacements(Code, Merged);
+    EXPECT_EQ(InSequenceRewrite, MergedRewrite);
+    if (InSequenceRewrite != MergedRewrite)
+      for (tooling::Replacement M : Merged)
+        llvm::errs() << M.getOffset() << " " << M.getLength() << " "
+                     << M.getReplacementText() << "\n";
+  }
+  void mergeAndTestRewrite(StringRef Code, const Replacements &First,
+                           const Replacements &Second) {
+    std::string InSequenceRewrite =
+        applyAllReplacements(applyAllReplacements(Code, First), Second);
+    tooling::Replacements Merged = mergeReplacements(First, Second);
+    std::string MergedRewrite = applyAllReplacements(Code, Merged);
+    EXPECT_EQ(InSequenceRewrite, MergedRewrite);
+    if (InSequenceRewrite != MergedRewrite)
+      for (tooling::Replacement M : Merged)
+        llvm::errs() << M.getOffset() << " " << M.getLength() << " "
+                     << M.getReplacementText() << "\n";
+  }
+};
+
+TEST_F(MergeReplacementsTest, Offsets) {
+  mergeAndTestRewrite("aaa", "aabab", "cacabab",
+                      {{"", 2, 0, "b"}, {"", 3, 0, "b"}},
+                      {{"", 0, 0, "c"}, {"", 1, 0, "c"}});
+  mergeAndTestRewrite("aaa", "babaa", "babacac",
+                      {{"", 0, 0, "b"}, {"", 1, 0, "b"}},
+                      {{"", 4, 0, "c"}, {"", 5, 0, "c"}});
+  mergeAndTestRewrite("aaaa", "aaa", "aac", {{"", 1, 1, ""}},
+                      {{"", 2, 1, "c"}});
+
+  mergeAndTestRewrite("aa", "bbabba", "bbabcba",
+                      {{"", 0, 0, "bb"}, {"", 1, 0, "bb"}}, {{"", 4, 0, "c"}});
+}
+
+TEST_F(MergeReplacementsTest, Concatenations) {
+  // Basic concatenations. It is important to merge these into a single
+  // replacement to ensure the correct order.
+  EXPECT_EQ((Replacements{{"", 0, 0, "ab"}}),
+            mergeReplacements({{"", 0, 0, "a"}}, {{"", 1, 0, "b"}}));
+  EXPECT_EQ((Replacements{{"", 0, 0, "ba"}}),
+            mergeReplacements({{"", 0, 0, "a"}}, {{"", 0, 0, "b"}}));
+  mergeAndTestRewrite("", "a", "ab", {{"", 0, 0, "a"}}, {{"", 1, 0, "b"}});
+  mergeAndTestRewrite("", "a", "ba", {{"", 0, 0, "a"}}, {{"", 0, 0, "b"}});
+}
+
+TEST_F(MergeReplacementsTest, NotChangingLengths) {
+  mergeAndTestRewrite("aaaa", "abba", "acca", {{"", 1, 2, "bb"}},
+                      {{"", 1, 2, "cc"}});
+  mergeAndTestRewrite("aaaa", "abba", "abcc", {{"", 1, 2, "bb"}},
+                      {{"", 2, 2, "cc"}});
+  mergeAndTestRewrite("aaaa", "abba", "ccba", {{"", 1, 2, "bb"}},
+                      {{"", 0, 2, "cc"}});
+  mergeAndTestRewrite("aaaaaa", "abbdda", "abccda",
+                      {{"", 1, 2, "bb"}, {"", 3, 2, "dd"}}, {{"", 2, 2, "cc"}});
+}
+
+TEST_F(MergeReplacementsTest, OverlappingRanges) {
+  mergeAndTestRewrite("aaa", "bbd", "bcbcd",
+                      {{"", 0, 1, "bb"}, {"", 1, 2, "d"}},
+                      {{"", 1, 0, "c"}, {"", 2, 0, "c"}});
+
+  mergeAndTestRewrite("aaaa", "aabbaa", "acccca", {{"", 2, 0, "bb"}},
+                      {{"", 1, 4, "cccc"}});
+  mergeAndTestRewrite("aaaa", "aababa", "acccca",
+                      {{"", 2, 0, "b"}, {"", 3, 0, "b"}}, {{"", 1, 4, "cccc"}});
+  mergeAndTestRewrite("aaaaaa", "abbbba", "abba", {{"", 1, 4, "bbbb"}},
+                      {{"", 2, 2, ""}});
+  mergeAndTestRewrite("aaaa", "aa", "cc", {{"", 1, 1, ""}, {"", 2, 1, ""}},
+                      {{"", 0, 2, "cc"}});
+  mergeAndTestRewrite("aa", "abbba", "abcbcba", {{"", 1, 0, "bbb"}},
+                      {{"", 2, 0, "c"}, {"", 3, 0, "c"}});
+
+  mergeAndTestRewrite("aaa", "abbab", "ccdd",
+                      {{"", 0, 1, ""}, {"", 2, 0, "bb"}, {"", 3, 0, "b"}},
+                      {{"", 0, 2, "cc"}, {"", 2, 3, "dd"}});
+  mergeAndTestRewrite("aa", "babbab", "ccdd",
+                      {{"", 0, 0, "b"}, {"", 1, 0, "bb"}, {"", 2, 0, "b"}},
+                      {{"", 0, 3, "cc"}, {"", 3, 3, "dd"}});
+}
+
 } // end namespace tooling
 } // end namespace clang
Index: unittests/Format/SortIncludesTest.cpp
===================================================================
--- /dev/null
+++ unittests/Format/SortIncludesTest.cpp
@@ -0,0 +1,108 @@
+//===- unittest/Format/SortIncludesTest.cpp - Include sort unit tests -----===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "FormatTestUtils.h"
+#include "clang/Format/Format.h"
+#include "llvm/Support/Debug.h"
+#include "gtest/gtest.h"
+
+#define DEBUG_TYPE "format-test"
+
+namespace clang {
+namespace format {
+namespace {
+
+class SortIncludesTest : public ::testing::Test {
+protected:
+  std::string sort(llvm::StringRef Code) {
+    std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
+    std::string Sorted = applyAllReplacements(
+        Code, sortIncludes(getLLVMStyle(), Code, Ranges, "input.cpp"));
+    return applyAllReplacements(
+        Sorted, reformat(getLLVMStyle(), Sorted, Ranges, "input.cpp"));
+  }
+};
+
+TEST_F(SortIncludesTest, BasicSorting) {
+  EXPECT_EQ("#include \"a.h\"\n"
+            "#include \"b.h\"\n"
+            "#include \"c.h\"\n",
+            sort("#include \"a.h\"\n"
+                 "#include \"c.h\"\n"
+                 "#include \"b.h\"\n"));
+}
+
+TEST_F(SortIncludesTest, FixTrailingComments) {
+  EXPECT_EQ("#include \"a.h\"  // comment\n"
+            "#include \"bb.h\" // comment\n"
+            "#include \"ccc.h\"\n",
+            sort("#include \"a.h\" // comment\n"
+                 "#include \"ccc.h\"\n"
+                 "#include \"bb.h\" // comment\n"));
+}
+
+TEST_F(SortIncludesTest, LeadingWhitespace) {
+  EXPECT_EQ("#include \"a.h\"\n"
+            "#include \"b.h\"\n"
+            "#include \"c.h\"\n",
+            sort(" #include \"a.h\"\n"
+                 "  #include \"c.h\"\n"
+                 "   #include \"b.h\"\n"));
+  EXPECT_EQ("#include \"a.h\"\n"
+            "#include \"b.h\"\n"
+            "#include \"c.h\"\n",
+            sort("# include \"a.h\"\n"
+                 "#  include \"c.h\"\n"
+                 "#   include \"b.h\"\n"));
+}
+
+TEST_F(SortIncludesTest, GreaterInComment) {
+  EXPECT_EQ("#include \"a.h\"\n"
+            "#include \"b.h\" // >\n"
+            "#include \"c.h\"\n",
+            sort("#include \"a.h\"\n"
+                 "#include \"c.h\"\n"
+                 "#include \"b.h\" // >\n"));
+}
+
+TEST_F(SortIncludesTest, SortsLocallyInEachBlock) {
+  EXPECT_EQ("#include \"a.h\"\n"
+            "#include \"c.h\"\n"
+            "\n"
+            "#include \"b.h\"\n",
+            sort("#include \"c.h\"\n"
+                 "#include \"a.h\"\n"
+                 "\n"
+                 "#include \"b.h\"\n"));
+}
+
+TEST_F(SortIncludesTest, HandlesAngledIncludesAsSeparateBlocks) {
+  EXPECT_EQ("#include <b.h>\n"
+            "#include <d.h>\n"
+            "#include \"a.h\"\n"
+            "#include \"c.h\"\n",
+            sort("#include <d.h>\n"
+                 "#include <b.h>\n"
+                 "#include \"c.h\"\n"
+                 "#include \"a.h\"\n"));
+}
+
+TEST_F(SortIncludesTest, HandlesMultilineIncludes) {
+  EXPECT_EQ("#include \"a.h\"\n"
+            "#include \"b.h\"\n"
+            "#include \"c.h\"\n",
+            sort("#include \"a.h\"\n"
+                 "#include \\\n"
+                 "\"c.h\"\n"
+                 "#include \"b.h\"\n"));
+}
+
+} // end namespace
+} // end namespace format
+} // end namespace clang
Index: unittests/Format/CMakeLists.txt
===================================================================
--- unittests/Format/CMakeLists.txt
+++ unittests/Format/CMakeLists.txt
@@ -8,6 +8,7 @@
   FormatTestJS.cpp
   FormatTestProto.cpp
   FormatTestSelective.cpp
+  SortIncludesTest.cpp
   )
 
 target_link_libraries(FormatTests
Index: tools/clang-format/clang-format.py
===================================================================
--- tools/clang-format/clang-format.py
+++ tools/clang-format/clang-format.py
@@ -72,7 +72,7 @@
     startupinfo.wShowWindow = subprocess.SW_HIDE
 
   # Call formatter.
-  command = [binary, '-style', style, '-cursor', str(cursor)]
+  command = [binary, '-style', style, '-cursor', str(cursor), '-sort-includes']
   if lines != 'all':
     command.extend(['-lines', lines])
   if fallback_style:
Index: tools/clang-format/ClangFormat.cpp
===================================================================
--- tools/clang-format/ClangFormat.cpp
+++ tools/clang-format/ClangFormat.cpp
@@ -19,14 +19,14 @@
 #include "clang/Basic/SourceManager.h"
 #include "clang/Basic/Version.h"
 #include "clang/Format/Format.h"
-#include "clang/Rewrite/Core/Rewriter.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Signals.h"
 
 using namespace llvm;
+using clang::tooling::Replacements;
 
 static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden);
 
@@ -97,6 +97,10 @@
                     "clang-format from an editor integration"),
            cl::init(0), cl::cat(ClangFormatCategory));
 
+static cl::opt<bool> SortIncludes("sort-includes",
+                                  cl::desc("Sort touched include lines"),
+                                  cl::cat(ClangFormatCategory));
+
 static cl::list<std::string> FileNames(cl::Positional, cl::desc("[<file> ...]"),
                                        cl::cat(ClangFormatCategory));
 
@@ -121,9 +125,14 @@
          LineRange.second.getAsInteger(0, ToLine);
 }
 
-static bool fillRanges(SourceManager &Sources, FileID ID,
-                       const MemoryBuffer *Code,
-                       std::vector<CharSourceRange> &Ranges) {
+static bool fillRanges(MemoryBuffer *Code,
+                       std::vector<tooling::Range> &Ranges) {
+  FileManager Files((FileSystemOptions()));
+  DiagnosticsEngine Diagnostics(
+      IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
+      new DiagnosticOptions);
+  SourceManager Sources(Diagnostics, Files);
+  FileID ID = createInMemoryFile("-", Code, Sources, Files);
   if (!LineRanges.empty()) {
     if (!Offsets.empty() || !Lengths.empty()) {
       llvm::errs() << "error: cannot use -lines with -offset/-length\n";
@@ -144,7 +153,9 @@
       SourceLocation End = Sources.translateLineCol(ID, ToLine, UINT_MAX);
       if (Start.isInvalid() || End.isInvalid())
         return true;
-      Ranges.push_back(CharSourceRange::getCharRange(Start, End));
+      unsigned Offset = Sources.getFileOffset(Start);
+      unsigned Length = Sources.getFileOffset(End) - Offset;
+      Ranges.push_back(tooling::Range(Offset, Length));
     }
     return false;
   }
@@ -177,7 +188,9 @@
     } else {
       End = Sources.getLocForEndOfFile(ID);
     }
-    Ranges.push_back(CharSourceRange::getCharRange(Start, End));
+    unsigned Offset = Sources.getFileOffset(Start);
+    unsigned Length = Sources.getFileOffset(End) - Offset;
+    Ranges.push_back(tooling::Range(Offset, Length));
   }
   return false;
 }
@@ -202,13 +215,18 @@
   llvm::outs() << Text.substr(From);
 }
 
+static void outputReplacementsXML(const Replacements &Replaces) {
+  for (const auto &R : Replaces) {
+    outs() << "<replacement "
+           << "offset='" << R.getOffset() << "' "
+           << "length='" << R.getLength() << "'>";
+    outputReplacementXML(R.getReplacementText());
+    outs() << "</replacement>\n";
+  }
+}
+
 // Returns true on error.
 static bool format(StringRef FileName) {
-  FileManager Files((FileSystemOptions()));
-  DiagnosticsEngine Diagnostics(
-      IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
-      new DiagnosticOptions);
-  SourceManager Sources(Diagnostics, Files);
   ErrorOr<std::unique_ptr<MemoryBuffer>> CodeOrErr =
       MemoryBuffer::getFileOrSTDIN(FileName);
   if (std::error_code EC = CodeOrErr.getError()) {
@@ -218,16 +236,27 @@
   std::unique_ptr<llvm::MemoryBuffer> Code = std::move(CodeOrErr.get());
   if (Code->getBufferSize() == 0)
     return false; // Empty files are formatted correctly.
-  FileID ID = createInMemoryFile(FileName, Code.get(), Sources, Files);
-  std::vector<CharSourceRange> Ranges;
-  if (fillRanges(Sources, ID, Code.get(), Ranges))
+  std::vector<tooling::Range> Ranges;
+  if (fillRanges(Code.get(), Ranges))
     return true;
-
   FormatStyle FormatStyle = getStyle(
       Style, (FileName == "-") ? AssumeFilename : FileName, FallbackStyle);
+  Replacements Replaces;
+  std::string ChangedCode;
+  if (SortIncludes) {
+    Replaces =
+        sortIncludes(FormatStyle, Code->getBuffer(), Ranges, FileName);
+    ChangedCode = tooling::applyAllReplacements(Code->getBuffer(), Replaces);
+    for (const auto &R : Replaces)
+      Ranges.push_back({R.getOffset(), R.getLength()});
+  } else {
+    ChangedCode = Code->getBuffer().str();
+  }
+
   bool IncompleteFormat = false;
-  tooling::Replacements Replaces =
-      reformat(FormatStyle, Sources, ID, Ranges, &IncompleteFormat);
+  Replaces = tooling::mergeReplacements(
+      Replaces,
+      reformat(FormatStyle, ChangedCode, Ranges, FileName, &IncompleteFormat));
   if (OutputXML) {
     llvm::outs() << "<?xml version='1.0'?>\n<replacements "
                     "xml:space='preserve' incomplete_format='"
@@ -237,31 +266,30 @@
                    << tooling::shiftedCodePosition(Replaces, Cursor)
                    << "</cursor>\n";
 
-    for (tooling::Replacements::const_iterator I = Replaces.begin(),
-                                               E = Replaces.end();
-         I != E; ++I) {
-      llvm::outs() << "<replacement "
-                   << "offset='" << I->getOffset() << "' "
-                   << "length='" << I->getLength() << "'>";
-      outputReplacementXML(I->getReplacementText());
-      llvm::outs() << "</replacement>\n";
-    }
+    outputReplacementsXML(Replaces); 
     llvm::outs() << "</replacements>\n";
   } else {
-    Rewriter Rewrite(Sources, LangOptions());
-    tooling::applyAllReplacements(Replaces, Rewrite);
+    std::string FormattedCode =
+        applyAllReplacements(Code->getBuffer(), Replaces);
     if (Inplace) {
       if (FileName == "-")
         llvm::errs() << "error: cannot use -i when reading from stdin.\n";
-      else if (Rewrite.overwriteChangedFiles())
-        return true;
+      else {
+        std::error_code EC;
+        raw_fd_ostream FileOut(FileName, EC, llvm::sys::fs::F_Text);
+        if (EC) {
+          llvm::errs() << EC.message() << "\n";
+          return true;
+        }
+        FileOut << FormattedCode;
+      }
     } else {
       if (Cursor.getNumOccurrences() != 0)
         outs() << "{ \"Cursor\": "
                << tooling::shiftedCodePosition(Replaces, Cursor)
                << ", \"IncompleteFormat\": "
                << (IncompleteFormat ? "true" : "false") << " }\n";
-      Rewrite.getEditBuffer(ID).write(outs());
+      outs() << FormattedCode;
     }
   }
   return false;
Index: tools/clang-format/CMakeLists.txt
===================================================================
--- tools/clang-format/CMakeLists.txt
+++ tools/clang-format/CMakeLists.txt
@@ -7,7 +7,6 @@
 set(CLANG_FORMAT_LIB_DEPS
   clangBasic
   clangFormat
-  clangRewrite
   clangToolingCore
   )
 
Index: lib/Tooling/Core/Replacement.cpp
===================================================================
--- lib/Tooling/Core/Replacement.cpp
+++ lib/Tooling/Core/Replacement.cpp
@@ -292,6 +292,97 @@
   return Result;
 }
 
+Replacements mergeReplacements(const Replacements &First,
+                               const Replacements &Second) {
+  if (First.empty() || Second.empty())
+    return First.empty() ? Second : First;
+
+  // Delta is the amount of characters that replacements from 'Second' need to
+  // be shifted by due to replacements from 'First'.
+  int Delta = 0;
+
+  Replacements Result;
+  // Iterate over both sets and always add the next element (smallest total
+  // Offset) from either 'First' or 'Second'. Merge that element with
+  // subsequent replacements as long as they overlap.
+  for (auto FirstI = First.begin(), SecondI = Second.begin();
+       FirstI != First.end() || SecondI != Second.end();) {
+    // 'MergeSecond' is true, if an element from 'Second' needs to be merged
+    // next, and false if an element from 'First' shoud be merged. As the input
+    // replacements are non-overlapping, we always need to merge an element
+    // from 'Second' into an element from 'First' or vice versa.
+    bool MergeSecond = SecondI == Second.end() ||
+                       FirstI->getOffset() < SecondI->getOffset() + Delta;
+    const Replacement &R = MergeSecond ? *FirstI : *SecondI;
+    if (MergeSecond)
+      ++FirstI;
+    else
+      ++SecondI;
+
+    // The combined data of the current merge starting with the first element.
+    unsigned MergedOffset = R.getOffset() + (MergeSecond ? 0 : Delta);
+    unsigned MergedLength = R.getLength();
+    std::string MergedText = R.getReplacementText();
+
+    // Deltas (i.e. Text.size() - Length) of replacements from 'First' and
+    // 'Second' during the current merge sequence. Within the same merge,
+    // replacements from 'Second' affect later replacements from 'Second'
+    // whereas replacements from 'First' (i.e. DeltaFirst) only affects
+    // subsequent, non-overlapping replacements.
+    int DeltaFirst = MergeSecond ? MergedText.size() - MergedLength : 0;
+    int DeltaSecond = MergeSecond ? 0 : MergedText.size() - MergedLength;
+    while ((MergeSecond && SecondI != Second.end()) ||
+           (!MergeSecond && FirstI != First.end())) {
+      if (MergeSecond) {
+        // If we are merging an element from 'Second', the length of the
+        // previous replacement text determines the end of the merge as
+        // elements from 'Second' refer to the replaced text.
+        unsigned MergedEnd = MergedOffset + MergedText.size();
+        unsigned Offset = SecondI->getOffset() + DeltaSecond + Delta;
+        if (Offset > MergedEnd)
+          break;
+
+        unsigned Length = SecondI->getLength();
+        unsigned End = Offset + SecondI->getLength();
+        if (End > MergedEnd) {
+          MergedLength += End - MergedEnd;
+          MergeSecond = false;
+        }
+        StringRef MergedRef = MergedText;
+        StringRef Head = MergedRef.substr(0, Offset - MergedOffset);
+        StringRef Tail = MergedRef.substr(End - MergedOffset);
+        MergedText = (Twine(Head) + SecondI->getReplacementText() + Tail).str();
+        DeltaSecond += SecondI->getReplacementText().size() - Length;
+        ++SecondI;
+      } else {
+        // If we are merging an element from 'First', the actual length of the
+        // merge (i.e. the number of replaced characters) determines the end of
+        // the merge as an element from 'First' refers to the original text.
+        unsigned MergedEnd = MergedOffset + MergedLength;
+        unsigned Offset = FirstI->getOffset();
+        if (Offset > MergedEnd)
+          break;
+
+        unsigned Length = FirstI->getLength();
+        StringRef Text = FirstI->getReplacementText();
+        StringRef Tail = Text.substr(MergedEnd - Offset);
+        MergedText = (Twine(MergedText) + Tail).str();
+        if (Offset + Text.size() > MergedEnd) {
+          MergedLength = Offset + Length - MergedOffset;
+          MergeSecond = true;
+        } else {
+          MergedLength += Length - Text.size();
+        }
+        DeltaFirst += Text.size() - Length;
+        ++FirstI;
+      }
+    }
+    Delta -= DeltaFirst;
+    Result.insert({R.getFilePath(), MergedOffset, MergedLength, MergedText});
+  }
+  return Result;
+}
+
 } // end namespace tooling
 } // end namespace clang
 
Index: lib/Format/FormatToken.cpp
===================================================================
--- lib/Format/FormatToken.cpp
+++ lib/Format/FormatToken.cpp
@@ -13,8 +13,8 @@
 ///
 //===----------------------------------------------------------------------===//
 
-#include "FormatToken.h"
 #include "ContinuationIndenter.h"
+#include "FormatToken.h"
 #include "clang/Format/Format.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/Support/Debug.h"
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -310,8 +310,8 @@
     return Seq[Index];
   }
 };
-}
-}
+} // namespace yaml
+} // namespace llvm
 
 namespace clang {
 namespace format {
@@ -1573,8 +1573,107 @@
   bool BinPackInconclusiveFunctions;
 };
 
+struct IncludeDirective {
+  StringRef Filename;
+  StringRef Text;
+  unsigned Offset;
+  bool IsAngled;
+};
+
 } // end anonymous namespace
 
+// Determines whether 'Ranges' intersects with ('Start', 'End').
+static bool affectsRange(ArrayRef<tooling::Range> Ranges, unsigned Start,
+                         unsigned End) {
+  for (auto Range : Ranges) {
+    if (Range.getOffset() < End &&
+        Range.getOffset() + Range.getLength() > Start)
+      return true;
+  }
+  return false;
+}
+
+// Sorts a block of includes given by 'Includes' alphabetically adding the
+// necessary replacement to 'Replaces'. 'Includes' must be in strict source
+// order.
+static void sortIncludes(const FormatStyle &Style,
+                         const SmallVectorImpl<IncludeDirective> &Includes,
+                         ArrayRef<tooling::Range> Ranges, StringRef FileName,
+                         tooling::Replacements &Replaces) {
+  if (Includes.empty() ||
+      !affectsRange(Ranges, Includes.front().Offset,
+                    Includes.back().Offset + Includes.back().Text.size()))
+    return;
+  SmallVector<unsigned, 16> Indices;
+  for (unsigned i = 0, e = Includes.size(); i != e; ++i)
+    Indices.push_back(i);
+  std::sort(Indices.begin(), Indices.end(), [&](unsigned LHSI, unsigned RHSI) {
+    return Includes[LHSI].Filename < Includes[RHSI].Filename;
+  });
+
+  bool OutOfOrder = false;
+  for (unsigned i = 1, e = Indices.size(); i != e; ++i) {
+    if (Indices[i] != i) {
+      OutOfOrder = true;
+      break;
+    }
+  }
+  if (!OutOfOrder)
+    return;
+
+  std::string result = Includes[Indices[0]].Text;
+  for (unsigned i = 1, e = Indices.size(); i != e; ++i) {
+    result += "\n";
+    result += Includes[Indices[i]].Text;
+  }
+
+  // Sorting #includes shouldn't change their total number of characters.
+  // This would otherwise mess up 'Ranges'.
+  assert(result.size() ==
+         Includes.back().Offset + Includes.back().Text.size() -
+             Includes.front().Offset);
+
+  Replaces.insert(tooling::Replacement(FileName, Includes.front().Offset,
+                                       result.size(), result));
+}
+
+tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code,
+                                   ArrayRef<tooling::Range> Ranges,
+                                   StringRef FileName) {
+  tooling::Replacements Replaces;
+  unsigned Prev = 0;
+  unsigned SearchFrom = 0;
+  llvm::Regex IncludeRegex(R"(^\ *#\ *include[^"<]*["<]([^">]*)([">]))");
+  SmallVector<StringRef, 4> Matches;
+  SmallVector<IncludeDirective, 16> IncludesInBlock;
+  for (;;) {
+    auto Pos = Code.find('\n', SearchFrom);
+    StringRef Line =
+        Code.substr(Prev, (Pos != StringRef::npos ? Pos : Code.size()) - Prev);
+    if (!Line.endswith("\\")) {
+      if (IncludeRegex.match(Line, &Matches)) {
+        bool IsAngled = Matches[2] == ">";
+        if (!IncludesInBlock.empty() &&
+            IsAngled != IncludesInBlock.back().IsAngled) {
+          sortIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces);
+          IncludesInBlock.clear();
+        }
+        IncludesInBlock.push_back({Matches[1], Line, Prev, Matches[2] == ">"});
+      } else {
+        sortIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces);
+        IncludesInBlock.clear();
+      }
+      Prev = Pos + 1;
+    }
+    if (Pos == StringRef::npos || Pos + 1 == Code.size())
+      break;
+    SearchFrom = Pos + 1;
+  }
+  if (!IncludesInBlock.empty())
+    sortIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces);
+  return Replaces;
+}
+
 tooling::Replacements reformat(const FormatStyle &Style,
                                SourceManager &SourceMgr, FileID ID,
                                ArrayRef<CharSourceRange> Ranges,
Index: include/clang/Tooling/Core/Replacement.h
===================================================================
--- include/clang/Tooling/Core/Replacement.h
+++ include/clang/Tooling/Core/Replacement.h
@@ -220,6 +220,11 @@
 /// replacements cannot be applied, this returns an empty \c string.
 std::string applyAllReplacements(StringRef Code, const Replacements &Replaces);
 
+/// \brief Merges to sets of replacements with the second set referring to the
+/// code after applying the first set.
+Replacements mergeReplacements(const Replacements &First,
+                               const Replacements &Second);
+
 template <typename Node>
 Replacement::Replacement(const SourceManager &Sources,
                          const Node &NodeToReplace, StringRef ReplacementText,
Index: include/clang/Format/Format.h
===================================================================
--- include/clang/Format/Format.h
+++ include/clang/Format/Format.h
@@ -571,6 +571,12 @@
 /// \brief Gets configuration in a YAML string.
 std::string configurationAsText(const FormatStyle &Style);
 
+/// \brief Returns the replacements necessary to sort all #include blocks that
+/// are affected by 'Ranges'.
+tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code,
+                                   ArrayRef<tooling::Range> Ranges,
+                                   StringRef FileName);
+
 /// \brief Reformats the given \p Ranges in the file \p ID.
 ///
 /// Each range is extended on either end to its next bigger logic unit, i.e.
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to