Patrick Horgan - Re: How do I find when the diagnostic pragmas first came into gcc? (original) (raw)
This is the mail archive of the gcc-help@gcc.gnu.orgmailing list for the GCC project.
... more elision by patrick ... I wrote this last week, which tames some of the ugliness:
#if ((GNUC * 100) + GNUC_MINOR)>= 405
define GCC_DIAG_DO_PRAGMA(x) _Pragma (#x)
define GCC_DIAG_PRAGMA(x) GCC_DIAG_DO_PRAGMA(GCC diagnostic x)
if ((GNUC * 100) + GNUC_MINOR)>= 406
define GCC_DIAG_OFF(x) GCC_DIAG_PRAGMA(push) \
GCC_DIAG_PRAGMA(ignored x)
define GCC_DIAG_ON(x) GCC_DIAG_PRAGMA(pop)
else
define GCC_DIAG_OFF(x) GCC_DIAG_PRAGMA(ignored x)
define GCC_DIAG_ON(x) GCC_DIAG_PRAGMA(warning x)
endif
#else
define GCC_DIAG_OFF(x)
define GCC_DIAG_ON(x)
#endif
That gives convenient macros which are used like so:
GCC_DIAG_OFF("-Wsign-compare") if (a< b) { GCC_DIAG_ON("-Wsign-compare") std::cout<< "a<b\n"; }
The macros use push/pop for 4.6+, or for 4.5 they just change the behaviour to "ignored" then back to "warning". They expand to nothing for older versions of GCC or for non-GCC compilers.
The 4.5 behaviour is wrong if e.g. -Werror=sign-compare was being used, because GCC_DIAG_ON re-enables a warning not an error. For my purposes that was sufficient, it's probably not acceptable for library code.
I've been using this for super-strict compilations with a modified GCC that has extra warnings I've added. One of my extra warnings has too many false positives, so I really needed to disable some known warnings I wasn't going to fix.
For these super-strict builds, I need to temporarily disable warnings in third-party libraries which I can't or don't want to change (Boost in particular.) To do this I include them with -isystem instead of -I, which has the same effect as adding the system_header pragma to them. That allows me to make warnings fatal with -Werror without having to worry about errors in code I don't own.