- It’s best practice to do warning free builds. Those harmless warnings you are ignoring could be hiding an important warning that’s bugging up your code.
- Recent version of GCC support disabling of specific warnings.
- Sometimes we use external libraries which we trust, or must accept — we don’t want to muck around with their internals — say for example the boost libraries.
Given these points, when we include a header which creates warnings, we’d like to disable just those warnings, for just those header files. This can be done with the following statements for the GCC compiler:
#pragma GCC diagnostic ignored "-Wparentheses"
#pragma GCC diagnostic ignored "-Wswitch"
#pragma GCC diagnostic warning "-Wswitch"
#pragma GCC diagnostic warning "-Wparentheses"
This comes up often enough I cranked out a trivial bit of elisp so I can do this in emacs a bit more automatically:
; @todo let this work if we have a range too.
(defun insert-pragmas (pragma-name)
"Wrap the current line with a pragma to disable the warning."
(interactive "MWarning to disable: ")
(if (string= "" pragma-name)
(message "ignoring empty pragma name")
(insert "#pragma GCC diagnostic ignored \"-W" pragma-name "\"\n")
(insert "\n#pragma GCC diagnostic warning \"-W" pragma-name "\"\n")