[PATCH v2 1/4] ada: Generate warning on useless 'Unchecked_Access with -gnatw.u

Samuel Tardieu sam@rfc1149.net
Wed Nov 14 12:04:00 GMT 2007


New version of the -gnatw.u warning with more RTS files cleaned up,
usage added and enabling of this warning in -gnatg.

    gcc/ada/
	* opt.ads: New warning Warn_On_Useless_Unchecked_Access.

	* sem_attr.adb (Resolve_Attribute): Check for unnecessary use
	of 'Unchecked_Access when 'Access could safely be used. If
	Warn_On_Useless_Unchecked_Access is True, post a warning.

	* sem_warn.adb (Set_Dot_Warning_Switch): Activate/deactivate
	Warn_On_Useless_Unchecked_Access on -gnatw.u/-gnatw.U.
	(Set_Warning_Switch): Activate/deactivate this warning
	on -gnatwa/-gnatwA.

	* switch-c.adb (Scan_Front_End_Switches): Activate
	Warn_On_Useless_Unchecked_Access on -gnatg.

	* usage.adb (Usage): Document -gnatw.u and -gnatw.U.
---
 gcc/ada/opt.ads      |    6 ++++++
 gcc/ada/sem_attr.adb |   22 ++++++++++++++++++++++
 gcc/ada/sem_warn.adb |    8 ++++++++
 gcc/ada/switch-c.adb |   37 +++++++++++++++++++------------------
 gcc/ada/usage.adb    |    4 ++++
 5 files changed, 59 insertions(+), 18 deletions(-)

diff --git a/gcc/ada/opt.ads b/gcc/ada/opt.ads
index 00a9cef..8e118f3 100644
--- a/gcc/ada/opt.ads
+++ b/gcc/ada/opt.ads
@@ -1258,6 +1258,12 @@ package Opt is
    --  which have a record representation clause but this component does not
    --  have a component clause. The default is that this warning is disabled.
 
+   Warn_On_Useless_Unchecked_Access : Boolean := False;
+   --  GNAT
+   --  Set to True to generate warnings on use of X'Unchecked_Access where
+   --  X'Access would have been legitimate. The default is that this warning
+   --  is disabled.
+
    type Warning_Mode_Type is (Suppress, Normal, Treat_As_Error);
    Warning_Mode : Warning_Mode_Type := Normal;
    --  GNAT, GNATBIND
diff --git a/gcc/ada/sem_attr.adb b/gcc/ada/sem_attr.adb
index ce66987..0056024 100644
--- a/gcc/ada/sem_attr.adb
+++ b/gcc/ada/sem_attr.adb
@@ -7673,6 +7673,28 @@ package body Sem_Attr is
                   Accessibility_Message;
                   return;
                end if;
+
+               --  Check whether Access could have been used instead of
+               --  Unchecked_Access. The check is disabled:
+               --    - when compiling a generic because the
+               --      accessibility level may not be known if, for
+               --      example, the instantiation happens in a local
+               --      declaration;
+               --    - when compiling an instance, as Access could be
+               --      permitted only in this particular case and
+               --      forbidden in others.
+
+               if Attr_Id = Attribute_Unchecked_Access
+                 and then Warn_On_Useless_Unchecked_Access
+                 and then not Inside_A_Generic
+                 and then not In_Instance
+                 and then Comes_From_Source (P)
+                 and then (Object_Access_Level (P) <= Type_Access_Level (Btyp)
+                   or else Ekind (Btyp) = E_Anonymous_Access_Type)
+               then
+                  Error_Msg_F
+                    ("?''Access attribute could be used here", P);
+               end if;
             end if;
 
             if Ekind (Btyp) = E_Access_Protected_Subprogram_Type
diff --git a/gcc/ada/sem_warn.adb b/gcc/ada/sem_warn.adb
index 65ea957..53dd04d 100644
--- a/gcc/ada/sem_warn.adb
+++ b/gcc/ada/sem_warn.adb
@@ -2542,6 +2542,12 @@ package body Sem_Warn is
          when 'R' =>
             Warn_On_Object_Renames_Function     := False;
 
