This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Ada] Implement new restriction No_Default_Initialization


Tested on i686-linux, committed on trunk

This patch implements a new restriction No_Default_Initialization which
prohibits any instance of default initialization of variables. The binder
implements a consistency rule which prevents any unit compiled without
the restriction from with'ing a unit with the restriction (this allows
init_proc generation to be skipped, since you can be sure that no call
is ever generated to an init_proc in a unit with the restriction active).
If used in conjunction with Initialize_Scalars or Normalize_Scalars, the
effect is to prohibit all cases of variables declared without a specific
initializer (including the case of OUT scalar parameters).

The following test shows the restriction in action:

Compiling: nodefinit.adb

     1. pragma Restrictions (No_Default_Initialization);
     2. procedure NoDefInit is
     3.    type R is access all integer;
     4.    R1 : R;
           |
        >>> violation of restriction "no_default_initialization" at line 1
        >>> warning: variable "R1" is never read and never assigned

     5.    R2 : R := new Integer'(3);
     6.
     7.    type V is array (1 .. 10) of R;
     8.    V1 : V;
           |
        >>> violation of restriction "no_default_initialization" at line 1

     9.    V2 : V := (others => R2);
    10.
    11.    type X is record
    12.       V : Integer := 3;
    13.    end record;
    14.
    15.    X1 : X;
           |
        >>> violation of restriction "no_default_initialization" at line 1

    16.    X2 : X := (V => 3);
    17.
    18.    type Y is record
    19.       V : Integer;
    20.    end record;
    21.
    22.    Y1 : Y;
           |
        >>> warning: variable "Y1" is never read and never assigned

    23.
    24.    I1 : Integer := 3;
    25.    I2 : Integer;
           |
        >>> warning: variable "I2" is never read and never assigned

    26.
    27.    procedure p (x : out integer) is
    28.    begin
    29.       null;
    30.    end;
    31. begin
    32.    null;
    33. end NoDefInit;

If this same test is compiled with a gnat.adc file that contains
pragma Initialize_Scalars, then additional diagnostics are output
for lines 25 and 27:

    25.    I2 : Integer;
           |
        >>> violation of restriction "no_default_initialization" at line 1
        >>> warning: variable "I2" is never read and never assigned

    26.
    27.    procedure p (x : out integer) is
                        |
        >>> violation of restriction "no_default_initialization" at line 1

The following test shows the suppression of init_procs:

pragma Restrictions (No_Default_Initialization);
package NoDefInit3 is
   type r is record
      x : Integer := 3;
   end record;
end;

The -gnatG output from compiling this package spec is:

pragma restrictions (no_default_initialization);
nodefinit3_E : boolean := false;

package nodefinit3 is
   type nodefinit3__r is record
      x : integer := 3;
   end record;
end nodefinit3;

showing that no init_proc is generated.

Finally this test shows the binder consistency check in action

package nodefinit6 is
end;

with nodefinit6;
procedure nodefinit7 is
   type r is access integer;
   rr : r;
begin
   null;
end;

If both units are compiled with a gnat.adc file containing the
restriction, then we get as expected at compile time:

nodefinit7.adb:4:04: violation of restriction
                     "no_default_initialization" at
                     gnat.adc:1

But if we compile nodefinit6.ads with this gnat.adc, and
then nodefinit7.adb without the restriction present, we have
an inconsistency that only gets detected at bind time:

error: Unit "nodefinit7 (body)" violates restriction
       No_Default_Initialization
error: and withs unit "nodefinit6 (spec)", compiled without
       this restriction

2008-04-08  Robert Dewar  <dewar@adacore.com>
	    Gary Dismukes  <dismukes@adacore.com>

	* s-rident.ads: Add No_Default_Initialization restriction

	* exp_tss.adb: 
	(Has_Non_Null_Base_Init_Proc): Handle No_Default_Initialization case
	(Set_TSS): Handle No_Default_Initialization case

	* exp_ch6.adb (Expand_N_Subprogram_Body): Handle restriction
	No_Default_Initialization
	(Expand_N_Subprogram_Body): Remove redundant initialization of out
	parameters when Normalize_Scalars is active.
	(Add_Final_List_Actual_To_Build_In_Place_Call): Add formal Sel_Comp
	Fix casing error in formal parameter name in call
	(Register_Predefined_DT_Entry): Replace occurrences of RE_Address by
	(Expand_Call, Propagate_Tag): Call Kill_Current_Values when processing a
	dispatching call on VM targets.

Attachment: difs
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]