Blame lib/warn-on-use.h

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