mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Bernd Petrovitsch <bernd@firmix.at>
To: Chris Friesen <cfriesen@nortel.com>
Cc: linux-kernel@vger.kernel.org, Amerigo Wang <xiyou.wangcong@gmail.com>
Subject: Re: question for C preprocessor wizards
Date: Thu, 23 Jul 2009 10:14:05 +0200	[thread overview]
Message-ID: <1248336845.7489.8.camel@spike.firmix.at> (raw)
In-Reply-To: <20090723010715.GA5712@cr0.nay.redhat.com>

On Thu, 2009-07-23 at 09:07 +0800, Amerigo Wang wrote:
> On Wed, Jul 22, 2009 at 12:50:49PM -0600, Chris Friesen wrote:
> >
> >I'm hoping someone can help me out.
> 
> 
> This is _not_ a question about kernel. :)
> 
> >
> >I've got a bunch of code that call a bunch of different wrapper
> >routines, with varying numbers of arguments.  Depending on whether a
> >compile flag is set, I want to do some stuff before and after calling
> >the "real" routine.  I can do this easily enough with a macro.
> >
> >#if FLAG
> >#define func_wrapper(args...) \
> >	do { \
> >		dostuff(); \
> >		func(args); \
> >		do_more_stuff(); \
> >	} while (0)
> >#else
> >#define func_wrapper(args...) func(args)
> >#endif
> >
> >
> >However, given that there are hundreds of functions, I'd like to
> >generate these macros with another macro, sort of like:
> >
> >#if FLAG
> >#define WRAPPER(func) \
> >	#define func # _wrapper(args...) \
> >	do { \
> >		dostuff(); \
> >		func(args); \
> >		do_more_stuff(); \
> >	} while (0)
> >#else
> >#define WRAPPER(func) \
> >	#define func ## _wrapper(args...) func(args)
> >#endif
> >
> >Where I could then do
> >
> >WRAPPER(func1)
> >WRAPPER(func2)
> >...
> >WRAPPER(func100)
> >
> >
> >However, the preprocessor complains about having that "#" in the macro
> >body where it isn't used for stringification.
One can't generate preprocessor directive via #define.

> >Anyone have any ideas how to accomplish this?  I had considered writing
> >an app to programmatically generate an include file as a precursor to
> >actually compiling the real app, but I was hoping there was a more
> >elegant solution.
Personally, I don't find it in elegant to generate simple/trivial .c
or .h files with some small-ish perl/shell-script via sane Makefile
rules. Perhaps unless the contents is (almost) never going to change
(and even then there might be good reasons).
Within the kernel, it's probably better to avoid perl-scripts for easier
acceptance.

> Well, you can define func() as a real function instead of a macro,
> i.e:
> 
> #define WRAPPER(func) \
>  void func ## _wrapper(args, ...) \
>  {  \
>     dostuff(); \
>     func(args); \
>     do_more_stuff(); \
>  }
ACK.

Or the other way around:
Have dostuff() at the begin and do_more_stuff() at the end of the real
function in the source and #if-them-away per
----  snip  ----
#if FLAG
void dostuff();
void do_more_stuff();
#else
#define dostuff() ((void)0)
#define do_more_stuff() ((void)0)
#endif
----  snip  ----

	Bernd
-- 
Firmix Software GmbH                   http://www.firmix.at/
mobil: +43 664 4416156                 fax: +43 1 7890849-55
          Embedded Linux Development and Services



  reply	other threads:[~2009-07-23  8:14 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-22 18:50 Chris Friesen
2009-07-23  1:07 ` Amerigo Wang
2009-07-23  8:14   ` Bernd Petrovitsch [this message]
2009-07-23 15:08 ` Dick Streefland

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1248336845.7489.8.camel@spike.firmix.at \
    --to=bernd@firmix.at \
    --cc=cfriesen@nortel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=xiyou.wangcong@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome