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