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