This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Aw: Re: enumeration value ... not handled in switch
- From: Daniel Brockman <daniel at brockman dot se>
- To: hartmut dot schirmer at arcormail dot de
- Cc: gcc at gcc dot gnu dot org
- Date: Tue, 11 Jan 2005 17:03:55 +0100
- Subject: Re: Aw: Re: enumeration value ... not handled in switch
- References: <87k6qkr0nt.fsf@wigwam.deepwood.net><1F5C7ECB324CD411B58600D0B723F181779402@oehhex01.becker.de><17455984.1105449856020.JavaMail.ngmail@webmail-05.arcor-online.net>
hartmut.schirmer@arcormail.de writes:
> Hi,
>
> [...]
>> As it seems, this problem cannot be solved without adding an attribute
>> that declares a default case to be exceptional.
>>
>> Consider the following code:
>>
>> struct fruit
>> {
>> enum { APPLE, MELON, ORANGE } type;
>> ...
>> } fruit;
> [...]
>
> Adding another fruit BANANA
>
>> [...] should fail as
>> quickly and noisily as possible:
>>
>> switch (fruit.type)
>> {
>> case APPLE:
>> ...; break;
>> case MELON:
>> ...; break;
>> case ORANGE:
>> ...; break;
>> default:
>> abort ();
>> }
>
> This may produce a warning like
> enum value 'BANANA' triggers noreturn function 'abort' in switch
I don't see how triggering noreturn functions in a switch in itself
warrants a warning. I have lots of code that looks like this:
void
eat_small_fruit (struct fruit *fruit)
{
switch (fruit.type)
{
case APPLE:
...; break;
case MELON:
abort ();
case ORANGE:
...; break;
}
}
Here, passing a melon to `eat_small_fruit' is a programmer error,
because a melon is not considered to be a small fruit.
> Creating something like __builtin_warning()
>
> switch (fruit.type)
> {
> case APPLE:
> ...; break;
> case MELON:
> ...; break;
> case ORANGE:
> ...; break;
> default:
> __builtin_warning();
> }
>
> could trigger a similar warning without changing the control flow.
That suffers the same problem. Should this trigger a warning?
void
eat_small_fruit (struct fruit *fruit)
{
switch (fruit.type)
{
case APPLE:
...; break;
case MELON:
__builtin_warning ();
case ORANGE:
...; break;
}
}
Regards,
--
Daniel Brockman <daniel@brockman.se>