Assertion violation when using Rewrite::ReplaceText()

Hi,

I’m working in an optimization tool that does some source-to-source rewritings over OpenCL kernels written in C. In former versions of my tool, I could use the Rewriter class with no problem (I was using the ReplaceText method in order to replace the text of a single Stmt with a new string specifically composed for it).

A couple of days ago, and with no apparent reason since I’ve not changed anything related with the replacement of source code lines (of course I have already checked that both the Stmt’s I’m passing into the ReplaceText method and the replacing string are OK), some invocations to ReplaceText result in the following assertion violation:

Application: <>/tools/clang/lib/Rewrite/RewriteRope.cpp:382: void::RopePieceBTreeLeaf::erase(unsigned int, unsigned int): Assertion `getPiece(StartPiece).size() > NumBytes’ failed.
Stack dump:
0. parser at end of file
Aborted

I’m talking about “some” invocations because not all the invocations to ReplaceText are leading to that violation. Obviously, when it appears the program is forced to abort, and there is no clue of the rewritten version of the OpenCL kernel. I’ve skimmed the RewriteRope.cpp file available in Clang Doxygen pages but I’ve not arrived at any clear conclusion, I’m not a very experienced C++ programmer and for me it’s just a bunch of strange code, :frowning:

I’ve tried also using InsertTextAfter instead of ReplaceText in failed invocations. In this case there is no assertion violation, the program seems to finish properly and a file with another version of the OpenCL kernel is generated, but this new version only reflects the changes made with ReplaceText that not were leading to the violation before. About all the changes that InsertTextAfter were suppose to make, there is no clue.

I’m a bit worried about this because this problem is the last hurdle I must jump in order to finish my tool…

Thank you so much in advance for your answers.

P.S.: Sorry for my bad English, :wink: