xref: /illumos-gate/usr/src/uts/common/sys/ccompile.h (revision 74e12c43fe52f2c30f36e65a4d0fb0e8dfd7068a)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * Copyright 2015 EveryCity Ltd. All rights reserved.
28  * Copyright 2019 Joyent, Inc.
29  */
30 
31 #ifndef	_SYS_CCOMPILE_H
32 #define	_SYS_CCOMPILE_H
33 
34 /*
35  * This file contains definitions designed to enable different compilers
36  * to be used harmoniously on Solaris systems.
37  */
38 
39 #ifdef	__cplusplus
40 extern "C" {
41 #endif
42 
43 /*
44  * Allow for version tests for compiler bugs and features.
45  */
46 #if defined(__GNUC__)
47 #define	__GNUC_VERSION	\
48 	(__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
49 #else
50 #define	__GNUC_VERSION	0
51 #endif
52 
53 #if defined(__ATTRIBUTE_IMPLEMENTED) || defined(__GNUC__)
54 
55 /*
56  * analogous to lint's PRINTFLIKEn
57  */
58 #define	__sun_attr___PRINTFLIKE__(__n)	\
59 		__attribute__((__format__(printf, __n, (__n)+1)))
60 #define	__sun_attr___VPRINTFLIKE__(__n)	\
61 		__attribute__((__format__(printf, __n, 0)))
62 
63 /*
64  * Handle the kernel printf routines that can take '%b' too
65  */
66 #if __GNUC_VERSION < 30402
67 /*
68  * XX64 at least this doesn't work correctly yet with 3.4.1 anyway!
69  */
70 #define	__sun_attr___KPRINTFLIKE__	__sun_attr___PRINTFLIKE__
71 #define	__sun_attr___KVPRINTFLIKE__	__sun_attr___VPRINTFLIKE__
72 #else
73 #define	__sun_attr___KPRINTFLIKE__(__n)	\
74 		__attribute__((__format__(cmn_err, __n, (__n)+1)))
75 #define	__sun_attr___KVPRINTFLIKE__(__n) \
76 		__attribute__((__format__(cmn_err, __n, 0)))
77 #endif
78 
79 /*
80  * This one's pretty obvious -- the function never returns
81  */
82 #define	__sun_attr___noreturn__ __attribute__((__noreturn__))
83 
84 /*
85  * The function is 'extern inline' and expects GNU C89 behaviour, not C99
86  * behaviour.
87  *
88  * Should only be used on 'extern inline' definitions for GCC.
89  */
90 #if __GNUC_VERSION >= 40200
91 #define	__sun_attr___gnu_inline__	__attribute__((__gnu_inline__))
92 #else
93 #define	__sun_attr___gnu_inline__
94 #endif
95 
96 /*
97  * The function has control flow such that it may return multiple times (in
98  * the manner of setjmp or vfork)
99  */
100 #if __GNUC_VERSION >= 40100
101 #define	__sun_attr___returns_twice__	__attribute__((__returns_twice__))
102 #else
103 #define	__sun_attr___returns_twice__
104 #endif
105 
106 /*
107  * This is an appropriate label for functions that do not
108  * modify their arguments, e.g. strlen()
109  */
110 #define	__sun_attr___pure__	__attribute__((__pure__))
111 
112 /*
113  * This is a stronger form of __pure__. Can be used for functions
114  * that do not modify their arguments and don't depend on global
115  * memory.
116  */
117 #define	__sun_attr___const__	__attribute__((__const__))
118 
119 #if __GNUC_VERSION >= 20700
120 #define	__aligned(x)		__attribute__((__aligned__(x)))
121 /*
122  * This attribute, attached to a variable, means that the variable is meant to
123  * be possibly unused. GCC will not produce a warning for this variable.
124  */
125 #define	__sun_attr___unused__	__attribute__((__unused__))
126 #endif
127 
128 #define	___sun_attr_inner(__a)	__sun_attr_##__a
129 #define	__sun_attr__(__a)	___sun_attr_inner __a
130 
131 #else	/* __ATTRIBUTE_IMPLEMENTED || __GNUC__ */
132 
133 #define	__aligned(x)
134 #define	__sun_attr__(__a)
135 #define	__sun_attr___unused__
136 
137 #endif	/* __ATTRIBUTE_IMPLEMENTED || __GNUC__ */
138 
139 #if __GNUC_VERSION >= 40100
140 #define	__sentinel(__n)	__attribute__((__sentinel__(__n)))
141 #else
142 #define	__sentinel(__n)
143 #endif
144 
145 /*
146  * Shorthand versions for readability
147  */
148 
149 #define	__PRINTFLIKE(__n)	__sun_attr__((__PRINTFLIKE__(__n)))
150 #define	__VPRINTFLIKE(__n)	__sun_attr__((__VPRINTFLIKE__(__n)))
151 #define	__KPRINTFLIKE(__n)	__sun_attr__((__KPRINTFLIKE__(__n)))
152 #define	__KVPRINTFLIKE(__n)	__sun_attr__((__KVPRINTFLIKE__(__n)))
153 #define	__NORETURN		__sun_attr__((__noreturn__))
154 #define	__GNU_INLINE		__inline__ __sun_attr__((__gnu_inline__))
155 #define	__RETURNS_TWICE		__sun_attr__((__returns_twice__))
156 #define	__CONST			__sun_attr__((__const__))
157 #define	__PURE			__sun_attr__((__pure__))
158 #define	__packed		__attribute__((__packed__))
159 #define	__section(x)		__attribute__((__section__(x)))
160 #define	__unused		__sun_attr__((__unused__))
161 #define	__used			__attribute__((__used__))
162 #define	__weak_symbol		__attribute__((__weak__))
163 #define	__HIDDEN		__attribute__((visibility("hidden")))
164 
165 #ifdef	__cplusplus
166 }
167 #endif
168 
169 #endif	/* _SYS_CCOMPILE_H */
170