[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Using C2x attributes more effectively in Gnulib

From: Paul Eggert
Subject: Using C2x attributes more effectively in Gnulib
Date: Sat, 31 Jul 2021 12:52:50 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0

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:

 -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:

  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:

  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.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]