@@@ fltfmt mess
[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
b1a20bee
MW
36.\" @CHECK_TYPE
37.\" @CONVERT_CAREFULLY
38.\" @UNCONST
39.\" @UNVOLATILE
40.\" @UNQUALIFY
41.\" @COMMA
6e683a79 42.
36188114
MW
43.\" @ISALNUM
44.\" @ISALPHA
45.\" @ISASCII
46.\" @ISBLANK
47.\" @ISCNTRL
48.\" @ISDIGIT
49.\" @ISGRAPH
50.\" @ISLOWER
51.\" @ISPRINT
52.\" @ISPUNCT
53.\" @ISSPACE
54.\" @ISUPPER
55.\" @ISXDIGIT
56.\" @TOASCII
57.\" @TOLOWER
58.\" @TOUPPER
6e683a79 59.
36188114
MW
60.\" @MEMCMP
61.\" @STRCMP
62.\" @STRNCMP
6e683a79 63.
3832000d
MW
64.\" @DISCARD
65.\" @IGNORE
6e683a79
MW
66.\" @LAUNDER
67.\" @RELAX
68.
3832000d 69.\" @DEPRECATED
3832000d 70.\" @IGNORABLE
8c470f2a 71.\" @MUST_CHECK
3832000d
MW
72.\" @NORETURN
73.\" @PRINTF_LIKE
74.\" @SCANF_LIKE
6e683a79
MW
75.\" @EXECL_LIKE
76.
3832000d
MW
77.\" @MUFFLE_WARNINGS_DECL
78.\" @MUFFLE_WARNINGS_EXPR
79.\" @MUFFLE_WARNINGS_STMT
3832000d 80.\" @GCC_WARNING
2f2cb664 81.\" @CLANG_WARNING
c4ccbbf9
MW
82.
83.\"--------------------------------------------------------------------------
84.SH NAME
85macros \- useful macros
86.
87.\"--------------------------------------------------------------------------
14d7100d 88.SH SYNOPSIS
c4ccbbf9 89.
14d7100d 90.nf
91.B "#include <mLib/macros.h>"
d056fbdf 92.PP
b1a20bee 93.BI "size_t N(" type " " array "[]);"
3832000d
MW
94.BI "STR(" tokens\fR... ")"
95.BI "GLUE(" tokens\fR... ", " tokens\fR... ")"
f50c1365 96.BI "STATIC_ASSERT(" cond ", " msg ");"
b1a20bee
MW
97.BI "int CHECK_TYPE(" expty ", " expty " " x );
98.IB newty " CONVERT_CAREFULLY(" newty ", " expty ", " expty " " x );
99.IB type " *UNCONST(" type ", const " type " *" p );
100.IB type " *UNVOLATILE(" type ", volatile " type " *" p );
101.IB type " *UNQUALIFY(" type ", const volatile " type " *" p );
102.B "#define COMMA ,"
d056fbdf 103.PP
36188114
MW
104.BI "ISALNUM(int " ch ");"
105.BI "ISALPHA(int " ch ");"
106.BI "ISASCII(int " ch ");"
107.BI "ISBLANK(int " ch ");"
108.BI "ISCNTRL(int " ch ");"
109.BI "ISDIGIT(int " ch ");"
110.BI "ISGRAPH(int " ch ");"
111.BI "ISLOWER(int " ch ");"
112.BI "ISPRINT(int " ch ");"
113.BI "ISPUNCT(int " ch ");"
114.BI "ISSPACE(int " ch ");"
115.BI "ISUPPER(int " ch ");"
116.BI "ISXDIGIT(int " ch ");"
117.BI "TOASCII(int " ch ");"
118.BI "TOLOWER(int " ch ");"
119.BI "TOUPPER(int " ch ");"
d056fbdf 120.PP
36188114
MW
121.BI "MEMCMP(const void *" x ", " op ", const void *" y ", size_t " n ");"
122.BI "STRCMP(const char *" x ", " op ", const char *" y ");"
123.BI "STRNCMP(const char *" x ", " op ", const char *" y ", size_t " n ");"
d056fbdf 124.PP
3832000d
MW
125.BI "void DISCARD(" scalar ");"
126.BI "void IGNORE(" variable ");"
d056fbdf 127.PP
3832000d
MW
128.BI "DEPRECATED(" msg ")"
129.BI "EXECL_LIKE(" ntrail ")"
130.BI "IGNORABLE"
8c470f2a 131.BI "MUST_CHECK"
3832000d
MW
132.BI "NORETURN"
133.BI "PRINTF_LIKE(" fmt-index ", " arg-index ")"
134.BI "SCANF_LIKE(" fmt-index ", " arg-index ")"
d056fbdf 135.PP
3832000d
MW
136.BI "MUFFLE_WARNINGS_DECL(" warns ", " decls ")"
137.BI "MUFFLE_WARNINGS_EXPR(" warns ", " expr ")"
138.BI "MUFFLE_WARNINGS_STMT(" warns ", " stmt ")"
d056fbdf 139.PP
3832000d 140.BI "GCC_WARNING(" option ")"
c418910f 141.BI "CLANG_WARNING(" option ")"
14d7100d 142.fi
c4ccbbf9
MW
143.
144.\"--------------------------------------------------------------------------
14d7100d 145.SH DESCRIPTION
c4ccbbf9 146.
3832000d 147.SS Utilities
14d7100d 148The
149.B N
150macro returns the number of elements in the named
151.IR array .
3832000d
MW
152.PP
153The
154.B STR
155macro expands to a string literal containing the result of expanding its
156argument
157.IR tokens .
158.PP
159The
160.B GLUE
161macro expands to a single token, which is the result of gluing together
162the tokens resulting from expanding its argument token lists. Each of
163the argument token lists must expand to a single preprocessing token,
164and the result of gluing these tokens together must be valid
165preprocessing token.
166.PP
167The
f50c1365 168.B STATIC_ASSERT
6e683a79 169macro causes compilation to fail if the integer constant expression
f50c1365
MW
170.I cond
171evaluates to zero. This macro uses the C11
172.B static_assert
173declaration if available, and the
174.I msg
175will be reported in the compiler's diagnostic messsage; otherwise, the macro
176falls back to a somewhat ugly hack which currently ignores the
177.IR msg .
178.PP
179The
b1a20bee
MW
180.B CHECK_TYPE
181macro checks at compile-time that
182.I x
183has (or, at least, is assignment-compatible with) type
184.IR expty .
185If so, the result is the integer zero.
186The
187.B CONVERT_CAREFULLY
188macro similarly checks at compile-time that
189.I x
190has type
191.IR expty ,
192and if so, the result is
193.I x
194converted to type
195.IR newty .
196.PP
197The
198.B UNCONST
199macro checks at compile-time that
200.I p
201has (or, at least, is assignment-compatible with) type
202.BI "const " type "\ *" \fR.
203If so, it returns
204.I p
205converted to type
206.IB type "\ *" \fR,
207i.e., it removes any
208.B const
209qualification from the type that
210.I p
211points to.
212The
213.B UNVOLATILE
214macro is similar, except that it removes any
215.B volatile
216qualification;
217and the
218.B UNQUALIFY
219macro
220removes any
221.B const
222.I or
223.B volatile
224qualification.
225.PP
226The
6e683a79
MW
227.B COMMA
228macro expands to a comma
229.BR ` , ',
230which is useful for smuggling commas into macro arguments
231if they can't be protected by parentheses.
232.PP
233The
36188114
MW
234.BR IS ...\&
235and
236.BR TO ...\&
237macros are wrappers around the corresponding standard
238.B <ctype.h>
239macros with the corresponding lowercase names. They take care of
240forcing the character argument
241.I ch
242to
243.BR "unsigned char" :
244this conversion is necessary on platforms with signed
245.B char
246to avoid passing negative values into the standard macros.
247.PP
248The
249.BR MEMCMP ,
250.BR STRCMP ,
251and
252.B STRNCMP
253macros are wrappers around the standard
254.B <string.h>
255functions with the corresponding lowercase names. They take an
256additional argument
257.I op
258which is a equality or ordering operator (e.g.,
259.B ==
260or
261.BR > )
262inserted between the two operands. The standard functions return a
263false value if and only if the operands are equal, which is
264counterintuitive and leads to mistakes; requiring an explicit relational
265operator should reduce the number of such mistakes.
266.PP
267The
3832000d
MW
268.B DISCARD
269macro discards its argument, which must be of some scalar type. This
270can be useful in muffling warnings about ignoring return codes in cases
271where you really don't care.
272.PP
273The
274.B IGNORE
275macro ignores its argument, which may be an expression of any type.
276This can be useful in muffling warnings about unused variables.
6e683a79
MW
277.PP
278The
279.B LAUNDER
280macro tries to confuse a compiler so that it `forgets' what it knows
281about a particular value. This is most useful in benchmarking or
282similar applications.
283.PP
284The
285.B RELAX
286macro tries do nothing, but in a way that a compiler won't optimize
287away.
c4ccbbf9 288.
3832000d
MW
289.SS Annotations
290The following annotations can be attached to function declarations and
291definitions, as part of the declaration specifiers. (Other positions
292may also work, depending on your compiler, but don't bet on it.) They
293might not have any effect, depending on your specific compiler.
294Currently only GCC is well supported, but exactly which features are
295available depend on the compiler version.
296.PP
297Using a function or variable marked as
298.B DEPRECATED
299may provoke a compiler warning; this warning may (depending on your
300compiler version) include the given
301.IR msg .
302.PP
303A variadic function marked as
304.B EXECL_LIKE
305must be called with a null pointer (i.e., an integer constant
306expression with value 0, cast to
307.BR "void *")
308in the variadic part of its argument list, followed by
309.I ntrail
310further arguments. Typically,
311.I ntrail
312is zero. Compilers may warn about misuse of such functions.
313.PP
314A function or variable marked as
315.B IGNORABLE
316need not be used. This may muffle warnings about leaving the marked
317definition unused.
318.PP
319A function marked as
8c470f2a
MW
320.B MUST_CHECK
321returns an important value: a warning may be issued if a caller
322ignores the value. The return type must not be
323.BR void .
324.PP
325A function marked as
3832000d
MW
326.B NORETURN
327must not return. It must have return type
328.BR void .
329This may be useful in muffling warnings about uninitialized variables,
330for example.
331.PP
332A variadic function marked as
333.B PRINTF_LIKE
334takes a
335.BR printf (3)-style
336format argument (in position
337.IR fmt-index ,
338counting from 1) and a variable-length list of arguments to be formatted
339(starting from position
340.IR arg-index ).
341Compilers may warn about misuse of such functions.
342.PP
343A variadic function marked as
344.B SCANF_LIKE
345takes a
346.BR scanf (3)-style
347format argument (in position
348.IR fmt-index ,
349counting from 1) and a variable-length list of arguments to be formatted
350(starting from position
351.IR arg-index ).
352Compilers may warn about misuse of such functions.
c4ccbbf9 353.
3832000d
MW
354.SS Muffling warnings
355Some compilers allow you to muffle warnings in particular pieces of
356code. These macros provide a compiler-neutral interface to such
357facilities. Each macro takes an argument
358.IR warns ,
359which is a sequence of calls to
360.IB compiler _WARNING
361macros listing the warnings to be muffled. The list may contain
362warnings for several different compilers. The other argument is a
363.I body
364consisting of declarations (in the case of
365.BR MUFFLE_WARNINGS_DECL ),
366an expression (for
367.BR MUFFLE_WARNINGS_EXPR ),
368or a statement (for
369.BR MUFFLE_WARNINGS_STMT ).
c4ccbbf9 370.
3832000d
MW
371.SS GCC-specific features
372The
3832000d
MW
373.B GCC_WARNING
374macro is intended to be used in
375.I warns
376lists (see above). It takes a string-literal argument
377.I option
378naming a GCC warning option, e.g.,
379.BR """\-Wdiv-by-zero""" .
c418910f
MW
380.PP
381The
382.B CLANG_WARNING
383is similar, except that it works with the Clang compiler.
33e3ac90
MW
384.PP
385Note that including
386.B <mLib/macros.h>
387also defines the compiler-test macros in
388.BR <mLib/compiler.h>;
389see
390.BR compiler (3).
c4ccbbf9
MW
391.
392.\"--------------------------------------------------------------------------
14d7100d 393.SH "SEE ALSO"
c4ccbbf9 394.
33e3ac90 395.BR compiler (3).
c4ccbbf9
MW
396.BR mLib (3),
397.
398.\"--------------------------------------------------------------------------
14d7100d 399.SH "AUTHOR"
c4ccbbf9 400.
9b5ac6ff 401Mark Wooding, <mdw@distorted.org.uk>
c4ccbbf9
MW
402.
403.\"----- That's all, folks --------------------------------------------------