utils/macros.h: Introduce a `STATIC_ASSERT' 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
3832000d
MW
9.\" @DISCARD
10.\" @IGNORE
11.\" @DEPRECATED
12.\" @EXECL_LIKE
13.\" @IGNORABLE
14.\" @NORETURN
15.\" @PRINTF_LIKE
16.\" @SCANF_LIKE
17.\" @MUFFLE_WARNINGS_DECL
18.\" @MUFFLE_WARNINGS_EXPR
19.\" @MUFFLE_WARNINGS_STMT
3832000d 20.\" @GCC_WARNING
14d7100d 21.SH SYNOPSIS
22.nf
23.B "#include <mLib/macros.h>"
24
25.BI "size_t N(" array ");"
3832000d
MW
26.BI "STR(" tokens\fR... ")"
27.BI "GLUE(" tokens\fR... ", " tokens\fR... ")"
f50c1365 28.BI "STATIC_ASSERT(" cond ", " msg ");"
3832000d
MW
29
30.BI "void DISCARD(" scalar ");"
31.BI "void IGNORE(" variable ");"
32
33.BI "DEPRECATED(" msg ")"
34.BI "EXECL_LIKE(" ntrail ")"
35.BI "IGNORABLE"
36.BI "NORETURN"
37.BI "PRINTF_LIKE(" fmt-index ", " arg-index ")"
38.BI "SCANF_LIKE(" fmt-index ", " arg-index ")"
39
40.BI "MUFFLE_WARNINGS_DECL(" warns ", " decls ")"
41.BI "MUFFLE_WARNINGS_EXPR(" warns ", " expr ")"
42.BI "MUFFLE_WARNINGS_STMT(" warns ", " stmt ")"
43
3832000d 44.BI "GCC_WARNING(" option ")"
c418910f 45.BI "CLANG_WARNING(" option ")"
14d7100d 46.fi
47.SH DESCRIPTION
3832000d 48.SS Utilities
14d7100d 49The
50.B N
51macro returns the number of elements in the named
52.IR array .
3832000d
MW
53.PP
54The
55.B STR
56macro expands to a string literal containing the result of expanding its
57argument
58.IR tokens .
59.PP
60The
61.B GLUE
62macro expands to a single token, which is the result of gluing together
63the tokens resulting from expanding its argument token lists. Each of
64the argument token lists must expand to a single preprocessing token,
65and the result of gluing these tokens together must be valid
66preprocessing token.
67.PP
68The
f50c1365
MW
69.B STATIC_ASSERT
70causes compilation to fail if the integer constant expression
71.I cond
72evaluates to zero. This macro uses the C11
73.B static_assert
74declaration if available, and the
75.I msg
76will be reported in the compiler's diagnostic messsage; otherwise, the macro
77falls back to a somewhat ugly hack which currently ignores the
78.IR msg .
79.PP
80The
3832000d
MW
81.B DISCARD
82macro discards its argument, which must be of some scalar type. This
83can be useful in muffling warnings about ignoring return codes in cases
84where you really don't care.
85.PP
86The
87.B IGNORE
88macro ignores its argument, which may be an expression of any type.
89This can be useful in muffling warnings about unused variables.
90.SS Annotations
91The following annotations can be attached to function declarations and
92definitions, as part of the declaration specifiers. (Other positions
93may also work, depending on your compiler, but don't bet on it.) They
94might not have any effect, depending on your specific compiler.
95Currently only GCC is well supported, but exactly which features are
96available depend on the compiler version.
97.PP
98Using a function or variable marked as
99.B DEPRECATED
100may provoke a compiler warning; this warning may (depending on your
101compiler version) include the given
102.IR msg .
103.PP
104A variadic function marked as
105.B EXECL_LIKE
106must be called with a null pointer (i.e., an integer constant
107expression with value 0, cast to
108.BR "void *")
109in the variadic part of its argument list, followed by
110.I ntrail
111further arguments. Typically,
112.I ntrail
113is zero. Compilers may warn about misuse of such functions.
114.PP
115A function or variable marked as
116.B IGNORABLE
117need not be used. This may muffle warnings about leaving the marked
118definition unused.
119.PP
120A function marked as
121.B NORETURN
122must not return. It must have return type
123.BR void .
124This may be useful in muffling warnings about uninitialized variables,
125for example.
126.PP
127A variadic function marked as
128.B PRINTF_LIKE
129takes a
130.BR printf (3)-style
131format argument (in position
132.IR fmt-index ,
133counting from 1) and a variable-length list of arguments to be formatted
134(starting from position
135.IR arg-index ).
136Compilers may warn about misuse of such functions.
137.PP
138A variadic function marked as
139.B SCANF_LIKE
140takes a
141.BR scanf (3)-style
142format argument (in position
143.IR fmt-index ,
144counting from 1) and a variable-length list of arguments to be formatted
145(starting from position
146.IR arg-index ).
147Compilers may warn about misuse of such functions.
148.SS Muffling warnings
149Some compilers allow you to muffle warnings in particular pieces of
150code. These macros provide a compiler-neutral interface to such
151facilities. Each macro takes an argument
152.IR warns ,
153which is a sequence of calls to
154.IB compiler _WARNING
155macros listing the warnings to be muffled. The list may contain
156warnings for several different compilers. The other argument is a
157.I body
158consisting of declarations (in the case of
159.BR MUFFLE_WARNINGS_DECL ),
160an expression (for
161.BR MUFFLE_WARNINGS_EXPR ),
162or a statement (for
163.BR MUFFLE_WARNINGS_STMT ).
164.SS GCC-specific features
165The
166.B GCC_VERSION_P
167macro returns a nonzero value if the compiler is at least version
168.IB maj . min
169of GCC, and zero otherwise. It's useful in preprocessor conditions.
170.PP
171The
172.B GCC_WARNING
173macro is intended to be used in
174.I warns
175lists (see above). It takes a string-literal argument
176.I option
177naming a GCC warning option, e.g.,
178.BR """\-Wdiv-by-zero""" .
c418910f
MW
179.PP
180The
181.B CLANG_WARNING
182is similar, except that it works with the Clang compiler.
33e3ac90
MW
183.PP
184Note that including
185.B <mLib/macros.h>
186also defines the compiler-test macros in
187.BR <mLib/compiler.h>;
188see
189.BR compiler (3).
14d7100d 190.SH "SEE ALSO"
33e3ac90
MW
191.BR mLib (3),
192.BR compiler (3).
14d7100d 193.SH "AUTHOR"
9b5ac6ff 194Mark Wooding, <mdw@distorted.org.uk>