CLANG libtooling AST rewriter


I wanted to ask whether this situation can create a problem:

Suppose I have a FunctionDecl and I need to insert some code before the first variable declaration within this function. Let's say this variable is named "temp".
Later on while visiting VarDecl of "temp", if I want to change the variable name "temp" to variable name "foo", would I be getting into any trouble?
I mean I am doing source to source transformation, where I do stumble upon such a situation in which first I am executing
rewriter.InsertTextBefore(tempdecl->getLocStart(), "some code");
during the FunctionDecl traversal and later on I have this
rewriter.ReplaceText(tempdecl->getSourceRange(), "new name");
while visiting this temp variable
I am running in to a segmentation fault at this point.
This is the error stack generated if I use valgrind to run my tool:
==4620== Use of uninitialised value of size 8
==4620== at 0xF30D90: (anonymous namespace)::RopePieceBTreeNode::size() const
==4620== by 0xF31D70: (anonymous namespace)::RopePieceBTreeInterior::erase(unsigned int, unsigned int)
==4620== by 0xF31FF6: (anonymous namespace)::RopePieceBTreeNode::erase(unsigned int, unsigned int)
==4620== by 0xF324DC: clang::RopePieceBTree::erase(unsigned int, unsigned int)
==4620== by 0xF35295: clang::RewriteRope::erase(unsigned int, unsigned int)
==4620== by 0xF331BC: clang::RewriteBuffer::ReplaceText(unsigned int, unsigned int, llvm::StringRef)
==4620== by 0xF33FB4: clang::Rewriter::ReplaceText(clang::SourceLocation, unsigned int, llvm::StringRef)
==4620== by 0x725C8B: NVMVisitor::VisitVarDecl(clang::VarDecl*) clang::RecursiveASTVisitor<NVMVisitor>::TraverseDecl(clang::Decl*) clang::RecursiveASTVisitor<NVMVisitor>::TraverseStmt(clang::Stmt*) clang::RecursiveASTVisitor<NVMVisitor>::TraverseCompoundStmt(clang::CompoundStmt*) clang::RecursiveASTVisitor<NVMVisitor>::TraverseStmt(clang::Stmt*)

I wanted to confirm whether this is happening because I am trying to do something that I cannot do using CLANG libtooling or I have some other bugs in my code.
I have ran my tool on other examples and it is working fine, however only for this case it breaks.

Please let me know why I am running into this kind of problem.