[fpc-devel] Data flow analysis (dfa) and "case ... of"

Martin fpc at mfriebe.de
Wed Jun 7 13:59:49 CEST 2017


On 07/06/2017 00:04, Denis Kozlov wrote:
>
> Consider the code fragment below. Currently, FCP 3.0.2 with -O3 shows 
> "uninitialized result" warning for Convert2 and Convert3 functions, 
> but not for Convert1. I find this perfect as is, and, of course, you 
> can rightfully disagree.
>
> You suggest that only Convert3 function should raise "uninitialized 
> result" warning, while Convert1 and Convert2 should not. I find this 
> less useful, and, again, you can rightfully disagree, but it won't 
> change the fact that it is still less useful for me (and possibly 
> others).

You did of course read Jonas' statement, that if you call
    Convertn(TConvertType(-1))

then Convert1 and Convert2 may crash, depending on compiler version and 
optimization level (this may also depend on how many elements 
TConvertType has)

Btw, from Jonas statement follows, that the "else" part in Convert1 may 
be silently removed by the compiler. (maybe now or maybe by future fpc 
versions). This may then give an "unreachable code" warning)

The warning however is only useful for Convert2, if you speculate that 
the "else" in Convert1 is used for TConvertType(-1).
If it isn't then you have a bigger problem, and that is not caught by 
this warning anyway.

>
> type
>   TConvertType = (ctA, ctB);
>
> function Convert1(Value: TConvertType): Integer;
> begin
>   case Value of
>     ctA: Result := 1;
>     ctB: Result := 2;
>     else Result := 0;
>   end;
> end;
>
> function Convert2(Value: TConvertType): Integer;
> begin
>   case Value of
>     ctA: Result := 1;
>     ctB: Result := 2;
>   end;
> end;
>
> function Convert3(Value: TConvertType): Integer;
> begin
>   case Value of
>     ctA: Result := 1;
>   end;
> end;




More information about the fpc-devel mailing list