How to avoid "the address of 'foo' will never be NULL" warning inside macros?
Steffen Dettmer
steffen.dettmer@googlemail.com
Sat Jun 18 19:39:00 GMT 2011
On Thu, Jun 9, 2011 at 11:23 AM, Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
>> - disabling the warning would (I guess) also affect when checking
>> addresses without using macros
>
> Not if you do it locally in the macro only, using _Pragma
> http://gcc.gnu.org/onlinedocs/gcc-4.6.0/gcc/Diagnostic-Pragmas.html
Thank you for the pointers.
Today I tried again to use it, but I'm unable to get it right.
I understood that I should use _Pragma inside my `function-style
macro', but I get errors. I assume because I'm trying to use
_Pragma `inside' an expression.
I'm sorry to bother you again but could you please give an
concrete example or implement/fix my example?
The example macro is getLen (implemented as macro because some
compilers may not `inline' correctly automatically and do not
offer a way to explicitely do so).
The usage is:
len = getLen(&stack_var);
Here some code I tried:
/* with "len = getLen(&bb);":
* (68 of 85): 4: warning: the comparison will always evaluate as 'true'
* for the address of 'ref' will never be NULL [-Waddress]
*/
struct bb_s {
void *m;
size_t l;
};
/* The macro that I would like to use without warnings: */
#define getLen(bb) (size_t)(((bb) != NULL ? (bb)->l : 0))
#if 0
/* (11 of 17): 1: error: expected expression before '#pragma' */
#define getLen(bb) \
_Pragma ("GCC diagnostic push") \
_Pragma ("GCC diagnostic ignored \"-Waddress\"") \
(size_t)(((bb) != NULL ? (bb)->l : 0)) \
_Pragma ("GCC diagnostic pop")
#endif
void func(void);
void func(void)
{
struct bb_s bb = { 0 };
size_t len;
/* This is the usage I would like to have without warnings */
len = getLen(&bb);
assert(len == 0);
}
Have a nice weekend everyone,
Steffen
More information about the Gcc-help
mailing list