Blame lib/warn-on-use.h

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