i'm trying to modify C++ code. I get a piece of code and line numbers and i need to apply code at the given line numbers. Like this:
1 void foo(){
2 int a = 5;
3 int b = 10;
4 }
And the lines numbers: 2,3. Now i want to automatically insert Code after the lines numbers:
1 void foo(){
2 int a = 5;
3 newcode();
4 int b = 10;
5 newcode();
6 }
In another Thread people said antlr is a good way for this. So i tried using the antlr runtime api. Its easy to generate a parse Tree. I also found ways to modify it. But now i dont know how to get the source code back from the parse tree? I dont really need the source code, it would also be enough to just compile the parse tree to an executable program. How can i do this?
Is there maybe an easier way to solve my problem? Maybe just read the code, count the \n and after 2 and 3 \n i put the my code?
Edit: For my bachelor thesis, i get a piece of parallel code and i need to force it to execute a given interleaving. Therefore i have the job to write a tool to automatically insert instructions like "EnterCriticalSection(...)" and "LeaveCriticalSection(...)" at given lines in the code. Now, i got another job, to rename the main function and insert my own main function. I think this won't work with counting lines.