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