+         when 'u' =>
+            Warn_On_Useless_Unchecked_Access    := True;
+
+         when 'U' =>
+            Warn_On_Useless_Unchecked_Access    := False;
+
          when 'x' =>
             Warn_On_Non_Local_Exception         := True;
 
@@ -2584,6 +2590,7 @@ package body Sem_Warn is
             Warn_On_Unchecked_Conversion        := True;
             Warn_On_Unrecognized_Pragma         := True;
             Warn_On_Unrepped_Components         := True;
+            Warn_On_Useless_Unchecked_Access    := True;
 
          when 'A' =>
             Check_Unreferenced                  := False;
@@ -2611,6 +2618,7 @@ package body Sem_Warn is
             Warn_On_Unchecked_Conversion        := False;
             Warn_On_Unrecognized_Pragma         := False;
             Warn_On_Unrepped_Components         := False;
+            Warn_On_Useless_Unchecked_Access    := False;
 
          when 'b' =>
             Warn_On_Bad_Fixed_Value             := True;
diff --git a/gcc/ada/switch-c.adb b/gcc/ada/switch-c.adb
index 76c47f2..50a100a 100644
--- a/gcc/ada/switch-c.adb
+++ b/gcc/ada/switch-c.adb
@@ -473,24 +473,25 @@ package body Switch.C is
 
                --  Set default warnings for -gnatg
 
-               Check_Unreferenced              := True;
-               Check_Unreferenced_Formals      := True;
-               Check_Withs                     := True;
-               Constant_Condition_Warnings     := True;
-               Implementation_Unit_Warnings    := True;
-               Ineffective_Inline_Warnings     := True;
-               Warn_On_Assumed_Low_Bound       := True;
-               Warn_On_Bad_Fixed_Value         := True;
-               Warn_On_Constant                := True;
-               Warn_On_Export_Import           := True;
-               Warn_On_Modified_Unread         := True;
-               Warn_On_No_Value_Assigned       := True;
-               Warn_On_Non_Local_Exception     := False;
-               Warn_On_Obsolescent_Feature     := True;
-               Warn_On_Redundant_Constructs    := True;
-               Warn_On_Object_Renames_Function := True;
-               Warn_On_Unchecked_Conversion    := True;
-               Warn_On_Unrecognized_Pragma     := True;
+               Check_Unreferenced               := True;
+               Check_Unreferenced_Formals       := True;
+               Check_Withs                      := True;
+               Constant_Condition_Warnings      := True;
+               Implementation_Unit_Warnings     := True;
+               Ineffective_Inline_Warnings      := True;
+               Warn_On_Assumed_Low_Bound        := True;
+               Warn_On_Bad_Fixed_Value          := True;
+               Warn_On_Constant                 := True;
+               Warn_On_Export_Import            := True;
+               Warn_On_Modified_Unread          := True;
+               Warn_On_No_Value_Assigned        := True;
+               Warn_On_Non_Local_Exception      := False;
+               Warn_On_Obsolescent_Feature      := True;
+               Warn_On_Redundant_Constructs     := True;
+               Warn_On_Object_Renames_Function  := True;
+               Warn_On_Unchecked_Conversion     := True;
+               Warn_On_Unrecognized_Pragma      := True;
+               Warn_On_Useless_Unchecked_Access := True;
 
                Set_GNAT_Style_Check_Options;
 
diff --git a/gcc/ada/usage.adb b/gcc/ada/usage.adb
index ae5ee42..9501758 100644
--- a/gcc/ada/usage.adb
+++ b/gcc/ada/usage.adb
@@ -421,6 +421,10 @@ begin
    Write_Line ("        T*   turn off warnings for tracking deleted code");
    Write_Line ("        u    turn on warnings for unused entity");
    Write_Line ("        U*   turn off warnings for unused entity");
+   Write_Line ("        .u   turn on warnings for unnecessary " &
+                                                  "unchecked access");
+   Write_Line ("        .U   turn off warnings for unnecessary " &
+                                                  "unchecked access");
    Write_Line ("        v*   turn on warnings for unassigned variable");
    Write_Line ("        V    turn off warnings for unassigned variable");
    Write_Line ("        w*   turn on warnings for wrong low bound assumption");
-- 
1.5.3.5



More information about the Gcc-patches mailing list