Search code examples
cgccsuppress-warnings

Partially disable pedantic warnings in gcc within source


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.


Solution

  • 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!