Blame lib/warn-on-use.h

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