xref: /dflybsd-src/contrib/tcpdump/funcattrs.h (revision 59c07fbdf8168fa08c76c515186d561b5a92690c)
1411677aeSAaron LI /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
2411677aeSAaron LI /*
3411677aeSAaron LI  * Copyright (c) 1993, 1994, 1995, 1996, 1997
4411677aeSAaron LI  *	The Regents of the University of California.  All rights reserved.
5411677aeSAaron LI  *
6411677aeSAaron LI  * Redistribution and use in source and binary forms, with or without
7411677aeSAaron LI  * modification, are permitted provided that the following conditions
8411677aeSAaron LI  * are met:
9411677aeSAaron LI  * 1. Redistributions of source code must retain the above copyright
10411677aeSAaron LI  *    notice, this list of conditions and the following disclaimer.
11411677aeSAaron LI  * 2. Redistributions in binary form must reproduce the above copyright
12411677aeSAaron LI  *    notice, this list of conditions and the following disclaimer in the
13411677aeSAaron LI  *    documentation and/or other materials provided with the distribution.
14411677aeSAaron LI  * 3. All advertising materials mentioning features or use of this software
15411677aeSAaron LI  *    must display the following acknowledgement:
16411677aeSAaron LI  *	This product includes software developed by the Computer Systems
17411677aeSAaron LI  *	Engineering Group at Lawrence Berkeley Laboratory.
18411677aeSAaron LI  * 4. Neither the name of the University nor of the Laboratory may be used
19411677aeSAaron LI  *    to endorse or promote products derived from this software without
20411677aeSAaron LI  *    specific prior written permission.
21411677aeSAaron LI  *
22411677aeSAaron LI  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23411677aeSAaron LI  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24411677aeSAaron LI  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25411677aeSAaron LI  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26411677aeSAaron LI  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27411677aeSAaron LI  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28411677aeSAaron LI  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29411677aeSAaron LI  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30411677aeSAaron LI  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31411677aeSAaron LI  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32411677aeSAaron LI  * SUCH DAMAGE.
33411677aeSAaron LI  */
34411677aeSAaron LI 
35411677aeSAaron LI #ifndef lib_funcattrs_h
36411677aeSAaron LI #define lib_funcattrs_h
37411677aeSAaron LI 
38*ed775ee7SAntonio Huete Jimenez #include "compiler-tests.h"
39*ed775ee7SAntonio Huete Jimenez 
40411677aeSAaron LI /*
41411677aeSAaron LI  * Attributes to apply to functions and their arguments, using various
42411677aeSAaron LI  * compiler-specific extensions.
43411677aeSAaron LI  */
44411677aeSAaron LI 
45411677aeSAaron LI /*
46411677aeSAaron LI  * This was introduced by Clang:
47411677aeSAaron LI  *
48*ed775ee7SAntonio Huete Jimenez  *     https://clang.llvm.org/docs/LanguageExtensions.html#has-attribute
49411677aeSAaron LI  *
50411677aeSAaron LI  * in some version (which version?); it has been picked up by GCC 5.0.
51411677aeSAaron LI  */
52411677aeSAaron LI #ifndef __has_attribute
53411677aeSAaron LI   /*
54411677aeSAaron LI    * It's a macro, so you can check whether it's defined to check
55411677aeSAaron LI    * whether it's supported.
56411677aeSAaron LI    *
57411677aeSAaron LI    * If it's not, define it to always return 0, so that we move on to
58411677aeSAaron LI    * the fallback checks.
59411677aeSAaron LI    */
60411677aeSAaron LI   #define __has_attribute(x) 0
61411677aeSAaron LI #endif
62411677aeSAaron LI 
63411677aeSAaron LI /*
64411677aeSAaron LI  * NORETURN, before a function declaration, means "this function
65411677aeSAaron LI  * never returns".  (It must go before the function declaration, e.g.
66411677aeSAaron LI  * "extern NORETURN func(...)" rather than after the function
67411677aeSAaron LI  * declaration, as the MSVC version has to go before the declaration.)
68411677aeSAaron LI  */
69411677aeSAaron LI #if __has_attribute(noreturn) \
70*ed775ee7SAntonio Huete Jimenez     || ND_IS_AT_LEAST_GNUC_VERSION(2,5) \
71*ed775ee7SAntonio Huete Jimenez     || ND_IS_AT_LEAST_SUNC_VERSION(5,9) \
72*ed775ee7SAntonio Huete Jimenez     || ND_IS_AT_LEAST_XL_C_VERSION(10,1) \
73*ed775ee7SAntonio Huete Jimenez     || ND_IS_AT_LEAST_HP_C_VERSION(6,10)
74411677aeSAaron LI   /*
75411677aeSAaron LI    * Compiler with support for __attribute((noreturn)), or GCC 2.5 and
76411677aeSAaron LI    * later, or Solaris Studio 12 (Sun C 5.9) and later, or IBM XL C 10.1
77411677aeSAaron LI    * and later (do any earlier versions of XL C support this?), or
78411677aeSAaron LI    * HP aCC A.06.10 and later.
79411677aeSAaron LI    */
80411677aeSAaron LI   #define NORETURN __attribute((noreturn))
81*ed775ee7SAntonio Huete Jimenez 
82*ed775ee7SAntonio Huete Jimenez   /*
83*ed775ee7SAntonio Huete Jimenez    * However, GCC didn't support that for function *pointers* until GCC
84*ed775ee7SAntonio Huete Jimenez    * 4.1.0; see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3481.
85*ed775ee7SAntonio Huete Jimenez    *
86*ed775ee7SAntonio Huete Jimenez    * Sun C/Oracle Studio C doesn't seem to support it, either.
87*ed775ee7SAntonio Huete Jimenez    */
88*ed775ee7SAntonio Huete Jimenez   #if (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) < 401)) \
89*ed775ee7SAntonio Huete Jimenez       || (defined(__SUNPRO_C))
90*ed775ee7SAntonio Huete Jimenez     #define NORETURN_FUNCPTR
91*ed775ee7SAntonio Huete Jimenez   #else
92*ed775ee7SAntonio Huete Jimenez     #define NORETURN_FUNCPTR __attribute((noreturn))
93*ed775ee7SAntonio Huete Jimenez   #endif
94411677aeSAaron LI #elif defined(_MSC_VER)
95411677aeSAaron LI   /*
96411677aeSAaron LI    * MSVC.
97*ed775ee7SAntonio Huete Jimenez    * It doesn't allow __declspec(noreturn) to be applied to function
98*ed775ee7SAntonio Huete Jimenez    * pointers.
99411677aeSAaron LI    */
100411677aeSAaron LI   #define NORETURN __declspec(noreturn)
101*ed775ee7SAntonio Huete Jimenez   #define NORETURN_FUNCPTR
102411677aeSAaron LI #else
103411677aeSAaron LI   #define NORETURN
104*ed775ee7SAntonio Huete Jimenez   #define NORETURN_FUNCPTR
105411677aeSAaron LI #endif
106411677aeSAaron LI 
107411677aeSAaron LI /*
108411677aeSAaron LI  * PRINTFLIKE(x,y), after a function declaration, means "this function
109411677aeSAaron LI  * does printf-style formatting, with the xth argument being the format
110411677aeSAaron LI  * string and the yth argument being the first argument for the format
111411677aeSAaron LI  * string".
112411677aeSAaron LI  */
113411677aeSAaron LI #if __has_attribute(__format__) \
114*ed775ee7SAntonio Huete Jimenez     || ND_IS_AT_LEAST_GNUC_VERSION(2,3) \
115*ed775ee7SAntonio Huete Jimenez     || ND_IS_AT_LEAST_XL_C_VERSION(10,1) \
116*ed775ee7SAntonio Huete Jimenez     || ND_IS_AT_LEAST_HP_C_VERSION(6,10)
117411677aeSAaron LI   /*
118411677aeSAaron LI    * Compiler with support for it, or GCC 2.3 and later, or IBM XL C 10.1
119411677aeSAaron LI    * and later (do any earlier versions of XL C support this?),
120411677aeSAaron LI    * or HP aCC A.06.10 and later.
121411677aeSAaron LI    */
122411677aeSAaron LI   #define PRINTFLIKE(x,y) __attribute__((__format__(__printf__,x,y)))
123*ed775ee7SAntonio Huete Jimenez 
124*ed775ee7SAntonio Huete Jimenez   /*
125*ed775ee7SAntonio Huete Jimenez    * However, GCC didn't support that for function *pointers* until GCC
126*ed775ee7SAntonio Huete Jimenez    * 4.1.0; see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3481.
127*ed775ee7SAntonio Huete Jimenez    */
128*ed775ee7SAntonio Huete Jimenez   #if (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) < 401))
129*ed775ee7SAntonio Huete Jimenez     #define PRINTFLIKE_FUNCPTR(x,y)
130*ed775ee7SAntonio Huete Jimenez   #else
131*ed775ee7SAntonio Huete Jimenez     #define PRINTFLIKE_FUNCPTR(x,y) __attribute__((__format__(__printf__,x,y)))
132*ed775ee7SAntonio Huete Jimenez   #endif
133411677aeSAaron LI #else
134411677aeSAaron LI   #define PRINTFLIKE(x,y)
135*ed775ee7SAntonio Huete Jimenez   #define PRINTFLIKE_FUNCPTR(x,y)
136411677aeSAaron LI #endif
137411677aeSAaron LI 
138411677aeSAaron LI /*
139411677aeSAaron LI  * For flagging arguments as format strings in MSVC.
140411677aeSAaron LI  */
141*ed775ee7SAntonio Huete Jimenez #ifdef _MSC_VER
142411677aeSAaron LI  #include <sal.h>
143411677aeSAaron LI  #define FORMAT_STRING(p) _Printf_format_string_ p
144411677aeSAaron LI #else
145411677aeSAaron LI  #define FORMAT_STRING(p) p
146411677aeSAaron LI #endif
147411677aeSAaron LI 
148411677aeSAaron LI #endif /* lib_funcattrs_h */
149