Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ada suppress unreachable code or missing return

I have a tagged type that implements a number of functions. In one case I need one of these functions to instead enter an infinite loop. Unfortunately as far as I can tell there is no way for me to compile this such that it doesn't raise a warning. I wish to still use -gnatwe to ensure there are no warnings in my code, but how can I implement such a function.

Here is what the function looks like:

function Foo (This : Some_Type) return Some_Type'Class is
begin
    loop
        Do_Useful_Stuff_Indefinitely_With (This);
    end loop;

    -- return This; (if needed?)
end Foo;

I have tried pragma (No_Return) except that is only applicable for procedures (and the Foo function is used as a proper function elsewhere so must have the same signature).

I also tried pragma Suppress (All_Checks) but that still raised a warning for unreachable code or missing return statement error.

Is there any way whatsoever to have a once-off function that runs forever without raising a warning?

like image 765
LambdaBeta Avatar asked Feb 18 '19 20:02

LambdaBeta


2 Answers

Taking the same example that Jean-François provided, you can avoid the warning by declaring and calling a "private" procedure (you don't have to declare it in spec) wrapping your loop as in the following :

package body foo is

   procedure Infinite_Loop is
   begin
      loop
        null;
      end loop;
   end Infinite_Loop;

   function bar return integer is
   begin
      Infinite_Loop;

      return 12;
   end bar;
end foo;
like image 178
Frédéric Praca Avatar answered Oct 17 '22 05:10

Frédéric Praca


pragma Suppress (All_Checks) acts on run-time checks. Won't help you there. Leave that alone unless you focus on performance, but then you have -p option to do it using command line switches

The pragma Suppress suppresses compiler-generated run-time checks. If a run-time check is disabled, an exception may be suppressed and undefined behavior could result. pragma Suppress is used at the programmer's risk.

You need the return statement, but you can wrap it around 2 pragma warnings statements (A case where have you tried turning it off and on again? works)

pragma warnings(off,"unreachable code");
return This;
pragma warnings(on,"unreachable code");

note that the text is optional but enables to filter other warnings that could occur (if needed). It's better since turning off all warnings is generally bad practice.

Note that you have to turn warnings on again after the statement.

Self-contained demo. foo.adb looks like:

package body foo is
 function bar return integer is
 begin
    loop
        null;
    end loop;
    pragma warnings(off,"unreachable code");
    return 12;
    pragma warnings(on,"unreachable code");
 end bar;
end foo;

foo.ads looks like:

package foo is
 function bar return integer;
end foo;

If I comment out the pragma lines:

$ gcc -c -gnatwe foo.adb
foo.adb:8:05: warning: unreachable code

uncommenting them removes the warning.

like image 36
Jean-François Fabre Avatar answered Oct 17 '22 04:10

Jean-François Fabre