From b90bec812a0e687c091b14a223613bd777736e96 Mon Sep 17 00:00:00 2001
From: Argyrios Kyrtzidis <akyrtzi@gmail.com>
Date: Sun, 8 Mar 2015 04:00:33 +0000
Subject: [PATCH] [Rewrite] Make RewriteBuffer accessible on its own, and add a
 unit test for it.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@231588 91177308-0d34-0410-b5e6-96231b3b80d8
---
 include/clang/Rewrite/Core/RewriteBuffer.h | 16 ++++---
 lib/Rewrite/Rewriter.cpp                   |  2 +-
 unittests/CMakeLists.txt                   |  1 +
 unittests/Makefile                         |  2 +-
 unittests/Rewrite/CMakeLists.txt           | 10 +++++
 unittests/Rewrite/Makefile                 | 16 +++++++
 unittests/Rewrite/RewriteBufferTest.cpp    | 51 ++++++++++++++++++++++
 7 files changed, 90 insertions(+), 8 deletions(-)
 create mode 100644 unittests/Rewrite/CMakeLists.txt
 create mode 100644 unittests/Rewrite/Makefile
 create mode 100644 unittests/Rewrite/RewriteBufferTest.cpp

diff --git a/include/clang/Rewrite/Core/RewriteBuffer.h b/include/clang/Rewrite/Core/RewriteBuffer.h
index 25267afc410..d69c69b81e4 100644
--- a/include/clang/Rewrite/Core/RewriteBuffer.h
+++ b/include/clang/Rewrite/Core/RewriteBuffer.h
@@ -10,6 +10,7 @@
 #ifndef LLVM_CLANG_REWRITE_CORE_REWRITEBUFFER_H
 #define LLVM_CLANG_REWRITE_CORE_REWRITEBUFFER_H
 
+#include "clang/Basic/LLVM.h"
 #include "clang/Rewrite/Core/DeltaTree.h"
 #include "clang/Rewrite/Core/RewriteRope.h"
 #include "llvm/ADT/StringRef.h"
@@ -35,6 +36,15 @@ public:
   iterator end() const { return Buffer.end(); }
   unsigned size() const { return Buffer.size(); }
 
+  /// Initialize - Start this rewrite buffer out with a copy of the unmodified
+  /// input buffer.
+  void Initialize(const char *BufStart, const char *BufEnd) {
+    Buffer.assign(BufStart, BufEnd);
+  }
+  void Initialize(StringRef Input) {
+    Initialize(Input.begin(), Input.end());
+  }
+
   /// \brief Write to \p Stream the result of applying all changes to the
   /// original buffer.
   /// Note that it isn't safe to use this function to overwrite memory mapped
@@ -79,12 +89,6 @@ public:
 
 private:  // Methods only usable by Rewriter.
 
-  /// Initialize - Start this rewrite buffer out with a copy of the unmodified
-  /// input buffer.
-  void Initialize(const char *BufStart, const char *BufEnd) {
-    Buffer.assign(BufStart, BufEnd);
-  }
-
   /// getMappedOffset - Given an offset into the original SourceBuffer that this
   /// RewriteBuffer is based on, map it into the offset space of the
   /// RewriteBuffer.  If AfterInserts is true and if the OrigOffset indicates a
