Suppressing GCC warning locally inside a macro

Segher Boessenkool segher@kernel.crashing.org
Tue Apr 23 19:35:00 GMT 2019


On Tue, Apr 23, 2019 at 07:43:06PM +0200, U.Mutlu wrote:
> Segher Boessenkool wrote on 04/23/2019 06:02 PM:
> >On Mon, Apr 22, 2019 at 11:11:47PM +0200, U.Mutlu wrote:
> >>is there a way to suppress a gcc compiler warning locally inside a macro
> >>code?
> >>
> >>#include <stddef.h>    // size_t
> >>#include <strings.h>   // ffs()
> >>
> >>#define bitsizeof(type, member) \
> >>   ({ type t; \
> >>      t.member = ~0u;  /* compiler warning -Woverflow happens */ \
> >>      const size_t rc = ffs(t.member + 1); \
> >>      !rc ? sizeof(unsigned) * 8 : rc - 1; \
> >>   })
> >
> >~0U does not work correctly for types bigger than int (but neither will
> >ffs, or that sizeof, and that 8 is not very portable either).
> >
> >If you write -1 instead, everything just works.
> 
> Ok, now using -1 for ~0u, and CHAR_BIT for 8.
> But what's your objection regarding sizeof?

It uses sizeof(int), which of course is wrong for bigger types.  Similarly
ffs works on ints only, not on bigger types.

My main point was that -1 will never warn here, and is (imho) the best way
to specify all-bits-set (it works for any integer type).


Segher



More information about the Gcc-help mailing list