diff mbox

[Ada] Add warning for overridden sizes

Message ID 20100910155325.GA3154@adacore.com
State New
Headers show

Commit Message

Arnaud Charlet Sept. 10, 2010, 3:53 p.m. UTC
This patch adds a warning when a record or array component size
specification overrides an explicit size clause for the component
type. The warning is activated by -gnatw.s, which is used to
compile the following test (with -gnatld7)

     1. package sizeoverride is
     2.     type M is range 0 .. 10;
     3.     for M'Size use 32;
     4.
     5.     type a1 is array (0 .. 10) of M;
     6.     for a1'Component_Size use 32; -- OK
     7.
     8.     type a2 is array (0 .. 10) of M;
     9.     for a2'Component_Size use 16; -- WARN
            |
        >>> warning: component size overrides size clause for "M"

    10.
    11.     type a3 is array (0 .. 10) of M;
    12.     for a3'Component_Size use 64; -- WARN
            |
        >>> warning: component size overrides size clause for "M"

    13.
    14.     type r is record
    15.        a, b, c : M;
    16.     end record;
    17.
    18.     for r use record
    19.        a at 0 range 0 .. 31; -- OK
    20.        b at 4 range 0 .. 63; -- WARN
               |
        >>> warning: component size overrides size clause for "M"

    21.        c at 12 range 0 .. 7; -- WARN
               |
        >>> warning: component size overrides size clause for "M"

    22.     end record;
    23. end sizeoverride;

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

2010-09-10  Robert Dewar  <dewar@adacore.com>

	* gnat_ugn.texi: Add documentation for -gnatw.s/S
	* sem_ch13.adb (Analyze_Attribute_Definition_Clause, case
	Component_Size): Implement warning on overriden size clause.
	(Analyze_Record_Representation_Clause): Implement warning on overriden
	size clause.
	* sem_warn.ads, sem_warn.adb (Warn_On_Overridden_Size): New flag
	(-gnatw.s/S).
	* ug_words: Add entries for -gnatw.s/S.
	* vms_data.ads, usage.adb: Add line for -gnatw.s/-gnatw.S.
diff mbox

Patch

Index: usage.adb
===================================================================
--- usage.adb	(revision 164186)
+++ usage.adb	(working copy)
@@ -468,6 +468,8 @@ 
    Write_Line ("        .r+  turn on warnings for object renaming function");
    Write_Line ("        .R*  turn off warnings for object renaming function");
    Write_Line ("        s    suppress all info/warnings");
+   Write_Line ("        .s   turn on warnings for overridden size clause");
+   Write_Line ("        .S*  turn off warnings for overridden size clause");
    Write_Line ("        t    turn on warnings for tracking deleted code");
    Write_Line ("        T*   turn off warnings for tracking deleted code");
    Write_Line ("        u+   turn on warnings for unused entity");
Index: ug_words
===================================================================
--- ug_words	(revision 164186)
+++ ug_words	(working copy)
@@ -161,6 +161,8 @@ 
 -gnatwP         ^ /WARNINGS=NOINEFFECTIVE_INLINE
 -gnatw.p        ^ /WARNINGS=PARAMETER_ORDER
 -gnatw.P        ^ /WARNINGS=NO_PARAMETER_ORDER
+-gnatw.h        ^ /WARNINGS=OVERRIDING_SIZE
+-gnatw.H        ^ /WARNINGS=NOOVERRIDING_SIZE
 -gnatwq         ^ /WARNINGS=MISSING_PARENS
 -gnatwQ         ^ /WARNINGS=NOMISSING_PARENS
 -gnatwr         ^ /WARNINGS=REDUNDANT
Index: gnat_ugn.texi
===================================================================
--- gnat_ugn.texi	(revision 164186)
+++ gnat_ugn.texi	(working copy)
@@ -5582,6 +5582,25 @@ 
 in addition to @option{-gnatws}. Also this switch has no effect on the
 handling of style check messages.
 
+@item -gnatw.s
+@emph{Activate warnings on overridden size clauses.}
+@cindex @option{-gnatw.s} (@command{gcc})
+@cindex Record Representation (component sizes)
+This switch activates warnings on component clauses in record
+representation clauses where the length given overrides that
+specified by an explicit size clause for the component type. A
+warning is similarly given in the array case if a specified
+component size overrides an explicit size clause for the array
+component type.
+Note that @option{-gnatwa} does not affect the setting of this warning option.
+
+@item -gnatw.S
+@emph{Suppress warnings on overriddebn size clauses.}
+@cindex @option{-gnatw.S} (@command{gcc})
+This switch suppresses warnings on component clauses in record
+representation clauses that override size clauses, and similar
+warnings when an array component size overrides a size clause.
+
 @item -gnatwt
 @emph{Activate warnings for tracking of deleted conditional code.}
 @cindex @option{-gnatwt} (@command{gcc})
