14

I am trying to get gcc to shut up about my usage of binary constants. They make the code more readable, but prevent me from using -pedantic which I comply with otherwise. I would either like to have a switch like -fnobinaryconstwarn or similar (which I don't think exists after perusing the man page for a while) or use a

#pragma GCC diagnostic ignored "-pedantic" 

to selectively disable the pedantic warnings for a short stretch like described here: Selectively disable GCC warnings for only part of a translation unit? Unfortunately that doesn't seem to work. What are my options?

For clang

#pragma GCC diagnostic ignored "-Wpedantic"

works, while the line above doesn't, but it generates an error for gcc.

Community
  • 1
  • 1
Max
  • 773
  • 8
  • 15
  • can you give an example of your "use of binary constants"? – Andreas Grapentin Jan 13 '13 at 14:43
  • Similar to these: `unsigned int piece = 0b10010101;` That feature was introduced in gcc-4.3.0. I actually think the problem is deeper though, since turning off the warnings introduced by `-pedantic` should be possible somehow, if that specific one doesn't have a flag. – Max Jan 13 '13 at 14:57
  • gcc should be more modular on these things, have explicit warning flags for all its features and then the `-Wall`, `-pedantic` whatever flags should clearly list all the primitive ones from which they are composed. But this is probably wishful thinking. – Jens Gustedt Jan 13 '13 at 18:04
  • 1
    GCC 4.8 will support `-Wpedantic` so the `#pragma` will work – Jonathan Wakely Jan 13 '13 at 18:15
  • @JensGustedt, that's how it works. When a warning is printed it tells you which `-Wxxx` option caused it. `-pedantic` is a single feature (diagnose non-standard extensions) so has a single flag. – Jonathan Wakely Jan 13 '13 at 18:16
  • @JonathanWakely, but it doesn't seem that all of these can be switched on or off via the pragma. – Jens Gustedt Jan 13 '13 at 18:17
  • Almost all of them can. See my other comment about `-Wpedantic`. There's no point complaining about "wishful thinking" on SO, if it bothers you add a feature request to GCC's bugzilla, that's the way to get changes made. e.g. `-Wpedantic` was added because someone asked for it (and implemented it) not because someone somewhere wished for it. – Jonathan Wakely Jan 13 '13 at 18:19
  • @JonathanWakely, wow, looks as you felt insulted, wasn't my intention. And for the extension in question, your statement is simply not true, for binary constants there is no `-Wsomething option` and the warning doesn't contain a hint how it could be switched off. – Jens Gustedt Jan 13 '13 at 19:41
  • So file an enhancement request in [GCC's bugzilla](http://gcc.gnu.org/bugs/) for an option controlling the warning. Wishing doesn't get things done. Or for this specific case, until a specific flag is added don't use `-pedantic` if you want to use non-standard extensions. – Jonathan Wakely Jan 13 '13 at 19:51
  • If you were to turn that comment into an answer, then I'd accept it, since it seems the most appropriate response. Other than that, I am a little curious and trying to actually understand the reasoning. I compile with (among other flags) `--std=gnu99 -pedantic` and as far as I understand the C standard regarding constants, it's OK to parse extra sorts of constants, so issuing a diagnostic isn't required even by strict conformance standards. I can sort of see why one would want it to appear for `--std=c99` but not for the gnu variety. – Max Jan 13 '13 at 20:03

2 Answers2

10

From the gcc manual at: http://gcc.gnu.org/onlinedocs/gcc-4.8.2/gcc/Alternate-Keywords.html#Alternate-Keywords

-pedantic and other options cause warnings for many GNU C extensions. You can prevent such warnings within one expression by writing __extension__ before the expression. __extension__ has no effect aside from this.

I just compiled the following block with -Wall -Wextra -Wpedantic with gcc-4.8.2, and no warning was printed:

static uint8_t shbl[2][9] = {
{ __extension__ 0b11111111,
  __extension__ 0b11111110,
  __extension__ 0b11111100,
  __extension__ 0b11111000,
  __extension__ 0b11110000,
  __extension__ 0b11100000,
  __extension__ 0b11000000,
  __extension__ 0b10000000,
  __extension__ 0b00000000 },
// BLOCK_RIGHT
{ __extension__ 0b11111111,
  __extension__ 0b01111111,
  __extension__ 0b00111111,
  __extension__ 0b00011111,
  __extension__ 0b00001111,
  __extension__ 0b00000111,
  __extension__ 0b00000011,
  __extension__ 0b00000001,
  __extension__ 0b00000000 }
};

(Of course this is ugly, and I'll change that to a precompiler macro. But for a test it was acceptable.)

Yamakuzure
  • 367
  • 2
  • 9
1

maybe, you could use a macro which can do what you want to achieve in a portable manner. here's a short example:

#include <stdio.h>

#define BINARY(N) strtol(#N, 0, 2)

int main()
{
    unsigned int piece = BINARY(10010101);
    printf("%u\n", piece);

    return 0;
}

in theory, gcc should be able to optimize the calls to strtol away and you don't lose readability.

EDIT: It seems that gcc does NOT optimize the strtol calls away as of now. However, your performance loss should be negligible.

Cheers!

Andreas Grapentin
  • 5,499
  • 4
  • 39
  • 57