utils/macros.h: Add <ctype.h> and `foocmp' helper macros.
[mLib] / utils / macros.3
1 .\" -*-nroff-*-
2 .TH macros 3 "13 December 2003" "Straylight/Edgeware" "mLib utilities library"
3 .SH NAME
4 macros \- useful macros
5 .\" @N
6 .\" @STR
7 .\" @GLUE
8 .\" @STATIC_ASSERT
9 .\" @ISALNUM
10 .\" @ISALPHA
11 .\" @ISASCII
12 .\" @ISBLANK
13 .\" @ISCNTRL
14 .\" @ISDIGIT
15 .\" @ISGRAPH
16 .\" @ISLOWER
17 .\" @ISPRINT
18 .\" @ISPUNCT
19 .\" @ISSPACE
20 .\" @ISUPPER
21 .\" @ISXDIGIT
22 .\" @TOASCII
23 .\" @TOLOWER
24 .\" @TOUPPER
25 .\" @MEMCMP
26 .\" @STRCMP
27 .\" @STRNCMP
28 .\" @DISCARD
29 .\" @IGNORE
30 .\" @DEPRECATED
31 .\" @EXECL_LIKE
32 .\" @IGNORABLE
33 .\" @NORETURN
34 .\" @PRINTF_LIKE
35 .\" @SCANF_LIKE
36 .\" @MUFFLE_WARNINGS_DECL
37 .\" @MUFFLE_WARNINGS_EXPR
38 .\" @MUFFLE_WARNINGS_STMT
39 .\" @GCC_WARNING
40 .SH SYNOPSIS
41 .nf
42 .B "#include <mLib/macros.h>"
43
44 .BI "size_t N(" array ");"
45 .BI "STR(" tokens\fR... ")"
46 .BI "GLUE(" tokens\fR... ", " tokens\fR... ")"
47 .BI "STATIC_ASSERT(" cond ", " msg ");"
48
49 .BI "ISALNUM(int " ch ");"
50 .BI "ISALPHA(int " ch ");"
51 .BI "ISASCII(int " ch ");"
52 .BI "ISBLANK(int " ch ");"
53 .BI "ISCNTRL(int " ch ");"
54 .BI "ISDIGIT(int " ch ");"
55 .BI "ISGRAPH(int " ch ");"
56 .BI "ISLOWER(int " ch ");"
57 .BI "ISPRINT(int " ch ");"
58 .BI "ISPUNCT(int " ch ");"
59 .BI "ISSPACE(int " ch ");"
60 .BI "ISUPPER(int " ch ");"
61 .BI "ISXDIGIT(int " ch ");"
62 .BI "TOASCII(int " ch ");"
63 .BI "TOLOWER(int " ch ");"
64 .BI "TOUPPER(int " ch ");"
65
66 .BI "MEMCMP(const void *" x ", " op ", const void *" y ", size_t " n ");"
67 .BI "STRCMP(const char *" x ", " op ", const char *" y ");"
68 .BI "STRNCMP(const char *" x ", " op ", const char *" y ", size_t " n ");"
69
70 .BI "void DISCARD(" scalar ");"
71 .BI "void IGNORE(" variable ");"
72
73 .BI "DEPRECATED(" msg ")"
74 .BI "EXECL_LIKE(" ntrail ")"
75 .BI "IGNORABLE"
76 .BI "NORETURN"
77 .BI "PRINTF_LIKE(" fmt-index ", " arg-index ")"
78 .BI "SCANF_LIKE(" fmt-index ", " arg-index ")"
79
80 .BI "MUFFLE_WARNINGS_DECL(" warns ", " decls ")"
81 .BI "MUFFLE_WARNINGS_EXPR(" warns ", " expr ")"
82 .BI "MUFFLE_WARNINGS_STMT(" warns ", " stmt ")"
83
84 .BI "GCC_WARNING(" option ")"
85 .BI "CLANG_WARNING(" option ")"
86 .fi
87 .SH DESCRIPTION
88 .SS Utilities
89 The
90 .B N
91 macro returns the number of elements in the named
92 .IR array .
93 .PP
94 The
95 .B STR
96 macro expands to a string literal containing the result of expanding its
97 argument
98 .IR tokens .
99 .PP
100 The
101 .B GLUE
102 macro expands to a single token, which is the result of gluing together
103 the tokens resulting from expanding its argument token lists. Each of
104 the argument token lists must expand to a single preprocessing token,
105 and the result of gluing these tokens together must be valid
106 preprocessing token.
107 .PP
108 The
109 .B STATIC_ASSERT
110 causes compilation to fail if the integer constant expression
111 .I cond
112 evaluates to zero. This macro uses the C11
113 .B static_assert
114 declaration if available, and the
115 .I msg
116 will be reported in the compiler's diagnostic messsage; otherwise, the macro
117 falls back to a somewhat ugly hack which currently ignores the
118 .IR msg .
119 .PP
120 The
121 .BR IS ...\&
122 and
123 .BR TO ...\&
124 macros are wrappers around the corresponding standard
125 .B <ctype.h>
126 macros with the corresponding lowercase names. They take care of
127 forcing the character argument
128 .I ch
129 to
130 .BR "unsigned char" :
131 this conversion is necessary on platforms with signed
132 .B char
133 to avoid passing negative values into the standard macros.
134 .PP
135 The
136 .BR MEMCMP ,
137 .BR STRCMP ,
138 and
139 .B STRNCMP
140 macros are wrappers around the standard
141 .B <string.h>
142 functions with the corresponding lowercase names. They take an
143 additional argument
144 .I op
145 which is a equality or ordering operator (e.g.,
146 .B ==
147 or
148 .BR > )
149 inserted between the two operands. The standard functions return a
150 false value if and only if the operands are equal, which is
151 counterintuitive and leads to mistakes; requiring an explicit relational
152 operator should reduce the number of such mistakes.
153 .PP
154 The
155 .B DISCARD
156 macro discards its argument, which must be of some scalar type. This
157 can be useful in muffling warnings about ignoring return codes in cases
158 where you really don't care.
159 .PP
160 The
161 .B IGNORE
162 macro ignores its argument, which may be an expression of any type.
163 This can be useful in muffling warnings about unused variables.
164 .SS Annotations
165 The following annotations can be attached to function declarations and
166 definitions, as part of the declaration specifiers. (Other positions
167 may also work, depending on your compiler, but don't bet on it.) They
168 might not have any effect, depending on your specific compiler.
169 Currently only GCC is well supported, but exactly which features are
170 available depend on the compiler version.
171 .PP
172 Using a function or variable marked as
173 .B DEPRECATED
174 may provoke a compiler warning; this warning may (depending on your
175 compiler version) include the given
176 .IR msg .
177 .PP
178 A variadic function marked as
179 .B EXECL_LIKE
180 must be called with a null pointer (i.e., an integer constant
181 expression with value 0, cast to
182 .BR "void *")
183 in the variadic part of its argument list, followed by
184 .I ntrail
185 further arguments. Typically,
186 .I ntrail
187 is zero. Compilers may warn about misuse of such functions.
188 .PP
189 A function or variable marked as
190 .B IGNORABLE
191 need not be used. This may muffle warnings about leaving the marked
192 definition unused.
193 .PP
194 A function marked as
195 .B NORETURN
196 must not return. It must have return type
197 .BR void .
198 This may be useful in muffling warnings about uninitialized variables,
199 for example.
200 .PP
201 A variadic function marked as
202 .B PRINTF_LIKE
203 takes a
204 .BR printf (3)-style
205 format argument (in position
206 .IR fmt-index ,
207 counting from 1) and a variable-length list of arguments to be formatted
208 (starting from position
209 .IR arg-index ).
210 Compilers may warn about misuse of such functions.
211 .PP
212 A variadic function marked as
213 .B SCANF_LIKE
214 takes a
215 .BR scanf (3)-style
216 format argument (in position
217 .IR fmt-index ,
218 counting from 1) and a variable-length list of arguments to be formatted
219 (starting from position
220 .IR arg-index ).
221 Compilers may warn about misuse of such functions.
222 .SS Muffling warnings
223 Some compilers allow you to muffle warnings in particular pieces of
224 code. These macros provide a compiler-neutral interface to such
225 facilities. Each macro takes an argument
226 .IR warns ,
227 which is a sequence of calls to
228 .IB compiler _WARNING
229 macros listing the warnings to be muffled. The list may contain
230 warnings for several different compilers. The other argument is a
231 .I body
232 consisting of declarations (in the case of
233 .BR MUFFLE_WARNINGS_DECL ),
234 an expression (for
235 .BR MUFFLE_WARNINGS_EXPR ),
236 or a statement (for
237 .BR MUFFLE_WARNINGS_STMT ).
238 .SS GCC-specific features
239 The
240 .B GCC_VERSION_P
241 macro returns a nonzero value if the compiler is at least version
242 .IB maj . min
243 of GCC, and zero otherwise. It's useful in preprocessor conditions.
244 .PP
245 The
246 .B GCC_WARNING
247 macro is intended to be used in
248 .I warns
249 lists (see above). It takes a string-literal argument
250 .I option
251 naming a GCC warning option, e.g.,
252 .BR """\-Wdiv-by-zero""" .
253 .PP
254 The
255 .B CLANG_WARNING
256 is similar, except that it works with the Clang compiler.
257 .PP
258 Note that including
259 .B <mLib/macros.h>
260 also defines the compiler-test macros in
261 .BR <mLib/compiler.h>;
262 see
263 .BR compiler (3).
264 .SH "SEE ALSO"
265 .BR mLib (3),
266 .BR compiler (3).
267 .SH "AUTHOR"
268 Mark Wooding, <mdw@distorted.org.uk>