Blame gnulib/lib/warn-on-use.h

Packit 06dd63
/* A C macro for emitting warnings if a function is used.
Packit 06dd63
   Copyright (C) 2010-2019 Free Software Foundation, Inc.
Packit 06dd63
Packit 06dd63
   This program is free software: you can redistribute it and/or modify it
Packit 06dd63
   under the terms of the GNU Lesser General Public License as published
Packit 06dd63
   by the Free Software Foundation; either version 2.1 of the License, or
Packit 06dd63
   (at your option) any later version.
Packit 06dd63
Packit 06dd63
   This program is distributed in the hope that it will be useful,
Packit 06dd63
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 06dd63
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 06dd63
   Lesser General Public License for more details.
Packit 06dd63
Packit 06dd63
   You should have received a copy of the GNU Lesser General Public License
Packit 06dd63
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit 06dd63
Packit 06dd63
/* _GL_WARN_ON_USE (function, "literal string") issues a declaration
Packit 06dd63
   for FUNCTION which will then trigger a compiler warning containing
Packit 06dd63
   the text of "literal string" anywhere that function is called, if
Packit 06dd63
   supported by the compiler.  If the compiler does not support this
Packit 06dd63
   feature, the macro expands to an unused extern declaration.
Packit 06dd63
Packit 06dd63
   _GL_WARN_ON_USE_ATTRIBUTE ("literal string") expands to the
Packit 06dd63
   attribute used in _GL_WARN_ON_USE.  If the compiler does not support
Packit 06dd63
   this feature, it expands to empty.
Packit 06dd63
Packit 06dd63
   These macros are useful for marking a function as a potential
Packit 06dd63
   portability trap, with the intent that "literal string" include
Packit 06dd63
   instructions on the replacement function that should be used
Packit 06dd63
   instead.
Packit 06dd63
   _GL_WARN_ON_USE is for functions with 'extern' linkage.
Packit 06dd63
   _GL_WARN_ON_USE_ATTRIBUTE is for functions with 'static' or 'inline'
Packit 06dd63
   linkage.
Packit 06dd63
Packit 06dd63
   However, one of the reasons that a function is a portability trap is
Packit 06dd63
   if it has the wrong signature.  Declaring FUNCTION with a different
Packit 06dd63
   signature in C is a compilation error, so this macro must use the
Packit 06dd63
   same type as any existing declaration so that programs that avoid
Packit 06dd63
   the problematic FUNCTION do not fail to compile merely because they
Packit 06dd63
   included a header that poisoned the function.  But this implies that
Packit 06dd63
   _GL_WARN_ON_USE is only safe to use if FUNCTION is known to already
Packit 06dd63
   have a declaration.  Use of this macro implies that there must not
Packit 06dd63
   be any other macro hiding the declaration of FUNCTION; but
Packit 06dd63
   undefining FUNCTION first is part of the poisoning process anyway
Packit 06dd63
   (although for symbols that are provided only via a macro, the result
Packit 06dd63
   is a compilation error rather than a warning containing
Packit 06dd63
   "literal string").  Also note that in C++, it is only safe to use if
Packit 06dd63
   FUNCTION has no overloads.
Packit 06dd63
Packit 06dd63
   For an example, it is possible to poison 'getline' by:
Packit 06dd63
   - adding a call to gl_WARN_ON_USE_PREPARE([[#include <stdio.h>]],
Packit 06dd63
     [getline]) in configure.ac, which potentially defines
Packit 06dd63
     HAVE_RAW_DECL_GETLINE
Packit 06dd63
   - adding this code to a header that wraps the system <stdio.h>:
Packit 06dd63
     #undef getline
Packit 06dd63
     #if HAVE_RAW_DECL_GETLINE
Packit 06dd63
     _GL_WARN_ON_USE (getline, "getline is required by POSIX 2008, but"
Packit 06dd63
       "not universally present; use the gnulib module getline");
Packit 06dd63
     #endif
Packit 06dd63
Packit 06dd63
   It is not possible to directly poison global variables.  But it is
Packit 06dd63
   possible to write a wrapper accessor function, and poison that
Packit 06dd63
   (less common usage, like &environ, will cause a compilation error
Packit 06dd63
   rather than issue the nice warning, but the end result of informing
Packit 06dd63
   the developer about their portability problem is still achieved):
Packit 06dd63
     #if HAVE_RAW_DECL_ENVIRON
Packit 06dd63
     static char ***
Packit 06dd63
     rpl_environ (void) { return &environ; }
Packit 06dd63
     _GL_WARN_ON_USE (rpl_environ, "environ is not always properly declared");
Packit 06dd63
     # undef environ
Packit 06dd63
     # define environ (*rpl_environ ())
Packit 06dd63
     #endif
Packit 06dd63
   or better (avoiding contradictory use of 'static' and 'extern'):
Packit 06dd63
     #if HAVE_RAW_DECL_ENVIRON
Packit 06dd63
     static char ***
Packit 06dd63
     _GL_WARN_ON_USE_ATTRIBUTE ("environ is not always properly declared")
Packit 06dd63
     rpl_environ (void) { return &environ; }
Packit 06dd63
     # undef environ
Packit 06dd63
     # define environ (*rpl_environ ())
Packit 06dd63
     #endif
Packit 06dd63
   */
