[Ada] Remove unnecessary use of exceptions

Arnaud Charlet charlet@adacore.com
Tue Nov 15 14:44:00 GMT 2005


Tested on i686-linux, comimtted on trunk

Error handling in packages Switch.C, Switch.B and Switch.M is no
longer using exceptions raised and caught.

2005-11-14  Vincent Celier  <celier@adacore.com>

	* switch.adb (Bad_Switch): New procedure
	(Scan_Nat, Scan_Pos): Directly call Osint.Fail with the appropriate
	message when in error.

	* switch.ads (Bad_Switch, Bad_Switch_Value, Missing_Switch_Value,
	Too_Many_Output_Files): Remove declarations, no longer used.
	(Scan_Nat): New parameter Switch
	(Scan_Pos): Ditto
	(Bad_Switch): New procedure

	* switch-b.adb (Scan_Binder_Switches): Replace "raise Bad_Switch;"
	with call to new procedure Bad_Switch. Call Scan_Pos and Scan_Natwith
	new parameter Switch. Replace "raise Too_Many_Output_Files;" with call
	to Osint.Fail. Do not handle any exception.

-------------- next part --------------
Index: switch.adb
===================================================================
--- switch.adb	(revision 106884)
+++ switch.adb	(working copy)
@@ -6,7 +6,7 @@
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---          Copyright (C) 1992-2004, Free Software Foundation, Inc.         --
+--          Copyright (C) 1992-2005, Free Software Foundation, Inc.         --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -24,8 +24,19 @@
 --                                                                          --
 ------------------------------------------------------------------------------
 
+with Osint;
+
 package body Switch is
 
+   ----------------
+   -- Bad_Switch --
+   ----------------
+
+   procedure Bad_Switch (Switch : Character) is
+   begin
+      Osint.Fail ("invalid switch: ", (1 => Switch));
+   end Bad_Switch;
+
    -------------------------
    -- Is_Front_End_Switch --
    -------------------------
@@ -61,24 +72,27 @@
      (Switch_Chars : String;
       Max          : Integer;
       Ptr          : in out Integer;
-      Result       : out Nat)
+      Result       : out Nat;
+      Switch       : Character)
    is
    begin
       Result := 0;
 
       if Ptr > Max or else Switch_Chars (Ptr) not in '0' .. '9' then
-         raise Missing_Switch_Value;
-      end if;
+         Osint.Fail ("missing numeric value for switch: ", (1 => Switch));
 
-      while Ptr <= Max and then Switch_Chars (Ptr) in '0' .. '9' loop
-         Result := Result * 10 +
-           Character'Pos (Switch_Chars (Ptr)) - Character'Pos ('0');
-         Ptr := Ptr + 1;
+      else
+         while Ptr <= Max and then Switch_Chars (Ptr) in '0' .. '9' loop
+            Result := Result * 10 +
+              Character'Pos (Switch_Chars (Ptr)) - Character'Pos ('0');
+            Ptr := Ptr + 1;
 
-         if Result > Switch_Max_Value then
-            raise Bad_Switch_Value;
-         end if;
-      end loop;
+            if Result > Switch_Max_Value then
+               Osint.Fail
+                 ("numeric value out of range for switch: ", (1 => Switch));
+            end if;
+         end loop;
+      end if;
    end Scan_Nat;
 
    --------------
@@ -89,15 +103,16 @@
      (Switch_Chars : String;
       Max          : Integer;
       Ptr          : in out Integer;
-      Result       : out Pos)
+      Result       : out Pos;
+      Switch       : Character)
    is
       Temp : Nat;
 
    begin
-      Scan_Nat (Switch_Chars, Max, Ptr, Temp);
+      Scan_Nat (Switch_Chars, Max, Ptr, Temp, Switch);
 
       if Temp = 0 then
-         raise Bad_Switch_Value;
+         Osint.Fail ("numeric value out of range for switch: ", (1 => Switch));
       end if;
 
       Result := Temp;
Index: switch.ads
===================================================================
--- switch.ads	(revision 106884)
+++ switch.ads	(working copy)
@@ -6,7 +6,7 @@
 --                                                                          --
 --                                 S p e c                                  --
 --                                                                          --
---          Copyright (C) 1992-2003 Free Software Foundation, Inc.          --
+--          Copyright (C) 1992-2005 Free Software Foundation, Inc.          --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -62,18 +62,6 @@
    --  child packages (there is one such child package for each tool that
    --  uses Switches to scan switches - Compiler/gnatbind/gnatmake/.
 
-   Bad_Switch : exception;
-   --  Exception raised if bad switch encountered
-
-   Bad_Switch_Value : exception;
-   --  Exception raised if bad switch value encountered
-
-   Missing_Switch_Value : exception;
-   --  Exception raised if no switch value encountered
-
-   Too_Many_Output_Files : exception;
-   --  Exception raised if the -o switch is encountered more than once
-
    Switch_Max_Value : constant := 999_999;
    --  Maximum value permitted in switches that take a value
 
@@ -81,7 +69,8 @@
      (Switch_Chars : String;
       Max          : Integer;
       Ptr          : in out Integer;
-      Result       : out Nat);
+      Result       : out Nat;
+      Switch       : Character);
    --  Scan natural integer parameter for switch. On entry, Ptr points
    --  just past the switch character, on exit it points past the last
    --  digit of the integer value.
