From 40884c052000b29d0f45f804cafc66e44c91367a Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Fri, 16 Apr 2010 18:49:45 +0000 Subject: [PATCH] Add a write(raw_ostream&) method to RewriteBuffer. This uses an inefficient implementation today but is the right place if we want to make it faster some day. llvm-svn: 101521 --- clang/include/clang/Rewrite/Rewriter.h | 14 +++++++++----- clang/lib/Frontend/FixItRewriter.cpp | 2 +- clang/lib/Rewrite/Rewriter.cpp | 8 ++++++-- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/clang/include/clang/Rewrite/Rewriter.h b/clang/include/clang/Rewrite/Rewriter.h index d99ca8bb1fb9..adda86699965 100644 --- a/clang/include/clang/Rewrite/Rewriter.h +++ b/clang/include/clang/Rewrite/Rewriter.h @@ -16,13 +16,15 @@ #define LLVM_CLANG_REWRITER_H #include "clang/Basic/SourceLocation.h" -#include "clang/Rewrite/RewriteRope.h" -#include -#include -#include -#include #include "clang/Rewrite/DeltaTree.h" +#include "clang/Rewrite/RewriteRope.h" #include "llvm/ADT/StringRef.h" +#include +#include +#include +#include + +namespace llvm { class raw_ostream; } namespace clang { class LangOptions; @@ -53,6 +55,8 @@ public: iterator end() const { return Buffer.end(); } unsigned size() const { return Buffer.size(); } + llvm::raw_ostream &write(llvm::raw_ostream &) const; + /// RemoveText - Remove the specified text. void RemoveText(unsigned OrigOffset, unsigned Size); diff --git a/clang/lib/Frontend/FixItRewriter.cpp b/clang/lib/Frontend/FixItRewriter.cpp index b26401212f41..7aff92340eae 100644 --- a/clang/lib/Frontend/FixItRewriter.cpp +++ b/clang/lib/Frontend/FixItRewriter.cpp @@ -39,7 +39,7 @@ FixItRewriter::~FixItRewriter() { bool FixItRewriter::WriteFixedFile(FileID ID, llvm::raw_ostream &OS) { const RewriteBuffer *RewriteBuf = Rewrite.getRewriteBufferFor(ID); if (!RewriteBuf) return true; - OS << std::string(RewriteBuf->begin(), RewriteBuf->end()); + RewriteBuf->write(OS); OS.flush(); return false; } diff --git a/clang/lib/Rewrite/Rewriter.cpp b/clang/lib/Rewrite/Rewriter.cpp index bf8ba701a842..376678a5d74e 100644 --- a/clang/lib/Rewrite/Rewriter.cpp +++ b/clang/lib/Rewrite/Rewriter.cpp @@ -20,6 +20,12 @@ #include "llvm/Support/raw_ostream.h" using namespace clang; +llvm::raw_ostream &RewriteBuffer::write(llvm::raw_ostream &os) const { + // FIXME: eliminate the copy by writing out each chunk at a time + os << std::string(begin(), end()); + return os; +} + void RewriteBuffer::RemoveText(unsigned OrigOffset, unsigned Size) { // Nothing to remove, exit early. if (Size == 0) return; @@ -222,5 +228,3 @@ bool Rewriter::ReplaceStmt(Stmt *From, Stmt *To) { ReplaceText(From->getLocStart(), Size, Str); return false; } - -