[PATCH] adjust warning_n() to take uhwi (PR 84207)
Pedro Alves
palves@redhat.com
Wed Feb 14 20:16:00 GMT 2018
On 02/13/2018 10:37 PM, Martin Sebor wrote:
> On 02/13/2018 01:59 PM, Manuel López-Ibáñez wrote:
>>
> Here's a sketch of what I tried to do:
>
> Â struct IntegerConverter
> Â {
> Â Â Â union {
> Â Â Â Â Â tree t;
> Â Â Â Â Â unsigned HOST_WIDE_INT hwi;
> Â Â Â Â Â // buffer for offset_int, wide_int, etc.
> Â Â Â } value;
>
> Â Â Â IntegerConverter (tree t)
> Â Â Â {
> Â Â Â Â Â value.t = t;
> Â Â Â }
>
> Â Â Â IntegerConverter (unsigned HOST_WIDE_INT x)
> Â Â Â {
> Â Â Â Â Â value.x = x;
> Â Â Â }
>
> Â Â Â ...
> Â };
>
> Â void error_n (int, const IntegerConverter &, const char*, ...);
> Â ...
>
> With that, the call
>
> Â error_n (loc, t, "%E thing", "%E things", t);
>
> works when t is a tree, and the call to the same function
>
> Â error_n (loc, i, "%wu thing", "%wu things", i);
>
> also works when i is a HOST_WIDE_INT. I chose this approach
> to avoid introducing additional overloads of the error_n()
> functions.
Instead of a class that has to have a constructor for every type
you want to pass as plural selector to the _n functions, which
increases coupling, I'd suggest using a conversion function, and
overload that. I.e., something like, in the core diagnostics code:
static inline unsigned HOST_WIDE_INT
as_plural_form (unsigned HOST_WIDE_INT val)
{
return val;
}
/* In some tree-diagnostics header. */
static inline unsigned HOST_WIDE_INT
as_plural_form (tree t)
{
// extract & return a HWI
}
/* In some whatever-other-type-diagnostics header. */
static inline unsigned HOST_WIDE_INT
as_plural_form (whatever_other_type v)
{
// like above
}
and then you call error_n and other similar functions like this:
error_n (loc, u, "%u thing", "%u things", u);
error_n (loc, as_plural_form (u), "%u thing", "%u things", u);
error_n (loc, as_plural_form (t), "%E thing", "%E things", t);
error_n (loc, as_plural_form (i), "%wu thing", "%wu things", i);
This is similar in spirit to std::to_string, etc.
BTW, the "plural_form" naming above comes from ngettext's documentation
of the N parameter:
char * ngettext (const char *msgid1, const char *msgid2, unsigned long int n);
"The parameter n is used to determine the plural form."
Pedro Alves
More information about the Gcc-patches
mailing list