Home | History | Annotate | Line # | Download | only in dist
      1 /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
      2 /*
      3  * Copyright (c) 1993, 1994, 1995, 1996, 1997
      4  *	The Regents of the University of California.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by the Computer Systems
     17  *	Engineering Group at Lawrence Berkeley Laboratory.
     18  * 4. Neither the name of the University nor of the Laboratory may be used
     19  *    to endorse or promote products derived from this software without
     20  *    specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #ifndef lib_funcattrs_h
     36 #define lib_funcattrs_h
     37 
     38 #include "compiler-tests.h"
     39 
     40 /*
     41  * Attributes to apply to functions and their arguments, using various
     42  * compiler-specific extensions.
     43  */
     44 
     45 /*
     46  * NORETURN, before a function declaration, means "this function
     47  * never returns".  (It must go before the function declaration, e.g.
     48  * "extern NORETURN func(...)" rather than after the function
     49  * declaration, as the MSVC version has to go before the declaration.)
     50  */
     51 #if __has_attribute(noreturn) \
     52     || ND_IS_AT_LEAST_GNUC_VERSION(2,5) \
     53     || ND_IS_AT_LEAST_SUNC_VERSION(5,9) \
     54     || ND_IS_AT_LEAST_XL_C_VERSION(10,1) \
     55     || ND_IS_AT_LEAST_HP_C_VERSION(6,10) \
     56     || __TINYC__
     57   /*
     58    * Compiler with support for __attribute((noreturn)), or GCC 2.5 and
     59    * later, or some compiler asserting compatibility with GCC 2.5 and
     60    * later, or Solaris Studio 12 (Sun C 5.9) and later, or IBM XL C 10.1
     61    * and later (do any earlier versions of XL C support this?), or HP aCC
     62    * A.06.10 and later, or current TinyCC.
     63    */
     64   #define NORETURN __attribute((noreturn))
     65 
     66   /*
     67    * However, GCC didn't support that for function *pointers* until GCC
     68    * 4.1.0; see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3481.
     69    *
     70    * Sun C/Oracle Studio C doesn't seem to support it, either.
     71    */
     72   #if (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) < 401)) \
     73       || (defined(__SUNPRO_C))
     74     #define NORETURN_FUNCPTR
     75   #else
     76     #define NORETURN_FUNCPTR __attribute((noreturn))
     77   #endif
     78 #elif defined(_MSC_VER)
     79   /*
     80    * MSVC.
     81    * It doesn't allow __declspec(noreturn) to be applied to function
     82    * pointers.
     83    */
     84   #define NORETURN __declspec(noreturn)
     85   #define NORETURN_FUNCPTR
     86 #else
     87   #define NORETURN
     88   #define NORETURN_FUNCPTR
     89 #endif
     90 
     91 /*
     92  * WARN_UNUSED_RESULT, before a function declaration, means "the caller
     93  * should use the result of this function" (even if it's just a success/
     94  * failure indication).
     95  */
     96 #if __has_attribute(warn_unused_result) \
     97     || ND_IS_AT_LEAST_GNUC_VERSION(3,4) \
     98     || ND_IS_AT_LEAST_HP_C_VERSION(6,25)
     99   #define WARN_UNUSED_RESULT __attribute((warn_unused_result))
    100 #else
    101   #define WARN_UNUSED_RESULT
    102 #endif
    103 
    104 /*
    105  * PRINTFLIKE(x,y), after a function declaration, means "this function
    106  * does printf-style formatting, with the xth argument being the format
    107  * string and the yth argument being the first argument for the format
    108  * string".
    109  */
    110 #if __has_attribute(__format__) \
    111     || ND_IS_AT_LEAST_GNUC_VERSION(2,3) \
    112     || ND_IS_AT_LEAST_XL_C_VERSION(10,1) \
    113     || ND_IS_AT_LEAST_HP_C_VERSION(6,10)
    114   /*
    115    * Compiler with support for it, or GCC 2.3 and later, or some compiler
    116    * asserting compatibility with GCC 2.3 and later, or IBM XL C 10.1
    117    * and later (do any earlier versions of XL C support this?),
    118    * or HP aCC A.06.10 and later.
    119    */
    120   #define PRINTFLIKE(x,y) __attribute__((__format__(__printf__,x,y)))
    121 
    122   /*
    123    * However, GCC didn't support that for function *pointers* until GCC
    124    * 4.1.0; see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3481.
    125    * XL C 16.1 (and possibly some earlier versions, but not 12.1 or 13.1) has
    126    * a similar bug, the bugfix for which was made in:
    127    * * version 16.1.1.8 for Linux (25 June 2020), which fixes
    128    *   https://www.ibm.com/support/pages/apar/LI81402
    129    * * version 16.1.0.5 for AIX (5 May 2020), which fixes
    130    *   https://www.ibm.com/support/pages/apar/IJ24678
    131    *
    132    * When testing versions, keep in mind that XL C 16.1 pretends to be both
    133    * GCC 4.2 and Clang 4.0 at once.
    134    */
    135   #if (ND_IS_AT_LEAST_GNUC_VERSION(4,1) \
    136        && !ND_IS_AT_LEAST_XL_C_VERSION(10,1)) \
    137       || (ND_IS_AT_LEAST_XL_C_VERSION(16,1) \
    138           && (ND_IS_AT_LEAST_XL_C_MODFIX(1, 8) && defined(__linux__)) \
    139               || (ND_IS_AT_LEAST_XL_C_MODFIX(0, 5) && defined(_AIX)))
    140     #define PRINTFLIKE_FUNCPTR(x,y) __attribute__((__format__(__printf__,x,y)))
    141   #endif
    142 #endif
    143 
    144 #if !defined(PRINTFLIKE)
    145 #define PRINTFLIKE(x,y)
    146 #endif
    147 #if !defined(PRINTFLIKE_FUNCPTR)
    148 #define PRINTFLIKE_FUNCPTR(x,y)
    149 #endif
    150 
    151 /*
    152  * For flagging arguments as format strings in MSVC.
    153  */
    154 #ifdef _MSC_VER
    155  #include <sal.h>
    156  #define FORMAT_STRING(p) _Printf_format_string_ p
    157 #else
    158  #define FORMAT_STRING(p) p
    159 #endif
    160 
    161 #endif /* lib_funcattrs_h */
    162