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