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