Automatically insert a statement in each function in C++
Submitted by msameer on Fri, 08/02/2008 - 4:16pm.
And I thought I'll never do such things again ?
I wanted to insert a debugging statement at the beginning of each function.
for i in *.cc *.c; do sed -i -e 's/\([a-zA-Z0-9 _*:~]*([^).]*) *{\)/\1 \nprintf("%s\\n", __PRETTY_FUNCTION__); /g' $i; doneIt can probably be used to insert anything too.
It's dirty but it worked for me. Just keep a backup or make sure the latest code is in any RCS just in case.
It only failed with a function that was commented using C++ style comments "//".
I didn't try it with class methods defined inside header files












My magic foobar code is usually:
qDebug("%s - %d", __LINE__, __FILE__ );