This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Aw: Re: enumeration value ... not handled in switch


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>


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]