utils/macros.h: Introduce a `STATIC_ASSERT' macro.
[mLib] / utils / macros.h
CommitLineData
14d7100d 1/* -*-c-*-
16139d88 2 *
16139d88 3 * Handy macros
14d7100d 4 *
5 * (c) 2003 Straylight/Edgeware
6 */
7
d4efbcd9 8/*----- Licensing notice --------------------------------------------------*
14d7100d 9 *
10 * This file is part of the mLib utilities library.
11 *
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
d4efbcd9 16 *
14d7100d 17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
d4efbcd9 21 *
14d7100d 22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
14d7100d 28#ifndef MLIB_MACROS_H
29#define MLIB_MACROS_H
30
31#ifdef __cplusplus
32 extern "C" {
33#endif
34
33e3ac90
MW
35/*----- Header files ------------------------------------------------------*/
36
f50c1365
MW
37#include <assert.h>
38
33e3ac90
MW
39#ifndef MLIB_COMPILER_H
40# include "compiler.h"
41#endif
42
3832000d 43/*----- Miscellaneous utility macros --------------------------------------*/
14d7100d 44
c066b1ff 45#define N(v) (sizeof(v)/sizeof(*(v)))
14d7100d 46
16139d88 47#define MLIB__STR(x) #x
48#define STR(x) MLIB__STR(x)
49
50#define MLIB__GLUE(x, y) x##y
51#define GLUE(x, y) MLIB__GLUE(x, y)
52
f50c1365
MW
53#ifdef static_assert
54# define STATIC_ASSERT(cond, msg) static_assert(cond, msg)
55#else
56# define STATIC_ASSERT(cond, msg) \
57 IGNORABLE extern char static_assert_failed[2*!!(cond) - 1]
58#endif
59
3832000d
MW
60/*----- Compiler diagnostics ----------------------------------------------*/
61
62/* --- Compiler-specific definitions --- */
63
ff3d3f01 64#if GCC_VERSION_P(2, 5) || CLANG_VERSION_P(3, 3)
2eea6973
MW
65# define NORETURN __attribute__((__noreturn__))
66# define PRINTF_LIKE(fix, aix) __attribute__((__format__(printf, fix, aix)))
67# define SCANF_LIKE(fix, aix) __attribute__((__format__(scanf, fix, aix)))
68# define IGNORABLE __attribute__((__unused__))
d94956b5
MW
69#endif
70
ff3d3f01 71#if GCC_VERSION_P(4, 5) || CLANG_VERSION_P(3, 3)
2eea6973 72# define DEPRECATED(msg) __attribute__((__deprecated__(msg)))
d94956b5 73#elif GCC_VERSION_P(3, 1)
2eea6973 74# define DEPRECATED(msg) __attribute__((__deprecated__))
d94956b5
MW
75#endif
76
ff3d3f01 77#if GCC_VERSION_P(4, 0) || CLANG_VERSION_P(3, 3)
2eea6973 78# define EXECL_LIKE(ntrail) __attribute__((__sentinel__(ntrail)))
d94956b5
MW
79#endif
80
ff3d3f01
MW
81#if CLANG_VERSION_P(3, 3)
82
83# define MLIB__PRAGMA_HACK(x) _Pragma(#x)
84# define MLIB__MUFFLE_WARNINGS(warns, body) \
85 _Pragma("clang diagnostic push") \
86 warns \
87 body \
88 _Pragma("clang diagnostic pop")
89# define CLANG_WARNING(warn) \
90 MLIB__PRAGMA_HACK(clang diagnostic ignored warn)
91# define MUFFLE_WARNINGS_DECL(warns, body) \
92 MLIB__MUFFLE_WARNINGS(warns, body)
93# define MUFFLE_WARNINGS_EXPR(warns, body) \
94 __extension__ ({ MLIB__MUFFLE_WARNINGS(warns, (body);) })
95# define MUFFLE_WARNINGS_STMT(warns, body) \
96 do { MLIB__MUFFLE_WARNINGS(warns, body) } while (0)
97
98#elif GCC_VERSION_P(4, 6)
d94956b5
MW
99
100 /* --- Diagnostic suppression in GCC: a tale of woe --- *
101 *
102 * This is extremely unpleasant, largely as a result of bugs in the GCC
103 * preprocessor's handling of @_Pragma@. The fundamental problem is
104 * that it's the preprocessor, and not the compiler proper, which
105 * detects @_Pragma@, emitting @#pragma@ lines into its output; and it
106 * does it during macro expansion, even if the macro is being expanded
107 * during argument collection. Since arguments are expanded before
108 * replacing the macro's invocation with its body, a pragma in an
109 * argument will be emitted %%\emph{before}%% any pragmata in the body,
110 * even if they appear before the argument in the body -- and even if
111 * the argument doesn't actually appear anywhere at all in the body.
112 *
113 * Another, rather less significant, problem is that @_Pragma@'s
114 * argument is a single string literal, recognized in translation phase
115 * 4, before string-literal concatenation in phase 6, so we must build
116 * pragma bodies as token lists and then stringify them.
117 *
118 * As a result, we need some subterfuge here. The @MLIB__PRAGMA_HACK@
119 * macro issues a @_Pragma@ on its argument token list, which it
120 * stringifies; this deals with the second problem. The first is
121 * trickier: we must delay expansion of @MLIB__PRAGMA_HACK@ from the
122 * argument collection phase to the body rescanning phase, and we do
123 * this by splitting the invocations between @GCC_WARNING@ macro calls:
124 * the name is left hanging from the previous call (or from
125 * @MLIB__MUFFLE_WARNINGS@, in the first case) and the body is supplied
126 * by @GCC_WARNING@, which also supplies the next @MLIB__PRAGMA_HACK@.
127 * The remaining problem is to make sure we can dispose of the final
128 * trailing @MLIB__PRAGMA_HACK@ harmlessly, which we do by introducing
129 * an extra argument @emitp@, which may be either @t@ or @nil@; this
130 * dispatches to an appropriate helper macro by means of token-pasting.
131 *
132 * I'm so sorry.
133 */
134
135# define MLIB__PRAGMA_HACK_t(x) _Pragma(#x)
136# define MLIB__PRAGMA_HACK_nil(x)
137# define MLIB__PRAGMA_HACK(emitp, x) MLIB__PRAGMA_HACK_##emitp(x)
138# define MLIB__MUFFLE_WARNINGS(warns, body) \
3832000d
MW
139 _Pragma("GCC diagnostic push") MLIB__PRAGMA_HACK \
140 warns \
141 (nil, nil) \
142 body \
143 _Pragma("GCC diagnostic pop")
d94956b5 144# define GCC_WARNING(warn) \
3832000d 145 (t, GCC diagnostic ignored warn) MLIB__PRAGMA_HACK
d94956b5 146# define MUFFLE_WARNINGS_DECL(warns, body) \
3832000d 147 MLIB__MUFFLE_WARNINGS(warns, body)
d94956b5 148# define MUFFLE_WARNINGS_EXPR(warns, body) \
3832000d 149 __extension__ ({ MLIB__MUFFLE_WARNINGS(warns, (body);) })
d94956b5 150# define MUFFLE_WARNINGS_STMT(warns, body) \
3832000d 151 do { MLIB__MUFFLE_WARNINGS(warns, body) } while (0)
3832000d
MW
152#endif
153
154/* --- Fallback definitions, mostly trivial --- */
155
3832000d
MW
156#ifndef DEPRECATED
157# define DEPRECATED(msg)
158#endif
159
160#ifndef EXECL_LIKE
161# define EXECL_LIKE(ntrail)
162#endif
163
164#ifndef DISCARD
165# define DISCARD(x) do if (x); while (0)
166#endif
167
168#ifndef IGNORE
169# define IGNORE(x) ((void)(x))
170#endif
171
172#ifndef MUFFLE_WARNINGS_DECL
173# define MUFFLE_WARNINGS_DECL(warns, body) body
174#endif
175
176#ifndef MUFFLE_WARNINGS_EXPR
177# define MUFFLE_WARNINGS_EXPR(warns, body) (body)
178#endif
179
180#ifndef MUFFLE_WARNINGS_STMT
181# define MUFFLE_WARNINGS_STMT(warns, body) do { body } while (0)
182#endif
183
184#ifndef PRINTF_LIKE
185# define PRINF_LIKE(fmtix, argix)
186#endif
187
188#ifndef SCANF_LIKE
189# define SCANF_LIKE(fmtix, argix)
190#endif
191
59eae7fd
MW
192#ifndef NORETURN
193# define NORETURN
194#endif
195
3832000d
MW
196#ifndef IGNORABLE
197# define IGNORABLE
198#endif
199
200#ifndef GCC_WARNING
201# define GCC_WARNING(warn)
202#endif
203
ff3d3f01
MW
204#ifndef CLANG_WARNING
205# define CLANG_WARNING(warn)
206#endif
207
14d7100d 208/*----- That's all, folks -------------------------------------------------*/
209
210#ifdef __cplusplus
211 }
212#endif
213
214#endif