Index: vms_data.ads
===================================================================
--- vms_data.ads	(revision 164186)
+++ vms_data.ads	(working copy)
@@ -3011,6 +3011,10 @@ 
                                                "-gnatw.R "                 &
                                             "SUPPRESS "                    &
                                                "-gnatws "                  &
+                                            "OVERRIDING_SIZE "             &
+                                               "-gnatw.s "                 &
+                                            "NOOVERRIDING_SIZE "           &
+                                               "-gnatw.S "                 &
                                             "DELETED_CODE "                &
                                                "-gnatwt "                  &
                                             "NODELETED_CODE "              &
Index: sem_warn.adb
===================================================================
--- sem_warn.adb	(revision 164186)
+++ sem_warn.adb	(working copy)
@@ -3085,6 +3085,7 @@ 
             Warn_On_Object_Renames_Function     := True;
             Warn_On_Obsolescent_Feature         := True;
             Warn_On_Overlap                     := True;
+            Warn_On_Overridden_Size             := True;
             Warn_On_Parameter_Order             := True;
             Warn_On_Questionable_Missing_Parens := True;
             Warn_On_Record_Holes                := True;
@@ -3135,6 +3136,12 @@ 
          when 'R' =>
             Warn_On_Object_Renames_Function     := False;
 
+         when 's' =>
+            Warn_On_Overridden_Size             := True;
+
+         when 'S' =>
+            Warn_On_Overridden_Size             := False;
+
          when 'u' =>
             Warn_On_Unordered_Enumeration_Type  := True;
 
@@ -3268,6 +3275,7 @@ 
             Warn_On_Object_Renames_Function     := False;
             Warn_On_Obsolescent_Feature         := False;
             Warn_On_Overlap                     := False;
+            Warn_On_Overridden_Size             := False;
             Warn_On_Parameter_Order             := False;
             Warn_On_Record_Holes                := False;
             Warn_On_Questionable_Missing_Parens := False;
Index: sem_warn.ads
===================================================================
--- sem_warn.ads	(revision 164186)
+++ sem_warn.ads	(working copy)
@@ -47,6 +47,12 @@ 
    --  Warn when explicit record component clauses leave uncovered holes (gaps)
    --  in a record layout. Off by default, set by -gnatw.h (but not -gnatwa).
 
+   Warn_On_Overridden_Size : Boolean := False;
+   --  Warn when explicit record component clause or array component_size
+   --  clause specifies a size that overrides a size for the typen which was
+   --  set with an explicit size clause. Off by default, set by -gnatw.sn (but
+   --  not -gnatwa).
+
    ------------------------
    -- Warnings Off Table --
    ------------------------
Index: sem_ch13.adb
===================================================================
--- sem_ch13.adb	(revision 164186)
+++ sem_ch13.adb	(working copy)
@@ -1283,6 +1283,7 @@ 
 
          when Attribute_Component_Size => Component_Size_Case : declare
             Csize    : constant Uint := Static_Integer (Expr);
+            Ctyp     : Entity_Id;
             Btype    : Entity_Id;
             Biased   : Boolean;
             New_Ctyp : Entity_Id;
@@ -1295,13 +1296,14 @@ 
             end if;
 
             Btype := Base_Type (U_Ent);
+            Ctyp := Component_Type (Btype);
 
             if Has_Component_Size_Clause (Btype) then
                Error_Msg_N
                  ("component size clause for& previously given", Nam);
 
             elsif Csize /= No_Uint then
-               Check_Size (Expr, Component_Type (Btype), Csize, Biased);
+               Check_Size (Expr, Ctyp, Csize, Biased);
 
                if Has_Aliased_Components (Btype)
                  and then Csize < 32
@@ -1367,6 +1369,17 @@ 
                   end if;
                end if;
 
+               --  Deal with warning on overridden size
+
+               if Warn_On_Overridden_Size
+                 and then Has_Size_Clause (Ctyp)
+                 and then RM_Size (Ctyp) /= Csize
+               then
+                  Error_Msg_NE
+                    ("?component size overrides size clause for&",
+                     N, Ctyp);
+               end if;
+
                Set_Has_Component_Size_Clause (Btype, True);
                Set_Has_Non_Standard_Rep      (Btype, True);
             end if;
@@ -2749,6 +2762,15 @@ 
                         Set_Normalized_First_Bit (Comp, Fbit mod SSU);
                         Set_Normalized_Position  (Comp, Fbit / SSU);
 
+                        if Warn_On_Overridden_Size
+                          and then Has_Size_Clause (Etype (Comp))
+                          and then RM_Size (Etype (Comp)) /= Esize (Comp)
+                        then
+                           Error_Msg_NE
+                             ("?component size overrides size clause for&",
+                              Component_Name (CC), Etype (Comp));
+                        end if;
+
                         --  This information is also set in the corresponding
                         --  component of the base type, found by accessing the
                         --  Original_Record_Component link if it is present.