warn-on-use.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* A C macro for emitting warnings if a function is used.
  2. Copyright (C) 2010-2021 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published
  5. by the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. /* _GL_WARN_ON_USE (function, "literal string") issues a declaration
  14. for FUNCTION which will then trigger a compiler warning containing
  15. the text of "literal string" anywhere that function is called, if
  16. supported by the compiler. If the compiler does not support this
  17. feature, the macro expands to an unused extern declaration.
  18. _GL_WARN_ON_USE_ATTRIBUTE ("literal string") expands to the
  19. attribute used in _GL_WARN_ON_USE. If the compiler does not support
  20. this feature, it expands to empty.
  21. These macros are useful for marking a function as a potential
  22. portability trap, with the intent that "literal string" include
  23. instructions on the replacement function that should be used
  24. instead.
  25. _GL_WARN_ON_USE is for functions with 'extern' linkage.
  26. _GL_WARN_ON_USE_ATTRIBUTE is for functions with 'static' or 'inline'
  27. linkage.
  28. However, one of the reasons that a function is a portability trap is
  29. if it has the wrong signature. Declaring FUNCTION with a different
  30. signature in C is a compilation error, so this macro must use the
  31. same type as any existing declaration so that programs that avoid
  32. the problematic FUNCTION do not fail to compile merely because they
  33. included a header that poisoned the function. But this implies that
  34. _GL_WARN_ON_USE is only safe to use if FUNCTION is known to already
  35. have a declaration. Use of this macro implies that there must not
  36. be any other macro hiding the declaration of FUNCTION; but
  37. undefining FUNCTION first is part of the poisoning process anyway
  38. (although for symbols that are provided only via a macro, the result
  39. is a compilation error rather than a warning containing
  40. "literal string"). Also note that in C++, it is only safe to use if
  41. FUNCTION has no overloads.
  42. For an example, it is possible to poison 'getline' by:
  43. - adding a call to gl_WARN_ON_USE_PREPARE([[#include <stdio.h>]],
  44. [getline]) in configure.ac, which potentially defines
  45. HAVE_RAW_DECL_GETLINE
  46. - adding this code to a header that wraps the system <stdio.h>:
  47. #undef getline
  48. #if HAVE_RAW_DECL_GETLINE
  49. _GL_WARN_ON_USE (getline, "getline is required by POSIX 2008, but"
  50. "not universally present; use the gnulib module getline");
  51. #endif
  52. It is not possible to directly poison global variables. But it is
  53. possible to write a wrapper accessor function, and poison that
  54. (less common usage, like &environ, will cause a compilation error
  55. rather than issue the nice warning, but the end result of informing
  56. the developer about their portability problem is still achieved):
  57. #if HAVE_RAW_DECL_ENVIRON
  58. static char ***
  59. rpl_environ (void) { return &environ; }
  60. _GL_WARN_ON_USE (rpl_environ, "environ is not always properly declared");
  61. # undef environ
  62. # define environ (*rpl_environ ())
  63. #endif
  64. or better (avoiding contradictory use of 'static' and 'extern'):
  65. #if HAVE_RAW_DECL_ENVIRON
  66. static char ***
  67. _GL_WARN_ON_USE_ATTRIBUTE ("environ is not always properly declared")
  68. rpl_environ (void) { return &environ; }
  69. # undef environ
  70. # define environ (*rpl_environ ())
  71. #endif
  72. */
  73. #ifndef _GL_WARN_ON_USE
  74. # if 4 < __GNUC__ || (__GNUC__ == 4 && 3 <= __GNUC_MINOR__)
  75. /* A compiler attribute is available in gcc versions 4.3.0 and later. */
  76. # define _GL_WARN_ON_USE(function, message) \
  77. extern __typeof__ (function) function __attribute__ ((__warning__ (message)))
  78. # define _GL_WARN_ON_USE_ATTRIBUTE(message) \
  79. __attribute__ ((__warning__ (message)))
  80. # elif __clang_major__ >= 4
  81. /* Another compiler attribute is available in clang. */
  82. # define _GL_WARN_ON_USE(function, message) \
  83. extern __typeof__ (function) function \
  84. __attribute__ ((__diagnose_if__ (1, message, "warning")))
  85. # define _GL_WARN_ON_USE_ATTRIBUTE(message) \
  86. __attribute__ ((__diagnose_if__ (1, message, "warning")))
  87. # elif __GNUC__ >= 3 && GNULIB_STRICT_CHECKING
  88. /* Verify the existence of the function. */
  89. # define _GL_WARN_ON_USE(function, message) \
  90. extern __typeof__ (function) function
  91. # define _GL_WARN_ON_USE_ATTRIBUTE(message)
  92. # else /* Unsupported. */
  93. # define _GL_WARN_ON_USE(function, message) \
  94. _GL_WARN_EXTERN_C int _gl_warn_on_use
  95. # define _GL_WARN_ON_USE_ATTRIBUTE(message)
  96. # endif
  97. #endif
  98. /* _GL_WARN_ON_USE_CXX (function, rettype_gcc, rettype_clang, parameters_and_attributes, "message")
  99. is like _GL_WARN_ON_USE (function, "message"), except that in C++ mode the
  100. function is declared with the given prototype, consisting of return type,
  101. parameters, and attributes.
  102. This variant is useful for overloaded functions in C++. _GL_WARN_ON_USE does
  103. not work in this case. */
  104. #ifndef _GL_WARN_ON_USE_CXX
  105. # if !defined __cplusplus
  106. # define _GL_WARN_ON_USE_CXX(function,rettype_gcc,rettype_clang,parameters_and_attributes,msg) \
  107. _GL_WARN_ON_USE (function, msg)
  108. # else
  109. # if 4 < __GNUC__ || (__GNUC__ == 4 && 3 <= __GNUC_MINOR__)
  110. /* A compiler attribute is available in gcc versions 4.3.0 and later. */
  111. # define _GL_WARN_ON_USE_CXX(function,rettype_gcc,rettype_clang,parameters_and_attributes,msg) \
  112. extern rettype_gcc function parameters_and_attributes \
  113. __attribute__ ((__warning__ (msg)))
  114. # elif __clang_major__ >= 4
  115. /* Another compiler attribute is available in clang. */
  116. # define _GL_WARN_ON_USE_CXX(function,rettype_gcc,rettype_clang,parameters_and_attributes,msg) \
  117. extern rettype_clang function parameters_and_attributes \
  118. __attribute__ ((__diagnose_if__ (1, msg, "warning")))
  119. # elif __GNUC__ >= 3 && GNULIB_STRICT_CHECKING
  120. /* Verify the existence of the function. */
  121. # define _GL_WARN_ON_USE_CXX(function,rettype_gcc,rettype_clang,parameters_and_attributes,msg) \
  122. extern rettype_gcc function parameters_and_attributes
  123. # else /* Unsupported. */
  124. # define _GL_WARN_ON_USE_CXX(function,rettype_gcc,rettype_clang,parameters_and_attributes,msg) \
  125. _GL_WARN_EXTERN_C int _gl_warn_on_use
  126. # endif
  127. # endif
  128. #endif
  129. /* _GL_WARN_EXTERN_C declaration;
  130. performs the declaration with C linkage. */
  131. #ifndef _GL_WARN_EXTERN_C
  132. # if defined __cplusplus
  133. # define _GL_WARN_EXTERN_C extern "C"
  134. # else
  135. # define _GL_WARN_EXTERN_C extern
  136. # endif
  137. #endif