Is using #pragma warning push/pop the right way to temporarily alter warning level?
This will work with multiple compilers (and different versions of compilers).
Header "push"
#if defined(__clang__)
# pragma clang diagnostic push
#endif
#if defined(_MSC_VER)
# pragma warning(push)
#endif
#if defined(YOUR_FAVORITE_COMPILER)
# pragma your compiler push warning
#endif
Header "pop"
#if defined(__clang__)
# pragma clang diagnostic pop
#endif
#if defined(_MSC_VER)
# pragma warning(pop)
#endif
Some warning
#if defined(__clang__)
# pragma clang diagnostic ignored "-Wunused-parameter"
# pragma clang diagnostic ignored "-Wunused-variable"
# if __has_warning("-Wnew-special-warning")
# pragma clang diagnostic ignored "-Wnew-special-warning"
# endif
#endif
#if defined(_MSC_VER)
# pragma warning(disable: 4100) // unreferenced formal parameter
# if _MSC_VER > _MSC_SOME_VERSION
# pragma warning(disable: xxxx) // disable one more for special version
# endif
#endif
Usage
// This code reports warnings
// ...
#include <ignore_compiler_warning/push>
#include <ignore_compiler_warning/warning_type_1>
#include <ignore_compiler_warning/warning_type_2>
#include <ignore_compiler_warning/warning_type_3>
// This code ignores warnings type_{1,2,3}
// ...
#include <ignore_compiler_warning/pop>
// Back to reporting warnings
// ...
Additionally include guards can check that there is no double push/pop/disable-warning pragmas.
Update
- Implementation
- List of available warnings
- CMake companion function to generate flags
The first method is the best way to do it, IMO. I know of no problems with it.
Simply bear in mind that a #pragma is compiler specific so don't expect it to work on every compiler out there :)