Writings Photos Code Contact Resume
Automatically insert a statement in each function in C++

You are here

Submitted by msameer on Fri, 08/02/2008 - 2: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; done

It 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


Submitted by elcuco (not verified) on Fri, 08/02/2008 - 8:21pm

My magic foobar code is usually:

qDebug("%s - %d", __LINE__, __FILE__ );
Submitted by Anonymous (not verified) on Wed, 18/06/2008 - 9:50am

Regex can really be useful; but I believe it would be way more efficient had the programming language itself supported performing this sort of operation.

For some reason I needed to get gcc to support naming variables in Arabic (utf-8). It only worked after going through a lot of workarounds..

Do you think it would be utterly foolish if we write a compiler or a gcc frontend for a simple programming language that would accomplish more of our needs?

Submitted by msameer on Wed, 18/06/2008 - 5:25pm

And what our needs are ?

Submitted by Anonymous (not verified) on Thu, 19/06/2008 - 3:01am

In your case I guess it's syntax that sports a set of constructs or a powerful preprocessor to do the job without that mess.

In my case what I needed was a compiler that is multilingual. It's ironic how gcc's O3 goes to insane lengths optimizing really bad code, yet it just wouldnt accept being fed a letter in Arabic.

Maybe it has something to do with the fact that RMS hates Muslims? j/k.

Oh, and languages that currently do that (e.g. PHP) are only interpreted, and multi-byte encoding support is typically poor.

Submitted by msameer on Thu, 19/06/2008 - 1:31pm

I don't think that RMS hates Muslims. And please don't bring up such topics here.

I'm not interested in an Arabic programming language and I doubt I'll ever be interested.

well, A tiny lex/yacc parser'd do the job but I'm fine with sed. It works ;-)

Submitted by Anonymous (not verified) on Thu, 19/06/2008 - 4:58pm

RMS as well as Eric S. Raymond (of the OSI) have both made tons of bigoted comments against Islam and Muslims in fact. However, I was just briging it up in an ironic way. My apologies.

Add new comment

Subscribe to /  digg  bookmark