Re: Suppressing GCC warning locally inside a macro

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

 



U.Mutlu wrote on 04/23/2019 11:31 PM:
Segher Boessenkool wrote on 04/23/2019 09:35 PM:
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.

For the sake of completeness and just FYI:
the following book (in German language) in footnote 1 claims that
on many DSPs (digital signal processors) CHAR_BIT is 16 or more:
http://openbook.rheinwerk-verlag.de/c_von_a_bis_z/030_c_anhang_b_009.htm

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).

Ok, in C++ one best would use a template to cover all types,
and in C one could achieve the same effect with macros,
and in both cases one would take the sizeof of the actual type.

I now stumbled upon this problem:
since C does not know the concept of "auto" variables,
then it's IMO impossible to write code that would function
with any integral type, not even by using macros.
For example the statement
  y = m(x + 1);
where m is a macro, won't work, b/c here macro m expects just a variable name,
not such an arithmetic statement x + 1.
I'm not sure if there can exist a solution for this problem in C.

A workaround would be to use the biggest integral type as a tmpvar
for the said statement, and pass that tmpvar to m. But this is inefficient,
and IMHO there's also no standard defining the biggest integral type.

And ffs() can easily be replaced. In fact I've posted a substitute,
but it too can be improved further to cover any valid type.




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux