Advertisement
jimklimov

GCC/CLANG/MSVS warning disable pragmas

Jan 8th, 2017
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.71 KB | None | 0 0
  1. #ifndef WARNING_DISABLE_PRAGMAS_H
  2. #define WARNING_DISABLE_PRAGMAS_H
  3.  
  4. /* This pragmatic code is courtesy of Martin Gerhardy posted on the web at
  5.  *   http://stackoverflow.com/questions/3378560/how-to-disable-gcc-warnings-for-a-few-lines-of-code
  6.  * and further integrated to NUT by Jim Klimov. From original post comments:
  7.  * ...This should do the trick for gcc, clang and msvc
  8.  * Can be called with e.g.:
  9. DISABLE_WARNING(unused-variable,unused-variable,42)
  10. [.... some code with warnings in here ....]
  11. ENABLE_WARNING(unused-variable,unused-variable,42)
  12.  * see https://gcc.gnu.org/onlinedocs/cpp/Pragmas.html
  13.  * and http://clang.llvm.org/docs/UsersManual.html#controlling-diagnostics-via-pragmas
  14.  * and https://msdn.microsoft.com/de-DE/library/d9x1s805.aspx for more details
  15.  * You need at least version 4.02 to use these kind of pragmas for gcc,
  16.  * not sure about msvc and clang about the versions.
  17.  */
  18. #if defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER)
  19. # define DIAG_STR(s) #s
  20. # define DIAG_JOINSTR(x,y) DIAG_STR(x ## y)
  21. # ifdef _MSC_VER
  22. #  define DIAG_DO_PRAGMA(x) __pragma (#x)
  23. #  define DIAG_PRAGMA(compiler,x) DIAG_DO_PRAGMA(warning(x))
  24. # else
  25. #  define DIAG_DO_PRAGMA(x) _Pragma (#x)
  26. #  define DIAG_PRAGMA(compiler,x) DIAG_DO_PRAGMA(compiler diagnostic x)
  27. # endif
  28. #else
  29. # define DIAG_STR(S)
  30. # define DIAG_JOINSTR(x,y)
  31. # define DIAG_DO_PRAGMA(x)
  32. # define DIAG_PRAGMA(compiler,x)
  33. #endif
  34.  
  35. #if defined(__clang__)
  36. # define DISABLE_WARNING(gcc_unused,clang_option,msvc_unused) DIAG_PRAGMA(clang,push) DIAG_PRAGMA(clang,ignored DIAG_JOINSTR(-W,clang_option))
  37. # define ENABLE_WARNING(gcc_unused,clang_option,msvc_unused) DIAG_PRAGMA(clang,pop)
  38. #elif defined(_MSC_VER)
  39. # define DISABLE_WARNING(gcc_unused,clang_unused,msvc_errorcode) DIAG_PRAGMA(msvc,push) DIAG_DO_PRAGMA(warning(disable:##msvc_errorcode))
  40. # define ENABLE_WARNING(gcc_unused,clang_unused,msvc_errorcode) DIAG_PRAGMA(msvc,pop)
  41. #elif defined(__GNUC__)
  42. # if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
  43. #  define DISABLE_WARNING(gcc_option,clang_unused,msvc_unused) DIAG_PRAGMA(GCC,push) DIAG_PRAGMA(GCC,ignored DIAG_JOINSTR(-W,gcc_option))
  44. #  define ENABLE_WARNING(gcc_option,clang_unused,msvc_unused) DIAG_PRAGMA(GCC,pop)
  45. # else
  46. #  define DISABLE_WARNING(gcc_option,clang_unused,msvc_unused) DIAG_PRAGMA(GCC,ignored DIAG_JOINSTR(-W,gcc_option))
  47. #  define ENABLE_WARNING(gcc_option,clang_option,msvc_unused) DIAG_PRAGMA(GCC,warning DIAG_JOINSTR(-W,gcc_option))
  48. # endif
  49. #else
  50. # define DISABLE_WARNING(gcc_option,clang_unused,msvc_unused) /* DISABLE_WARNING not supported for this compiler */
  51. # define ENABLE_WARNING(gcc_option,clang_unused,msvc_unused)  /* ENABLE_WARNING not supported for this compiler */
  52. #endif
  53.  
  54. #endif /* WARNING_DISABLE_PRAGMAS_H */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement