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
diff --git a/Rewrite/Rewriter.cpp b/Rewrite/Rewriter.cpp
index 514b822..9e7d1b3 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 @@
   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;
+}
+
+