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