r231588 - [Rewrite] Make RewriteBuffer accessible on its own, and add a unit test for it.
Argyrios Kyrtzidis
akyrtzi at gmail.com
Sat Mar 7 20:00:34 PST 2015
Author: akirtzidis
Date: Sat Mar 7 22:00:33 2015
New Revision: 231588
URL: http://llvm.org/viewvc/llvm-project?rev=231588&view=rev
Log:
[Rewrite] Make RewriteBuffer accessible on its own, and add a unit test for it.
Added:
cfe/trunk/unittests/Rewrite/
cfe/trunk/unittests/Rewrite/CMakeLists.txt
cfe/trunk/unittests/Rewrite/Makefile
cfe/trunk/unittests/Rewrite/RewriteBufferTest.cpp
Modified:
cfe/trunk/include/clang/Rewrite/Core/RewriteBuffer.h
cfe/trunk/lib/Rewrite/Rewriter.cpp
cfe/trunk/unittests/CMakeLists.txt
cfe/trunk/unittests/Makefile
Modified: cfe/trunk/include/clang/Rewrite/Core/RewriteBuffer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Rewrite/Core/RewriteBuffer.h?rev=231588&r1=231587&r2=231588&view=diff
==============================================================================
--- cfe/trunk/include/clang/Rewrite/Core/RewriteBuffer.h (original)
+++ cfe/trunk/include/clang/Rewrite/Core/RewriteBuffer.h Sat Mar 7 22:00:33 2015
@@ -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
Modified: cfe/trunk/lib/Rewrite/Rewriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Rewrite/Rewriter.cpp?rev=231588&r1=231587&r2=231588&view=diff
==============================================================================
--- cfe/trunk/lib/Rewrite/Rewriter.cpp (original)
+++ cfe/trunk/lib/Rewrite/Rewriter.cpp Sat Mar 7 22:00:33 2015
@@ -54,7 +54,7 @@ void RewriteBuffer::RemoveText(unsigned
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);
Modified: cfe/trunk/unittests/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/CMakeLists.txt?rev=231588&r1=231587&r2=231588&view=diff
==============================================================================
--- cfe/trunk/unittests/CMakeLists.txt (original)
+++ cfe/trunk/unittests/CMakeLists.txt Sat Mar 7 22:00:33 2015
@@ -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?
Modified: cfe/trunk/unittests/Makefile
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Makefile?rev=231588&r1=231587&r2=231588&view=diff
==============================================================================
--- cfe/trunk/unittests/Makefile (original)
+++ cfe/trunk/unittests/Makefile Sat Mar 7 22:00:33 2015
@@ -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
Added: cfe/trunk/unittests/Rewrite/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Rewrite/CMakeLists.txt?rev=231588&view=auto
==============================================================================
--- cfe/trunk/unittests/Rewrite/CMakeLists.txt (added)
+++ cfe/trunk/unittests/Rewrite/CMakeLists.txt Sat Mar 7 22:00:33 2015
@@ -0,0 +1,10 @@
+set(LLVM_LINK_COMPONENTS
+ Support
+ )
+
+add_clang_unittest(RewriteTests
+ RewriteBufferTest.cpp
+ )
+target_link_libraries(RewriteTests
+ clangRewrite
+ )
Added: cfe/trunk/unittests/Rewrite/Makefile
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Rewrite/Makefile?rev=231588&view=auto
==============================================================================
--- cfe/trunk/unittests/Rewrite/Makefile (added)
+++ cfe/trunk/unittests/Rewrite/Makefile Sat Mar 7 22:00:33 2015
@@ -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
Added: cfe/trunk/unittests/Rewrite/RewriteBufferTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Rewrite/RewriteBufferTest.cpp?rev=231588&view=auto
==============================================================================
--- cfe/trunk/unittests/Rewrite/RewriteBufferTest.cpp (added)
+++ cfe/trunk/unittests/Rewrite/RewriteBufferTest.cpp Sat Mar 7 22:00:33 2015
@@ -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
More information about the cfe-commits
mailing list