bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
* Using C2x attributes more effectively in Gnulib
@ 2021-07-31 19:52 Paul Eggert
  2021-08-08  1:40 ` Bruno Haible
  0 siblings, 1 reply; 6+ messages in thread
From: Paul Eggert @ 2021-07-31 19:52 UTC (permalink / raw)
  To: Gnulib bugs

Because draft C2x requires using [[maybe_unused]] before a parameter 
instead of after, the recent C2x-related changes to Gnulib caused me to 
move all uses of _GL_UNUSED_PARAMETER; and while I was at it I changed 
it to _GL_ATTRIBUTE_MAYBE_UNUSED FILE thus removing a dependency on the 
snippet/unused-parameter module. For example:

   SE_SELINUX_INLINE int
  -fsetfilecon (int fd _GL_UNUSED_PARAMETER,
  -             char const *con _GL_UNUSED_PARAMETER)
  +fsetfilecon (_GL_ATTRIBUTE_MAYBE_UNUSED int fd,
  +             _GL_ATTRIBUTE_MAYBE_UNUSED char const *con)
     { errno = ENOTSUP; return -1; }

A nice property of the change is that the text lines up a bit better. 
However, _GL_ATTRIBUTE_MAYBE_UNUSED is too long, so I propose we rename 
it to something shorter.

Also, draft C2x lets one write the above function without naming the 
parameters, as follows:

   SE_SELINUX_INLINE int
   fsetfilecon (int, char const *)
     { errno = ENOTSUP; return -1; }

This is nicer than [[maybe_unused]], because it says the arguments are 
*definitely* unused instead of merely *maybe* unused, and that allows a 
bit more checking of the code.

So, how about the following ideas:

* Rename _GL_ATTRIBUTE_MAYBE_UNUSED to _GL_maybe_unused. Similarly for 
_GL_deprecated, _GL_fallthrough, and _GL_nodiscard. As C2x becomes more 
popular, it'll be easy to read _GL_deprecated as shorthand for 
"[[deprecated]] if supported, empty otherwise". Using lowercase in the 
macro names helps readability and will help avoid collisions between 
future C2x-like attributes and other Gnulib macros.

* Remove all uses of _GL_UNUSED after arguments in Gnulib, replacing 
them with _GL_maybe_unused before arguments. This will support non-GCC 
C2x compilers better.  Deprecate _GL_UNUSED.

* Define a macro _GL_UNUSED_ARG(TYPE, NAME) that expands to 'TYPE' in 
draft C2x, and to '_GL_maybe_unused TYPE NAME' otherwise. That way, one 
can write:

   SE_SELINUX_INLINE int
   fsetfilecon (_GL_UNUSED_ARG (int, fd),
                _GL_UNUSED_ARG (char const *, con))
     { errno = ENOTSUP; return -1; }

* Define a macro _GL_UNUSED_ARGNAME(NAME) that expands to NAME if not 
draft C2x, empty otherwise. Programs can use this macro for complicated 
argument types where _GL_UNUSED_ARG does not suffice, e.g., 
'_GL_maybe_unused int (*_GL_UNUSED_ARGNAME (p)) (int)'.

* Remove the snippet/unused-parameter module as it's not used now.


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-08-23  1:06 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-31 19:52 Using C2x attributes more effectively in Gnulib Paul Eggert
2021-08-08  1:40 ` Bruno Haible
2021-08-09 10:08   ` Paul Eggert
2021-08-22 13:33     ` Bruno Haible
2021-08-22 20:05     ` gnulib-common.m4: Use C2x [[maybe_unused]] when possible Bruno Haible
2021-08-23  1:06     ` snippet/unused-parameter: Mark unused module obsolete Bruno Haible

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).