diagnostics.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Copyright (C) 2017-2022 Free Software Foundation, Inc.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  12. #ifndef DIAGNOSTICS_H
  13. #define DIAGNOSTICS_H
  14. /* If at all possible, fix the source rather than using these macros
  15. to silence warnings. If you do use these macros be aware that
  16. you'll need to condition their use on particular compiler versions,
  17. which can be done for gcc using ansidecl.h's GCC_VERSION macro.
  18. gcc versions between 4.2 and 4.6 do not allow pragma control of
  19. diagnostics inside functions, giving a hard error if you try to use
  20. the finer control available with later versions.
  21. gcc prior to 4.2 warns about diagnostic push and pop.
  22. The other macros have restrictions too, for example gcc-5, gcc-6
  23. and gcc-7 warn that -Wstringop-truncation is unknown, unless you
  24. also add DIAGNOSTIC_IGNORE ("-Wpragma"). */
  25. #ifdef __GNUC__
  26. # define DIAGNOSTIC_PUSH _Pragma ("GCC diagnostic push")
  27. # define DIAGNOSTIC_POP _Pragma ("GCC diagnostic pop")
  28. /* Stringification. */
  29. # define DIAGNOSTIC_STRINGIFY_1(x) #x
  30. # define DIAGNOSTIC_STRINGIFY(x) DIAGNOSTIC_STRINGIFY_1 (x)
  31. # define DIAGNOSTIC_IGNORE(option) \
  32. _Pragma (DIAGNOSTIC_STRINGIFY (GCC diagnostic ignored option))
  33. # define DIAGNOSTIC_ERROR(option) \
  34. _Pragma (DIAGNOSTIC_STRINGIFY (GCC diagnostic error option))
  35. #else
  36. # define DIAGNOSTIC_PUSH
  37. # define DIAGNOSTIC_POP
  38. # define DIAGNOSTIC_IGNORE(option)
  39. #endif
  40. #if defined (__clang__) /* clang */
  41. # define DIAGNOSTIC_IGNORE_SELF_MOVE DIAGNOSTIC_IGNORE ("-Wself-move")
  42. # define DIAGNOSTIC_IGNORE_DEPRECATED_DECLARATIONS \
  43. DIAGNOSTIC_IGNORE ("-Wdeprecated-declarations")
  44. # define DIAGNOSTIC_IGNORE_DEPRECATED_REGISTER \
  45. DIAGNOSTIC_IGNORE ("-Wdeprecated-register")
  46. # if __has_warning ("-Wenum-compare-switch")
  47. # define DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES \
  48. DIAGNOSTIC_IGNORE ("-Wenum-compare-switch")
  49. # endif
  50. # define DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL \
  51. DIAGNOSTIC_IGNORE ("-Wformat-nonliteral")
  52. # define DIAGNOSTIC_ERROR_SWITCH \
  53. DIAGNOSTIC_ERROR ("-Wswitch")
  54. #elif defined (__GNUC__) /* GCC */
  55. # if __GNUC__ >= 7
  56. # define DIAGNOSTIC_IGNORE_DEPRECATED_REGISTER \
  57. DIAGNOSTIC_IGNORE ("-Wregister")
  58. # endif
  59. # define DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION \
  60. DIAGNOSTIC_IGNORE ("-Wstringop-truncation")
  61. # define DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL \
  62. DIAGNOSTIC_IGNORE ("-Wformat-nonliteral")
  63. /* GCC 4.8's "diagnostic push/pop" seems broken when using this, -Wswitch
  64. remains enabled at the error level even after a pop. Therefore, don't
  65. use it for GCC < 5. */
  66. # if __GNUC__ >= 5
  67. # define DIAGNOSTIC_ERROR_SWITCH DIAGNOSTIC_ERROR ("-Wswitch")
  68. # endif
  69. #endif
  70. #ifndef DIAGNOSTIC_IGNORE_SELF_MOVE
  71. # define DIAGNOSTIC_IGNORE_SELF_MOVE
  72. #endif
  73. #ifndef DIAGNOSTIC_IGNORE_DEPRECATED_DECLARATIONS
  74. # define DIAGNOSTIC_IGNORE_DEPRECATED_DECLARATIONS
  75. #endif
  76. #ifndef DIAGNOSTIC_IGNORE_DEPRECATED_REGISTER
  77. # define DIAGNOSTIC_IGNORE_DEPRECATED_REGISTER
  78. #endif
  79. #ifndef DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
  80. # define DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
  81. #endif
  82. #ifndef DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION
  83. # define DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION
  84. #endif
  85. #ifndef DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
  86. # define DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
  87. #endif
  88. #ifndef DIAGNOSTIC_ERROR_SWITCH
  89. # define DIAGNOSTIC_ERROR_SWITCH
  90. #endif
  91. #endif /* DIAGNOSTICS_H */