aboutsummaryrefslogtreecommitdiff
path: root/Rewrite/Rewriter.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2007-10-17 22:35:30 +0000
committerChris Lattner <sabre@nondot.org>2007-10-17 22:35:30 +0000
commit01c5748c29e75b29cab5fc7d8ad1b173b29c7ecf (patch)
tree3f625a44b7a88371f36b44fb582f8d35608cd43d /Rewrite/Rewriter.cpp
parent5075477aad33b82b3657e5363459045e230f865d (diff)
Add new API to rewrite one stmt/expr with another.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@43101 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'Rewrite/Rewriter.cpp')
-rw-r--r--Rewrite/Rewriter.cpp22
1 files changed, 22 insertions, 0 deletions
diff --git a/Rewrite/Rewriter.cpp b/Rewrite/Rewriter.cpp
index 514b82235a..9e7d1b3143 100644
--- a/Rewrite/Rewriter.cpp
+++ b/Rewrite/Rewriter.cpp
@@ -13,8 +13,10 @@
//===----------------------------------------------------------------------===//
#include "clang/Rewrite/Rewriter.h"
+#include "clang/AST/Stmt.h"
#include "clang/Lex/Lexer.h"
#include "clang/Basic/SourceManager.h"
+#include <sstream>
using namespace clang;
/// getMappedOffset - Given an offset into the original SourceBuffer that this
@@ -208,3 +210,23 @@ void Rewriter::ReplaceText(SourceLocation Start, unsigned OrigLength,
getEditBuffer(StartFileID).ReplaceText(StartOffs, OrigLength,
NewStr, NewLength);
}
+
+/// ReplaceStmt - This replaces a Stmt/Expr with another, using the pretty
+/// printer to generate the replacement code. This returns true if the input
+/// could not be rewritten, or false if successful.
+bool Rewriter::ReplaceStmt(Stmt *From, Stmt *To) {
+ // Measaure the old text.
+ int Size = getRangeSize(From->getSourceRange());
+ if (Size == -1)
+ return true;
+
+ // Get the new text.
+ std::ostringstream S;
+ To->printPretty(S);
+ const std::string &Str = S.str();
+
+ ReplaceText(From->getLocStart(), Size, &Str[0], Str.size());
+ return false;
+}
+
+