xref: /freebsd/share/man/man9/cdefs.9 (revision b64c5a0ace59af62eff52bfe110a521dc73c937b)
1.\"-
2.\" Copyright (c) 2024 M. Warner Losh <imp@FreeBSD.org>
3.\"
4.\" SPDX-License-Identifier: BSD-2-Clause
5.\"
6.Dd December 6, 2024
7.Dt CDEFS 9
8.Os
9.Sh NAME
10.Nm cdefs
11.Nd compiler portability macro definitions
12.Sh DESCRIPTION
13.In sys/cdefs.h
14defines macros for compiler, C language standard portability, POSIX standards
15compliance and symbol visibility.
16It defines programming interfaces for the system header files to adopt to the
17many environments
18.Fx
19supports compilation for.
20It defines convenience macros for the
21.Fx
22sources, tailored to the base
23system's needs.
24.Pp
25Most of these macros are for use inside the
26.Fx
27sources only.
28They are not intended as a general portability layer.
29.Sh Supported Compilers
30.Bl -tag -offset 2n -width 0
31.It Compilers supported for building programs on Fx :
32.Bl -column -offset 0n indent-two
33.It Sy Compiler Ta Sy Versions
34.It gcc Ta 9, 10, 11, 12, 13, 14
35.It clang Ta 10, 11, 12, 13, 14, 15, 16, 17, 18
36.It TinyC (tcc) Ta 0.9
37.It pcc Ta 1.1
38.El
39.Pp
40Due to testing constraints, tcc and pcc may not always work.
41.It Compilers supported for building Fx itself:
42.Bl -column -offset 0n indent-two
43.It Sy Compiler Ta Sy Major Versions Supported
44.It gcc Ta 12, 13, 14
45.It clang Ta 16, 17, 18
46.El
47.Pp
48Please note: Not every single minor versions of these compilers
49will work or are supported.
50.El
51.Sh Macros and Magic for Programming Environment
52.Nm
53defines (or refrains from defining) a number of macros to increase portability
54of compiled programs.
55These are to allow more advanced language features to appear in header files.
56The header files assume a compiler that accepts C prototype function
57declarations.
58They also assume that the compiler accepts ANSI C89 keywords for all language
59dialects.
60.Ss General Macros
61General macros that facilitate multiple language environments and language
62dialects.
63.Bl -column "---------------"
64.It Sy Macro Ta Sy Description
65.It Dv __volatile Ta expands to volatile in C++ and C89 and newer environments,
66__volatile in pre-ANSI environments that support this extension or nothing
67otherwise.
68.It Dv __inline Ta expands to inline in C++ and C89 and newer environments,
69__inline in pre-ANSI environments that support this extension or nothing
70otherwise.
71.It Dv __restrict Ta expands to restrict in C99 and newer environments, or
72__restrict otherwise.
73.It Dv __CONCAT Ta used to paste two pre-processor tokens.
74.It Dv __STRING Ta used to convert the argument to a string.
75.It Dv __BEGIN_DECLS Ta Start a group of functions.
76.It Dv __END_DECLS Ta End a group of functions.
77In a C environment, these are defined as nothing.
78In a C++ environment, these declare the functions to have
79.Dq C
80linkage.
81.El
82.Ss Function, Structure and Variable Modifiers
83.Bl -column "---------------"
84.It Sy Macro Ta Sy Description
85.It Sy __weak_symbol Ta Declare the symbol to be a weak symbol
86.It Sy __dead2 Ta Function will not return
87.It Sy __pure2 Ta Function has no side effects
88.It Sy __unused Ta To Variable may be unused (usually arguments), so do not
89warn about it
90.It Sy __used Ta Function really is used, so emit it even if it appears unused.
91.It Sy __deprecated Ta Function interface has been deprecated, and clients
92should migrate to a new interface.
93A warning will be issued for clients of this interface.
94.It Sy __deprecated1(msg) Ta Function interface has been deprecated, and clients
95should migrate to a new interface.
96The string
97.Fa msg
98will be included in a warning issued for clients of this interface.
99.It Sy __packed Ta \&Do not have space between structure elements for natural alignment.
100Used when communicating with external protocols.
101.It Sy __aligned(x) Ta Specify in bytes the minimum alignment for the specified field, structure or variable
102.It Sy __section(x) Ta Place function or variable in section Fa x
103.It Sy __writeonly Ta Hint that the variable is only assigned to, but do not warn about it.
104Useful for macros and other places the eventual use of the result is unknown.
105.It Sy __alloc_size(x) Ta The function always returns at least the number of
106bytes determined by argument number Fa x
107.It Sy __alloc_size2(x,n) Ta  The function always returns an array, whose size
108is at least the number of bytes determined by argument number Fa x times the
109number of elements specified by argument number Fa n
110.It Sy __alloc_align(x) Ta Function either returns a pointer aligned to Fa x bytes
111or Dv NULL.
112.It Sy __min_size Ta Declare the array to have a certain, minimum size
113.It Sy __malloc_like Ta Function behaves like the
114.Dq malloc
115family of functions.
116.It Sy __pure Ta Function has no side effects
117.It Sy __always_inline Ta Always inline this function when called
118.It Sy __fastcall Ta Use the
119.Dq fastcall
120ABI to call and name mangle this function.
121.It Sy __result_use_check Ta Warn if function caller does not use it's return value
122.It Sy __result_use_or_ignore_check Ta Warn if function caller does not use it's return value.
123Allows the value to be explicitly ignored with a (void) cast.
124.It Sy __returns_twice Ta Returns multiple times, like
125.Xr fork 2
126.It Sy __unreachable Ta This code is not reachable at runtime
127.It Sy __predict_true(x) Ta Hint to the compiler that
128.Fa x
129is true most of the time.
130Should only be used when performance is improved for a frequently called bit of code.
131.It Sy __predict_false(x) Ta Hint to the compiler that
132.Fa x
133is false most of the time.
134Should only be used when performance is improved for a frequently called bit of code.
135.It Sy __null_sentinel Ta The varadic function contains a parameter that is
136a NULL sentinel to mark the end of its arguments.
137.It Sy __exported Ta
138.It Sy __hidden Ta
139.It Sy __printflike(fmtarg,firstvararg) Ta Function is similar to
140.Fn printf
141which specifies the format argument with
142.Fa fmtarg
143and where the arguments formatted by that format start with the
144.Fa firstvararg ,
145with 0 meaning that
146.Dv va_arg
147is used and cannot be checked.
148.It Sy __scanflike(fmtarg,firstvararg) Ta Function is similar to
149.Fn scanf
150which specifies the format argument with
151.Fa fmtarg
152and where the arguments formatted by that format start with the
153.Fa firstvararg ,
154with 0 meaning that
155.Dv va_arg
156is used and cannot be checked.
157.It Sy __format_arg(f) Ta Specifies that arg
158.Fa f
159contains a string that will be passed to a function like
160.Fn printf
161or
162.Fa scanf
163after being translated in some way.
164.It Sy __strfmonlike(fmtarg,firstvararg) Ta Function is similar to
165.Fn scanf
166which specifies the format argument with
167.Fa fmtarg
168and where the arguments formatted by that format start with the
169.Fa firstvararg ,
170with 0 meaning that
171.Dv va_arg
172is used and cannot be checked.
173.It Sy __strtimelike(fmtarg,firstvararg) Ta Function is similar to
174.Fn scanf
175which specifies the format argument with
176.Fa fmtarg
177and where the arguments formatted by that format start with the
178.Fa firstvararg ,
179with 0 meaning that
180.Dv va_arg
181is used and cannot be checked.
182.It Sy __printf0like(fmtarg,firstvararg) Ta Exactly the same
183as
184.Sy __printflike
185except
186.Fa fmtarg
187may be
188.Dv NULL.
189.It Sy __strong_reference(sym,aliassym) Ta
190.It Sy __weak_reference(sym,alias) Ta
191.It Sy __warn_references(sym,msg) Ta
192.It Sy __sym_compat(sym,impl,verid) Ta
193.It Sy __sym_default(sym,impl,verid) Ta
194.It Sy __GLOBAL(sym) Ta
195.It Sy __WEAK(sym) Ta
196.It Sy __DECONST(type,var) Ta
197.It Sy __DEVOLATILE(type,var) Ta
198.It Sy __DEQUALIFY(type,var) Ta
199.It Sy __RENAME(x) Ta
200.It Sy __arg_type_tag Ta
201.It Sy __datatype_type_tag Ta
202.It Sy __align_up(x,y) Ta
203.It Sy __align_down(x,y) Ta
204.It Sy __is_aligned(x,y) Ta
205.El
206.Ss Locking and Debugging Macros
207Macros for lock annotation and debugging, as well as some general debugging
208macros for address sanitizers.
209.Bl -column "---------------"
210.It Sy __lock_annotate(x) Ta
211.It Sy __lockable Ta
212.It Sy __locks_exclusive Ta
213.It Sy __locks_shared Ta
214.It Sy __trylocks_exclusive Ta
215.It Sy __trylocks_shared Ta
216.It Sy __unlocks Ta
217.It Sy __asserts_exclusive Ta
218.It Sy __asserts_shared Ta
219.It Sy __requires_exclusive Ta
220.It Sy __requires_shared Ta
221.It Sy __requires_unlocked Ta
222.It Sy __no_lock_analysis Ta
223.It Sy __nosanitizeaddress Ta
224.It Sy __nosanitizememory Ta
225.It Sy __nosanitizethread Ta
226.It Sy __nostackprotector Ta
227.It Sy __guarded_by(x) Ta
228.It Sy __pt_guarded_by(x) Ta
229.El
230.Ss Emulated Keywords
231As C evolves, many of the old macros we once used have been incorporated into
232the standard language.
233As this happens, we add support for these keywords as macros for older
234compilation environments.
235Sometimes this results in a nop in the older environment.
236.Bl -column "---------------"
237.It Sy Keyword Ta Sy Description
238.It Sy _Alignas(x) Ta
239.It Sy _Alignof(x) Ta
240.It Sy _Noreturn Ta Expands to
241.Dq [[noreturn]]
242in C++-11 and newer compilation environments, otherwise
243.Dq __dead2
244.It Sy _Static_assert(x, y) Ta Compile time assertion that
245.Fa x
246is true, otherwise emit
247.Fa y
248as the error message.
249.It Sy _Thread_local Ta Designate variable as thread local storage
250.It Sy __generic Ta implement _Generic-like features which aren't entirely possible to emulate the _Generic keyword
251.It Sy __noexcept Ta to emulate the C++11 argument-less noexcept form
252.It Sy __noexcept_if Ta to emulate the C++11 conditional noexcept form
253.It Sy _Nonnull Ta
254.It Sy _Nullable Ta
255.It Sy _Null_unspecified Ta
256.El
257.Ss Support Macros
258The following macros are defined, or have specific values, to denote certain
259things about the build environment.
260.Bl -column "---------------"
261.It Sy Macro Ta Sy Description
262.It Sy __LONG_LONG_SUPPORTED Ta Variables may be declared
263.Dq long long .
264This is defined for C99 or newer and C++ environments.
265.It Sy __STDC_LIMIT_MACROS Ta
266.It Sy __STDC_CONSTANT_MACROS Ta
267.El
268.Ss Convenience Macros
269These macros make it easier to do a number of things, even though strictly
270speaking the standard places their normal form in another header.
271.Bl -column "---------------"
272.It Sy Macro Ta Sy Description
273.It Sy __offsetof(type,field) Ta
274.It Sy __rangeof(type,start,end) Ta
275.It Sy __containerof(x,s,m) Ta
276.El
277.Ss ID Strings
278This section is deprecated, but is kept around because too much contrib software
279still uses these.
280.Bl -column "---------------"
281.It Sy Macro Ta Sy Description
282.It Sy __IDSTRING(name,string) Ta
283.It Sy __FBSDID(s) Ta
284.It Sy __RCSID(s) Ta
285.It Sy __RCSID_SOURCE(s) Ta
286.It Sy __SCCSID(s) Ta
287.It Sy __COPYRIGHT(s) Ta
288.El
289.Sh Supported C Environments
290.Fx
291supports a number C standard environments.
292Selection of the language dialect is a compiler-dependent command line option,
293though it is usually
294.Fl std=XX
295where XX is the standard to set for compiling, such as c89 or c23.
296.Fx
297provides a number of selection macros to control visibility of symbols.
298Please see the section on Selection Macros for the specifics.
299.Pp
300.Bl -tag
301.It K \*(Am R
302Pre-ANSI Kernighan and Ritchie C.
303Sometimes called
304.Dq knr
305or
306.Dq C78
307to distinguish it from newer standards.
308Support for this compilation environment is dependent on compilers supporting
309this configuration.
310Most of the old forms of C have been deprecated or removed in
311.St -isoC-2024 .
312Compilers make compiling in this mode increasingly difficult and support for it
313may ultimately be removed from the tree.
314.It St -ansiC
315.Dv __STDC__
316is defined, however
317.Dv __STDC_VERSION__
318is not.
319.Pp
320Strict environment selected with
321.Dv _ANSI_SOURCE .
322.It St -isoC-99
323.Dv __STDC_VERSION__ = 199901L
324.Pp
325Strict environment selected with
326.Dv _C99_SOURCE .
327.It St -isoC-2011
328.Dv __STDC_VERSION__ = 201112L
329.Pp
330Strict environment selected with
331.Dv _C11_SOURCE .
332.It ISO/IEC 9899:2018 (“ISO C17”)
333.Dv __STDC_VERSION__ = 201710L
334.Pp
335Strict environment selected with
336.Dv _C11_SOURCE
337since there are no new C17 only symbols or macros.
338.Pp
339This version of the standard did not introduce any new features, only made
340minor, technical corrections.
341.It St -isoC-2024
342.Dv __STDC_VERSION__ = 202311L
343Strict environment selected with
344.Dv _C23_SOURCE
345though ISO C23 support is only partially implemented.
346.El
347.Pp
348For more information on C standards, see
349.Xr c 7 .
350.Ss Programming Environment Selection Macros
351Defining the macros outlined below requests that the system header files provide
352only the functions, structures and macros (symbols) defined by the appropriate
353standard, while suppressing all extensions.
354However, system headers not defined by that standard may define extensions.
355.Bl -column "---------------"
356.It Sy Macro Ta Sy Environment
357.It Dv _POSIX_SOURCE Ta St -p1003.1-88 including St -ansiC
358.It Dv _POSIX_C_SOURCE = 1 Ta St -p1003.1-88 including St -ansiC
359.It Dv _POSIX_C_SOURCE = 2 Ta St -p1003.1-90 including St -ansiC
360.It Dv _POSIX_C_SOURCE = 199309 Ta St -p1003.1b-93 including St -ansiC
361.It Dv _POSIX_C_SOURCE = 199506 Ta St -p1003.1c-95 including St -ansiC
362.It Dv _POSIX_C_SOURCE = 200112 Ta St -p1003.1-2001 including St -isoC-99
363.It Dv _POSIX_C_SOURCE = 200809 Ta St -p1003.1-2008 including St -isoC-99
364.It Dv _POSIX_C_SOURCE = 202405 Ta St -p1003.1-2024 including ISO/IEC 9899:2018 ("ISO C17"),
365.It Dv _XOPEN_SOURCE defined Ta St -p1003.1-90 with XPG Extensions to St -susv1 including St -ansiC .
366However,
367.Fx
368implements this as a NOP because too much software breaks with the correct strict environment.
369.It Dv _XOPEN_SOURCE = 500 Ta St -p1003.1c-95 and XPG extensions to St -susv2 including St -ansiC
370.It Dv _XOPEN_SOURCE = 600 Ta St -p1003.1-2001 and XPG extensions to St -susv3 including St -isoC-99
371.It Dv _XOPEN_SOURCE = 700 Ta St -p1003.1-2008 and XPG extensions to St -susv4 including St -isoC-99
372.It Dv _XOPEN_SOURCE = 800 Ta St -p1003.1-2024 and XPG extensions to Version 5 of the Single UNIX Specification (“SUSv5”) including ISO/IEC 9899:2018 (“ISO C17”)
373.It Dv _ANSI_SOURCE Ta St -ansiC
374.It Dv _C99_SOURCE Ta St -isoC-99
375.It Dv _C11_SOURCE Ta St -isoC-2011
376.It Dv _C23_SOURCE Ta St -isoC-2024
377.It Dv _BSD_SOURCE Ta Everything, including Fx extensions
378.El
379.Pp
380Note:
381.St -p1003.1-2024
382and XPG extensions to Version 5 of the Single UNIX Specification ("SUSv5")
383support is incomplete.
384.Pp
385When both POSIX and C environments are selected, the POSIX environment selects
386which C environment is used.
387However, when C11 dialect is selected with
388.St -p1003.1-2008 ,
389definitions for
390.St -isoC-2011
391are also included.
392Likewise, when C23 dialog is selected with
393.St -p1003.1-2008
394or
395.St -p1003.1-2024 ,
396definitions for
397.St -isoC-2024
398are also included.
399.Ss Header Visibility Macros
400These macros are set by
401.Nm
402to control the visibility of different standards.
403Users should not use these, but they are documented here for developers.
404.Bl -column "---------------"
405.It Dv __XSI_VISIBLE Ta Restricts the visibility of XOPEN Single Unix Standard version.
406Possible values are 500, 600, 700 or 800, corresponding to Issue 5, 6, 7, or 8
407of the Single Unix Standard.
408These are extra functions in addition to the normal POSIX ones.
409.It Dv __POSIX_VISIBLE Ta Make symbols associated with certain standards versions visible.
410Set to the value assigned to
411.Dv _POSIX_C_SOURCE
412by convention with 199009 for
413.St -p1003.1-88
414and 199209
415.St -p1003.1-90 .
416.It Dv __ISO_C_VISIBLE Ta The C level that's visible.
417Possible values include 1990, 1999, 2011, 2017 and 2023 for
418.St -isoC-90 ,
419.St -isoC-99 ,
420.St -isoC-2011 ,
421ISO/IEC 9899:2018 ("ISO C17"), and
422.St -isoC-2024 ,
423respectively.
424.It Dv __BSD_VISIBLE Ta 1 if the
425.Fx
426extensions are visible, 0 otherwise.
427.It Dv __EXT1_VISIBLE Ta 1 if the
428.St -isoC-2011
429Appendix K 3.7.4.1
430extensions are visible, 0 otherwise.
431.El
432.Sh Supported C++ Environments
433.Fx
434supports C++11 and newer standards fully.
435.Bl -tag
436.It ISO/IEC 14882:1998 ("C++98")
437.Dv __cplusplus = 199711
438.Pp
439The first standardized version of C++.
440Unlike K \*(Am R support in C, compilers dropped support for versions of
441the language prior to C++98.
442.It ISO/IEC 14882:2003 ("C++03")
443.Dv __cplusplus = 199711
444.Pp
445Note, this is the same value as C++98.
446C++03 did not define a new value for
447.Dv __cplusplus .
448There is no way, at compile time, to detect the difference.
449The standard resolved a number of defect reports and slightly
450expanded value initialization.
451Most compilers support it the same as C++98.
452.It ISO/IEC 14882:2011 ("C++11")
453.Dv __cplusplus = 201103
454.It ISO/IEC 14882:2014 ("C++14")
455.Dv __cplusplus = 201402
456.It ISO/IEC 14882:2017 ("C++17")
457.Dv __cplusplus = 201703
458.It ISO/IEC 14882:2020 ("C++20")
459.Dv __cplusplus = 202002
460.It ISO/IEC 14882:2023 ("C++23")
461.Dv __cplusplus = 202302
462.El
463.Pp
464.Fx
465uses llvm project's libc++.
466However, they are removing support for C++ prior to C++11.
467While programs can still build with earlier environments for now, these changes
468mean that
469.Fl pedantic-errors
470cannot be reliably enabled for standards older than C++11.
471.Sh HISTORY
472.In sys/cdefs.h
473first appeared in
474.Bx 4.3 NET/2 .
475