How to avoid "the address of 'foo' will never be NULL" warning inside macros?

Steffen Dettmer steffen.dettmer@googlemail.com
Thu Jun 9 11:26:00 GMT 2011


Hi,

we implement some features either as functions or macros (if
small). One of the used compilers is gcc. With gcc-4.6.0 we get
new warnings.

With macros, we for example have:

  #define fooGetFooPtr(bar) \
     (const uint8*)(((bar) != NULL ? (bar)->foo_ : (const uint8*)bar))

when used as:

  struct bar_s bar;
  ptr *p = fooGetFooPtr(&bar);

leading to:

  error: the comparison will always evaluate as 'true' for the
  address of 'bar' will never be NULL [-Werror=address]

Of course, we considering the preprocessor output this is
obiously a warning and the check can be omitted, but for the
developer it is not because "hidden" inside the macro. The
developer shall not need to care whether the used fooGet*
function is implemented in form of a C function or a CPP macro
(the usage semantics should be equal).

What is recommended to do here?

- Removing the check from the macro changes behavior when called
  with a "free" pointer passed from somewhere else,

- implementing the macro as function would probably help but may
  cause overhead and may have additional effects

- implementing the macro as function for gcc of gcc-4.6.0 only
  seems to be ugly and needlessly conditional

- disabling the warning would (I guess) also affect when checking
  addresses without using macros

How to address this in a good way? What is recommended?

Regards,
Steffen



More information about the Gcc-help mailing list