Partially disable pedantic warnings in gcc within source

北慕城南 提交于 2019-12-18 14:52:53

问题


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.


回答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!




回答2:


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



来源:https://stackoverflow.com/questions/14304255/partially-disable-pedantic-warnings-in-gcc-within-source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!