mirror of
https://github.com/capstone-engine/llvm-capstone.git
synced 2025-02-25 13:05:04 +00:00
[Rewrite] Make RewriteBuffer accessible on its own, and add a unit test for it.
llvm-svn: 231588
This commit is contained in:
parent
fe0d61d245
commit
69e6f1f257
@ -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
|
||||
|
@ -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);
|
||||
|
@ -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?
|
||||
|
@ -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
|
||||
|
||||
|
10
clang/unittests/Rewrite/CMakeLists.txt
Normal file
10
clang/unittests/Rewrite/CMakeLists.txt
Normal file
@ -0,0 +1,10 @@
|
||||
set(LLVM_LINK_COMPONENTS
|
||||
Support
|
||||
)
|
||||
|
||||
add_clang_unittest(RewriteTests
|
||||
RewriteBufferTest.cpp
|
||||
)
|
||||
target_link_libraries(RewriteTests
|
||||
clangRewrite
|
||||
)
|
16
clang/unittests/Rewrite/Makefile
Normal file
16
clang/unittests/Rewrite/Makefile
Normal 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
|
51
clang/unittests/Rewrite/RewriteBufferTest.cpp
Normal file
51
clang/unittests/Rewrite/RewriteBufferTest.cpp
Normal 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
|
Loading…
x
Reference in New Issue
Block a user