[cfe-commits] r43101 - in /cfe/trunk: Driver/RewriteTest.cpp Rewrite/Rewriter.cpp include/clang/Rewrite/Rewriter.h

Chris Lattner sabre at nondot.org
Wed Oct 17 15:35:30 PDT 2007


Author: lattner
Date: Wed Oct 17 17:35:30 2007
New Revision: 43101

URL: http://llvm.org/viewvc/llvm-project?rev=43101&view=rev
Log:
Add new API to rewrite one stmt/expr with another.

Modified:
    cfe/trunk/Driver/RewriteTest.cpp
    cfe/trunk/Rewrite/Rewriter.cpp
    cfe/trunk/include/clang/Rewrite/Rewriter.h

Modified: cfe/trunk/Driver/RewriteTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/RewriteTest.cpp?rev=43101&r1=43100&r2=43101&view=diff

==============================================================================
--- cfe/trunk/Driver/RewriteTest.cpp (original)
+++ cfe/trunk/Driver/RewriteTest.cpp Wed Oct 17 17:35:30 2007
@@ -22,14 +22,16 @@
 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::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;
 }
 
 

Modified: cfe/trunk/Rewrite/Rewriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Rewrite/Rewriter.cpp?rev=43101&r1=43100&r2=43101&view=diff

==============================================================================
--- cfe/trunk/Rewrite/Rewriter.cpp (original)
+++ cfe/trunk/Rewrite/Rewriter.cpp Wed Oct 17 17:35:30 2007
@@ -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;
+}
+
+

Modified: cfe/trunk/include/clang/Rewrite/Rewriter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Rewrite/Rewriter.h?rev=43101&r1=43100&r2=43101&view=diff

==============================================================================
--- cfe/trunk/include/clang/Rewrite/Rewriter.h (original)
+++ cfe/trunk/include/clang/Rewrite/Rewriter.h Wed Oct 17 17:35:30 2007
@@ -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 @@
   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.





More information about the cfe-commits mailing list