warn-on-use.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* A C macro for emitting warnings if a function is used.
  2. Copyright (C) 2010-2011 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 <http://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. This macro is useful for marking a function as a potential
  19. portability trap, with the intent that "literal string" include
  20. instructions on the replacement function that should be used
  21. instead. However, one of the reasons that a function is a
  22. portability trap is if it has the wrong signature. Declaring
  23. FUNCTION with a different signature in C is a compilation error, so
  24. this macro must use the same type as any existing declaration so
  25. that programs that avoid the problematic FUNCTION do not fail to
  26. compile merely because they included a header that poisoned the
  27. function. But this implies that _GL_WARN_ON_USE is only safe to
  28. use if FUNCTION is known to already have a declaration. Use of
  29. this macro implies that there must not be any other macro hiding
  30. the declaration of FUNCTION; but undefining FUNCTION first is part
  31. of the poisoning process anyway (although for symbols that are
  32. provided only via a macro, the result is a compilation error rather
  33. than a warning containing "literal string"). Also note that in
  34. C++, it is only safe to use if FUNCTION has no overloads.
  35. For an example, it is possible to poison 'getline' by:
  36. - adding a call to gl_WARN_ON_USE_PREPARE([[#include <stdio.h>]],
  37. [getline]) in configure.ac, which potentially defines
  38. HAVE_RAW_DECL_GETLINE
  39. - adding this code to a header that wraps the system <stdio.h>:
  40. #undef getline
  41. #if HAVE_RAW_DECL_GETLINE
  42. _GL_WARN_ON_USE (getline, "getline is required by POSIX 2008, but"
  43. "not universally present; use the gnulib module getline");
  44. #endif
  45. It is not possible to directly poison global variables. But it is
  46. possible to write a wrapper accessor function, and poison that
  47. (less common usage, like &environ, will cause a compilation error
  48. rather than issue the nice warning, but the end result of informing
  49. the developer about their portability problem is still achieved):
  50. #if HAVE_RAW_DECL_ENVIRON
  51. static inline char ***rpl_environ (void) { return &environ; }
  52. _GL_WARN_ON_USE (rpl_environ, "environ is not always properly declared");
  53. # undef environ
  54. # define environ (*rpl_environ ())
  55. #endif
  56. */
  57. #ifndef _GL_WARN_ON_USE
  58. # if 4 < __GNUC__ || (__GNUC__ == 4 && 3 <= __GNUC_MINOR__)
  59. /* A compiler attribute is available in gcc versions 4.3.0 and later. */
  60. # define _GL_WARN_ON_USE(function, message) \
  61. extern __typeof__ (function) function __attribute__ ((__warning__ (message)))
  62. # elif __GNUC__ >= 3 && GNULIB_STRICT_CHECKING
  63. /* Verify the existence of the function. */
  64. # define _GL_WARN_ON_USE(function, message) \
  65. extern __typeof__ (function) function
  66. # else /* Unsupported. */
  67. # define _GL_WARN_ON_USE(function, message) \
  68. _GL_WARN_EXTERN_C int _gl_warn_on_use
  69. # endif
  70. #endif
  71. /* _GL_WARN_ON_USE_CXX (function, rettype, parameters_and_attributes, "string")
  72. is like _GL_WARN_ON_USE (function, "string"), except that the function is
  73. declared with the given prototype, consisting of return type, parameters,
  74. and attributes.
  75. This variant is useful for overloaded functions in C++. _GL_WARN_ON_USE does
  76. not work in this case. */
  77. #ifndef _GL_WARN_ON_USE_CXX
  78. # if 4 < __GNUC__ || (__GNUC__ == 4 && 3 <= __GNUC_MINOR__)
  79. # define _GL_WARN_ON_USE_CXX(function,rettype,parameters_and_attributes,msg) \
  80. extern rettype function parameters_and_attributes \
  81. __attribute__ ((__warning__ (msg)))
  82. # elif __GNUC__ >= 3 && GNULIB_STRICT_CHECKING
  83. /* Verify the existence of the function. */
  84. # define _GL_WARN_ON_USE_CXX(function,rettype,parameters_and_attributes,msg) \
  85. extern rettype function parameters_and_attributes
  86. # else /* Unsupported. */
  87. # define _GL_WARN_ON_USE_CXX(function,rettype,parameters_and_attributes,msg) \
  88. _GL_WARN_EXTERN_C int _gl_warn_on_use
  89. # endif
  90. #endif
  91. /* _GL_WARN_EXTERN_C declaration;
  92. performs the declaration with C linkage. */
  93. #ifndef _GL_WARN_EXTERN_C
  94. # if defined __cplusplus
  95. # define _GL_WARN_EXTERN_C extern "C"
  96. # else
  97. # define _GL_WARN_EXTERN_C extern
  98. # endif
  99. #endif