#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"
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
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
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);
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?
IS_UNITTEST_LEVEL := 1
CLANG_LEVEL := ..
PARALLEL_DIRS = CodeGen Basic Lex Driver Format ASTMatchers AST Tooling \
- Sema
+ Rewrite Sema
include $(CLANG_LEVEL)/../..//Makefile.config
--- /dev/null
+set(LLVM_LINK_COMPONENTS
+ Support
+ )
+
+add_clang_unittest(RewriteTests
+ RewriteBufferTest.cpp
+ )
+target_link_libraries(RewriteTests
+ clangRewrite
+ )
--- /dev/null
+##===- 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
--- /dev/null
+//===- 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