libc-diag.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Macros for controlling diagnostic output from the compiler.
  2. Copyright (C) 2014-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifndef _LIBC_DIAG_H
  16. #define _LIBC_DIAG_H 1
  17. /* Ignore the value of an expression when a cast to void does not
  18. suffice (in particular, for a call to a function declared with
  19. attribute warn_unused_result). */
  20. #define ignore_value(x) \
  21. ({ __typeof__ (x) __ignored_value = (x); (void) __ignored_value; })
  22. /* The macros to control diagnostics are structured like this, rather
  23. than a single macro that both pushes and pops diagnostic state and
  24. takes the affected code as an argument, because the GCC pragmas
  25. work by disabling the diagnostic for a range of source locations
  26. and do not work when all the pragmas and the affected code are in a
  27. single macro expansion. */
  28. /* Push diagnostic state. */
  29. #define DIAG_PUSH_NEEDS_COMMENT _Pragma ("GCC diagnostic push")
  30. /* Pop diagnostic state. */
  31. #define DIAG_POP_NEEDS_COMMENT _Pragma ("GCC diagnostic pop")
  32. /* These macros are used to push/pop diagnostic states for warnings only
  33. supported by clang. */
  34. #ifdef __clang__
  35. # define DIAG_PUSH_NEEDS_COMMENT_CLANG _Pragma ("clang diagnostic push")
  36. # define DIAG_POP_NEEDS_COMMENT_CLANG _Pragma ("clang diagnostic pop")
  37. #else
  38. # define DIAG_PUSH_NEEDS_COMMENT_CLANG
  39. # define DIAG_POP_NEEDS_COMMENT_CLANG
  40. #endif
  41. #define _DIAG_STR1(s) #s
  42. #define _DIAG_STR(s) _DIAG_STR1(s)
  43. /* Ignore the diagnostic OPTION. VERSION is the most recent GCC
  44. version for which the diagnostic has been confirmed to appear in
  45. the absence of the pragma (in the form MAJOR.MINOR for GCC 4.x,
  46. just MAJOR for GCC 5 and later). Uses of this pragma should be
  47. reviewed when the GCC version given is no longer supported for
  48. building glibc; the version number should always be on the same
  49. source line as the macro name, so such uses can be found with grep.
  50. Uses should come with a comment giving more details of the
  51. diagnostic, and an architecture on which it is seen if possibly
  52. optimization-related and not in architecture-specific code. This
  53. macro should only be used if the diagnostic seems hard to fix (for
  54. example, optimization-related false positives). */
  55. #define DIAG_IGNORE_NEEDS_COMMENT(version, option) \
  56. _Pragma (_DIAG_STR (GCC diagnostic ignored option))
  57. /* Similar to DIAG_IGNORE_NEEDS_COMMENT the following macro ignores the
  58. diagnostic OPTION but only if optimizations for size are enabled.
  59. This is required because different warnings may be generated for
  60. different optimization levels. For example a key piece of code may
  61. only generate a warning when compiled at -Os, but at -O2 you could
  62. still want the warning to be enabled to catch errors. In this case
  63. you would use DIAG_IGNORE_Os_NEEDS_COMMENT to disable the warning
  64. only for -Os. */
  65. #ifdef __OPTIMIZE_SIZE__
  66. # define DIAG_IGNORE_Os_NEEDS_COMMENT(version, option) \
  67. _Pragma (_DIAG_STR (GCC diagnostic ignored option))
  68. #else
  69. # define DIAG_IGNORE_Os_NEEDS_COMMENT(version, option)
  70. #endif
  71. /* Similar to DIAG_IGNORE_NEEDS_COMMENT, these macros should be used
  72. to suppress warning supported by the specific compiler. */
  73. #ifndef __clang__
  74. # define DIAG_IGNORE_NEEDS_COMMENT_GCC(VERSION, WARNING) \
  75. DIAG_IGNORE_NEEDS_COMMENT (VERSION, WARNING)
  76. # define DIAG_IGNORE_Os_NEEDS_COMMENT_GCC(VERSION, WARNING) \
  77. DIAG_IGNORE_Os_NEEDS_COMMENT (VERSION, WARNING)
  78. # define DIAG_IGNORE_NEEDS_COMMENT_CLANG(version, option)
  79. #else
  80. # define DIAG_IGNORE_NEEDS_COMMENT_GCC(VERSION, WARNING)
  81. # define DIAG_IGNORE_Os_NEEDS_COMMENT_GCC(VERSION, WARNING)
  82. # define DIAG_IGNORE_NEEDS_COMMENT_CLANG(version, option) \
  83. _Pragma (_DIAG_STR (clang diagnostic ignored option))
  84. #endif
  85. #endif /* libc-diag.h */