Re: ## issue with glib.h



Hi,

I had confused the GCC varargs syntax for the ISO C 1999 syntax:
thanks to Owen for clearing that up.  The final draft of the standard
is even available online -- I shouldn't have been confused ;-)

Darin Adler <darin@eazel.com> writes:
> on 8/9/00 5:00 PM, Owen Taylor at otaylor@redhat.com wrote:
> > The best way of fixing it is probably to check for __VA_ARGS__
> > in configure, and if found, use that even for GCC. Moving
> > to Raja's variant above will remove the warning, but does
> > not get rid of the use of a GCC extension.

I think we should just use the __STDC_VERSION__ define -- no need for
autoconf.

  #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
  # define g_message(...) g_log (DOM, LOG_MSG, __VA_ARGS__)
  #elif defined __GNUC__
  # define g_message(format_args...) g_log (DOM, LOG_MSG, format_args)
  #else
  ...
  #endif

> Getting rid of the warning would be useful in and of itself, in my
> humble opinion. Especially if Red Hat 7.0 ships with the GCC version
> with the (now questionable and perhaps to be removed) warning.

The GCC warning should be fixed in glib.h.  The ISO C version can
wait, but the change needed appears simple enough.  The warning itself
is not questionable -- the meaning of the ## there is questionable.

In GCC varargs, the obvious and "most readable" way to implement
g_message (just an example of a printf-like varargs macro) would be

  #define g_message(format, args...) g_log(DOM, LOG_MSG, format, args)

But, wait: suppose you didn't pass any args -- the replacement text
would be
  
  g_message("foo")
  => g_log(DOM, LOG_MSG, "foo",)

which is not valid.  The simple fix that has always worked is to use
g_message(format_args...).  But someone was clever and said if the
replacement text is

  #define g_message(format, args...) g_log(DOM, LOG_MSG, format, ##args)

then, if args is empty the ## swallows the immediately preceding
characters upto and including a non-space character, usually the
comma.  However, this has nasty problems with the new CPP in GCC,
which is token based (according to the Standard C preprocessing
model), not text based like the earlier GCC CPP or older K&R CPPs.  I
think the new CPP swallows the immediately preceding token, which
happens to be the comma in this case, and things work.  However,
people doing "clever" things with this ## syntax will get unexpected
results.  This ## usage is overly clever, the simpler solution works,
and is better, IMO.
 
> Doing a more forward-looking fix, suitable for future GCC versions
> without the extension and for other C99 compilers, would be even
> better, but if it's a lot more complicated I'd like to see the
> warning removed in the more expedient way first.

I think my suggestion above is simple enough, and should work with
older GCCs.

- Hari
-- 
Raja R Harinath ------------------------------ harinath@cs.umn.edu
"When all else fails, read the instructions."      -- Cahn's Axiom
"Our policy is, when in doubt, do the right thing."   -- Roy L Ash




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]