I’m looking for a tool, regex or other magic to do some heavy code replacements.
Ideally I would be able to replace all instances of the new operator with a call to a function preserving the arguments.
What are my options?
Update:
Example:
ClassA* a = new ClassA<int>(1,2,3,4,new ClassB(1,2),"horrible");
Should transform to:
ClassA* a = FUNCTION(ClassA<int>(1,2,3,4,FUNCTION(ClassB(1,2)),"horrible"));
Where FUNCTION will do something like:
FUNCTION(...) Debug(new __VA_ARGS__, __FILE__)
A simple replace nearly does it well, it only lacks the last ).
Update:
My initial thought was to use a macro to track some additional info like __FILE__, store it in a std container and then call new. What will happen if the container calls new? How do I add __FILE__ inside an overloaded new?
You don’t mention why you want to do this, but I do wonder whether overloading the new operator might be a better option for you.
Why would one replace default new and delete operators?
Edit
The following links might also be relevant:
overloading new and delete in c++
Overriding "new" and Logging data about the caller
Macro to replace C++ operator new
http://blogs.msdn.com/b/calvin_hsia/archive/2009/01/19/9341632.aspx
It seems like this is a problem many people have tackled before. Googling for
overload|override new __line__ __file__will throw up further possibilities.