Packit 06dd63
#ifndef _GL_WARN_ON_USE
Packit 06dd63
Packit 06dd63
# if 4 < __GNUC__ || (__GNUC__ == 4 && 3 <= __GNUC_MINOR__)
Packit 06dd63
/* A compiler attribute is available in gcc versions 4.3.0 and later.  */
Packit 06dd63
#  define _GL_WARN_ON_USE(function, message) \
Packit 06dd63
extern __typeof__ (function) function __attribute__ ((__warning__ (message)))
Packit 06dd63
#  define _GL_WARN_ON_USE_ATTRIBUTE(message) \
Packit 06dd63
  __attribute__ ((__warning__ (message)))
Packit 06dd63
# elif __GNUC__ >= 3 && GNULIB_STRICT_CHECKING
Packit 06dd63
/* Verify the existence of the function.  */
Packit 06dd63
#  define _GL_WARN_ON_USE(function, message) \
Packit 06dd63
extern __typeof__ (function) function
Packit 06dd63
#  define _GL_WARN_ON_USE_ATTRIBUTE(message)
Packit 06dd63
# else /* Unsupported.  */
Packit 06dd63
#  define _GL_WARN_ON_USE(function, message) \
Packit 06dd63
_GL_WARN_EXTERN_C int _gl_warn_on_use
Packit 06dd63
#  define _GL_WARN_ON_USE_ATTRIBUTE(message)
Packit 06dd63
# endif
Packit 06dd63
#endif
Packit 06dd63
Packit 06dd63
/* _GL_WARN_ON_USE_CXX (function, rettype, parameters_and_attributes, "string")
Packit 06dd63
   is like _GL_WARN_ON_USE (function, "string"), except that the function is
Packit 06dd63
   declared with the given prototype, consisting of return type, parameters,
Packit 06dd63
   and attributes.
Packit 06dd63
   This variant is useful for overloaded functions in C++. _GL_WARN_ON_USE does
Packit 06dd63
   not work in this case.  */
Packit 06dd63
#ifndef _GL_WARN_ON_USE_CXX
Packit 06dd63
# if 4 < __GNUC__ || (__GNUC__ == 4 && 3 <= __GNUC_MINOR__)
Packit 06dd63
#  define _GL_WARN_ON_USE_CXX(function,rettype,parameters_and_attributes,msg) \
Packit 06dd63
extern rettype function parameters_and_attributes \
Packit 06dd63
     __attribute__ ((__warning__ (msg)))
Packit 06dd63
# elif __GNUC__ >= 3 && GNULIB_STRICT_CHECKING
Packit 06dd63
/* Verify the existence of the function.  */
Packit 06dd63
#  define _GL_WARN_ON_USE_CXX(function,rettype,parameters_and_attributes,msg) \
Packit 06dd63
extern rettype function parameters_and_attributes
Packit 06dd63
# else /* Unsupported.  */
Packit 06dd63
#  define _GL_WARN_ON_USE_CXX(function,rettype,parameters_and_attributes,msg) \
Packit 06dd63
_GL_WARN_EXTERN_C int _gl_warn_on_use
Packit 06dd63
# endif
Packit 06dd63
#endif
Packit 06dd63
Packit 06dd63
/* _GL_WARN_EXTERN_C declaration;
Packit 06dd63
   performs the declaration with C linkage.  */
Packit 06dd63
#ifndef _GL_WARN_EXTERN_C
Packit 06dd63
# if defined __cplusplus
Packit 06dd63
#  define _GL_WARN_EXTERN_C extern "C"
Packit 06dd63
# else
Packit 06dd63
#  define _GL_WARN_EXTERN_C extern
Packit 06dd63
# endif
Packit 06dd63
#endif