xref: /freebsd-src/contrib/tcpdump/compiler-tests.h (revision ee67461e56828dd1f8de165947ba83f6d9148a87)
1*ee67461eSJoseph Mingrone /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
2*ee67461eSJoseph Mingrone /*
3*ee67461eSJoseph Mingrone  * Copyright (c) 1993, 1994, 1995, 1996, 1997
4*ee67461eSJoseph Mingrone  *	The Regents of the University of California.  All rights reserved.
5*ee67461eSJoseph Mingrone  *
6*ee67461eSJoseph Mingrone  * Redistribution and use in source and binary forms, with or without
7*ee67461eSJoseph Mingrone  * modification, are permitted provided that the following conditions
8*ee67461eSJoseph Mingrone  * are met:
9*ee67461eSJoseph Mingrone  * 1. Redistributions of source code must retain the above copyright
10*ee67461eSJoseph Mingrone  *    notice, this list of conditions and the following disclaimer.
11*ee67461eSJoseph Mingrone  * 2. Redistributions in binary form must reproduce the above copyright
12*ee67461eSJoseph Mingrone  *    notice, this list of conditions and the following disclaimer in the
13*ee67461eSJoseph Mingrone  *    documentation and/or other materials provided with the distribution.
14*ee67461eSJoseph Mingrone  * 3. All advertising materials mentioning features or use of this software
15*ee67461eSJoseph Mingrone  *    must display the following acknowledgement:
16*ee67461eSJoseph Mingrone  *	This product includes software developed by the Computer Systems
17*ee67461eSJoseph Mingrone  *	Engineering Group at Lawrence Berkeley Laboratory.
18*ee67461eSJoseph Mingrone  * 4. Neither the name of the University nor of the Laboratory may be used
19*ee67461eSJoseph Mingrone  *    to endorse or promote products derived from this software without
20*ee67461eSJoseph Mingrone  *    specific prior written permission.
21*ee67461eSJoseph Mingrone  *
22*ee67461eSJoseph Mingrone  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*ee67461eSJoseph Mingrone  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*ee67461eSJoseph Mingrone  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*ee67461eSJoseph Mingrone  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*ee67461eSJoseph Mingrone  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*ee67461eSJoseph Mingrone  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*ee67461eSJoseph Mingrone  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*ee67461eSJoseph Mingrone  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*ee67461eSJoseph Mingrone  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*ee67461eSJoseph Mingrone  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*ee67461eSJoseph Mingrone  * SUCH DAMAGE.
33*ee67461eSJoseph Mingrone  */
34*ee67461eSJoseph Mingrone 
35*ee67461eSJoseph Mingrone #ifndef nd_compiler_tests_h
36*ee67461eSJoseph Mingrone #define nd_compiler_tests_h
37*ee67461eSJoseph Mingrone 
38*ee67461eSJoseph Mingrone /*
39*ee67461eSJoseph Mingrone  * This was introduced by Clang:
40*ee67461eSJoseph Mingrone  *
41*ee67461eSJoseph Mingrone  *     https://clang.llvm.org/docs/LanguageExtensions.html#has-attribute
42*ee67461eSJoseph Mingrone  *
43*ee67461eSJoseph Mingrone  * in some version (which version?); it has been picked up by GCC 5.0.
44*ee67461eSJoseph Mingrone  */
45*ee67461eSJoseph Mingrone #ifndef __has_attribute
46*ee67461eSJoseph Mingrone   /*
47*ee67461eSJoseph Mingrone    * It's a macro, so you can check whether it's defined to check
48*ee67461eSJoseph Mingrone    * whether it's supported.
49*ee67461eSJoseph Mingrone    *
50*ee67461eSJoseph Mingrone    * If it's not, define it to always return 0, so that we move on to
51*ee67461eSJoseph Mingrone    * the fallback checks.
52*ee67461eSJoseph Mingrone    */
53*ee67461eSJoseph Mingrone   #define __has_attribute(x) 0
54*ee67461eSJoseph Mingrone #endif
55*ee67461eSJoseph Mingrone 
56*ee67461eSJoseph Mingrone /*
57*ee67461eSJoseph Mingrone  * Note that the C90 spec's "6.8.1 Conditional inclusion" and the
58*ee67461eSJoseph Mingrone  * C99 spec's and C11 spec's "6.10.1 Conditional inclusion" say:
59*ee67461eSJoseph Mingrone  *
60*ee67461eSJoseph Mingrone  *    Prior to evaluation, macro invocations in the list of preprocessing
61*ee67461eSJoseph Mingrone  *    tokens that will become the controlling constant expression are
62*ee67461eSJoseph Mingrone  *    replaced (except for those macro names modified by the defined unary
63*ee67461eSJoseph Mingrone  *    operator), just as in normal text.  If the token "defined" is
64*ee67461eSJoseph Mingrone  *    generated as a result of this replacement process or use of the
65*ee67461eSJoseph Mingrone  *    "defined" unary operator does not match one of the two specified
66*ee67461eSJoseph Mingrone  *    forms prior to macro replacement, the behavior is undefined.
67*ee67461eSJoseph Mingrone  *
68*ee67461eSJoseph Mingrone  * so you shouldn't use defined() in a #define that's used in #if or
69*ee67461eSJoseph Mingrone  * #elif.  Some versions of Clang, for example, will warn about this.
70*ee67461eSJoseph Mingrone  *
71*ee67461eSJoseph Mingrone  * Instead, we check whether the pre-defined macros for particular
72*ee67461eSJoseph Mingrone  * compilers are defined and, if not, define the "is this version XXX
73*ee67461eSJoseph Mingrone  * or a later version of this compiler" macros as 0.
74*ee67461eSJoseph Mingrone  */
75*ee67461eSJoseph Mingrone 
76*ee67461eSJoseph Mingrone /*
77*ee67461eSJoseph Mingrone  * Check whether this is GCC major.minor or a later release, or some
78*ee67461eSJoseph Mingrone  * compiler that claims to be "just like GCC" of that version or a
79*ee67461eSJoseph Mingrone  * later release.
80*ee67461eSJoseph Mingrone  */
81*ee67461eSJoseph Mingrone 
82*ee67461eSJoseph Mingrone #if ! defined(__GNUC__)
83*ee67461eSJoseph Mingrone   /* Not GCC and not "just like GCC" */
84*ee67461eSJoseph Mingrone   #define ND_IS_AT_LEAST_GNUC_VERSION(major, minor) 0
85*ee67461eSJoseph Mingrone #else
86*ee67461eSJoseph Mingrone   /* GCC or "just like GCC" */
87*ee67461eSJoseph Mingrone   #define ND_IS_AT_LEAST_GNUC_VERSION(major, minor) \
88*ee67461eSJoseph Mingrone 	(__GNUC__ > (major) || \
89*ee67461eSJoseph Mingrone 	 (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
90*ee67461eSJoseph Mingrone #endif
91*ee67461eSJoseph Mingrone 
92*ee67461eSJoseph Mingrone /*
93*ee67461eSJoseph Mingrone  * Check whether this is Clang major.minor or a later release.
94*ee67461eSJoseph Mingrone  */
95*ee67461eSJoseph Mingrone 
96*ee67461eSJoseph Mingrone #if !defined(__clang__)
97*ee67461eSJoseph Mingrone   /* Not Clang */
98*ee67461eSJoseph Mingrone   #define ND_IS_AT_LEAST_CLANG_VERSION(major, minor) 0
99*ee67461eSJoseph Mingrone #else
100*ee67461eSJoseph Mingrone   /* Clang */
101*ee67461eSJoseph Mingrone   #define ND_IS_AT_LEAST_CLANG_VERSION(major, minor) \
102*ee67461eSJoseph Mingrone 	(__clang_major__ > (major) || \
103*ee67461eSJoseph Mingrone 	 (__clang_major__ == (major) && __clang_minor__ >= (minor)))
104*ee67461eSJoseph Mingrone #endif
105*ee67461eSJoseph Mingrone 
106*ee67461eSJoseph Mingrone /*
107*ee67461eSJoseph Mingrone  * Check whether this is Sun C/SunPro C/Oracle Studio major.minor
108*ee67461eSJoseph Mingrone  * or a later release.
109*ee67461eSJoseph Mingrone  *
110*ee67461eSJoseph Mingrone  * The version number in __SUNPRO_C is encoded in hex BCD, with the
111*ee67461eSJoseph Mingrone  * uppermost hex digit being the major version number, the next
112*ee67461eSJoseph Mingrone  * one or two hex digits being the minor version number, and
113*ee67461eSJoseph Mingrone  * the last digit being the patch version.
114*ee67461eSJoseph Mingrone  *
115*ee67461eSJoseph Mingrone  * It represents the *compiler* version, not the product version;
116*ee67461eSJoseph Mingrone  * see
117*ee67461eSJoseph Mingrone  *
118*ee67461eSJoseph Mingrone  *    https://sourceforge.net/p/predef/wiki/Compilers/
119*ee67461eSJoseph Mingrone  *
120*ee67461eSJoseph Mingrone  * for a partial mapping, which we assume continues for later
121*ee67461eSJoseph Mingrone  * 12.x product releases.
122*ee67461eSJoseph Mingrone  */
123*ee67461eSJoseph Mingrone 
124*ee67461eSJoseph Mingrone #if ! defined(__SUNPRO_C)
125*ee67461eSJoseph Mingrone   /* Not Sun/Oracle C */
126*ee67461eSJoseph Mingrone   #define ND_IS_AT_LEAST_SUNC_VERSION(major,minor) 0
127*ee67461eSJoseph Mingrone #else
128*ee67461eSJoseph Mingrone   /* Sun/Oracle C */
129*ee67461eSJoseph Mingrone   #define ND_SUNPRO_VERSION_TO_BCD(major, minor) \
130*ee67461eSJoseph Mingrone 	(((minor) >= 10) ? \
131*ee67461eSJoseph Mingrone 	    (((major) << 12) | (((minor)/10) << 8) | (((minor)%10) << 4)) : \
132*ee67461eSJoseph Mingrone 	    (((major) << 8) | ((minor) << 4)))
133*ee67461eSJoseph Mingrone   #define ND_IS_AT_LEAST_SUNC_VERSION(major,minor) \
134*ee67461eSJoseph Mingrone 	(__SUNPRO_C >= ND_SUNPRO_VERSION_TO_BCD((major), (minor)))
135*ee67461eSJoseph Mingrone #endif
136*ee67461eSJoseph Mingrone 
137*ee67461eSJoseph Mingrone /*
138*ee67461eSJoseph Mingrone  * Check whether this is IBM XL C major.minor or a later release.
139*ee67461eSJoseph Mingrone  *
140*ee67461eSJoseph Mingrone  * The version number in __xlC__ has the major version in the
141*ee67461eSJoseph Mingrone  * upper 8 bits and the minor version in the lower 8 bits.
142*ee67461eSJoseph Mingrone  * On AIX __xlC__ is always defined, __ibmxl__ becomes defined in XL C 16.1.
143*ee67461eSJoseph Mingrone  * On Linux since XL C 13.1.6 __xlC__ is not defined by default anymore, but
144*ee67461eSJoseph Mingrone  * __ibmxl__ is defined since at least XL C 13.1.1.
145*ee67461eSJoseph Mingrone  */
146*ee67461eSJoseph Mingrone 
147*ee67461eSJoseph Mingrone #if ! defined(__xlC__) && ! defined(__ibmxl__)
148*ee67461eSJoseph Mingrone   /* Not XL C */
149*ee67461eSJoseph Mingrone   #define ND_IS_AT_LEAST_XL_C_VERSION(major,minor) 0
150*ee67461eSJoseph Mingrone   #define ND_IS_AT_LEAST_XL_C_MODFIX(modification, fixlevel) 0
151*ee67461eSJoseph Mingrone #else
152*ee67461eSJoseph Mingrone   /* XL C */
153*ee67461eSJoseph Mingrone   #if defined(__ibmxl__)
154*ee67461eSJoseph Mingrone     /*
155*ee67461eSJoseph Mingrone      * Later Linux version of XL C; use __ibmxl_version__ to test
156*ee67461eSJoseph Mingrone      * the version.
157*ee67461eSJoseph Mingrone      */
158*ee67461eSJoseph Mingrone     #define ND_IS_AT_LEAST_XL_C_VERSION(major, minor) \
159*ee67461eSJoseph Mingrone 	(__ibmxl_version__ > (major) || \
160*ee67461eSJoseph Mingrone 	 (__ibmxl_version__ == (major) && __ibmxl_release__ >= (minor)))
161*ee67461eSJoseph Mingrone     #define ND_IS_AT_LEAST_XL_C_MODFIX(modification, fixlevel) \
162*ee67461eSJoseph Mingrone 	(__ibmxl_modification__ > (modification) || \
163*ee67461eSJoseph Mingrone 	 (__ibmxl_modification__ == (modification) && \
164*ee67461eSJoseph Mingrone 	  __ibmxl_ptf_fix_level__ >= (fixlevel)))
165*ee67461eSJoseph Mingrone   #else /* __ibmxl__ */
166*ee67461eSJoseph Mingrone     /*
167*ee67461eSJoseph Mingrone      * __ibmxl__ not defined; use __xlC__ to test the version.
168*ee67461eSJoseph Mingrone      */
169*ee67461eSJoseph Mingrone     #define ND_IS_AT_LEAST_XL_C_VERSION(major, minor) \
170*ee67461eSJoseph Mingrone 	(__xlC__ >= (((major) << 8) | (minor)))
171*ee67461eSJoseph Mingrone     #define ND_IS_AT_LEAST_XL_C_MODFIX(modification, fixlevel) \
172*ee67461eSJoseph Mingrone 	((((modification) << 8) | (fixlevel)) >= __xlC_ver__)
173*ee67461eSJoseph Mingrone   #endif /* __ibmxl__ */
174*ee67461eSJoseph Mingrone #endif
175*ee67461eSJoseph Mingrone 
176*ee67461eSJoseph Mingrone /*
177*ee67461eSJoseph Mingrone  * Check whether this is HP aC++/HP C major.minor or a later release.
178*ee67461eSJoseph Mingrone  *
179*ee67461eSJoseph Mingrone  * The version number in __HP_aCC is encoded in zero-padded decimal BCD,
180*ee67461eSJoseph Mingrone  * with the "A." stripped off, the uppermost two decimal digits being
181*ee67461eSJoseph Mingrone  * the major version number, the next two decimal digits being the minor
182*ee67461eSJoseph Mingrone  * version number, and the last two decimal digits being the patch version.
183*ee67461eSJoseph Mingrone  * (Strip off the A., remove the . between the major and minor version
184*ee67461eSJoseph Mingrone  * number, and add two digits of patch.)
185*ee67461eSJoseph Mingrone  */
186*ee67461eSJoseph Mingrone 
187*ee67461eSJoseph Mingrone #if ! defined(__HP_aCC)
188*ee67461eSJoseph Mingrone   /* Not HP C */
189*ee67461eSJoseph Mingrone   #define ND_IS_AT_LEAST_HP_C_VERSION(major,minor) 0
190*ee67461eSJoseph Mingrone #else
191*ee67461eSJoseph Mingrone   /* HP C */
192*ee67461eSJoseph Mingrone   #define ND_IS_AT_LEAST_HP_C_VERSION(major,minor) \
193*ee67461eSJoseph Mingrone 	(__HP_aCC >= ((major)*10000 + (minor)*100))
194*ee67461eSJoseph Mingrone #endif
195*ee67461eSJoseph Mingrone 
196*ee67461eSJoseph Mingrone #endif /* nd_funcattrs_h */
197