[Rewrite] Make RewriteBuffer accessible on its own, and add a unit test for it.

llvm-svn: 231588
This commit is contained in:
Argyrios Kyrtzidis 2015-03-08 04:00:33 +00:00
parent fe0d61d245
commit 69e6f1f257
7 changed files with 90 additions and 8 deletions

View File

@ -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

View File

@ -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);

View File

@ -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?

View File

@ -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

View File

@ -0,0 +1,10 @@
set(LLVM_LINK_COMPONENTS
Support
)
add_clang_unittest(RewriteTests
RewriteBufferTest.cpp
)
target_link_libraries(RewriteTests
clangRewrite
)

View File

@ -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

View File

@ -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