5 * (c) 2003 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
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.
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.
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,
35 /*----- Header files ------------------------------------------------------*/
39 #ifndef MLIB_COMPILER_H
40 # include "compiler.h"
43 /*----- Miscellaneous utility macros --------------------------------------*/
47 * Arguments: @type v[]@ = an actual array, not a pointer
49 * Returns: The number of elements in @v@.
52 #define N(v) (sizeof(v)/sizeof(*(v)))
56 * Arguments: @x@ = some tokens
58 * Returns: A string literal containing the macro-expanded text of @x@.
61 #define MLIB__STR(x) #x
62 #define STR(x) MLIB__STR(x)
64 /* --- @GLUE@, @GLUE3@ --- *
66 * Arguments: @x, y@ = two sequences of tokens
67 * @z@ = a third sequence of tokens
69 * Returns: A single token formed by gluing together the macro-expansions
70 * of @x@ and @y@, and @z@ for @GLUE3@.
73 #define MLIB__GLUE(x, y) x##y
74 #define GLUE(x, y) MLIB__GLUE(x, y)
75 #define GLUE3(x, y, z) GLUE(x, MLIB__GLUE(y, z))
77 /* --- @STATIC_ASSERT@ --- *
79 * Arguments: @int cond@ = a condition
80 * @msg@ = a string literal message
84 * Use: Fail at compile time unless @cond@ is nonzero. The failure
89 # define STATIC_ASSERT(cond, msg) static_assert(!!(cond), msg)
91 # define STATIC_ASSERT(cond, msg) \
92 IGNORABLE extern char static_assert_failed[1 - 2*!(cond)]
95 /* --- @CHECK_TYPE@ ---
97 * Arguments: @expty@ = expected type of @x@
98 * @expty x@ = some object
100 * Returns: Integer zero.
102 * Use: Cause a compile-time failure unless the type of @x@ is
103 * assignment-compatible with @expty@.
106 #define CHECK_TYPE(expty, x) (!sizeof(*(expty *)0 = (x)))
108 /* --- @CONVERT_CAREFULLY@ ---
110 * Arguments: @newty@ = new type for the result
111 * @expty@ = expected type of @x@
112 * @expty x@ = some object
114 * Returns: @x@, but coerced to type @newty@.
116 * Use: Like @(newty)x@, except that it checks at compile-time that
117 * @x@ is at least assignment-compatible with type @expty@
121 #define CONVERT_CAREFULLY(newty, expty, x) \
122 (CHECK_TYPE(expty, x) + (/*unconst unvolatile*/ newty)(x))
124 /* --- @UNCONST@, @UNVOLATILE@, @UNQUALIFY@ --- *
126 * Arguments: @type@ = a type name
127 * @type *p@ = a pointer
129 * Returns: @p@, but without @const@, @volatile@ or both qualifiers.
131 * Use: Strips qualifiers from pointer types.
133 * The @UNCONST@ macro strips @const@. It checks that @p@
134 * has type `pointer to @type@ or @const type@'; if not, a
135 * compile-time error results. Otherwise, it returns the value
136 * of @p@, converted to `pointer to (non-constant) @type@'. It
137 * will not silently strip a @volatile@ qualifier.
139 * The @UNVOLATILE@ macro is similar, except that it strips
140 * @volatile@ instead of @const@. The @UNQUALIFY@ macro strips
144 #define UNCONST(type, p) CONVERT_CAREFULLY(type *, const type *, p)
145 #define UNVOLATILE(type, p) CONVERT_CAREFULLY(type *, volatile type *, p)
146 #define UNQUALIFY(type, p) \
147 CONVERT_CAREFULLY(type *, const volatile type *, p)
153 * Returns: The empty token sequence.
162 * Returns: A `%|,|%' token, which can be usefully passed to macros to
163 * avoid argument splitting.
168 /*----- String and character hacks ----------------------------------------*/
172 * Arguments: @int ch@ = a character code, but not @EOF@
174 * Returns: Nonzero if @ch@ is in the relevant @<ctype.h>@ category.
176 * Use: Classifies characters, but safely even if characters are
179 * There is a macro for each of the @<ctype.h>@ @is...@
183 #define CTYPE_HACK(func, ch) (func((unsigned char)(ch)))
185 #define ISALNUM(ch) CTYPE_HACK(isalnum, ch)
186 #define ISALPHA(ch) CTYPE_HACK(isalpha, ch)
187 #define ISASCII(ch) CTYPE_HACK(isascii, ch)
188 #define ISBLANK(ch) CTYPE_HACK(isblank, ch)
189 #define ISCNTRL(ch) CTYPE_HACK(iscntrl, ch)
190 #define ISDIGIT(ch) CTYPE_HACK(isdigit, ch)
191 #define ISGRAPH(ch) CTYPE_HACK(isgraph, ch)
192 #define ISLOWER(ch) CTYPE_HACK(islower, ch)
193 #define ISPRINT(ch) CTYPE_HACK(isprint, ch)
194 #define ISPUNCT(ch) CTYPE_HACK(ispunct, ch)
195 #define ISSPACE(ch) CTYPE_HACK(isspace, ch)
196 #define ISUPPER(ch) CTYPE_HACK(isupper, ch)
197 #define ISXDIGIT(ch) CTYPE_HACK(isxdigit, ch)
201 * Arguments: @int ch@ = a character code, but not @EOF@
203 * Returns: The converted character code.
205 * Use: Converts characters, but safely even if characters are
208 * There is a macro for each of the @<ctype.h>@ @to...@
212 #define TOASCII(ch) CTYPE_HACK(toascii, ch)
213 #define TOLOWER(ch) CTYPE_HACK(tolower, ch)
214 #define TOUPPER(ch) CTYPE_HACK(toupper, ch)
216 /* --- @MEMCMP@, @STRCMP@, @STRNCMP@ --- *
218 * Arguments: @const type *x, *y@ = pointers to strings
219 * @op@ = a relational operator symbol
220 * @size_t n@ = length of the strings
222 * Returns: Nonzero if the relationship between the strings satisfies the
223 * operator @op@, otherwise zero.
225 * Use: These macros mitigate the author's frequent error of failing
226 * to compare the result of the underlying standard functions
227 * against zero, effectively reversing the sense of an intended
231 #define MEMCMP(x, op, y, n) (memcmp((x), (y), (n)) op 0)
232 #define STRCMP(x, op, y) (strcmp((x), (y)) op 0)
233 #define STRNCMP(x, op, y, n) (strncmp((x), (y), (n)) op 0)
235 /*----- Compiler-specific definitions -------------------------------------*/
237 /* The descriptions of these are given below, with the fallback
241 #if GCC_VERSION_P(2, 5) || CLANG_VERSION_P(3, 3)
242 # define NORETURN __attribute__((__noreturn__))
243 # define PRINTF_LIKE(fix, aix) __attribute__((__format__(printf, fix, aix)))
244 # define SCANF_LIKE(fix, aix) __attribute__((__format__(scanf, fix, aix)))
245 # define IGNORABLE __attribute__((__unused__))
248 #if GCC_VERSION_P(3, 4) || CLANG_VERSION_P(3, 3)
249 # define MUST_CHECK __attribute__((__warn_unused_result__))
252 #if GCC_VERSION_P(4, 5) || CLANG_VERSION_P(3, 3)
253 # define DEPRECATED(msg) __attribute__((__deprecated__(msg)))
254 #elif GCC_VERSION_P(3, 1)
255 # define DEPRECATED(msg) __attribute__((__deprecated__))
258 #if GCC_VERSION_P(4, 0) || CLANG_VERSION_P(3, 3)
259 # define EXECL_LIKE(ntrail) __attribute__((__sentinel__(ntrail)))
262 #if GCC_VERSION_P(2, 7) || CLANG_VERSION_P(0, 0)
263 # define LAUNDER(x) \
264 ({ __typeof__(x) _y; __asm__("" : "=g"(_y) : "0"(x)); _y; })
266 ({ __asm__("" :: "g"(x)); })
267 # define ADMIRE_BUF(p, sz) \
268 ({ __asm__("" :: "m"(*(unsigned char *)p), "g"(sz) : "memory"); })
269 # define RELAX do __asm__(""); while (0)
272 #if CLANG_VERSION_P(3, 3)
274 # define MLIB__PRAGMA_HACK(x) _Pragma(#x)
275 # define MLIB__MUFFLE_WARNINGS(warns, body) \
276 _Pragma("clang diagnostic push") \
279 _Pragma("clang diagnostic pop")
280 # define CLANG_WARNING(warn) \
281 MLIB__PRAGMA_HACK(clang diagnostic ignored warn)
282 # define MUFFLE_WARNINGS_DECL(warns, body) \
283 MLIB__MUFFLE_WARNINGS(warns, body)
284 # define MUFFLE_WARNINGS_EXPR(warns, body) \
285 __extension__ ({ MLIB__MUFFLE_WARNINGS(warns, (body);) })
286 # define MUFFLE_WARNINGS_STMT(warns, body) \
287 do { MLIB__MUFFLE_WARNINGS(warns, body) } while (0)
291 #if GCC_VERSION_P(4, 6)
293 /* --- Diagnostic suppression in GCC: a tale of woe --- *
295 * This is extremely unpleasant, largely as a result of bugs in the GCC
296 * preprocessor's handling of @_Pragma@. The fundamental problem is
297 * that it's the preprocessor, and not the compiler proper, which
298 * detects @_Pragma@, emitting @#pragma@ lines into its output; and it
299 * does it during macro expansion, even if the macro is being expanded
300 * during argument collection. Since arguments are expanded before
301 * replacing the macro's invocation with its body, a pragma in an
302 * argument will be emitted %%\emph{before}%% any pragmata in the body,
303 * even if they appear before the argument in the body -- and even if
304 * the argument doesn't actually appear anywhere at all in the body.
306 * Another, rather less significant, problem is that @_Pragma@'s
307 * argument is a single string literal, recognized in translation phase
308 * 4, before string-literal concatenation in phase 6, so we must build
309 * pragma bodies as token lists and then stringify them.
311 * As a result, we need some subterfuge here. The @MLIB__PRAGMA_HACK@
312 * macro issues a @_Pragma@ on its argument token list, which it
313 * stringifies; this deals with the second problem. The first is
314 * trickier: we must delay expansion of @MLIB__PRAGMA_HACK@ from the
315 * argument collection phase to the body rescanning phase, and we do
316 * this by splitting the invocations between @GCC_WARNING@ macro calls:
317 * the name is left hanging from the previous call (or from
318 * @MLIB__MUFFLE_WARNINGS@, in the first case) and the body is supplied
319 * by @GCC_WARNING@, which also supplies the next @MLIB__PRAGMA_HACK@.
320 * The remaining problem is to make sure we can dispose of the final
321 * trailing @MLIB__PRAGMA_HACK@ harmlessly, which we do by introducing
322 * an extra argument @emitp@, which may be either @t@ or @nil@; this
323 * dispatches to an appropriate helper macro by means of token-pasting.
328 # define MLIB__PRAGMA_HACK_t(x) _Pragma(#x)
329 # define MLIB__PRAGMA_HACK_nil(x)
330 # define MLIB__PRAGMA_HACK(emitp, x) MLIB__PRAGMA_HACK_##emitp(x)
331 # define MLIB__MUFFLE_WARNINGS(warns, body) \
332 _Pragma("GCC diagnostic push") MLIB__PRAGMA_HACK \
336 _Pragma("GCC diagnostic pop")
337 # define GCC_WARNING(warn) \
338 (t, GCC diagnostic ignored warn) MLIB__PRAGMA_HACK
339 # define MUFFLE_WARNINGS_DECL(warns, body) \
340 MLIB__MUFFLE_WARNINGS(warns, body)
341 # define MUFFLE_WARNINGS_EXPR(warns, body) \
342 __extension__ ({ MLIB__MUFFLE_WARNINGS(warns, (body);) })
343 # define MUFFLE_WARNINGS_STMT(warns, body) \
344 do { MLIB__MUFFLE_WARNINGS(warns, body) } while (0)
347 /* --- Fallback definitions, mostly trivial --- */
349 /* --- @DISCARD@ --- *
351 * Arguments: @x@ = a function call
355 * Use: Explicitly discard the result of @x@. This counteracts a
356 * @MUST_CHECK@ attribute on the called function.
360 # define DISCARD(x) do if (x); while (0)
363 /* --- @IGNORE@ --- *
365 * Arguments: @x@ = any expression
369 * Use: Ignore the value of @x@, overriding compiler warnings.
373 # define IGNORE(x) ((void)(x))
376 /* --- @LAUNDER@ --- *
378 * Arguments: @x@ = some integer expression
382 * Use: Causes a compiler to know nothing about the value of @x@,
383 * even if it looks obvious, e.g., it's a constant.
387 # define LAUNDER(x) (x)
390 /* --- @ADMIRE@, @ADMIRE_BUF@ --- *
392 * Arguments: @x@ = some scalar expression
393 * @const void *p@, @size_t sz@ = a pointer and length
397 * Use: Ensures that the compiler generates code to compute @x@ or
398 * the contents of the buffer at @p@.
402 # define ADMIRE(x) ((void)(x))
405 # define ADMIRE_BUF(p, sz) ((void)(p), (void)(sz))
414 * Use: Does nothing, but the compiler doesn't know that.
421 /* --- @DEPRECATED@, @NORETURN@, @IGNORABLE@, @MUST_CHECK@ --- *
423 * Use: These are (mostly) function attributes; write them among the
424 * declaration specifiers for a function definition or
425 * declaration. These may not do anything, but the intended
426 * behaviour is as follows.
428 * * @DEPRECATED(msg)@ -- report a warning, quoting the string
429 * literal @msg@, if the function is called.
431 * * @NORETURN@ -- promise that the function doesn't return to
432 * its caller: either it kills the process, or it performs
433 * some nonlocal transfer.
435 * * @IGNORABLE@ -- the item (which might be data rather than
436 * a function) might not be referred to, but that's OK:
437 * don't warn about it.
439 * @ @MUST_CHECK@ -- warn if the return value of a function is
440 * ignored. Use @DISCARD@ if you really don't care.
444 # define DEPRECATED(msg)
459 /* --- @PRINTF_LIKE@, @SCANF_LIKE@, @EXECL_LIKE@ --- *
461 * Arguments: @int fmtix@ = format string argument index (starting from 1)
462 * @int argix@ = variable format argument tail index (starting
464 * @int ntrail@ = number of arguments following terminator
466 * Use: These are function attributes. Again, they might not do
467 * anything at all. By intention, they give the compiler
468 * information about a variadic function's arguments, so that it
469 * can warn about misuse.
471 * * @PRINTF_LIKE@ -- the function takes a @printf@-style
472 * format string as argument @fmtix@ and an argument tail
473 * (which may be empty) beginning with argument @argix@.
475 * * @SCANF_LIKE@ -- the function takes a @scanf@-style
476 * format string as argument @fmtix@ and an argument tail
477 * (which may be empty) beginning with argument @argix@.
479 * * @EXECL_LIKE@ -- the function takes a sequence of pointer
480 * arguments terminated by a null pointer, followed by
481 * @ntrail@ further arguments.
485 # define PRINF_LIKE(fmtix, argix)
489 # define SCANF_LIKE(fmtix, argix)
493 # define EXECL_LIKE(ntrail)
496 /* --- @MUFFLE_WARNINGS_...@ --- *
498 * Arguments: @warns@ = a sequence of @..._WARNING@ calls (see below)
499 * @body@ = some program text
501 * Use: Muffle specific warnings within the program text.
503 * For @MUFFLE_WARNINGS_DECL@, the program text is a
504 * declaration; for @MUFFLE_WARNINGS_EXPR@, it is an expression,
505 * and for @MUFFLE_WARNINGS_STMT@, it is a statement.
507 * The warnings to be muffled are given as a list of
508 * @..._WARNING@ macros, with no separators. The list can
509 * list warnings from multiple different compilers: entries for
510 * irrelevant compilers will be ignored.
513 #ifndef MUFFLE_WARNINGS_DECL
514 # define MUFFLE_WARNINGS_DECL(warns, body) body
517 #ifndef MUFFLE_WARNINGS_EXPR
518 # define MUFFLE_WARNINGS_EXPR(warns, body) (body)
521 #ifndef MUFFLE_WARNINGS_STMT
522 # define MUFFLE_WARNINGS_STMT(warns, body) do { body } while (0)
525 /* --- @GCC_WARNING@ --- *
527 * Arguments: @warn@ = a string literal naming a warning, with `%|-W...|%'
530 * Use: Names a GCC warning: use within @MUFFLE_WARNINGS_...@.
532 * Note that GCC's warning suppression is very buggy.
536 # define GCC_WARNING(warn)
539 /* --- @CLANG_WARNING@ --- *
541 * Arguments: @warn@ = a string literal naming a warning, with `%|-W...|%'
544 * Use: Names a Clang warning: use within @MUFFLE_WARNINGS_...@.
547 #ifndef CLANG_WARNING
548 # define CLANG_WARNING(warn)
551 /*----- That's all, folks -------------------------------------------------*/