Page MenuHomePhabricator

Make stdarg.h compatible with FreeBSD
Needs ReviewPublic

Authored by arichardson on Mar 17 2018, 4:57 PM.



On FreeBSD it is currently not possible to use the clang builtin
headers to build the base system. The build will fail with the
following error if I don't delete the std* headers from the clang
install directory:

/sources/freebsd-mips/include/stdio.h:72:19: error:
redefinition of typedef 'va_list' is a C11 feature
typedef __va_list       va_list;
note: previous definition is here
typedef __builtin_va_list va_list;

/sources/freebsd-mips/sys/sys/_stdarg.h:46:11: error: 'va_start' macro redefined
  #define       va_start(ap, last)      __builtin_va_start((ap), (last))
note: previous definition is here
  #define va_start(ap, param) __builtin_va_start(ap, param)

FreeBSD includes definitions of va_list, va_copy, etc in the
<machine/stdarg.h> header and some files end up including both that
header and <stdarg.h>.

Only defining the va_* macros if they are not yet defined fixes the
macro redefiniton issue and probably also makes sense for other
operating systems. However, for the va_list typedef I can't think
of a better solution than to also check for _VA_LIST_DECLARED and
define it.

Diff Detail

Event Timeline

arichardson created this revision.Mar 17 2018, 4:57 PM
dim added a reviewer: emaste.Mar 17 2018, 5:15 PM
dim added a comment.Mar 17 2018, 5:17 PM

I'm fine with either this approach, or with the approach in stdint.h, e.g. using __has_include_next(), and in that case first including the system-provided header before this one.

This does not fix the potential problem on NetBSD. We define __VA_LIST_DECLARED (with extra _ on the beginning).

The proper solution looks to go for:

#if __STDC_HOSTED__ && __has_include_next(<stdarg.h>)
bsdjhb added a subscriber: bsdjhb.Apr 18 2018, 10:39 AM

FWIW, I ended up fixing FreeBSD to only use <machine/stdarg.h> in freestanding environments and always use <stdarg.h> in userland which makes this patch no longer necessary. (Only one place needed to be fixed.)