Blame build-aux/snippet/warn-on-use.h

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