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 --------------------------------------*/
45 #define N(v) (sizeof(v)/sizeof(*(v)))
47 #define MLIB__STR(x) #x
48 #define STR(x) MLIB__STR(x)
50 #define MLIB__GLUE(x, y) x##y
51 #define GLUE(x, y) MLIB__GLUE(x, y)
54 # define STATIC_ASSERT(cond, msg) static_assert(!!(cond), msg)
56 # define STATIC_ASSERT(cond, msg) \
57 IGNORABLE extern char static_assert_failed[2*!!(cond) - 1]
62 /*----- String and character hacks ----------------------------------------*/
64 #define CTYPE_HACK(func, ch) (func((unsigned char)(ch)))
66 #define ISALNUM(ch) CTYPE_HACK(isalnum, ch)
67 #define ISALPHA(ch) CTYPE_HACK(isalpha, ch)
68 #define ISASCII(ch) CTYPE_HACK(isascii, ch)
69 #define ISBLANK(ch) CTYPE_HACK(isblank, ch)
70 #define ISCNTRL(ch) CTYPE_HACK(iscntrl, ch)
71 #define ISDIGIT(ch) CTYPE_HACK(isdigit, ch)
72 #define ISGRAPH(ch) CTYPE_HACK(isgraph, ch)
73 #define ISLOWER(ch) CTYPE_HACK(islower, ch)
74 #define ISPRINT(ch) CTYPE_HACK(isprint, ch)
75 #define ISPUNCT(ch) CTYPE_HACK(ispunct, ch)
76 #define ISSPACE(ch) CTYPE_HACK(isspace, ch)
77 #define ISUPPER(ch) CTYPE_HACK(isupper, ch)
78 #define ISXDIGIT(ch) CTYPE_HACK(isxdigit, ch)
80 #define TOASCII(ch) CTYPE_HACK(toascii, ch)
81 #define TOLOWER(ch) CTYPE_HACK(tolower, ch)
82 #define TOUPPER(ch) CTYPE_HACK(toupper, ch)
84 #define MEMCMP(x, op, y, n) (memcmp((x), (y), (n)) op 0)
85 #define STRCMP(x, op, y) (strcmp((x), (y)) op 0)
86 #define STRNCMP(x, op, y, n) (strncmp((x), (y), (n)) op 0)
88 /*----- Compiler diagnostics ----------------------------------------------*/
90 /* --- Compiler-specific definitions --- */
92 #if GCC_VERSION_P(2, 5) || CLANG_VERSION_P(3, 3)
93 # define NORETURN __attribute__((__noreturn__))
94 # define PRINTF_LIKE(fix, aix) __attribute__((__format__(printf, fix, aix)))
95 # define SCANF_LIKE(fix, aix) __attribute__((__format__(scanf, fix, aix)))
96 # define IGNORABLE __attribute__((__unused__))
99 #if GCC_VERSION_P(3, 4) || CLANG_VERSION_P(3, 3)
100 # define MUST_CHECK __attribute__((__warn_unused_result__))
103 #if GCC_VERSION_P(4, 5) || CLANG_VERSION_P(3, 3)
104 # define DEPRECATED(msg) __attribute__((__deprecated__(msg)))
105 #elif GCC_VERSION_P(3, 1)
106 # define DEPRECATED(msg) __attribute__((__deprecated__))
109 #if GCC_VERSION_P(4, 0) || CLANG_VERSION_P(3, 3)
110 # define EXECL_LIKE(ntrail) __attribute__((__sentinel__(ntrail)))
113 #if GCC_VERSION_P(2, 7) || CLANG_VERSION_P(0, 0)
114 # define LAUNDER(x) \
115 ({ __typeof__(x) _y; __asm__("" : "=g"(_y) : "0"(x)); _y; })
116 # define RELAX do __asm__ __volatile__("" ::: "memory"); while (0)
119 #if CLANG_VERSION_P(3, 3)
121 # define MLIB__PRAGMA_HACK(x) _Pragma(#x)
122 # define MLIB__MUFFLE_WARNINGS(warns, body) \
123 _Pragma("clang diagnostic push") \
126 _Pragma("clang diagnostic pop")
127 # define CLANG_WARNING(warn) \
128 MLIB__PRAGMA_HACK(clang diagnostic ignored warn)
129 # define MUFFLE_WARNINGS_DECL(warns, body) \
130 MLIB__MUFFLE_WARNINGS(warns, body)
131 # define MUFFLE_WARNINGS_EXPR(warns, body) \
132 __extension__ ({ MLIB__MUFFLE_WARNINGS(warns, (body);) })
133 # define MUFFLE_WARNINGS_STMT(warns, body) \
134 do { MLIB__MUFFLE_WARNINGS(warns, body) } while (0)
138 #if GCC_VERSION_P(4, 6)
140 /* --- Diagnostic suppression in GCC: a tale of woe --- *
142 * This is extremely unpleasant, largely as a result of bugs in the GCC
143 * preprocessor's handling of @_Pragma@. The fundamental problem is
144 * that it's the preprocessor, and not the compiler proper, which
145 * detects @_Pragma@, emitting @#pragma@ lines into its output; and it
146 * does it during macro expansion, even if the macro is being expanded
147 * during argument collection. Since arguments are expanded before
148 * replacing the macro's invocation with its body, a pragma in an
149 * argument will be emitted %%\emph{before}%% any pragmata in the body,
150 * even if they appear before the argument in the body -- and even if
151 * the argument doesn't actually appear anywhere at all in the body.
153 * Another, rather less significant, problem is that @_Pragma@'s
154 * argument is a single string literal, recognized in translation phase
155 * 4, before string-literal concatenation in phase 6, so we must build
156 * pragma bodies as token lists and then stringify them.
158 * As a result, we need some subterfuge here. The @MLIB__PRAGMA_HACK@
159 * macro issues a @_Pragma@ on its argument token list, which it
160 * stringifies; this deals with the second problem. The first is
161 * trickier: we must delay expansion of @MLIB__PRAGMA_HACK@ from the
162 * argument collection phase to the body rescanning phase, and we do
163 * this by splitting the invocations between @GCC_WARNING@ macro calls:
164 * the name is left hanging from the previous call (or from
165 * @MLIB__MUFFLE_WARNINGS@, in the first case) and the body is supplied
166 * by @GCC_WARNING@, which also supplies the next @MLIB__PRAGMA_HACK@.
167 * The remaining problem is to make sure we can dispose of the final
168 * trailing @MLIB__PRAGMA_HACK@ harmlessly, which we do by introducing
169 * an extra argument @emitp@, which may be either @t@ or @nil@; this
170 * dispatches to an appropriate helper macro by means of token-pasting.
175 # define MLIB__PRAGMA_HACK_t(x) _Pragma(#x)
176 # define MLIB__PRAGMA_HACK_nil(x)
177 # define MLIB__PRAGMA_HACK(emitp, x) MLIB__PRAGMA_HACK_##emitp(x)
178 # define MLIB__MUFFLE_WARNINGS(warns, body) \
179 _Pragma("GCC diagnostic push") MLIB__PRAGMA_HACK \
183 _Pragma("GCC diagnostic pop")
184 # define GCC_WARNING(warn) \
185 (t, GCC diagnostic ignored warn) MLIB__PRAGMA_HACK
186 # define MUFFLE_WARNINGS_DECL(warns, body) \
187 MLIB__MUFFLE_WARNINGS(warns, body)
188 # define MUFFLE_WARNINGS_EXPR(warns, body) \
189 __extension__ ({ MLIB__MUFFLE_WARNINGS(warns, (body);) })
190 # define MUFFLE_WARNINGS_STMT(warns, body) \
191 do { MLIB__MUFFLE_WARNINGS(warns, body) } while (0)
194 /* --- Fallback definitions, mostly trivial --- */
197 # define DEPRECATED(msg)
201 # define EXECL_LIKE(ntrail)
205 # define DISCARD(x) do if (x); while (0)
209 # define IGNORE(x) ((void)(x))
212 #ifndef MUFFLE_WARNINGS_DECL
213 # define MUFFLE_WARNINGS_DECL(warns, body) body
216 #ifndef MUFFLE_WARNINGS_EXPR
217 # define MUFFLE_WARNINGS_EXPR(warns, body) (body)
220 #ifndef MUFFLE_WARNINGS_STMT
221 # define MUFFLE_WARNINGS_STMT(warns, body) do { body } while (0)
225 # define PRINF_LIKE(fmtix, argix)
229 # define SCANF_LIKE(fmtix, argix)
253 # define GCC_WARNING(warn)
256 #ifndef CLANG_WARNING
257 # define CLANG_WARNING(warn)
260 /*----- That's all, folks -------------------------------------------------*/