xref: /freebsd/contrib/xz/src/common/tuklib_common.h (revision 128836d304d93f2d00eb14069c27089ab46c38d4)
1 // SPDX-License-Identifier: 0BSD
2 
3 ///////////////////////////////////////////////////////////////////////////////
4 //
5 /// \file       tuklib_common.h
6 /// \brief      Common definitions for tuklib modules
7 //
8 //  Author:     Lasse Collin
9 //
10 ///////////////////////////////////////////////////////////////////////////////
11 
12 #ifndef TUKLIB_COMMON_H
13 #define TUKLIB_COMMON_H
14 
15 // The config file may be replaced by a package-specific file.
16 // It should include at least stddef.h, stdbool.h, inttypes.h, and limits.h.
17 #include "tuklib_config.h"
18 
19 // TUKLIB_SYMBOL_PREFIX is prefixed to all symbols exported by
20 // the tuklib modules. If you use a tuklib module in a library,
21 // you should use TUKLIB_SYMBOL_PREFIX to make sure that there
22 // are no symbol conflicts in case someone links your library
23 // into application that also uses the same tuklib module.
24 #ifndef TUKLIB_SYMBOL_PREFIX
25 #	define TUKLIB_SYMBOL_PREFIX
26 #endif
27 
28 #define TUKLIB_CAT_X(a, b) a ## b
29 #define TUKLIB_CAT(a, b) TUKLIB_CAT_X(a, b)
30 
31 #ifndef TUKLIB_SYMBOL
32 #	define TUKLIB_SYMBOL(sym) TUKLIB_CAT(TUKLIB_SYMBOL_PREFIX, sym)
33 #endif
34 
35 #ifndef TUKLIB_DECLS_BEGIN
36 #	ifdef __cplusplus
37 #		define TUKLIB_DECLS_BEGIN extern "C" {
38 #	else
39 #		define TUKLIB_DECLS_BEGIN
40 #	endif
41 #endif
42 
43 #ifndef TUKLIB_DECLS_END
44 #	ifdef __cplusplus
45 #		define TUKLIB_DECLS_END }
46 #	else
47 #		define TUKLIB_DECLS_END
48 #	endif
49 #endif
50 
51 #if defined(__GNUC__) && defined(__GNUC_MINOR__)
52 #	define TUKLIB_GNUC_REQ(major, minor) \
53 		((__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)) \
54 			|| __GNUC__ > (major))
55 #else
56 #	define TUKLIB_GNUC_REQ(major, minor) 0
57 #endif
58 
59 #if defined(__GNUC__) || defined(__clang__)
60 #	define tuklib_attr_format_printf(fmt_index, args_index) \
61 		__attribute__((__format__(__printf__, fmt_index, args_index)))
62 #else
63 #	define tuklib_attr_format_printf(fmt_index, args_index)
64 #endif
65 
66 // tuklib_attr_noreturn attribute is used to mark functions as non-returning.
67 // We cannot use "noreturn" as the macro name because then C23 code that
68 // uses [[noreturn]] would break as it would expand to [[ [[noreturn]] ]].
69 //
70 // tuklib_attr_noreturn must be used at the beginning of function declaration
71 // to work in all cases. The [[noreturn]] syntax is the most limiting, it
72 // must be even before any GNU C's __attribute__ keywords:
73 //
74 //     tuklib_attr_noreturn
75 //     __attribute__((nonnull(1)))
76 //     extern void foo(const char *s);
77 //
78 #if   defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311
79 #	define tuklib_attr_noreturn [[noreturn]]
80 #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112
81 #	define tuklib_attr_noreturn _Noreturn
82 #elif TUKLIB_GNUC_REQ(2, 5)
83 #	define tuklib_attr_noreturn __attribute__((__noreturn__))
84 #elif defined(_MSC_VER)
85 #	define tuklib_attr_noreturn __declspec(noreturn)
86 #else
87 #	define tuklib_attr_noreturn
88 #endif
89 
90 #if (defined(_WIN32) && !defined(__CYGWIN__)) \
91 		|| defined(__OS2__) || defined(__MSDOS__)
92 #	define TUKLIB_DOSLIKE 1
93 #endif
94 
95 #endif
96