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]

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


This serie of three patches adds a new GNAT warning "-gnatw.u". This
warning, which is also turned on by "-gnata", warns the user when
'Unchecked_Access is used and the accessibility checks show that
'Access could have been used instead. This helped find three such
cases in GNAT's own runtime.

The first patch implements the warning, the second one adds the
corresponding documentation and the third one changes three files
in GNAT runtime to use 'Access instead of 'Unchecked_Access.

This idea comes from reading the code in GNAT.Sockets where I noticed
some unnecessary 'Unchecked_Access.

2007-11-05  Samuel Tardieu  <sam@rfc1149.net>

	* 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.
---
 gcc/ada/opt.ads      |    6 ++++++
 gcc/ada/sem_attr.adb |   10 ++++++++++
 gcc/ada/sem_warn.adb |    8 ++++++++
 3 files changed, 24 insertions(+), 0 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 6c3e3dc..88991c8 100644
--- a/gcc/ada/sem_attr.adb
+++ b/gcc/ada/sem_attr.adb
@@ -7674,6 +7674,16 @@ package body Sem_Attr is
                   Accessibility_Message;
                   return;
                end if;
+
+               if Attr_Id = Attribute_Unchecked_Access
+                 and then Warn_On_Useless_Unchecked_Access
+                 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;
-- 
1.5.3.5


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