* How to avoid "the address of 'foo' will never be NULL" warning inside macros?
@ 2011-06-09 11:26 Steffen Dettmer
2011-06-09 12:52 ` Jonathan Wakely
0 siblings, 1 reply; 3+ messages in thread
From: Steffen Dettmer @ 2011-06-09 11:26 UTC (permalink / raw)
To: gcc-help
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: How to avoid "the address of 'foo' will never be NULL" warning inside macros?
2011-06-09 11:26 How to avoid "the address of 'foo' will never be NULL" warning inside macros? Steffen Dettmer
@ 2011-06-09 12:52 ` Jonathan Wakely
2011-06-18 19:39 ` Steffen Dettmer
0 siblings, 1 reply; 3+ messages in thread
From: Jonathan Wakely @ 2011-06-09 12:52 UTC (permalink / raw)
To: Steffen Dettmer; +Cc: gcc-help
On 9 June 2011 09:15, Steffen Dettmer wrote:
>
> - implementing the macro as function would probably help but may
> cause overhead and may have additional effects
http://gcc.gnu.org/onlinedocs/gcc-4.6.0/gcc/Inline.html
> - 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
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: How to avoid "the address of 'foo' will never be NULL" warning inside macros?
2011-06-09 12:52 ` Jonathan Wakely
@ 2011-06-18 19:39 ` Steffen Dettmer
0 siblings, 0 replies; 3+ messages in thread
From: Steffen Dettmer @ 2011-06-18 19:39 UTC (permalink / raw)
To: gcc-help
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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2011-06-17 17:33 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-06-09 11:26 How to avoid "the address of 'foo' will never be NULL" warning inside macros? Steffen Dettmer
2011-06-09 12:52 ` Jonathan Wakely
2011-06-18 19:39 ` Steffen Dettmer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).