Writings Photos Code Contact Resume Me
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; 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

Syndicate content  digg  bookmark

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

My magic foobar code is usually:

qDebug("%s - %d", __LINE__, __FILE__ );

Submitted by Anonymous (not verified) on Wed, 18/06/2008 - 11: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 - 7:25pm.

And what our needs are ?

Submitted by Anonymous (not verified) on Thu, 19/06/2008 - 5: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 - 3: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 - 6: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.

Post new comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <b> <dd> <dl> <dt> <i> <s> <li> <ol> <u> <ul> <br> <br /> <blockquote>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use BBCode tags in the text. URLs will automatically be converted to links.
  • Lines and paragraphs break automatically.
  • You may write mixed Arabic and English freely, line direction will be computed automaticaly

More information about formatting options