Home | History | Annotate | Line # | Download | only in import
      1      1.1  christos /* A C macro for emitting warnings if a function is used.
      2  1.1.1.2  christos    Copyright (C) 2010-2022 Free Software Foundation, Inc.
      3      1.1  christos 
      4      1.1  christos    This program is free software: you can redistribute it and/or modify it
      5  1.1.1.2  christos    under the terms of the GNU Lesser General Public License as published
      6  1.1.1.2  christos    by the Free Software Foundation; either version 2 of the License, or
      7      1.1  christos    (at your option) any later version.
      8      1.1  christos 
      9      1.1  christos    This program is distributed in the hope that it will be useful,
     10      1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     11      1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12  1.1.1.2  christos    Lesser General Public License for more details.
     13      1.1  christos 
     14  1.1.1.2  christos    You should have received a copy of the GNU Lesser General Public License
     15      1.1  christos    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
     16      1.1  christos 
     17      1.1  christos /* _GL_WARN_ON_USE (function, "literal string") issues a declaration
     18      1.1  christos    for FUNCTION which will then trigger a compiler warning containing
     19      1.1  christos    the text of "literal string" anywhere that function is called, if
     20      1.1  christos    supported by the compiler.  If the compiler does not support this
     21      1.1  christos    feature, the macro expands to an unused extern declaration.
     22      1.1  christos 
     23      1.1  christos    _GL_WARN_ON_USE_ATTRIBUTE ("literal string") expands to the
     24      1.1  christos    attribute used in _GL_WARN_ON_USE.  If the compiler does not support
     25      1.1  christos    this feature, it expands to empty.
     26      1.1  christos 
     27      1.1  christos    These macros are useful for marking a function as a potential
     28      1.1  christos    portability trap, with the intent that "literal string" include
     29      1.1  christos    instructions on the replacement function that should be used
     30      1.1  christos    instead.
     31      1.1  christos    _GL_WARN_ON_USE is for functions with 'extern' linkage.
     32      1.1  christos    _GL_WARN_ON_USE_ATTRIBUTE is for functions with 'static' or 'inline'
     33      1.1  christos    linkage.
     34      1.1  christos 
     35      1.1  christos    However, one of the reasons that a function is a portability trap is
     36      1.1  christos    if it has the wrong signature.  Declaring FUNCTION with a different
     37      1.1  christos    signature in C is a compilation error, so this macro must use the
     38      1.1  christos    same type as any existing declaration so that programs that avoid
     39      1.1  christos    the problematic FUNCTION do not fail to compile merely because they
     40      1.1  christos    included a header that poisoned the function.  But this implies that
     41      1.1  christos    _GL_WARN_ON_USE is only safe to use if FUNCTION is known to already
     42      1.1  christos    have a declaration.  Use of this macro implies that there must not
     43      1.1  christos    be any other macro hiding the declaration of FUNCTION; but
     44      1.1  christos    undefining FUNCTION first is part of the poisoning process anyway
     45      1.1  christos    (although for symbols that are provided only via a macro, the result
     46      1.1  christos    is a compilation error rather than a warning containing
     47      1.1  christos    "literal string").  Also note that in C++, it is only safe to use if
     48      1.1  christos    FUNCTION has no overloads.
     49      1.1  christos 
     50      1.1  christos    For an example, it is possible to poison 'getline' by:
     51      1.1  christos    - adding a call to gl_WARN_ON_USE_PREPARE([[#include <stdio.h>]],
     52      1.1  christos      [getline]) in configure.ac, which potentially defines
     53      1.1  christos      HAVE_RAW_DECL_GETLINE
     54      1.1  christos    - adding this code to a header that wraps the system <stdio.h>:
     55      1.1  christos      #undef getline
     56      1.1  christos      #if HAVE_RAW_DECL_GETLINE
     57      1.1  christos      _GL_WARN_ON_USE (getline, "getline is required by POSIX 2008, but"
     58      1.1  christos        "not universally present; use the gnulib module getline");
     59      1.1  christos      #endif
     60      1.1  christos 
     61      1.1  christos    It is not possible to directly poison global variables.  But it is
     62      1.1  christos    possible to write a wrapper accessor function, and poison that
     63      1.1  christos    (less common usage, like &environ, will cause a compilation error
     64      1.1  christos    rather than issue the nice warning, but the end result of informing
     65      1.1  christos    the developer about their portability problem is still achieved):
     66      1.1  christos      #if HAVE_RAW_DECL_ENVIRON
     67      1.1  christos      static char ***
     68      1.1  christos      rpl_environ (void) { return &environ; }
     69      1.1  christos      _GL_WARN_ON_USE (rpl_environ, "environ is not always properly declared");
     70      1.1  christos      # undef environ
     71      1.1  christos      # define environ (*rpl_environ ())
     72      1.1  christos      #endif
     73      1.1  christos    or better (avoiding contradictory use of 'static' and 'extern'):
     74      1.1  christos      #if HAVE_RAW_DECL_ENVIRON
     75      1.1  christos      static char ***
     76      1.1  christos      _GL_WARN_ON_USE_ATTRIBUTE ("environ is not always properly declared")
     77      1.1  christos      rpl_environ (void) { return &environ; }
     78      1.1  christos      # undef environ
     79      1.1  christos      # define environ (*rpl_environ ())
     80      1.1  christos      #endif
     81      1.1  christos    */
     82      1.1  christos #ifndef _GL_WARN_ON_USE
     83      1.1  christos 
     84      1.1  christos # if 4 < __GNUC__ || (__GNUC__ == 4 && 3 <= __GNUC_MINOR__)
     85      1.1  christos /* A compiler attribute is available in gcc versions 4.3.0 and later.  */
     86      1.1  christos #  define _GL_WARN_ON_USE(function, message) \
     87  1.1.1.2  christos _GL_WARN_EXTERN_C __typeof__ (function) function __attribute__ ((__warning__ (message)))
     88      1.1  christos #  define _GL_WARN_ON_USE_ATTRIBUTE(message) \
     89      1.1  christos   __attribute__ ((__warning__ (message)))
     90  1.1.1.2  christos # elif __clang_major__ >= 4
     91  1.1.1.2  christos /* Another compiler attribute is available in clang.  */
     92  1.1.1.2  christos #  define _GL_WARN_ON_USE(function, message) \
     93  1.1.1.2  christos _GL_WARN_EXTERN_C __typeof__ (function) function \
     94  1.1.1.2  christos   __attribute__ ((__diagnose_if__ (1, message, "warning")))
     95  1.1.1.2  christos #  define _GL_WARN_ON_USE_ATTRIBUTE(message) \
     96  1.1.1.2  christos   __attribute__ ((__diagnose_if__ (1, message, "warning")))
     97      1.1  christos # elif __GNUC__ >= 3 && GNULIB_STRICT_CHECKING
     98      1.1  christos /* Verify the existence of the function.  */
     99      1.1  christos #  define _GL_WARN_ON_USE(function, message) \
    100  1.1.1.2  christos _GL_WARN_EXTERN_C __typeof__ (function) function
    101      1.1  christos #  define _GL_WARN_ON_USE_ATTRIBUTE(message)
    102      1.1  christos # else /* Unsupported.  */
    103      1.1  christos #  define _GL_WARN_ON_USE(function, message) \
    104      1.1  christos _GL_WARN_EXTERN_C int _gl_warn_on_use
    105      1.1  christos #  define _GL_WARN_ON_USE_ATTRIBUTE(message)
    106      1.1  christos # endif
    107      1.1  christos #endif
    108      1.1  christos 
    109  1.1.1.2  christos /* _GL_WARN_ON_USE_CXX (function, rettype_gcc, rettype_clang, parameters_and_attributes, "message")
    110  1.1.1.2  christos    is like _GL_WARN_ON_USE (function, "message"), except that in C++ mode the
    111      1.1  christos    function is declared with the given prototype, consisting of return type,
    112      1.1  christos    parameters, and attributes.
    113      1.1  christos    This variant is useful for overloaded functions in C++. _GL_WARN_ON_USE does
    114      1.1  christos    not work in this case.  */
    115      1.1  christos #ifndef _GL_WARN_ON_USE_CXX
    116      1.1  christos # if !defined __cplusplus
    117  1.1.1.2  christos #  define _GL_WARN_ON_USE_CXX(function,rettype_gcc,rettype_clang,parameters_and_attributes,msg) \
    118      1.1  christos      _GL_WARN_ON_USE (function, msg)
    119      1.1  christos # else
    120      1.1  christos #  if 4 < __GNUC__ || (__GNUC__ == 4 && 3 <= __GNUC_MINOR__)
    121  1.1.1.2  christos /* A compiler attribute is available in gcc versions 4.3.0 and later.  */
    122  1.1.1.2  christos #   define _GL_WARN_ON_USE_CXX(function,rettype_gcc,rettype_clang,parameters_and_attributes,msg) \
    123  1.1.1.2  christos extern rettype_gcc function parameters_and_attributes \
    124  1.1.1.2  christos   __attribute__ ((__warning__ (msg)))
    125  1.1.1.2  christos #  elif __clang_major__ >= 4
    126  1.1.1.2  christos /* Another compiler attribute is available in clang.  */
    127  1.1.1.2  christos #   define _GL_WARN_ON_USE_CXX(function,rettype_gcc,rettype_clang,parameters_and_attributes,msg) \
    128  1.1.1.2  christos extern rettype_clang function parameters_and_attributes \
    129  1.1.1.2  christos   __attribute__ ((__diagnose_if__ (1, msg, "warning")))
    130      1.1  christos #  elif __GNUC__ >= 3 && GNULIB_STRICT_CHECKING
    131      1.1  christos /* Verify the existence of the function.  */
    132  1.1.1.2  christos #   define _GL_WARN_ON_USE_CXX(function,rettype_gcc,rettype_clang,parameters_and_attributes,msg) \
    133  1.1.1.2  christos extern rettype_gcc function parameters_and_attributes
    134      1.1  christos #  else /* Unsupported.  */
    135  1.1.1.2  christos #   define _GL_WARN_ON_USE_CXX(function,rettype_gcc,rettype_clang,parameters_and_attributes,msg) \
    136      1.1  christos _GL_WARN_EXTERN_C int _gl_warn_on_use
    137      1.1  christos #  endif
    138      1.1  christos # endif
    139      1.1  christos #endif
    140      1.1  christos 
    141      1.1  christos /* _GL_WARN_EXTERN_C declaration;
    142      1.1  christos    performs the declaration with C linkage.  */
    143      1.1  christos #ifndef _GL_WARN_EXTERN_C
    144      1.1  christos # if defined __cplusplus
    145      1.1  christos #  define _GL_WARN_EXTERN_C extern "C"
    146      1.1  christos # else
    147      1.1  christos #  define _GL_WARN_EXTERN_C extern
    148      1.1  christos # endif
    149      1.1  christos #endif
    150