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