]> granicus.if.org Git - clang/commitdiff
Add new API to rewrite one stmt/expr with another.
authorChris Lattner <sabre@nondot.org>
Wed, 17 Oct 2007 22:35:30 +0000 (22:35 +0000)
committerChris Lattner <sabre@nondot.org>
Wed, 17 Oct 2007 22:35:30 +0000 (22:35 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@43101 91177308-0d34-0410-b5e6-96231b3b80d8

Driver/RewriteTest.cpp
Rewrite/Rewriter.cpp
include/clang/Rewrite/Rewriter.h

index 6972f35853f4e42dca79d9e6458f34e2922313e8..508f7996081aa3c72a92de44aac6a5779ba61336 100644 (file)
@@ -22,14 +22,16 @@ using namespace clang;
 namespace {
   class RewriteTest : public ASTConsumer {
     Rewriter Rewrite;
+    ASTContext *Context;
     SourceManager *SM;
     unsigned MainFileID;
     SourceLocation LastIncLoc;
   public:
-    void Initialize(ASTContext &Context, unsigned mainFileID) {
-      SM = &Context.SourceMgr;
+    void Initialize(ASTContext &context, unsigned mainFileID) {
+      Context = &context;
+      SM = &Context->SourceMgr;
       MainFileID = mainFileID;
-      Rewrite.setSourceMgr(Context.SourceMgr);
+      Rewrite.setSourceMgr(Context->SourceMgr);
     }
     
     virtual void HandleTopLevelDecl(Decl *D);
@@ -109,13 +111,12 @@ void RewriteTest::RewriteFunctionBody(Stmt *S) {
 }
 
 void RewriteTest::RewriteAtEncode(ObjCEncodeExpr *Exp) {
-  int Size = Rewrite.getRangeSize(Exp->getSourceRange());
-  if (Size == -1) {
-    printf("BLAH!");
-    return;
-  }
-  
-  Rewrite.ReplaceText(Exp->getAtLoc(), Size, "\"foo\"", 5);
+  // Create a new string expression.
+  QualType StrType = Context->getPointerType(Context->CharTy);
+  Expr *Replacement = new StringLiteral("foo", 3, false, StrType, 
+                                        SourceLocation(), SourceLocation());
+  Rewrite.ReplaceStmt(Exp, Replacement);
+  delete Replacement;
 }
 
 
index 514b82235a47605a64abf1eb23ff16726c4a9246..9e7d1b3143a778eaa6a41bef5a98ad5b4e998744 100644 (file)
 //===----------------------------------------------------------------------===//
 
 #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;
+}
+
+
index bf4baf25af2d20783c75168fbca9a0387dc5b0a9..8e8988308c4712a98cf756db832a60fedd1a9cfe 100644 (file)
@@ -22,6 +22,7 @@
 namespace clang {
   class SourceManager;
   class Rewriter;
+  class Stmt;
   
 /// SourceDelta - As code in the original input buffer is added and deleted,
 /// SourceDelta records are used to keep track of how the input SourceLocation
@@ -141,8 +142,10 @@ public:
   void ReplaceText(SourceLocation Start, unsigned OrigLength,
                    const char *NewStr, unsigned NewLength);
   
-  // TODO: Replace Stmt/Expr with another.  Return bool to indicate whether the
-  // locations were rewritable.
+  /// 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 ReplaceStmt(Stmt *From, Stmt *To);
   
   /// getRewriteBufferFor - Return the rewrite buffer for the specified FileID.
   /// If no modification has been made to it, return null.