svn commit: r334702 - head/sys/sys

Bruce Evans brde at optusnet.com.au
Wed Jun 6 12:28:44 UTC 2018


On Wed, 6 Jun 2018, Benjamin Kaduk wrote:

> On Wed, Jun 6, 2018 at 6:35 AM, Ravi Pokala <rpokala at freebsd.org> wrote:
>
>> Hi Mateusz,
>> ...
>>> ...
>>>  #ifdef _KERNEL
>>>  #define      malloc(size, type, flags) ({
>>       \
>>
>> Now that I'm taking another look at this, I'm confused as to why the
>> entire macro expansion is inside parentheses? (The braces make sense, since
>> this is a block with local variables which need to be contained.)
>
> This is a gcc (and clang) extension to allow the macro body to be a code
> block -- standard C gets unhappy with just the curly braces.
> https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html is a maybe-relevant
> page that google found me.

Not really.

This is a syntax error which is only accepted by broken compilers,
since the language standard is doubly mis-specified by hard-coding it
to a wrong value using CSTD=c89 in kern.mk.  This asks for c99 with
no extensions, but many extensions are needed.  These bugs are missing
in the corresponding userland makefile bsd.sys.mk.  That uses CSTD?=gnu99.
This asks for c99 with gnu extensions, which is what is needed, but allows
the user to ask for another standard by setting CSTD.

To allow the user to ask for c99, or just to not depend on compiler bugs
when kern.mk asks for c99, use of this and other extensions should be
marked with __extension__, as is most often done for this particular
extension.

Braces in macros are perfectly standard.  The extension is to allow a
compound statement (delimited by braces) to return a value.  This is done
by enclosing the statement in parentheses.  The value of the statement is
the value of the last expression in it.  This is an extension of c99, since
in c99 about the only things that can be enclosed in parentheses are
expressions, but general statements are not expressions.  Especially
compound statements.

This used to be properly documented (in installed documentation) in
/usr/share/info/gcc.info.gz.

Bruce


More information about the svn-src-head mailing list