[linux-elitists] Stupid C tricks

Martin Pool mbp@sourcefrog.net
Sat Aug 28 16:14:40 PDT 2004


On 27 Aug 2004, Jason Spence <jspence@lightconsulting.com> wrote:
> On Fri, Aug 27, 2004 at 09:56:51PM -0700, Greg KH wrote: 
> > 
> > Ick, what about something like what the kernel does in places:
> > 
> > #ifdef DEBUG
> > #define dbg(format, arg...) printk(KERN_DEBUG "%s: " format "\n" , __FILE__ , ## arg)
> > #else
> > #define dbg(format, arg...) do {} while (0)
> > #endif
> > 
> > Put __LINE__ in there too and you are good to go.  Variable length
> > arguments with a macro, fun stuff (don't mess with the spaces in that
> > definition, older versions of gcc are very picky...)
> 
> The problem is that the trick relies on support for variable arguments
> in macros, something MS C doesn't support.  Older versions of
> HP/Compaq/DEC C don't support it either, although it turns out the
> recent versions do:
> 
> http://h71000.www7.hp.com/commercial/c/docs/6180p020.html#macro_sub
> 
> Hmm, this is interesting: apparently MS C is the only compiler without
> variable argument macro support.  Open Watcom 1.2, Intel C, Digital
> Mars, and my other favorite compilers support C99 macros just fine (I
> just checked).  I was under the impression this was a not so supported
> C99 thing that I couldn't rely on being available.  In light of the
> fact that Intel C (which I prefer over MS C anyway, when I can get
> licenses for it) supports it, I think I just found a reason to finally
> ditch MS C.

You can define things so that you do without the function name on MS
C.  This may be enough if gcc is your main platform:

  http://sourcefrog.net/archzoom/mbp@sourcefrog.net--2004/distcc--devel--2--patch-109/src/trace.h

#if defined(HAVE_VARARG_MACROS)  && defined(__GNUC__)
#define rs_log(l, s, str...) do {              \
     rs_log0((l), __FUNCTION__, (s) , ##str);  \
     } while (0)
#else
void rs_log0_nofn(int level, char const *fmt, ...);
#define rs_log rs_log0_nofn
#endif

--
Martin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://allium.zgp.org/pipermail/linux-elitists/attachments/20040829/ca5542a2/attachment.pgp 


More information about the linux-elitists mailing list