xref: /freebsd/contrib/tcpdump/funcattrs.h (revision 0a7e5f1f02aad2ff5fff1c60f44c6975fd07e1d9)
10bff6a5aSEd Maste /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
20bff6a5aSEd Maste /*
30bff6a5aSEd Maste  * Copyright (c) 1993, 1994, 1995, 1996, 1997
40bff6a5aSEd Maste  *	The Regents of the University of California.  All rights reserved.
50bff6a5aSEd Maste  *
60bff6a5aSEd Maste  * Redistribution and use in source and binary forms, with or without
70bff6a5aSEd Maste  * modification, are permitted provided that the following conditions
80bff6a5aSEd Maste  * are met:
90bff6a5aSEd Maste  * 1. Redistributions of source code must retain the above copyright
100bff6a5aSEd Maste  *    notice, this list of conditions and the following disclaimer.
110bff6a5aSEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
120bff6a5aSEd Maste  *    notice, this list of conditions and the following disclaimer in the
130bff6a5aSEd Maste  *    documentation and/or other materials provided with the distribution.
140bff6a5aSEd Maste  * 3. All advertising materials mentioning features or use of this software
150bff6a5aSEd Maste  *    must display the following acknowledgement:
160bff6a5aSEd Maste  *	This product includes software developed by the Computer Systems
170bff6a5aSEd Maste  *	Engineering Group at Lawrence Berkeley Laboratory.
180bff6a5aSEd Maste  * 4. Neither the name of the University nor of the Laboratory may be used
190bff6a5aSEd Maste  *    to endorse or promote products derived from this software without
200bff6a5aSEd Maste  *    specific prior written permission.
210bff6a5aSEd Maste  *
220bff6a5aSEd Maste  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
230bff6a5aSEd Maste  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
240bff6a5aSEd Maste  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
250bff6a5aSEd Maste  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
260bff6a5aSEd Maste  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
270bff6a5aSEd Maste  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
280bff6a5aSEd Maste  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
290bff6a5aSEd Maste  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
300bff6a5aSEd Maste  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
310bff6a5aSEd Maste  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
320bff6a5aSEd Maste  * SUCH DAMAGE.
330bff6a5aSEd Maste  */
340bff6a5aSEd Maste 
350bff6a5aSEd Maste #ifndef lib_funcattrs_h
360bff6a5aSEd Maste #define lib_funcattrs_h
370bff6a5aSEd Maste 
38ee67461eSJoseph Mingrone #include "compiler-tests.h"
39ee67461eSJoseph Mingrone 
400bff6a5aSEd Maste /*
410bff6a5aSEd Maste  * Attributes to apply to functions and their arguments, using various
420bff6a5aSEd Maste  * compiler-specific extensions.
430bff6a5aSEd Maste  */
440bff6a5aSEd Maste 
450bff6a5aSEd Maste /*
460bff6a5aSEd Maste  * NORETURN, before a function declaration, means "this function
470bff6a5aSEd Maste  * never returns".  (It must go before the function declaration, e.g.
480bff6a5aSEd Maste  * "extern NORETURN func(...)" rather than after the function
490bff6a5aSEd Maste  * declaration, as the MSVC version has to go before the declaration.)
500bff6a5aSEd Maste  */
510bff6a5aSEd Maste #if __has_attribute(noreturn) \
52ee67461eSJoseph Mingrone     || ND_IS_AT_LEAST_GNUC_VERSION(2,5) \
53ee67461eSJoseph Mingrone     || ND_IS_AT_LEAST_SUNC_VERSION(5,9) \
54ee67461eSJoseph Mingrone     || ND_IS_AT_LEAST_XL_C_VERSION(10,1) \
55*0a7e5f1fSJoseph Mingrone     || ND_IS_AT_LEAST_HP_C_VERSION(6,10) \
56*0a7e5f1fSJoseph Mingrone     || __TINYC__
570bff6a5aSEd Maste   /*
580bff6a5aSEd Maste    * Compiler with support for __attribute((noreturn)), or GCC 2.5 and
59ee67461eSJoseph Mingrone    * later, or some compiler asserting compatibility with GCC 2.5 and
600bff6a5aSEd Maste    * later, or Solaris Studio 12 (Sun C 5.9) and later, or IBM XL C 10.1
61ee67461eSJoseph Mingrone    * and later (do any earlier versions of XL C support this?), or HP aCC
62*0a7e5f1fSJoseph Mingrone    * A.06.10 and later, or current TinyCC.
630bff6a5aSEd Maste    */
640bff6a5aSEd Maste   #define NORETURN __attribute((noreturn))
65ee67461eSJoseph Mingrone 
66ee67461eSJoseph Mingrone   /*
67ee67461eSJoseph Mingrone    * However, GCC didn't support that for function *pointers* until GCC
68ee67461eSJoseph Mingrone    * 4.1.0; see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3481.
69ee67461eSJoseph Mingrone    *
70ee67461eSJoseph Mingrone    * Sun C/Oracle Studio C doesn't seem to support it, either.
71ee67461eSJoseph Mingrone    */
72ee67461eSJoseph Mingrone   #if (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) < 401)) \
73ee67461eSJoseph Mingrone       || (defined(__SUNPRO_C))
74ee67461eSJoseph Mingrone     #define NORETURN_FUNCPTR
75ee67461eSJoseph Mingrone   #else
76ee67461eSJoseph Mingrone     #define NORETURN_FUNCPTR __attribute((noreturn))
77ee67461eSJoseph Mingrone   #endif
780bff6a5aSEd Maste #elif defined(_MSC_VER)
790bff6a5aSEd Maste   /*
800bff6a5aSEd Maste    * MSVC.
81ee67461eSJoseph Mingrone    * It doesn't allow __declspec(noreturn) to be applied to function
82ee67461eSJoseph Mingrone    * pointers.
830bff6a5aSEd Maste    */
840bff6a5aSEd Maste   #define NORETURN __declspec(noreturn)
85ee67461eSJoseph Mingrone   #define NORETURN_FUNCPTR
860bff6a5aSEd Maste #else
870bff6a5aSEd Maste   #define NORETURN
88ee67461eSJoseph Mingrone   #define NORETURN_FUNCPTR
89ee67461eSJoseph Mingrone #endif
90ee67461eSJoseph Mingrone 
91ee67461eSJoseph Mingrone /*
92ee67461eSJoseph Mingrone  * WARN_UNUSED_RESULT, before a function declaration, means "the caller
93ee67461eSJoseph Mingrone  * should use the result of this function" (even if it's just a success/
94ee67461eSJoseph Mingrone  * failure indication).
95ee67461eSJoseph Mingrone  */
96ee67461eSJoseph Mingrone #if __has_attribute(warn_unused_result) \
97ee67461eSJoseph Mingrone     || ND_IS_AT_LEAST_GNUC_VERSION(3,4) \
98ee67461eSJoseph Mingrone     || ND_IS_AT_LEAST_HP_C_VERSION(6,25)
99ee67461eSJoseph Mingrone   #define WARN_UNUSED_RESULT __attribute((warn_unused_result))
100ee67461eSJoseph Mingrone #else
101ee67461eSJoseph Mingrone   #define WARN_UNUSED_RESULT
1020bff6a5aSEd Maste #endif
1030bff6a5aSEd Maste 
1040bff6a5aSEd Maste /*
1050bff6a5aSEd Maste  * PRINTFLIKE(x,y), after a function declaration, means "this function
1060bff6a5aSEd Maste  * does printf-style formatting, with the xth argument being the format
1070bff6a5aSEd Maste  * string and the yth argument being the first argument for the format
1080bff6a5aSEd Maste  * string".
1090bff6a5aSEd Maste  */
1100bff6a5aSEd Maste #if __has_attribute(__format__) \
111ee67461eSJoseph Mingrone     || ND_IS_AT_LEAST_GNUC_VERSION(2,3) \
112ee67461eSJoseph Mingrone     || ND_IS_AT_LEAST_XL_C_VERSION(10,1) \
113ee67461eSJoseph Mingrone     || ND_IS_AT_LEAST_HP_C_VERSION(6,10)
1140bff6a5aSEd Maste   /*
115ee67461eSJoseph Mingrone    * Compiler with support for it, or GCC 2.3 and later, or some compiler
116ee67461eSJoseph Mingrone    * asserting compatibility with GCC 2.3 and later, or IBM XL C 10.1
1170bff6a5aSEd Maste    * and later (do any earlier versions of XL C support this?),
1180bff6a5aSEd Maste    * or HP aCC A.06.10 and later.
1190bff6a5aSEd Maste    */
1200bff6a5aSEd Maste   #define PRINTFLIKE(x,y) __attribute__((__format__(__printf__,x,y)))
121ee67461eSJoseph Mingrone 
122ee67461eSJoseph Mingrone   /*
123ee67461eSJoseph Mingrone    * However, GCC didn't support that for function *pointers* until GCC
124ee67461eSJoseph Mingrone    * 4.1.0; see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3481.
125ee67461eSJoseph Mingrone    * XL C 16.1 (and possibly some earlier versions, but not 12.1 or 13.1) has
126ee67461eSJoseph Mingrone    * a similar bug, the bugfix for which was made in:
127ee67461eSJoseph Mingrone    * * version 16.1.1.8 for Linux (25 June 2020), which fixes
128ee67461eSJoseph Mingrone    *   https://www.ibm.com/support/pages/apar/LI81402
129ee67461eSJoseph Mingrone    * * version 16.1.0.5 for AIX (5 May 2020), which fixes
130ee67461eSJoseph Mingrone    *   https://www.ibm.com/support/pages/apar/IJ24678
131ee67461eSJoseph Mingrone    *
132ee67461eSJoseph Mingrone    * When testing versions, keep in mind that XL C 16.1 pretends to be both
133ee67461eSJoseph Mingrone    * GCC 4.2 and Clang 4.0 at once.
134ee67461eSJoseph Mingrone    */
135ee67461eSJoseph Mingrone   #if (ND_IS_AT_LEAST_GNUC_VERSION(4,1) \
136ee67461eSJoseph Mingrone        && !ND_IS_AT_LEAST_XL_C_VERSION(10,1)) \
137ee67461eSJoseph Mingrone       || (ND_IS_AT_LEAST_XL_C_VERSION(16,1) \
138ee67461eSJoseph Mingrone           && (ND_IS_AT_LEAST_XL_C_MODFIX(1, 8) && defined(__linux__)) \
139ee67461eSJoseph Mingrone               || (ND_IS_AT_LEAST_XL_C_MODFIX(0, 5) && defined(_AIX)))
140ee67461eSJoseph Mingrone     #define PRINTFLIKE_FUNCPTR(x,y) __attribute__((__format__(__printf__,x,y)))
141ee67461eSJoseph Mingrone   #endif
142ee67461eSJoseph Mingrone #endif
143ee67461eSJoseph Mingrone 
144ee67461eSJoseph Mingrone #if !defined(PRINTFLIKE)
1450bff6a5aSEd Maste #define PRINTFLIKE(x,y)
1460bff6a5aSEd Maste #endif
147ee67461eSJoseph Mingrone #if !defined(PRINTFLIKE_FUNCPTR)
148ee67461eSJoseph Mingrone #define PRINTFLIKE_FUNCPTR(x,y)
149ee67461eSJoseph Mingrone #endif
1500bff6a5aSEd Maste 
1510bff6a5aSEd Maste /*
1520bff6a5aSEd Maste  * For flagging arguments as format strings in MSVC.
1530bff6a5aSEd Maste  */
154ee67461eSJoseph Mingrone #ifdef _MSC_VER
1550bff6a5aSEd Maste  #include <sal.h>
1560bff6a5aSEd Maste  #define FORMAT_STRING(p) _Printf_format_string_ p
1570bff6a5aSEd Maste #else
1580bff6a5aSEd Maste  #define FORMAT_STRING(p) p
1590bff6a5aSEd Maste #endif
1600bff6a5aSEd Maste 
1610bff6a5aSEd Maste #endif /* lib_funcattrs_h */
162