@@ -90,9 +79,13 @@
      (Switch_Chars : String;
       Max          : Integer;
       Ptr          : in out Integer;
-      Result       : out Pos);
+      Result       : out Pos;
+      Switch       : Character);
    --  Scan positive integer parameter for switch. On entry, Ptr points
    --  just past the switch character, on exit it points past the last
    --  digit of the integer value.
 
+   procedure Bad_Switch (Switch : Character);
+   --  Fail with an appropriate message when a switch is not recognized
+
 end Switch;
Index: switch-b.adb
===================================================================
--- switch-b.adb	(revision 106884)
+++ switch-b.adb	(working copy)
@@ -6,7 +6,7 @@
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---          Copyright (C) 2001-2005 Free Software Foundation, Inc.          --
+--          Copyright (C) 2001-2005, Free Software Foundation, Inc.         --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -45,7 +45,7 @@
       --  Skip past the initial character (must be the switch character)
 
       if Ptr = Max then
-         raise Bad_Switch;
+         Bad_Switch (C);
       else
          Ptr := Ptr + 1;
       end if;
@@ -122,7 +122,7 @@
                then
                   Set_Debug_Flag (C);
                else
-                  raise Bad_Switch;
+                  Bad_Switch (C);
                end if;
             end loop;
 
@@ -132,7 +132,8 @@
 
          when 'D' =>
             Ptr := Ptr + 1;
-            Scan_Pos (Switch_Chars, Max, Ptr, Default_Sec_Stack_Size);
+            Scan_Pos
+              (Switch_Chars, Max, Ptr, Default_Sec_Stack_Size, C);
 
          --  Processing for e switch
 
@@ -181,7 +182,7 @@
 
          when 'i' =>
             if Ptr = Max then
-               raise Bad_Switch;
+               Bad_Switch (C);
             end if;
 
             Ptr := Ptr + 1;
@@ -197,7 +198,7 @@
                Identifier_Character_Set := C;
                Ptr := Ptr + 1;
             else
-               raise Bad_Switch;
+               Bad_Switch (C);
             end if;
 
          --  Processing for K switch
@@ -216,7 +217,7 @@
 
          when 'm' =>
             Ptr := Ptr + 1;
-            Scan_Pos (Switch_Chars, Max, Ptr, Maximum_Errors);
+            Scan_Pos (Switch_Chars, Max, Ptr, Maximum_Errors, C);
 
          --  Processing for n switch
 
@@ -234,7 +235,7 @@
             Ptr := Ptr + 1;
 
             if Output_File_Name_Present then
-               raise Too_Many_Output_Files;
+               Osint.Fail ("duplicate -o switch");
 
             else
                Output_File_Name_Present := True;
@@ -282,7 +283,7 @@
          when 'T' =>
             Ptr := Ptr + 1;
             Time_Slice_Set := True;
-            Scan_Nat (Switch_Chars, Max, Ptr, Time_Slice_Value);
+            Scan_Nat (Switch_Chars, Max, Ptr, Time_Slice_Value, C);
             Time_Slice_Value := Time_Slice_Value * 1_000;
 
          --  Processing for v switch
@@ -308,7 +309,7 @@
                   Warning_Mode  := Suppress;
 
                when others =>
-                  raise Bad_Switch;
+                  Bad_Switch (C);
             end case;
 
             Ptr := Ptr + 1;
@@ -324,7 +325,7 @@
                   exit;
 
                elsif J = WC_Encoding_Method'Last then
-                  raise Bad_Switch;
+                  Bad_Switch (C);
                end if;
             end loop;
 
@@ -345,7 +346,7 @@
 
          when 'X' =>
             Ptr := Ptr + 1;
-            Scan_Pos (Switch_Chars, Max, Ptr, Default_Exit_Status);
+            Scan_Pos (Switch_Chars, Max, Ptr, Default_Exit_Status, C);
 
          --  Processing for z switch
 
@@ -402,7 +403,7 @@
                         RTS_Src_Path_Name := Src_Path_Name;
                         RTS_Lib_Path_Name := Lib_Path_Name;
 
-                        --  We can exit as there can not be another switch
+                        --  We can exit as there cannot be another switch
                         --  after --RTS
 
                         exit;
@@ -429,22 +430,9 @@
          --  Anything else is an error (illegal switch character)
 
          when others =>
-            raise Bad_Switch;
+            Bad_Switch (C);
          end case;
       end loop;
-
-   exception
-      when Bad_Switch =>
-         Osint.Fail ("invalid switch: ", (1 => C));
-
-      when Bad_Switch_Value =>
-         Osint.Fail ("numeric value out of range for switch: ", (1 => C));
-
-      when Missing_Switch_Value =>
-         Osint.Fail ("missing numeric value for switch: ", (1 => C));
-
-      when Too_Many_Output_Files =>
-         Osint.Fail ("duplicate -o switch");
    end Scan_Binder_Switches;
 
 end Switch.B;


More information about the Gcc-patches mailing list