diff --git a/lib/Rewrite/Rewriter.cpp b/lib/Rewrite/Rewriter.cpp
index 60cdcf70317..be09a363a61 100644
--- a/lib/Rewrite/Rewriter.cpp
+++ b/lib/Rewrite/Rewriter.cpp
@@ -54,7 +54,7 @@ void RewriteBuffer::RemoveText(unsigned OrigOffset, unsigned Size,
   if (Size == 0) return;
 
   unsigned RealOffset = getMappedOffset(OrigOffset, true);
-  assert(RealOffset+Size < Buffer.size() && "Invalid location");
+  assert(RealOffset+Size <= Buffer.size() && "Invalid location");
 
   // Remove the dead characters.
   Buffer.erase(RealOffset, Size);
diff --git a/unittests/CMakeLists.txt b/unittests/CMakeLists.txt
index bc55cdf4bf9..0e67ec71537 100644
--- a/unittests/CMakeLists.txt
+++ b/unittests/CMakeLists.txt
@@ -20,6 +20,7 @@ add_subdirectory(ASTMatchers)
 add_subdirectory(AST)
 add_subdirectory(Tooling)
 add_subdirectory(Format)
+add_subdirectory(Rewrite)
 add_subdirectory(Sema)
 add_subdirectory(CodeGen)
 # FIXME: Why are the libclang unit tests disabled on Windows?
diff --git a/unittests/Makefile b/unittests/Makefile
index 07c0d0e0871..2a0b5bc9dd4 100644
--- a/unittests/Makefile
+++ b/unittests/Makefile
@@ -15,7 +15,7 @@ ifndef CLANG_LEVEL
 IS_UNITTEST_LEVEL := 1
 CLANG_LEVEL := ..
 PARALLEL_DIRS = CodeGen Basic Lex Driver Format ASTMatchers AST Tooling \
-	        Sema
+	        Rewrite Sema
 
 include $(CLANG_LEVEL)/../..//Makefile.config
 
diff --git a/unittests/Rewrite/CMakeLists.txt b/unittests/Rewrite/CMakeLists.txt
new file mode 100644
index 00000000000..bee7ff6d554
--- /dev/null
+++ b/unittests/Rewrite/CMakeLists.txt
@@ -0,0 +1,10 @@
+set(LLVM_LINK_COMPONENTS
+  Support
+  )
+
+add_clang_unittest(RewriteTests
+  RewriteBufferTest.cpp
+  )
+target_link_libraries(RewriteTests
+  clangRewrite
+  )
diff --git a/unittests/Rewrite/Makefile b/unittests/Rewrite/Makefile
new file mode 100644
index 00000000000..43538d56075
--- /dev/null
+++ b/unittests/Rewrite/Makefile
@@ -0,0 +1,16 @@
+##===- unittests/Rewrite/Makefile --------------------------*- Makefile -*-===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+
+CLANG_LEVEL = ../..
+TESTNAME = Rewrite
+include $(CLANG_LEVEL)/../../Makefile.config
+LINK_COMPONENTS := $(TARGETS_TO_BUILD) support
+USEDLIBS = clangRewrite.a clangLex.a clangBasic.a
+
+include $(CLANG_LEVEL)/unittests/Makefile
diff --git a/unittests/Rewrite/RewriteBufferTest.cpp b/unittests/Rewrite/RewriteBufferTest.cpp
new file mode 100644
index 00000000000..e3b7d1fb889
--- /dev/null
+++ b/unittests/Rewrite/RewriteBufferTest.cpp
@@ -0,0 +1,51 @@
+//===- unittests/Rewrite/RewriteBufferTest.cpp - RewriteBuffer tests ------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Rewrite/Core/RewriteBuffer.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+using namespace clang;
+
+namespace {
+
+static void tagRange(unsigned Offset, unsigned Len, StringRef tagName,
+                     RewriteBuffer &Buf) {
+  std::string BeginTag;
+  raw_string_ostream(BeginTag) << '<' << tagName << '>';
+  std::string EndTag;
+  raw_string_ostream(EndTag) << "</" << tagName << '>';
+
+  Buf.InsertTextAfter(Offset, BeginTag);
+  Buf.InsertTextBefore(Offset+Len, EndTag);
+}
+
+TEST(RewriteBuffer, TagRanges) {
+  StringRef Input = "hello world";
+  const char *Output = "<outer><inner>hello</inner></outer> ";
+
+  RewriteBuffer Buf;
+  Buf.Initialize(Input);
+  StringRef RemoveStr = "world";
+  size_t Pos = Input.find(RemoveStr);
+  Buf.RemoveText(Pos, RemoveStr.size());
+
+  StringRef TagStr = "hello";
+  Pos = Input.find(TagStr);
+  tagRange(Pos, TagStr.size(), "outer", Buf);
+  tagRange(Pos, TagStr.size(), "inner", Buf);
+
+  std::string Result;
+  raw_string_ostream OS(Result);
+  Buf.write(OS);
+  OS.flush();
+  EXPECT_EQ(Output, Result);
+}
+
+} // anonymous namespace
-- 
GitLab