"Invalid region to erase" on rewriting ObjCMethodDecl

I have a RecursiveASTVisitor as below, attempting to reqrite ObjC methods:

    virtual bool VisitObjCMethodDecl(ObjCMethodDecl *mdecl) {
if(astContext->getSourceManager().isInMainFile(mdecl->getLocStart())) {
            if(!mdecl->isOverriding()) {
funclist.push_back(mdecl->getClassInterface()->getNameAsString() + " : " +
        return true;

    void rewriteMeth(ObjCMethodDecl *meth) {
        unsigned num = meth->getNumSelectorLocs();
        Selector selector = meth->getSelector();
        for(unsigned i = 0; i < num; i++) {
selector.getNameForSlot(i).size(), "rewrittenMeth");

This works well for small programs, but for larger ones, I get an 'Assertion
failed: (Offset+NumBytes <= size() && "Invalid region to erase!"), function
erase, file
/<redacted>/llvm/tools/clang/include/clang/Rewrite/Core/RewriteRope.h, line
232.' after rewriting 3-4 methods. Changing the order of functions in the
source code causes it to fail in a different function. It appears to fail
fairly deterministically after 3-4 rewrites. Any idea what could be going

Can a FileID identifier point to multiple physical source files?

After some more digging, I found that after a few rewrites a rewrite region
extends beyond the range of the current RewriteBuffer, resulting in that
error. I printed out debug info for the file used for creating
RewriteBuffers, and the same FileID (1) is used for two different physical
source files - is that expected? How does the buffer get mapped to different
physical files?