This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH] Use mode_t in open and friends
- From: Pedro Alves <palves at redhat dot com>
- To: OndÅej BÃlka <neleai at seznam dot cz>
- Cc: Andreas Schwab <schwab at linux-m68k dot org>, Rasmus Villemoes <rv at rasmusvillemoes dot dk>, libc-alpha at sourceware dot org
- Date: Sat, 03 May 2014 17:52:02 +0100
- Subject: Re: [PATCH] Use mode_t in open and friends
- Authentication-results: sourceware.org; auth=none
- References: <87k3afhsua dot fsf at rasmusvillemoes dot dk> <87a9banc4i dot fsf at igel dot home> <20140503092848 dot GC15537 at domone dot podge>
On 05/03/2014 10:28 AM, OndÅej BÃlka wrote:
> On Thu, Apr 24, 2014 at 02:30:21PM +0200, Andreas Schwab wrote:
>> Rasmus Villemoes <rv@rasmusvillemoes.dk> writes:
>>
>>> The sole varargs argument to open and friends has type mode_t, not int.
>>
>> Varargs use the promoted type, but mode_t traditionally has been
>> unsigned short.
>>
> So which solution do you prefer? Adding a custom type for that as
> suggested in sibling threads? What about just adding a cast like
>
> mode = (mode_t) va_arg (arg, int);
>
Might be overkill for this instance alone, but another option
would be letting the compiler do the work. It avoids having
to care whether mode_t might be wider than int, or unsigned.
Can be done with C11's _Generic, or GCC's
__builtin_choose_expr/__builtin_types_compatible_p.
Here's how it might look with the latter:
mode = __unpromoted_va_arg (arg, mode_t);
#define __maybe_va_arg(list, type, unpromoted, promoted, else_expr) \
__builtin_choose_expr (__builtin_types_compatible_p (type, unpromoted), \
va_arg (list, promoted), \
else_expr)
/* Like va_arg, but accepts unpromoted types. */
#define __promoted_va_arg(list, type) \
(type) __maybe_va_arg (list, type, float, double, \
__maybe_va_arg (list, type, char, int, \
__maybe_va_arg (list, type, short int, int, \
__maybe_va_arg (list, type, unsigned char, unsigned int, \
__maybe_va_arg (list, type, unsigned short int, unsigned int, \
va_arg (list, type))))))
--
Pedro Alves