Commit | Line | Data |
---|---|---|
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 | ||
37 | #ifndef MLIB_COMPILER_H | |
38 | # include "compiler.h" | |
39 | #endif | |
40 | ||
3832000d | 41 | /*----- Miscellaneous utility macros --------------------------------------*/ |
14d7100d | 42 | |
43 | #define N(v) (sizeof(v)/sizeof(*v)) | |
44 | ||
16139d88 | 45 | #define MLIB__STR(x) #x |
46 | #define STR(x) MLIB__STR(x) | |
47 | ||
48 | #define MLIB__GLUE(x, y) x##y | |
49 | #define GLUE(x, y) MLIB__GLUE(x, y) | |
50 | ||
3832000d MW |
51 | /*----- Compiler diagnostics ----------------------------------------------*/ |
52 | ||
53 | /* --- Compiler-specific definitions --- */ | |
54 | ||
ff3d3f01 | 55 | #if GCC_VERSION_P(2, 5) || CLANG_VERSION_P(3, 3) |
d94956b5 MW |
56 | # define NORETURN __attribute__((noreturn)) |
57 | # define PRINTF_LIKE(fix, aix) __attribute__((format(printf, fix, aix))) | |
58 | # define SCANF_LIKE(fix, aix) __attribute__((format(scanf, fix, aix))) | |
59 | # define IGNORABLE __attribute__((unused)) | |
60 | #endif | |
61 | ||
ff3d3f01 | 62 | #if GCC_VERSION_P(4, 5) || CLANG_VERSION_P(3, 3) |
d94956b5 MW |
63 | # define DEPRECATED(msg) __attribute__((deprecated(msg))) |
64 | #elif GCC_VERSION_P(3, 1) | |
65 | # define DEPRECATED(msg) __attribute__((deprecated)) | |
66 | #endif | |
67 | ||
ff3d3f01 | 68 | #if GCC_VERSION_P(4, 0) || CLANG_VERSION_P(3, 3) |
d94956b5 MW |
69 | # define EXECL_LIKE(ntrail) __attribute__((sentinel(ntrail))) |
70 | #endif | |
71 | ||
ff3d3f01 MW |
72 | #if CLANG_VERSION_P(3, 3) |
73 | ||
74 | # define MLIB__PRAGMA_HACK(x) _Pragma(#x) | |
75 | # define MLIB__MUFFLE_WARNINGS(warns, body) \ | |
76 | _Pragma("clang diagnostic push") \ | |
77 | warns \ | |
78 | body \ | |
79 | _Pragma("clang diagnostic pop") | |
80 | # define CLANG_WARNING(warn) \ | |
81 | MLIB__PRAGMA_HACK(clang diagnostic ignored warn) | |
82 | # define MUFFLE_WARNINGS_DECL(warns, body) \ | |
83 | MLIB__MUFFLE_WARNINGS(warns, body) | |
84 | # define MUFFLE_WARNINGS_EXPR(warns, body) \ | |
85 | __extension__ ({ MLIB__MUFFLE_WARNINGS(warns, (body);) }) | |
86 | # define MUFFLE_WARNINGS_STMT(warns, body) \ | |
87 | do { MLIB__MUFFLE_WARNINGS(warns, body) } while (0) | |
88 | ||
89 | #elif GCC_VERSION_P(4, 6) | |
d94956b5 MW |
90 | |
91 | /* --- Diagnostic suppression in GCC: a tale of woe --- * | |
92 | * | |
93 | * This is extremely unpleasant, largely as a result of bugs in the GCC | |
94 | * preprocessor's handling of @_Pragma@. The fundamental problem is | |
95 | * that it's the preprocessor, and not the compiler proper, which | |
96 | * detects @_Pragma@, emitting @#pragma@ lines into its output; and it | |
97 | * does it during macro expansion, even if the macro is being expanded | |
98 | * during argument collection. Since arguments are expanded before | |
99 | * replacing the macro's invocation with its body, a pragma in an | |
100 | * argument will be emitted %%\emph{before}%% any pragmata in the body, | |
101 | * even if they appear before the argument in the body -- and even if | |
102 | * the argument doesn't actually appear anywhere at all in the body. | |
103 | * | |
104 | * Another, rather less significant, problem is that @_Pragma@'s | |
105 | * argument is a single string literal, recognized in translation phase | |
106 | * 4, before string-literal concatenation in phase 6, so we must build | |
107 | * pragma bodies as token lists and then stringify them. | |
108 | * | |
109 | * As a result, we need some subterfuge here. The @MLIB__PRAGMA_HACK@ | |
110 | * macro issues a @_Pragma@ on its argument token list, which it | |
111 | * stringifies; this deals with the second problem. The first is | |
112 | * trickier: we must delay expansion of @MLIB__PRAGMA_HACK@ from the | |
113 | * argument collection phase to the body rescanning phase, and we do | |
114 | * this by splitting the invocations between @GCC_WARNING@ macro calls: | |
115 | * the name is left hanging from the previous call (or from | |
116 | * @MLIB__MUFFLE_WARNINGS@, in the first case) and the body is supplied | |
117 | * by @GCC_WARNING@, which also supplies the next @MLIB__PRAGMA_HACK@. | |
118 | * The remaining problem is to make sure we can dispose of the final | |
119 | * trailing @MLIB__PRAGMA_HACK@ harmlessly, which we do by introducing | |
120 | * an extra argument @emitp@, which may be either @t@ or @nil@; this | |
121 | * dispatches to an appropriate helper macro by means of token-pasting. | |
122 | * | |
123 | * I'm so sorry. | |
124 | */ | |
125 | ||
126 | # define MLIB__PRAGMA_HACK_t(x) _Pragma(#x) | |
127 | # define MLIB__PRAGMA_HACK_nil(x) | |
128 | # define MLIB__PRAGMA_HACK(emitp, x) MLIB__PRAGMA_HACK_##emitp(x) | |
129 | # define MLIB__MUFFLE_WARNINGS(warns, body) \ | |
3832000d MW |
130 | _Pragma("GCC diagnostic push") MLIB__PRAGMA_HACK \ |
131 | warns \ | |
132 | (nil, nil) \ | |
133 | body \ | |
134 | _Pragma("GCC diagnostic pop") | |
d94956b5 | 135 | # define GCC_WARNING(warn) \ |
3832000d | 136 | (t, GCC diagnostic ignored warn) MLIB__PRAGMA_HACK |
d94956b5 | 137 | # define MUFFLE_WARNINGS_DECL(warns, body) \ |
3832000d | 138 | MLIB__MUFFLE_WARNINGS(warns, body) |
d94956b5 | 139 | # define MUFFLE_WARNINGS_EXPR(warns, body) \ |
3832000d | 140 | __extension__ ({ MLIB__MUFFLE_WARNINGS(warns, (body);) }) |
d94956b5 | 141 | # define MUFFLE_WARNINGS_STMT(warns, body) \ |
3832000d | 142 | do { MLIB__MUFFLE_WARNINGS(warns, body) } while (0) |
3832000d MW |
143 | #endif |
144 | ||
145 | /* --- Fallback definitions, mostly trivial --- */ | |
146 | ||
3832000d MW |
147 | #ifndef DEPRECATED |
148 | # define DEPRECATED(msg) | |
149 | #endif | |
150 | ||
151 | #ifndef EXECL_LIKE | |
152 | # define EXECL_LIKE(ntrail) | |
153 | #endif | |
154 | ||
155 | #ifndef DISCARD | |
156 | # define DISCARD(x) do if (x); while (0) | |
157 | #endif | |
158 | ||
159 | #ifndef IGNORE | |
160 | # define IGNORE(x) ((void)(x)) | |
161 | #endif | |
162 | ||
163 | #ifndef MUFFLE_WARNINGS_DECL | |
164 | # define MUFFLE_WARNINGS_DECL(warns, body) body | |
165 | #endif | |
166 | ||
167 | #ifndef MUFFLE_WARNINGS_EXPR | |
168 | # define MUFFLE_WARNINGS_EXPR(warns, body) (body) | |
169 | #endif | |
170 | ||
171 | #ifndef MUFFLE_WARNINGS_STMT | |
172 | # define MUFFLE_WARNINGS_STMT(warns, body) do { body } while (0) | |
173 | #endif | |
174 | ||
175 | #ifndef PRINTF_LIKE | |
176 | # define PRINF_LIKE(fmtix, argix) | |
177 | #endif | |
178 | ||
179 | #ifndef SCANF_LIKE | |
180 | # define SCANF_LIKE(fmtix, argix) | |
181 | #endif | |
182 | ||
183 | #ifndef IGNORABLE | |
184 | # define IGNORABLE | |
185 | #endif | |
186 | ||
187 | #ifndef GCC_WARNING | |
188 | # define GCC_WARNING(warn) | |
189 | #endif | |
190 | ||
ff3d3f01 MW |
191 | #ifndef CLANG_WARNING |
192 | # define CLANG_WARNING(warn) | |
193 | #endif | |
194 | ||
14d7100d | 195 | /*----- That's all, folks -------------------------------------------------*/ |
196 | ||
197 | #ifdef __cplusplus | |
198 | } | |
199 | #endif | |
200 | ||
201 | #endif |