[Ada] Overflow checking is now on by default

Arnaud Charlet charlet@adacore.com
Thu Jul 31 09:51:00 GMT 2014


All previous versions of GNAT have set overflow checking off by
default (with -gnato switches to enable overflow checking). This
update sets the default to checking on, and implements a new
switch -gnato0 to turn overflow checking on. The old switch
-gnato, which used to enable overflow checking can still be
used but simply sets the default, so normally has no effect.

The following program:

     1. procedure OvTest is
     2.    A : Integer;
     3.    function Ident (X : Integer) return Integer is
     4.    begin
     5.       return X;
     6.    end;
     7. begin
     8.    A := Ident (Integer'Last) + 1;
     9. end;

raises an exception:

   raised CONSTRAINT_ERROR : ovtest.adb:8 overflow check failed

if compiled with no options or with -gnato, and runs silently
if compiled with -gnato0.

Tested on x86_64-pc-linux-gnu, committed on trunk

2014-07-31  Robert Dewar  <dewar@adacore.com>

	* gnat1drv.adb (Adjust_Global_Switches): Default for overflow
	checking is enabled except in GNAT_Mode.
	* switch-c.adb (Scan_Front_End_Switches): Implement -gnato0
	(suppress overflow checks).

-------------- next part --------------
Index: switch-c.adb
===================================================================
--- switch-c.adb	(revision 213263)
+++ switch-c.adb	(working copy)
@@ -953,38 +953,57 @@
 
             when 'o' =>
                Ptr := Ptr + 1;
-               Suppress_Options.Suppress (Overflow_Check) := False;
 
-               --  Case of no digits after the -gnato
+               --  Case of -gnato0 (overflow checking turned off)
 
-               if Ptr > Max or else Switch_Chars (Ptr) not in '1' .. '3' then
+               if Ptr <= Max and then Switch_Chars (Ptr) = '0' then
+                  Ptr := Ptr + 1;
+                  Suppress_Options.Suppress (Overflow_Check) := True;
+
+                  --  We set strict mode in case overflow checking is turned
+                  --  on locally (also records that we had a -gnato switch).
+
                   Suppress_Options.Overflow_Mode_General    := Strict;
                   Suppress_Options.Overflow_Mode_Assertions := Strict;
 
-               --  At least one digit after the -gnato
+               --  All cases other than -gnato0 (overflow checking turned on)
 
                else
-                  --  Handle first digit after -gnato
+                  Suppress_Options.Suppress (Overflow_Check) := False;
 
-                  Suppress_Options.Overflow_Mode_General :=
-                    Get_Overflow_Mode (Switch_Chars (Ptr));
-                  Ptr := Ptr + 1;
+                  --  Case of no digits after the -gnato
 
-                  --  Only one digit after -gnato, set assertions mode to
-                  --  be the same as general mode.
-
                   if Ptr > Max
                     or else Switch_Chars (Ptr) not in '1' .. '3'
                   then
-                     Suppress_Options.Overflow_Mode_Assertions :=
-                       Suppress_Options.Overflow_Mode_General;
+                     Suppress_Options.Overflow_Mode_General    := Strict;
+                     Suppress_Options.Overflow_Mode_Assertions := Strict;
 
-                  --  Process second digit after -gnato
+                  --  At least one digit after the -gnato
 
                   else
-                     Suppress_Options.Overflow_Mode_Assertions :=
+                     --  Handle first digit after -gnato
+
+                     Suppress_Options.Overflow_Mode_General :=
                        Get_Overflow_Mode (Switch_Chars (Ptr));
                      Ptr := Ptr + 1;
+
+                     --  Only one digit after -gnato, set assertions mode to be
+                     --  the same as general mode.
+
+                     if Ptr > Max
+                       or else Switch_Chars (Ptr) not in '1' .. '3'
+                     then
+                        Suppress_Options.Overflow_Mode_Assertions :=
+                          Suppress_Options.Overflow_Mode_General;
+
+                     --  Process second digit after -gnato
+
+                     else
+                        Suppress_Options.Overflow_Mode_Assertions :=
+                          Get_Overflow_Mode (Switch_Chars (Ptr));
+                        Ptr := Ptr + 1;
+                     end if;
                   end if;
                end if;
 
@@ -1026,6 +1045,13 @@
 
                   Validity_Checks_On  := False;
                   Opt.Suppress_Checks := True;
+
+                  --  Set overflow mode checking to strict in case it gets
+                  --  turned on locally (also signals that overflow checking
+                  --  has been specifically turned off).
+
+                  Suppress_Options.Overflow_Mode_General    := Strict;
+                  Suppress_Options.Overflow_Mode_Assertions := Strict;
                end if;
 
             --  -gnatP (periodic poll)
Index: gnat1drv.adb
===================================================================
--- gnat1drv.adb	(revision 213263)
+++ gnat1drv.adb	(working copy)
@@ -511,9 +511,13 @@
       --  Otherwise set overflow mode defaults
 
       else
-         --  Otherwise set overflow checks off by default
+         --  Overflow checks are on by default (Suppress set False) except in
+         --  GNAT_Mode, where we want them off by default (we are not ready to
+         --  enable overflow checks in the compiler yet, for one thing the case
+         --  of 64-bit checks needs System.Arith_64 which is not a compiler
+         --  unit and it is a pain to try to include it in the compiler.
 
-         Suppress_Options.Suppress (Overflow_Check) := True;
+         Suppress_Options.Suppress (Overflow_Check) := GNAT_Mode;
 
          --  Set appropriate default overflow handling mode. Note: at present
          --  we set STRICT in all three of the following cases. They are
@@ -531,8 +535,8 @@
          --  flags set, so this was dead code anyway.
 
          elsif Targparm.Backend_Divide_Checks_On_Target
-           and
-             Targparm.Backend_Overflow_Checks_On_Target
+                 and
+               Targparm.Backend_Overflow_Checks_On_Target
          then
             Suppress_Options.Overflow_Mode_General    := Strict;
             Suppress_Options.Overflow_Mode_Assertions := Strict;


More information about the Gcc-patches mailing list