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