2.7 How to detect run time problems at compile time

Consider the following program:

MODULE assignvalue ;  (*!m2iso+gm2*)

PROCEDURE bad () : INTEGER ;
VAR
   i: INTEGER ;
BEGIN
   i := -1 ;
   RETURN i
END bad ;

VAR
   foo: CARDINAL ;
BEGIN
   (* The m2rte plugin will detect this as an error, post
      optimization.  *)
   foo := bad ()
END assignvalue.

here we see that the programmer has overlooked that the return value from ‘bad’ will cause an overflow to ‘foo’. If we compile the code with the following options:

$ gm2 -g -fsoft-check-all -O2 -c assignvalue.mod
assignvalue.mod:16:0:inevitable that this error will occur at run time,
assignment will result in an overflow

The gm2 semantic plugin is automatically run and will generate a warning message for every exception call which is known as reachable. It is highly advised to run the optimizer (‘-O2’ or ‘-O3’) with ‘-fsoft-check-all’ so that the compiler is able to run the optimizer and perform variable and flow analysis before the semantic plugin is invoked.