overwrite rewritten text

Hi All,

How can I overwrite or remove some code parts which were already rewritten?

Here is my code:

string S = …
Rewriter.InsertText(Loc, S);
Rewriter.RemoveText(Loc, S.length()); <=== Here, Segmentation fault.

Thanks for your help!

Best,
Kihong

Hi,

did you consider creating a Replacement instead of a Insert-Remove
combination?

Best, Jonas

Hi,

Thanks for your suggestion.
But the result is the same.
I noticed that It works as if the new string was not inserted.
For example, “Loc” points to the location after the inserted string, and “Loc.getWithOffset(-1)” points to the location before the string.
Is there any solution to sync up the mismatch?

Thanks,
Kihong

I am a bit puzzled of what you want to achieve.

AFAIK the change must be applied to the file and then the AST must be
recreated to get the new Source Locations, but if you `InsertText`
something and then `RemoveText` at the same location and same length
what do you intent to do?
For me this sounds like just replace a piece of original code with new
code. Otherwise that would be a no-op, not?

Best Jonas

My intention is to transform the input source code. But this transformation is not pre-defined but dynamic. I first apply some transformation (here, adding some statement); check its correctness with an off-the-shelf checker; it is wrong then I want to roll back. Is there any systematic way to do that?

Thanks,
Kihong

I guess this sounds like the wanted functionality is not possible right
now. You probably need to patch Rewriter.

Best, Jonas