dpkg 1.22.7-3-g89f48
Loading...
Searching...
No Matches
macros.h
Go to the documentation of this file.
1/*
2 * libdpkg - Debian packaging suite library routines
3 * macros.h - C language support macros
4 *
5 * Copyright © 2008-2012 Guillem Jover <guillem@debian.org>
6 *
7 * This is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#ifndef LIBDPKG_MACROS_H
22#define LIBDPKG_MACROS_H
23
30#ifndef LIBDPKG_VOLATILE_API
31#error "The libdpkg API is to be considered volatile, please read 'README.api'."
32#endif
33
34/* Language definitions. */
35
36#ifndef __has_include
37#define __has_include(x) 0
38#endif
39
40/* Supported since clang 1.0. */
41#ifndef __has_warning
42#define __has_warning(w) (0)
43#endif
44
45#define DPKG_PRAGMA(x) _Pragma(#x)
46
47#if defined(__clang__)
48#define DPKG_PRAGMA_CC(x) DPKG_PRAGMA(clang x)
49#elif defined(__GNUC__)
50#define DPKG_PRAGMA_CC(x) DPKG_PRAGMA(GCC x)
51#else
52#define DPKG_PRAGMA_CC(x)
53#endif
54
55#define DPKG_IGNORE_WARNING(w) \
56 DPKG_PRAGMA_CC(diagnostic push); \
57 DPKG_PRAGMA_CC(diagnostic ignored w)
58#define DPKG_ACCEPT_WARNING() \
59 DPKG_PRAGMA_CC(diagnostic pop)
60
61#if __has_warning("-Wassign-enum")
62#define DPKG_IGNORE_WARNING_ASSIGN_ENUM() DPKG_IGNORE_WARNING("-Wassign-enum")
63#define DPKG_ACCEPT_WARNING_ASSIGN_ENUM() DPKG_ACCEPT_WARNING()
64#else
65#define DPKG_IGNORE_WARNING_ASSIGN_ENUM()
66#define DPKG_ACCEPT_WARNING_ASSIGN_ENUM()
67#endif
68
69/* Supported since gcc 5.1.0 and clang 2.9.0. For attributes that appeared
70 * before these versions, in addition we need to do version checks. */
71#ifndef __has_attribute
72#define __has_attribute(x) 0
73#endif
74
75#ifdef __GNUC__
76#define DPKG_GCC_VERSION (__GNUC__ << 8 | __GNUC_MINOR__)
77#else
78#define DPKG_GCC_VERSION 0
79#endif
80
81#if DPKG_GCC_VERSION >= 0x0300 || __has_attribute(__unused__)
82#define DPKG_ATTR_UNUSED __attribute__((__unused__))
83#else
84#define DPKG_ATTR_UNUSED
85#endif
86
87#if DPKG_GCC_VERSION >= 0x0300 || __has_attribute(__const__)
88#define DPKG_ATTR_CONST __attribute__((__const__))
89#else
90#define DPKG_ATTR_CONST
91#endif
92
93#if DPKG_GCC_VERSION >= 0x0300 || __has_attribute(__pure__)
94#define DPKG_ATTR_PURE __attribute__((__pure__))
95#else
96#define DPKG_ATTR_PURE
97#endif
98
99#if DPKG_GCC_VERSION >= 0x0300 || __has_attribute(__malloc__)
100#define DPKG_ATTR_MALLOC __attribute__((__malloc__))
101#else
102#define DPKG_ATTR_MALLOC
103#endif
104
105#if DPKG_GCC_VERSION >= 0x0300 || __has_attribute(__noreturn__)
106#define DPKG_ATTR_NORET __attribute__((__noreturn__))
107#else
108#define DPKG_ATTR_NORET
109#endif
110
111#if DPKG_GCC_VERSION >= 0x0300 || __has_attribute(__format__)
112#define DPKG_ATTR_FMT(t, f, a) __attribute__((__format__(t, f, a)))
113#define DPKG_ATTR_PRINTF(n) DPKG_ATTR_FMT(__printf__, n, n + 1)
114#define DPKG_ATTR_VPRINTF(n) DPKG_ATTR_FMT(__printf__, n, 0)
115#else
116#define DPKG_ATTR_FMT(t, f, a)
117#define DPKG_ATTR_PRINTF(n)
118#define DPKG_ATTR_VPRINTF(n)
119#endif
120
121#if DPKG_GCC_VERSION > 0x0302 || __has_attribute(__nonnull__)
122#define DPKG_ATTR_NONNULL(...) __attribute__((__nonnull__(__VA_ARGS__)))
123#else
124#define DPKG_ATTR_NONNULL(...)
125#endif
126
127#if DPKG_GCC_VERSION > 0x0302 || __has_attribute(__warn_unused_result__)
128#define DPKG_ATTR_REQRET __attribute__((__warn_unused_result__))
129#else
130#define DPKG_ATTR_REQRET
131#endif
132
133#if DPKG_GCC_VERSION >= 0x0400 || __has_attribute(__sentinel__)
134#define DPKG_ATTR_SENTINEL __attribute__((__sentinel__))
135#else
136#define DPKG_ATTR_SENTINEL
137#endif
138
139#if DPKG_GCC_VERSION >= 0x0801 || __has_attribute(__nonstring__)
140#define DPKG_ATTR_NONSTRING __attribute__((__nonstring__))
141#else
142#define DPKG_ATTR_NONSTRING
143#endif
144
145#if __has_attribute(__enum_extensibility__)
146#define DPKG_ATTR_ENUM_FLAGS \
147 __attribute__((__enum_extensibility__(closed),__flag_enum__))
148#else
149#define DPKG_ATTR_ENUM_FLAGS
150#endif
151
152#if defined(__cplusplus) && __cplusplus >= 201103L
153#define DPKG_ATTR_THROW(exception)
154#define DPKG_ATTR_NOEXCEPT noexcept
155#elif defined(__cplusplus)
156#define DPKG_ATTR_THROW(exception) throw(exception)
157#define DPKG_ATTR_NOEXCEPT throw()
158#endif
159
160#ifdef __cplusplus
161#define DPKG_BEGIN_DECLS extern "C" {
162#define DPKG_END_DECLS }
163#else
164#define DPKG_BEGIN_DECLS
165#define DPKG_END_DECLS
166#endif
167
177#if defined(__cplusplus) || __STDC_VERSION__ > 201710L
178#define DPKG_NULL nullptr
179#else
180#define DPKG_NULL NULL
181#endif
182
192#if defined(__cplusplus)
193#define DPKG_STATIC_CAST(type, expr) static_cast<type>(expr)
194#else
195#define DPKG_STATIC_CAST(type, expr) (type)(expr)
196#endif
197
203#define DPKG_BIT(n) (1UL << (n))
204
210#ifndef array_count
211#define array_count(a) (sizeof(a) / sizeof((a)[0]))
212#endif
213
214/* For C++ use native implementations from STL or similar. */
215#ifndef __cplusplus
216#ifndef min
217#define min(a, b) ((a) < (b) ? (a) : (b))
218#endif
219
220#ifndef max
221#define max(a, b) ((a) > (b) ? (a) : (b))
222#endif
223#endif
224
234/* For C++ use native implementations from STL or similar. */
235#ifndef __cplusplus
236#ifndef clamp
237#define clamp(v, l, h) ((v) > (h) ? (h) : ((v) < (l) ? (l) : (v)))
238#endif
239#endif
240
243#endif /* LIBDPKG_MACROS_H */