3 .\" Manual for miscellaneous macros
5 .\" (c) 2003, 2005, 2009, 2013, 2018, 2019, 2022, 2024 Straylight/Edgeware
8 .\"----- Licensing notice ---------------------------------------------------
10 .\" This file is part of the mLib utilities library.
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.
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.
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,
27 .\"--------------------------------------------------------------------------
28 .so ../defs.man \" @@@PRE@@@
30 .\"--------------------------------------------------------------------------
31 .TH macros 3mLib "13 December 2003" "Straylight/Edgeware" "mLib utilities library"
38 .\" @CONVERT_CAREFULLY
80 .\" @MUFFLE_WARNINGS_DECL
81 .\" @MUFFLE_WARNINGS_EXPR
82 .\" @MUFFLE_WARNINGS_STMT
86 .\"--------------------------------------------------------------------------
88 macros \- useful macros
90 .\"--------------------------------------------------------------------------
94 .B "#include <mLib/macros.h>"
96 .BI "size_t N(" type " " array "[]);"
97 .BI "STR(" tokens\fR... ")"
98 .BI "GLUE(" tokens\fR... ", " tokens\fR... ")"
99 .BI "GLUE3(" tokens\fR... ", " tokens\fR... ", " tokens\fR... ")"
100 .BI "STATIC_ASSERT(" cond ", " msg ");"
101 .BI "int CHECK_TYPE(" expty ", " expty " " x );
102 .IB newty " CONVERT_CAREFULLY(" newty ", " expty ", " expty " " x );
103 .IB type " *UNCONST(" type ", const " type " *" p );
104 .IB type " *UNVOLATILE(" type ", volatile " type " *" p );
105 .IB type " *UNQUALIFY(" type ", const volatile " type " *" p );
108 .BI "ISALNUM(int " ch ");"
109 .BI "ISALPHA(int " ch ");"
110 .BI "ISASCII(int " ch ");"
111 .BI "ISBLANK(int " ch ");"
112 .BI "ISCNTRL(int " ch ");"
113 .BI "ISDIGIT(int " ch ");"
114 .BI "ISGRAPH(int " ch ");"
115 .BI "ISLOWER(int " ch ");"
116 .BI "ISPRINT(int " ch ");"
117 .BI "ISPUNCT(int " ch ");"
118 .BI "ISSPACE(int " ch ");"
119 .BI "ISUPPER(int " ch ");"
120 .BI "ISXDIGIT(int " ch ");"
121 .BI "TOASCII(int " ch ");"
122 .BI "TOLOWER(int " ch ");"
123 .BI "TOUPPER(int " ch ");"
125 .BI "MEMCMP(const void *" x ", " op ", const void *" y ", size_t " n ");"
126 .BI "STRCMP(const char *" x ", " op ", const char *" y ");"
127 .BI "STRNCMP(const char *" x ", " op ", const char *" y ", size_t " n ");"
129 .BI "void DISCARD(" scalar ");"
130 .BI "void IGNORE(" variable ");"
131 .IB type " LAUNDER(" type " " x ");"
132 .BI "void ADMIRE(" type " " x ");"
133 .BI "void ADMIRE_BUF(void *" p ", size_t " sz ");"
136 .BI "DEPRECATED(" msg ")"
137 .BI "EXECL_LIKE(" ntrail ")"
141 .BI "PRINTF_LIKE(" fmt-index ", " arg-index ")"
142 .BI "SCANF_LIKE(" fmt-index ", " arg-index ")"
144 .BI "MUFFLE_WARNINGS_DECL(" warns ", " decls ")"
145 .BI "MUFFLE_WARNINGS_EXPR(" warns ", " expr ")"
146 .BI "MUFFLE_WARNINGS_STMT(" warns ", " stmt ")"
148 .BI "GCC_WARNING(" option ")"
149 .BI "CLANG_WARNING(" option ")"
152 .\"--------------------------------------------------------------------------
158 macro returns the number of elements in the named
163 macro expands to a string literal containing the result of expanding its
169 macro expands to a single token, which is the result of gluing together
170 the tokens resulting from expanding its argument token lists. Each of
171 the argument token lists must expand to a single preprocessing token,
172 and the result of gluing these tokens together must be valid
173 preprocessing token. The
175 macro does the same, except that it glues together
177 argument token lists rather than two.
181 macro causes compilation to fail if the integer constant expression
183 evaluates to zero. This macro uses the C11
185 declaration if available, and the
187 will be reported in the compiler's diagnostic messsage; otherwise, the macro
188 falls back to a somewhat ugly hack which currently ignores the
193 macro checks at compile-time that
195 has (or, at least, is assignment-compatible with) type
197 If so, the result is the integer zero.
200 macro similarly checks at compile-time that
204 and if so, the result is
211 macro checks at compile-time that
213 has (or, at least, is assignment-compatible with) type
214 .BI "const " type "\ *" \fR.
221 qualification from the type that
226 macro is similar, except that it removes any
240 macro expands to a comma
242 which is useful for smuggling commas into macro arguments
243 if they can't be protected by parentheses.
249 macros are wrappers around the corresponding standard
251 macros with the corresponding lowercase names. They take care of
252 forcing the character argument
255 .BR "unsigned char" :
256 this conversion is necessary on platforms with signed
258 to avoid passing negative values into the standard macros.
265 macros are wrappers around the standard
267 functions with the corresponding lowercase names. They take an
270 which is a equality or ordering operator (e.g.,
274 inserted between the two operands. The standard functions return a
275 false value if and only if the operands are equal, which is
276 counterintuitive and leads to mistakes; requiring an explicit relational
277 operator should reduce the number of such mistakes.
281 macro discards its argument, which must be of some scalar type. This
282 can be useful in muffling warnings about ignoring return codes in cases
283 where you really don't care.
287 macro ignores its argument, which may be an expression of any type.
288 This can be useful in muffling warnings about unused variables.
292 macro tries to confuse a compiler so that it `forgets' what it knows
293 about a particular value.
297 macro tries to confuse a compiler so that it will faithfully computes
300 even though it's not used for anything. The
302 macro works similarly, but on regions of memory.
306 macro tries do nothing, but in a way that a compiler won't optimize
315 macros are most useful in benchmarking and similar applications.
317 The following annotations can be attached to function declarations and
318 definitions, as part of the declaration specifiers. (Other positions
319 may also work, depending on your compiler, but don't bet on it.) They
320 might not have any effect, depending on your specific compiler.
321 Currently only GCC is well supported, but exactly which features are
322 available depend on the compiler version.
324 Using a function or variable marked as
326 may provoke a compiler warning; this warning may (depending on your
327 compiler version) include the given
330 A variadic function marked as
332 must be called with a null pointer (i.e., an integer constant
333 expression with value 0, cast to
335 in the variadic part of its argument list, followed by
337 further arguments. Typically,
339 is zero. Compilers may warn about misuse of such functions.
341 A function or variable marked as
343 need not be used. This may muffle warnings about leaving the marked
348 returns an important value: a warning may be issued if a caller
349 ignores the value. The return type must not be
354 must not return. It must have return type
356 This may be useful in muffling warnings about uninitialized variables,
359 A variadic function marked as
363 format argument (in position
365 counting from 1) and a variable-length list of arguments to be formatted
366 (starting from position
368 Compilers may warn about misuse of such functions.
370 A variadic function marked as
374 format argument (in position
376 counting from 1) and a variable-length list of arguments to be formatted
377 (starting from position
379 Compilers may warn about misuse of such functions.
381 .SS Muffling warnings
382 Some compilers allow you to muffle warnings in particular pieces of
383 code. These macros provide a compiler-neutral interface to such
384 facilities. Each macro takes an argument
386 which is a sequence of calls to
387 .IB compiler _WARNING
388 macros listing the warnings to be muffled. The list may contain
389 warnings for several different compilers. The other argument is a
391 consisting of declarations (in the case of
392 .BR MUFFLE_WARNINGS_DECL ),
394 .BR MUFFLE_WARNINGS_EXPR ),
396 .BR MUFFLE_WARNINGS_STMT ).
398 .SS GCC-specific features
401 macro is intended to be used in
403 lists (see above). It takes a string-literal argument
405 naming a GCC warning option, e.g.,
406 .BR """\-Wdiv-by-zero""" .
410 is similar, except that it works with the Clang compiler.
414 also defines the compiler-test macros in
415 .BR <mLib/compiler.h>;
419 .\"--------------------------------------------------------------------------
425 .\"--------------------------------------------------------------------------
428 Mark Wooding, <mdw@distorted.org.uk>
430 .\"----- That's all, folks --------------------------------------------------