/* * File: warnings_1.cpp * Author: Paul * * Created on 06 December 2012, 09:56 * * -Wall -Wno-unused-variable -Wno-maybe-uninitialized -Wno-uninitialized */ #ifdef __clang__ # define CLANG_DIAG_STR(s) # s // stringize s to "no-unused-variable" # define CLANG_DIAG_JOINSTR(x,y) CLANG_DIAG_STR(x ## y) // join -W with no-unused-variable to "-Wno-unused-variable" # define CLANG_DIAG_DO_PRAGMA(x) _Pragma (#x) // _Pragma is unary operator #pragma ("") # define CLANG_DIAG_PRAGMA(x) CLANG_DIAG_DO_PRAGMA(clang diagnostic x) # define CLANG_DIAG_OFF(x) CLANG_DIAG_PRAGMA(push) \ CLANG_DIAG_PRAGMA(ignored CLANG_DIAG_JOINSTR(-W,x)) // For example: #pragma clang diagnostic ignored "-Wno-unused-variable" # define CLANG_DIAG_ON(x) CLANG_DIAG_PRAGMA(pop) // For example: #pragma clang diagnostic warning "-Wno-unused-variable" #else // Ensure these macros so nothing for other compilers. # define CLANG_DIAG_OFF(x) # define CLANG_DIAG_ON(x) # define CLANG_DIAG_PRAGMA(x) #endif /* Usage: CLANG_DIAG_OFF(unused-variable) CLANG_DIAG_OFF(unused-parameter) CLANG_DIAG_OFF(uninitialized) */ #include #include #include int main() { #ifdef __clang__ std::cout << "Clang "<< __clang_major__ << '.' << __clang_minor__<< '.' << __clang_patchlevel__ << std::endl; #endif // Clang method of saving and restoring current warning state. #pragma clang diagnostic push #pragma clang diagnostic pop // Example of using Clang push'n'pop around two ignored warnings. #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wmultichar" #pragma clang diagnostic ignored "-Wconstant-conversion" #pragma clang diagnostic ignored "-Wunused-variable" char b = 'df'; // Most unwisely ignoring ALL warnings. #pragma clang diagnostic pop // Example of using macro for push and pop pragmas. CLANG_DIAG_PRAGMA(push); #pragma clang diagnostic ignored "-Wunused-variable" int unused; CLANG_DIAG_PRAGMA(pop); // example of using macro to push warning state,suppress a warning and restore state. CLANG_DIAG_OFF(unused-variable) int unused_two; // No warning from unused variable. CLANG_DIAG_ON(unused-variable) // int unused_too; // Expect a warning. return 0; } /* Output: Clang 3.1.0 clang++.exe -dD -Wall -Wextra -c -g -Wall -I/i/boost-trunk -MMD -MP -MF build/Debug/MinGW_Clang-Windows/warnings_1.o.d -o build/Debug/MinGW_Clang-Windows/warnings_1.o warnings_1.cpp warnings_1.cpp:97:7: warning: unused variable 'unused_too' [-Wunused-variable] int unused_too; // Expect a warning. ^ 1 warning generated. * * * but for MSVC * ClCompile: warning_1.cpp warning_1.cpp(77): warning C4068: unknown pragma warning_1.cpp(78): warning C4068: unknown pragma warning_1.cpp(81): warning C4068: unknown pragma warning_1.cpp(82): warning C4068: unknown pragma warning_1.cpp(83): warning C4068: unknown pragma warning_1.cpp(84): warning C4068: unknown pragma warning_1.cpp(85): warning C4305: 'initializing' : truncation from 'int' to 'char' warning_1.cpp(85): warning C4309: 'initializing' : truncation of constant value warning_1.cpp(86): warning C4068: unknown pragma warning_1.cpp(90): warning C4068: unknown pragma warning_1.cpp(99): warning C4101: 'unused_too' : unreferenced local variable warning_1.cpp(96): warning C4101: 'unused_two' : unreferenced local variable warning_1.cpp(85): warning C4189: 'b' : local variable is initialized but not referenced warning_1.cpp(91): warning C4101: 'unused' : unreferenced local variable j:\cpp\svg\warning_1\warning_1.cpp(66): warning C4701: potentially uninitialized local variable 'x' used */