OSDN Git Service

2008-05-27 Vincent Celier <celier@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / sem_warn.adb
index af50d9c..5fe9743 100644 (file)
@@ -6,26 +6,25 @@
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---          Copyright (C) 1999-2006, Free Software Foundation, Inc.         --
+--          Copyright (C) 1999-2008, 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- --
--- ware  Foundation;  either version 2,  or (at your option) any later ver- --
+-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
 -- for  more details.  You should have  received  a copy of the GNU General --
--- Public License  distributed with GNAT;  see file COPYING.  If not, write --
--- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
--- Boston, MA 02110-1301, USA.                                              --
+-- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
+-- http://www.gnu.org/licenses for a complete copy of the license.          --
 --                                                                          --
 -- GNAT was originally developed  by the GNAT team at  New York University. --
 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
 --                                                                          --
 ------------------------------------------------------------------------------
 
-with Alloc;
 with Atree;    use Atree;
+with Debug;    use Debug;
 with Einfo;    use Einfo;
 with Errout;   use Errout;
 with Exp_Code; use Exp_Code;
@@ -34,8 +33,10 @@ with Lib;      use Lib;
 with Namet;    use Namet;
 with Nlists;   use Nlists;
 with Opt;      use Opt;
+with Rtsfind;  use Rtsfind;
 with Sem;      use Sem;
 with Sem_Ch8;  use Sem_Ch8;
+with Sem_Aux;  use Sem_Aux;
 with Sem_Eval; use Sem_Eval;
 with Sem_Util; use Sem_Util;
 with Sinfo;    use Sinfo;
@@ -43,13 +44,13 @@ with Sinput;   use Sinput;
 with Snames;   use Snames;
 with Stand;    use Stand;
 with Stringt;  use Stringt;
-with Table;
 with Uintp;    use Uintp;
 
 package body Sem_Warn is
 
    --  The following table collects Id's of entities that are potentially
    --  unreferenced. See Check_Unset_Reference for further details.
+   --  ??? Check_Unset_Reference has zero information about this table.
 
    package Unreferenced_Entities is new Table.Table (
      Table_Component_Type => Entity_Id,
@@ -59,6 +60,57 @@ package body Sem_Warn is
      Table_Increment      => Alloc.Unreferenced_Entities_Increment,
      Table_Name           => "Unreferenced_Entities");
 
+   --  The following table collects potential warnings for IN OUT parameters
+   --  that are referenced but not modified. These warnings are processed when
+   --  the front end calls the procedure Output_Non_Modified_In_Out_Warnings.
+   --  The reason that we defer output of these messages is that we want to
+   --  detect the case where the relevant procedure is used as a generic actual
+   --  in an instantiation, since we suppress the warnings in this case. The
+   --  flag Used_As_Generic_Actual will be set in this case, but only at the
+   --  point of usage. Similarly, we suppress the message if the address of the
+   --  procedure is taken, where the flag Address_Taken may be set later.
+
+   package In_Out_Warnings is new Table.Table (
+     Table_Component_Type => Entity_Id,
+     Table_Index_Type     => Nat,
+     Table_Low_Bound      => 1,
+     Table_Initial        => Alloc.In_Out_Warnings_Initial,
+     Table_Increment      => Alloc.In_Out_Warnings_Increment,
+     Table_Name           => "In_Out_Warnings");
+
+   --------------------------------------------------------
+   -- Handling of Warnings Off, Unmodified, Unreferenced --
+   --------------------------------------------------------
+
+   --  The functions Has_Warnings_Off, Has_Unmodified, Has_Unreferenced must
+   --  generally be used instead of Warnings_Off, Has_Pragma_Unmodified and
+   --  Has_Pragma_Unreferenced, as noted in the specs in Einfo.
+
+   --  In order to avoid losing warnings in -gnatw.w (warn on unnecessary
+   --  warnings off pragma) mode, i.e. to avoid false negatives, the code
+   --  must follow some important rules.
+
+   --  Call these functions as late as possible, after completing all other
+   --  tests, just before the warnings is given. For example, don't write:
+
+   --     if not Has_Warnings_Off (E)
+   --       and then some-other-predicate-on-E then ..
+
+   --  Instead the following is preferred
+
+   --     if some-other-predicate-on-E
+   --       and then Has_Warnings_Off (E)
+
+   --  This way if some-other-predicate is false, we avoid a false indication
+   --  that a Warnings (Off,E) pragma was useful in preventing a warning.
+
+   --  The second rule is that if both Has_Unmodified and Has_Warnings_Off, or
+   --  Has_Unreferenced and Has_Warnings_Off are called, make sure that the
+   --  call to Has_Unmodified/Has_Unreferenced comes first, this way we record
+   --  that the Warnings (Off) could have been Unreferenced or Unmodified. In
+   --  fact Has_Unmodified/Has_Unreferenced includes a test for Warnings Off,
+   --  and so a subsequent test is not needed anyway (though it is harmless).
+
    -----------------------
    -- Local Subprograms --
    -----------------------
@@ -67,8 +119,30 @@ package body Sem_Warn is
    --  This returns true if the entity E is declared within a generic package.
    --  The point of this is to detect variables which are not assigned within
    --  the generic, but might be assigned outside the package for any given
-   --  instance. These are cases where we leave the warnings to be posted
-   --  for the instance, when we will know more.
+   --  instance. These are cases where we leave the warnings to be posted for
+   --  the instance, when we will know more.
+
+   function Goto_Spec_Entity (E : Entity_Id) return Entity_Id;
+   --  If E is a parameter entity for a subprogram body, then this function
+   --  returns the corresponding spec entity, if not, E is returned unchanged.
+
+   function Has_Pragma_Unmodified_Check_Spec (E : Entity_Id) return Boolean;
+   --  Tests Has_Pragma_Unmodified flag for entity E. If E is not a formal,
+   --  this is simply the setting of the flag Has_Pragma_Unmodified. If E is
+   --  a body formal, the setting of the flag in the corresponding spec is
+   --  also checked (and True returned if either flag is True).
+
+   function Has_Pragma_Unreferenced_Check_Spec (E : Entity_Id) return Boolean;
+   --  Tests Has_Pragma_Unreferenced flag for entity E. If E is not a formal,
+   --  this is simply the setting of the flag Has_Pragma_Unreferenced. If E is
+   --  a body formal, the setting of the flag in the corresponding spec is
+   --  also checked (and True returned if either flag is True).
+
+   function Never_Set_In_Source_Check_Spec (E : Entity_Id) return Boolean;
+   --  Tests Never_Set_In_Source status for entity E. If E is not a formal,
+   --  this is simply the setting of the flag Never_Set_In_Source. If E is
+   --  a body formal, the setting of the flag in the corresponding spec is
+   --  also checked (and False returned if either flag is False).
 
    function Operand_Has_Warnings_Suppressed (N : Node_Id) return Boolean;
    --  This function traverses the expression tree represented by the node N
@@ -76,6 +150,37 @@ package body Sem_Warn is
    --  the Warnings_Off flag is set. True is returned if such an entity is
    --  encountered, and False otherwise.
 
+   function Referenced_Check_Spec (E : Entity_Id) return Boolean;
+   --  Tests Referenced status for entity E. If E is not a formal, this is
+   --  simply the setting of the flag Referenced. If E is a body formal, the
+   --  setting of the flag in the corresponding spec is also checked (and True
+   --  returned if either flag is True).
+
+   function Referenced_As_LHS_Check_Spec (E : Entity_Id) return Boolean;
+   --  Tests Referenced_As_LHS status for entity E. If E is not a formal, this
+   --  is simply the setting of the flag Referenced_As_LHS. If E is a body
+   --  formal, the setting of the flag in the corresponding spec is also
+   --  checked (and True returned if either flag is True).
+
+   function Referenced_As_Out_Parameter_Check_Spec
+     (E : Entity_Id) return Boolean;
+   --  Tests Referenced_As_Out_Parameter status for entity E. If E is not a
+   --  formal, this is simply the setting of Referenced_As_Out_Parameter. If E
+   --  is a body formal, the setting of the flag in the corresponding spec is
+   --  also checked (and True returned if either flag is True).
+
+   procedure Warn_On_Unreferenced_Entity
+     (Spec_E : Entity_Id;
+      Body_E : Entity_Id := Empty);
+   --  Output warnings for unreferenced entity E. For the case of an entry
+   --  formal, Body_E is the corresponding body entity for a particular
+   --  accept statement, and the message is posted on Body_E. In all other
+   --  cases, Body_E is ignored and must be Empty.
+
+   function Warnings_Off_Check_Spec (E : Entity_Id) return Boolean;
+   --  Returns True if Warnings_Off is set for the entity E or (in the case
+   --  where there is a Spec_Entity), Warnings_Off is set for the Spec_Entity.
+
    --------------------------
    -- Check_Code_Statement --
    --------------------------
@@ -94,7 +199,7 @@ package body Sem_Warn is
 
       if No (Asm_Input_Value) then
          Error_Msg_F
-           ("?code statement with no inputs should usually be Volatile", N);
+           ("?code statement with no inputs should usually be Volatile!", N);
          return;
       end if;
 
@@ -102,7 +207,7 @@ package body Sem_Warn is
 
       if No (Asm_Output_Variable) then
          Error_Msg_F
-           ("?code statement with no outputs should usually be Volatile", N);
+           ("?code statement with no outputs should usually be Volatile!", N);
          return;
       end if;
 
@@ -113,19 +218,400 @@ package body Sem_Warn is
         and then Nkind (Prev (N)) = N_Code_Statement
       then
          Error_Msg_F
-           ("?code statements in sequence should usually be Volatile", N);
+           ("?code statements in sequence should usually be Volatile!", N);
          Error_Msg_F
-           ("\?(suggest using template with multiple instructions)", N);
+           ("\?(suggest using template with multiple instructions)!", N);
       end if;
    end Check_Code_Statement;
 
+   ---------------------------------
+   -- Check_Infinite_Loop_Warning --
+   ---------------------------------
+
+   --  The case we look for is a while loop which tests a local variable, where
+   --  there is no obvious direct or possible indirect update of the variable
+   --  within the body of the loop.
+
+   procedure Check_Infinite_Loop_Warning (Loop_Statement : Node_Id) is
+      Iter : constant Node_Id := Iteration_Scheme (Loop_Statement);
+
+      Ref : Node_Id := Empty;
+      --  Reference in iteration scheme to variable that may not be modified in
+      --  loop, indicating a possible infinite loop.
+
+      Var : Entity_Id := Empty;
+      --  Corresponding entity (entity of Ref)
+
+      procedure Find_Var (N : Node_Id);
+      --  Inspect condition to see if it depends on a single entity reference.
+      --  If so, Ref is set to point to the reference node, and Var is set to
+      --  the referenced Entity.
+
+      function Has_Indirection (T : Entity_Id) return Boolean;
+      --  If the controlling variable is an access type, or is a record type
+      --  with access components, assume that it is changed indirectly and
+      --  suppress the warning. As a concession to low-level programming, in
+      --  particular within Declib, we also suppress warnings on a record
+      --  type that contains components of type Address or Short_Address.
+
+      function Is_Suspicious_Function_Name (E : Entity_Id) return Boolean;
+      --  Given an entity name, see if the name appears to have something to
+      --  do with I/O or network stuff, and if so, return True. Used to kill
+      --  some false positives on a heuristic basis that such functions will
+      --  likely have some strange side effect dependencies. A rather funny
+      --  kludge, but warning messages are in the heuristics business.
+
+      function Test_Ref (N : Node_Id) return Traverse_Result;
+      --  Test for reference to variable in question. Returns Abandon if
+      --  matching reference found.
+
+      function Find_Ref is new Traverse_Func (Test_Ref);
+      --  Function to traverse body of procedure. Returns Abandon if matching
+      --  reference found.
+
+      --------------
+      -- Find_Var --
+      --------------
+
+      procedure Find_Var (N : Node_Id) is
+      begin
+         --  Condition is a direct variable reference
+
+         if Is_Entity_Name (N) then
+            Ref := N;
+            Var := Entity (Ref);
+
+         --  Case of condition is a comparison with compile time known value
+
+         elsif Nkind (N) in N_Op_Compare then
+            if Compile_Time_Known_Value (Right_Opnd (N)) then
+               Find_Var (Left_Opnd (N));
+
+            elsif Compile_Time_Known_Value (Left_Opnd (N)) then
+               Find_Var (Right_Opnd (N));
+
+            --  Ignore any other comparison
+
+            else
+               return;
+            end if;
+
+         --  If condition is a negation, check its operand
+
+         elsif Nkind (N) = N_Op_Not then
+            Find_Var (Right_Opnd (N));
+
+         --  Case of condition is function call
+
+         elsif Nkind (N) = N_Function_Call then
+
+            --  Forget it if function name is not entity, who knows what
+            --  we might be calling?
+
+            if not Is_Entity_Name (Name (N)) then
+               return;
+
+            --  Forget it if function name is suspicious. A strange test
+            --  but warning generation is in the heuristics business!
+
+            elsif Is_Suspicious_Function_Name (Entity (Name (N))) then
+               return;
+
+            --  Forget it if warnings are suppressed on function entity
+
+            elsif Has_Warnings_Off (Entity (Name (N))) then
+               return;
+            end if;
+
+            --  OK, see if we have one argument
+
+            declare
+               PA : constant List_Id := Parameter_Associations (N);
+
+            begin
+               --  One argument, so check the argument
+
+               if Present (PA)
+                 and then List_Length (PA) = 1
+               then
+                  if Nkind (First (PA)) = N_Parameter_Association then
+                     Find_Var (Explicit_Actual_Parameter (First (PA)));
+                  else
+                     Find_Var (First (PA));
+                  end if;
+
+               --  Not one argument
+
+               else
+                  return;
+               end if;
+            end;
+
+         --  Any other kind of node is not something we warn for
+
+         else
+            return;
+         end if;
+      end Find_Var;
+
+      ---------------------
+      -- Has_Indirection --
+      ---------------------
+
+      function Has_Indirection (T : Entity_Id) return Boolean is
+         Comp : Entity_Id;
+         Rec  : Entity_Id;
+
+      begin
+         if Is_Access_Type (T) then
+            return True;
+
+         elsif Is_Private_Type (T)
+           and then Present (Full_View (T))
+           and then Is_Access_Type (Full_View (T))
+         then
+            return True;
+
+         elsif Is_Record_Type (T) then
+            Rec := T;
+
+         elsif Is_Private_Type (T)
+           and then Present (Full_View (T))
+           and then Is_Record_Type (Full_View (T))
+         then
+            Rec := Full_View (T);
+         else
+            return False;
+         end if;
+
+         Comp := First_Component (Rec);
+         while Present (Comp) loop
+            if Is_Access_Type (Etype (Comp))
+              or else Is_Descendent_Of_Address (Etype (Comp))
+            then
+               return True;
+            end if;
+
+            Next_Component (Comp);
+         end loop;
+
+         return False;
+      end Has_Indirection;
+
+      ---------------------------------
+      -- Is_Suspicious_Function_Name --
+      ---------------------------------
+
+      function Is_Suspicious_Function_Name (E : Entity_Id) return Boolean is
+         S : Entity_Id;
+
+         function Substring_Present (S : String) return Boolean;
+         --  Returns True if name buffer has given string delimited by non-
+         --  alphabetic characters or by end of string. S is lower case.
+
+         -----------------------
+         -- Substring_Present --
+         -----------------------
+
+         function Substring_Present (S : String) return Boolean is
+            Len : constant Natural := S'Length;
+
+         begin
+            for J in 1 .. Name_Len - (Len - 1) loop
+               if Name_Buffer (J .. J + (Len - 1)) = S
+                 and then
+                   (J = 1
+                     or else Name_Buffer (J - 1) not in 'a' .. 'z')
+                 and then
+                   (J + Len > Name_Len
+                     or else Name_Buffer (J + Len) not in 'a' .. 'z')
+               then
+                  return True;
+               end if;
+            end loop;
+
+            return False;
+         end Substring_Present;
+
+      --  Start of processing for Is_Suspicious_Function_Name
+
+      begin
+         S := E;
+         while Present (S) and then S /= Standard_Standard loop
+            Get_Name_String (Chars (S));
+
+            if Substring_Present ("io")
+              or else Substring_Present ("file")
+              or else Substring_Present ("network")
+            then
+               return True;
+            else
+               S := Scope (S);
+            end if;
+         end loop;
+
+         return False;
+      end Is_Suspicious_Function_Name;
+
+      --------------
+      -- Test_Ref --
+      --------------
+
+      function Test_Ref (N : Node_Id) return Traverse_Result is
+      begin
+         --  Waste of time to look at iteration scheme
+
+         if N = Iter then
+            return Skip;
+
+         --  Direct reference to variable in question
+
+         elsif Is_Entity_Name (N)
+           and then Present (Entity (N))
+           and then Entity (N) = Var
+         then
+            --  If this is an Lvalue, then definitely abandon, since
+            --  this could be a direct modification of the variable.
+
+            if May_Be_Lvalue (N) then
+               return Abandon;
+            end if;
+
+            --  If we appear in the context of a procedure call, then also
+            --  abandon, since there may be issues of non-visible side
+            --  effects going on in the call.
+
+            declare
+               P : Node_Id;
+
+            begin
+               P := N;
+               loop
+                  P := Parent (P);
+                  exit when P = Loop_Statement;
+
+                  if Nkind (P) = N_Procedure_Call_Statement then
+                     return Abandon;
+                  end if;
+               end loop;
+            end;
+
+            --  Reference to variable renaming variable in question
+
+         elsif Is_Entity_Name (N)
+           and then Present (Entity (N))
+           and then Ekind (Entity (N)) = E_Variable
+           and then Present (Renamed_Object (Entity (N)))
+           and then Is_Entity_Name (Renamed_Object (Entity (N)))
+           and then Entity (Renamed_Object (Entity (N))) = Var
+           and then May_Be_Lvalue (N)
+         then
+            return Abandon;
+
+            --  Call to subprogram
+
+         elsif Nkind (N) = N_Procedure_Call_Statement
+           or else Nkind (N) = N_Function_Call
+         then
+            --  If subprogram is within the scope of the entity we are dealing
+            --  with as the loop variable, then it could modify this parameter,
+            --  so we abandon in this case. In the case of a subprogram that is
+            --  not an entity we also abandon. The check for no entity being
+            --  present is a defense against previous errors.
+
+            if not Is_Entity_Name (Name (N))
+              or else No (Entity (Name (N)))
+              or else Scope_Within (Entity (Name (N)), Scope (Var))
+            then
+               return Abandon;
+            end if;
+         end if;
+
+         --  All OK, continue scan
+
+         return OK;
+      end Test_Ref;
+
+   --  Start of processing for Check_Infinite_Loop_Warning
+
+   begin
+      --  We need a while iteration with no condition actions. Conditions
+      --  actions just make things too complicated to get the warning right.
+
+      if No (Iter)
+        or else No (Condition (Iter))
+        or else Present (Condition_Actions (Iter))
+        or else Debug_Flag_Dot_W
+      then
+         return;
+      end if;
+
+      --  Initial conditions met, see if condition is of right form
+
+      Find_Var (Condition (Iter));
+
+      --  Nothing to do if local variable from source not found
+
+      if No (Var)
+        or else Ekind (Var) /= E_Variable
+        or else Is_Library_Level_Entity (Var)
+        or else not Comes_From_Source (Var)
+      then
+         return;
+
+      --  Nothing to do if there is some indirection involved (assume that the
+      --  designated variable might be modified in some way we don't see).
+
+      elsif Has_Indirection (Etype (Var)) then
+         return;
+
+      --  Same sort of thing for volatile variable, might be modified by
+      --  some other task or by the operating system in some way.
+
+      elsif Is_Volatile (Var) then
+         return;
+      end if;
+
+      --  Filter out case of original statement sequence starting with delay.
+      --  We assume this is a multi-tasking program and that the condition
+      --  is affected by other threads (some kind of busy wait).
+
+      declare
+         Fstm : constant Node_Id :=
+                  Original_Node (First (Statements (Loop_Statement)));
+      begin
+         if Nkind (Fstm) = N_Delay_Relative_Statement
+           or else Nkind (Fstm) = N_Delay_Until_Statement
+         then
+            return;
+         end if;
+      end;
+
+      --  We have a variable reference of the right form, now we scan the loop
+      --  body to see if it looks like it might not be modified
+
+      if Find_Ref (Loop_Statement) = OK then
+         Error_Msg_NE
+           ("?variable& is not modified in loop body!", Ref, Var);
+         Error_Msg_N
+           ("\?possible infinite loop!", Ref);
+      end if;
+   end Check_Infinite_Loop_Warning;
+
    ----------------------
    -- Check_References --
    ----------------------
 
    procedure Check_References (E : Entity_Id; Anod : Node_Id := Empty) is
-      E1 : Entity_Id;
-      UR : Node_Id;
+      E1  : Entity_Id;
+      E1T : Entity_Id;
+      UR  : Node_Id;
+
+      function Body_Formal
+        (E                : Entity_Id;
+         Accept_Statement : Node_Id) return Entity_Id;
+      --  For an entry formal entity from an entry declaration, find the
+      --  corresponding body formal from the given accept statement.
 
       function Missing_Subunits return Boolean;
       --  We suppress warnings when there are missing subunits, because this
@@ -142,6 +628,40 @@ package body Sem_Warn is
       --  from another unit. This is true for entities in packages that are at
       --  the library level.
 
+      function Warnings_Off_E1 return Boolean;
+      --  Return True if Warnings_Off is set for E1, or for its Etype (E1T),
+      --  or for the base type of E1T.
+
+      -----------------
+      -- Body_Formal --
+      -----------------
+
+      function Body_Formal
+        (E                : Entity_Id;
+         Accept_Statement : Node_Id) return Entity_Id
+      is
+         Body_Param : Node_Id;
+         Body_E     : Entity_Id;
+
+      begin
+         --  Loop to find matching parameter in accept statement
+
+         Body_Param := First (Parameter_Specifications (Accept_Statement));
+         while Present (Body_Param) loop
+            Body_E := Defining_Identifier (Body_Param);
+
+            if Chars (Body_E) = Chars (E) then
+               return Body_E;
+            end if;
+
+            Next (Body_Param);
+         end loop;
+
+         --  Should never fall through, should always find a match
+
+         raise Program_Error;
+      end Body_Formal;
+
       ----------------------
       -- Missing_Subunits --
       ----------------------
@@ -190,6 +710,23 @@ package body Sem_Warn is
 
       procedure Output_Reference_Error (M : String) is
       begin
+         --  Never issue messages for internal names
+
+         if Is_Internal_Name (Chars (E1)) then
+            return;
+         end if;
+
+         --  Don't output message for IN OUT formal unless we have the warning
+         --  flag specifically set. It is a bit odd to distinguish IN OUT
+         --  formals from other cases. This distinction is historical in
+         --  nature. Warnings for IN OUT formals were added fairly late.
+
+         if Ekind (E1) = E_In_Out_Parameter
+           and then not Check_Unreferenced_Formals
+         then
+            return;
+         end if;
+
          --  Other than accept case, post error on defining identifier
 
          if No (Anod) then
@@ -198,30 +735,8 @@ package body Sem_Warn is
          --  Accept case, find body formal to post the message
 
          else
-            declare
-               Parm  : Node_Id;
-               Enod  : Node_Id;
-               Defid : Entity_Id;
-
-            begin
-               Enod := Anod;
-
-               if Present (Parameter_Specifications (Anod)) then
-                  Parm := First (Parameter_Specifications (Anod));
-                  while Present (Parm) loop
-                     Defid := Defining_Identifier (Parm);
+            Error_Msg_NE (M, Body_Formal (E1, Accept_Statement => Anod), E1);
 
-                     if Chars (E1) = Chars (Defid) then
-                        Enod := Defid;
-                        exit;
-                     end if;
-
-                     Next (Parm);
-                  end loop;
-               end if;
-
-               Error_Msg_NE (M, Enod, E1);
-            end;
          end if;
       end Output_Reference_Error;
 
@@ -234,6 +749,13 @@ package body Sem_Warn is
          Prev : Node_Id;
 
       begin
+         --  A formal parameter is never referenceable outside the body of its
+         --  subprogram or entry.
+
+         if Is_Formal (Ent) then
+            return False;
+         end if;
+
          --  Examine parents to look for a library level package spec. But if
          --  we find a body or block or other similar construct along the way,
          --  we cannot be referenced.
@@ -255,8 +777,8 @@ package body Sem_Warn is
                --  we will get a warning for the package entity.
 
                --  Note that generic formal parameters are themselves not
-               --  publicly referenceable in an instance, and warnings on
-               --  them are useful.
+               --  publicly referenceable in an instance, and warnings on them
+               --  are useful.
 
                when N_Generic_Package_Declaration =>
                   return
@@ -264,8 +786,8 @@ package body Sem_Warn is
                       or else List_Containing (Prev)
                         /= Generic_Formal_Declarations (P);
 
-               --  Similarly, the generic formals of a generic subprogram
-               --  are not accessible.
+               --  Similarly, the generic formals of a generic subprogram are
+               --  not accessible.
 
                when N_Generic_Subprogram_Declaration  =>
                   if Is_List_Member (Prev)
@@ -308,6 +830,17 @@ package body Sem_Warn is
          end loop;
       end Publicly_Referenceable;
 
+      ---------------------
+      -- Warnings_Off_E1 --
+      ---------------------
+
+      function Warnings_Off_E1 return Boolean is
+      begin
+         return Has_Warnings_Off (E1T)
+           or else Has_Warnings_Off (Base_Type (E1T))
+           or else Warnings_Off_Check_Spec (E1);
+      end Warnings_Off_E1;
+
    --  Start of processing for Check_References
 
    begin
@@ -333,22 +866,28 @@ package body Sem_Warn is
 
       E1 := First_Entity (E);
       while Present (E1) loop
+         E1T := Etype (E1);
 
-         --  We only look at source entities with warning flag on
+         --  We are only interested in source entities. We also don't issue
+         --  warnings within instances, since the proper place for such
+         --  warnings is on the template when it is compiled.
 
-         if Comes_From_Source (E1) and then not Warnings_Off (E1) then
-
-            --  We are interested in variables and out parameters, but we
-            --  exclude protected types, too complicated to worry about.
+         if Comes_From_Source (E1)
+           and then Instantiation_Location (Sloc (E1)) = No_Location
+         then
+            --  We are interested in variables and out/in-out parameters, but
+            --  we exclude protected types, too complicated to worry about.
 
             if Ekind (E1) = E_Variable
                  or else
-               (Ekind (E1) = E_Out_Parameter
+                ((Ekind (E1) = E_Out_Parameter
+                    or else Ekind (E1) = E_In_Out_Parameter)
                   and then not Is_Protected_Type (Current_Scope))
             then
-               --  Post warning if this object not assigned. Note that we do
-               --  not consider the implicit initialization of an access type
-               --  to be the assignment of a value for this purpose.
+               --  Case of an unassigned variable
+
+               --  First gather any Unset_Reference indication for E1. In the
+               --  case of a parameter, it is the Spec_Entity that is relevant.
 
                if Ekind (E1) = E_Out_Parameter
                  and then Present (Spec_Entity (E1))
@@ -358,164 +897,333 @@ package body Sem_Warn is
                   UR := Unset_Reference (E1);
                end if;
 
-               --  If the entity is an out parameter of the current subprogram
-               --  body, check the warning status of the parameter in the spec.
+               --  Special processing for access types
+
+               if Present (UR)
+                 and then Is_Access_Type (E1T)
+               then
+                  --  For access types, the only time we made a UR entry was
+                  --  for a dereference, and so we post the appropriate warning
+                  --  here (note that the dereference may not be explicit in
+                  --  the source, for example in the case of a dispatching call
+                  --  with an anonymous access controlling formal, or of an
+                  --  assignment of a pointer involving discriminant check
+                  --  on the designated object).
+
+                  if not Warnings_Off_E1 then
+                     Error_Msg_NE ("?& may be null!", UR, E1);
+                  end if;
+
+                  goto Continue;
+
+               --  Case of variable that could be a constant. Note that we
+               --  never signal such messages for generic package entities,
+               --  since a given instance could have modifications outside
+               --  the package.
+
+               elsif Warn_On_Constant
+                 and then (Ekind (E1) = E_Variable
+                             and then Has_Initial_Value (E1))
+                 and then Never_Set_In_Source_Check_Spec (E1)
+                 and then not Address_Taken (E1)
+                 and then not Generic_Package_Spec_Entity (E1)
+               then
+                  --  A special case, if this variable is volatile and not
+                  --  imported, it is not helpful to tell the programmer
+                  --  to mark the variable as constant, since this would be
+                  --  illegal by virtue of RM C.6(13).
+
+                  if (Is_Volatile (E1) or else Has_Volatile_Components (E1))
+                    and then not Is_Imported (E1)
+                  then
+                     Error_Msg_N
+                       ("?& is not modified, volatile has no effect!", E1);
+
+                  --  Another special case, Exception_Occurrence, this catches
+                  --  the case of exception choice (and a bit more too, but not
+                  --  worth doing more investigation here).
+
+                  elsif Is_RTE (E1T, RE_Exception_Occurrence) then
+                     null;
+
+                  --  Here we give the warning if referenced and no pragma
+                  --  Unreferenced or Unmodified is present.
+
+                  else
+                     --  Variable case
+
+                     if Ekind (E1) = E_Variable then
+                        if Referenced_Check_Spec (E1)
+                          and then not Has_Pragma_Unreferenced_Check_Spec (E1)
+                          and then not Has_Pragma_Unmodified_Check_Spec (E1)
+                        then
+                           if not Warnings_Off_E1 then
+                              Error_Msg_N
+                                ("?& is not modified, "
+                                 & "could be declared constant!",
+                                 E1);
+                           end if;
+                        end if;
+                     end if;
+                  end if;
+
+               --  Other cases of a variable or parameter never set in source
+
+               elsif Never_Set_In_Source_Check_Spec (E1)
+
+                  --  No warning if warning for this case turned off
+
+                  and then Warn_On_No_Value_Assigned
+
+                  --  No warning if address taken somewhere
+
+                  and then not Address_Taken (E1)
+
+                  --  No warning if explicit initial value
+
+                  and then not Has_Initial_Value (E1)
+
+                  --  No warning for generic package spec entities, since we
+                  --  might set them in a child unit or something like that
+
+                  and then not Generic_Package_Spec_Entity (E1)
 
-               if Ekind (E1) = E_Out_Parameter
-                 and then Present (Spec_Entity (E1))
-                 and then Warnings_Off (Spec_Entity (E1))
-               then
-                  null;
+                  --  No warning if fully initialized type, except that for
+                  --  this purpose we do not consider access types to qualify
+                  --  as fully initialized types (relying on an access type
+                  --  variable being null when it is never set is a bit odd!)
 
-               elsif Present (UR)
-                 and then Is_Access_Type (Etype (E1))
+                  --  Also we generate warning for an out parameter that is
+                  --  never referenced, since again it seems odd to rely on
+                  --  default initialization to set an out parameter value.
+
+                 and then (Is_Access_Type (E1T)
+                            or else Ekind (E1) = E_Out_Parameter
+                            or else not Is_Fully_Initialized_Type (E1T))
                then
+                  --  Do not output complaint about never being assigned a
+                  --  value if a pragma Unmodified applies to the variable
+                  --  we are examining, or if it is a parameter, if there is
+                  --  a pragma Unreferenced for the corresponding spec, of
+                  --  if the type is marked as having unreferenced objects.
+                  --  The last is a little peculiar, but better too few than
+                  --  too many warnings in this situation.
+
+                  if Has_Pragma_Unreferenced_Objects (E1T)
+                    or else Has_Pragma_Unmodified_Check_Spec (E1)
+                  then
+                     null;
 
-                  --  For access types, the only time we made a UR entry was
-                  --  for a dereference, and so we post the appropriate warning
-                  --  here (note that the dereference may not be explicit in
-                  --  the source, for example in the case of a dispatching call
-                  --  with an anonymous access controlling formal, or of an
-                  --  assignment of a pointer involving discriminant check on
-                  --  the designated object).
+                  --  IN OUT parameter case where parameter is referenced. We
+                  --  separate this out, since this is the case where we delay
+                  --  output of the warning until more information is available
+                  --  (about use in an instantiation or address being taken).
 
-                  Error_Msg_NE ("& may be null?", UR, E1);
-                  goto Continue;
+                  elsif Ekind (E1) = E_In_Out_Parameter
+                    and then Referenced_Check_Spec (E1)
+                  then
+                     --  Suppress warning if private type, and the procedure
+                     --  has a separate declaration in a different unit. This
+                     --  is the case where the client of a package sees only
+                     --  the private type, and it it may be quite reasonable
+                     --  for the logical view to be in out, even if the
+                     --  implementation ends up using access types or some
+                     --  other method to achieve the local effect of a
+                     --  modification. On the other hand if the spec and body
+                     --  are in the same unit, we are in the package body and
+                     --  there we have less excuse for a junk IN OUT parameter.
+
+                     if Has_Private_Declaration (E1T)
+                       and then Present (Spec_Entity (E1))
+                       and then not In_Same_Source_Unit (E1, Spec_Entity (E1))
+                     then
+                        null;
 
-               elsif Never_Set_In_Source (E1)
-                 and then not Generic_Package_Spec_Entity (E1)
-               then
-                  if Warn_On_No_Value_Assigned then
-
-                     --  Do not output complaint about never being assigned a
-                     --  value if a pragma Unreferenced applies to the variable
-                     --  or if it is a parameter, to the corresponding spec.
-
-                     if Has_Pragma_Unreferenced (E1)
-                       or else Has_Pragma_Unreferenced_Objects (Etype (E1))
-                       or else (Is_Formal (E1)
-                                  and then Present (Spec_Entity (E1))
-                                  and then
-                                    Has_Pragma_Unreferenced (Spec_Entity (E1)))
+                     --  Suppress warning for any parameter of a dispatching
+                     --  operation, since it is quite reasonable to have an
+                     --  operation that is overridden, and for some subclasses
+                     --  needs the formal to be IN OUT and for others happens
+                     --  not to assign it.
+
+                     elsif Is_Dispatching_Operation
+                             (Scope (Goto_Spec_Entity (E1)))
+                     then
+                        null;
+
+                     --  Suppress warning if composite type containing any
+                     --  access element component, since the logical effect
+                     --  of modifying a parameter may be achieved by modifying
+                     --  a referenced entity.
+
+                     elsif Is_Composite_Type (E1T)
+                       and then Has_Access_Values (E1T)
                      then
                         null;
 
-                     --  Pragma Unreferenced not set, so output message
+                     --  OK, looks like warning for an IN OUT parameter that
+                     --  could be IN makes sense, but we delay the output of
+                     --  the warning, pending possibly finding out later on
+                     --  that the associated subprogram is used as a generic
+                     --  actual, or its address/access is taken. In these two
+                     --  cases, we suppress the warning because the context may
+                     --  force use of IN OUT, even if in this particular case
+                     --  the formal is not modified.
 
                      else
-                        if Referenced (E1) then
-                           Output_Reference_Error
-                             ("variable& is read but never assigned?");
-                        else
+                        In_Out_Warnings.Append (E1);
+                     end if;
+
+                  --  Other cases of formals
+
+                  elsif Is_Formal (E1) then
+                     if not Is_Trivial_Subprogram (Scope (E1)) then
+                        if Referenced_Check_Spec (E1) then
+                           if not Has_Pragma_Unmodified_Check_Spec (E1)
+                             and then not Warnings_Off_E1
+                           then
+                              Output_Reference_Error
+                                ("?formal parameter& is read but "
+                                 & "never assigned!");
+                           end if;
+
+                        elsif not Has_Pragma_Unreferenced_Check_Spec (E1)
+                          and then not Warnings_Off_E1
+                        then
                            Output_Reference_Error
-                             ("variable& is never read and never assigned?");
+                             ("?formal parameter& is not referenced!");
                         end if;
+                     end if;
 
-                        --  Deal with special case where this variable is
-                        --  hidden by a loop variable
+                  --  Case of variable
 
-                        if Ekind (E1) = E_Variable
-                          and then Present (Hiding_Loop_Variable (E1))
+                  else
+                     if Referenced (E1) then
+                        if not Has_Unmodified (E1)
+                          and then not Warnings_Off_E1
                         then
-                           Error_Msg_Sloc := Sloc (E1);
-                           Error_Msg_N
-                             ("declaration hides &#?",
-                              Hiding_Loop_Variable (E1));
-                           Error_Msg_N
-                             ("for loop implicitly declares loop variable?",
-                              Hiding_Loop_Variable (E1));
+                           Output_Reference_Error
+                             ("?variable& is read but never assigned!");
                         end if;
+
+                     elsif not Has_Unreferenced (E1)
+                       and then not Warnings_Off_E1
+                     then
+                        Output_Reference_Error
+                          ("?variable& is never read and never assigned!");
                      end if;
-                  end if;
-                  goto Continue;
 
-               --  Case of variable that could be a constant. Note that we
-               --  never signal such messages for generic package entities,
-               --  since a given instance could have modifications outside
-               --  the package.
+                     --  Deal with special case where this variable is hidden
+                     --  by a loop variable.
 
-               elsif Warn_On_Constant
-                 and then Ekind (E1) = E_Variable
-                 and then Is_True_Constant (E1)
-                 and then not Generic_Package_Spec_Entity (E1)
-               then
-                  --  A special case, if this variable is volatile and not
-                  --  imported, it is not helpful to tell the programmer
-                  --  to mark the variable as constant, since this would be
-                  --  illegal by virtue of RM C.6(13).
+                     if Ekind (E1) = E_Variable
+                       and then Present (Hiding_Loop_Variable (E1))
+                       and then not Warnings_Off_E1
+                     then
+                        Error_Msg_N
+                          ("?for loop implicitly declares loop variable!",
+                           Hiding_Loop_Variable (E1));
 
-                  if (Is_Volatile (E1) or else Has_Volatile_Components (E1))
-                    and then not Is_Imported (E1)
-                  then
-                     Error_Msg_N
-                       ("& is not modified, volatile has no effect?", E1);
-                  else
-                     Error_Msg_N
-                       ("& is not modified, could be declared constant?", E1);
+                        Error_Msg_Sloc := Sloc (E1);
+                        Error_Msg_N
+                          ("\?declaration hides & declared#!",
+                           Hiding_Loop_Variable (E1));
+                     end if;
                   end if;
+
+                  goto Continue;
                end if;
 
-               --  Check for unset reference, note that we exclude access
-               --  types from this check, since access types do always have
-               --  a null value, and that seems legitimate in this case.
+               --  Check for unset reference
 
                if Warn_On_No_Value_Assigned and then Present (UR) then
 
-                  --  For other than access type, go back to original node
-                  --  to deal with case where original unset reference
-                  --  has been rewritten during expansion.
-
-                  UR := Original_Node (UR);
+                  --  For other than access type, go back to original node to
+                  --  deal with case where original unset reference has been
+                  --  rewritten during expansion.
 
-                  --  In some cases, the original node may be a type
-                  --  conversion or qualification, and in this case
-                  --  we want the object entity inside.
+                  --  In some cases, the original node may be a type conversion
+                  --  or qualification, and in this case we want the object
+                  --  entity inside.
 
+                  UR := Original_Node (UR);
                   while Nkind (UR) = N_Type_Conversion
                     or else Nkind (UR) = N_Qualified_Expression
                   loop
                      UR := Expression (UR);
                   end loop;
 
-                  --  Here we issue the warning, all checks completed If the
-                  --  unset reference is prefix of a selected component that
-                  --  comes from source, mention the component as well. If the
-                  --  selected component comes from expansion, all we know is
-                  --  that the entity is not fully initialized at the point of
-                  --  the reference. Locate an unintialized component to get a
-                  --  better error message.
+                  --  Here we issue the warning, all checks completed
 
-                  if Nkind (Parent (UR)) = N_Selected_Component then
-                     Error_Msg_Node_2 := Selector_Name (Parent (UR));
+                  --  If we have a return statement, this was a case of an OUT
+                  --  parameter not being set at the time of the return. (Note:
+                  --  it can't be N_Extended_Return_Statement, because those
+                  --  are only for functions, and functions do not allow OUT
+                  --  parameters.)
 
-                     if not Comes_From_Source (Parent (UR)) then
-                        declare
-                           Comp : Entity_Id;
+                  if not Is_Trivial_Subprogram (Scope (E1)) then
+                     if Nkind (UR) = N_Simple_Return_Statement
+                       and then not Has_Pragma_Unmodified_Check_Spec (E1)
+                     then
+                        if not Warnings_Off_E1 then
+                           Error_Msg_NE
+                             ("?OUT parameter& not set before return", UR, E1);
+                        end if;
 
-                        begin
-                           Comp := First_Entity (Etype (E1));
-                           while Present (Comp) loop
-                              if Ekind (Comp) = E_Component
-                                and then Nkind (Parent (Comp)) =
-                                  N_Component_Declaration
-                                and then No (Expression (Parent (Comp)))
-                              then
-                                 Error_Msg_Node_2 := Comp;
-                                 exit;
+                        --  If the unset reference is a selected component
+                        --  prefix from source, mention the component as well.
+                        --  If the selected component comes from expansion, all
+                        --  we know is that the entity is not fully initialized
+                        --  at the point of the reference. Locate a random
+                        --  uninitialized component to get a better message.
+
+                     elsif Nkind (Parent (UR)) = N_Selected_Component then
+                        Error_Msg_Node_2 := Selector_Name (Parent (UR));
+
+                        if not Comes_From_Source (Parent (UR)) then
+                           declare
+                              Comp : Entity_Id;
+
+                           begin
+                              Comp := First_Entity (E1T);
+                              while Present (Comp) loop
+                                 if Ekind (Comp) = E_Component
+                                   and then Nkind (Parent (Comp)) =
+                                   N_Component_Declaration
+                                   and then No (Expression (Parent (Comp)))
+                                 then
+                                    Error_Msg_Node_2 := Comp;
+                                    exit;
+                                 end if;
+
+                                 Next_Entity (Comp);
+                              end loop;
+                           end;
+                        end if;
+
+                        --  Issue proper warning. This is a case of referencing
+                        --  a variable before it has been explicitly assigned.
+                        --  For access types, UR was only set for dereferences,
+                        --  so the issue is that the value may be null.
+
+                        if not Is_Trivial_Subprogram (Scope (E1)) then
+                           if not Warnings_Off_E1 then
+                              if Is_Access_Type (Etype (Parent (UR))) then
+                                 Error_Msg_N ("?`&.&` may be null!", UR);
+                              else
+                                 Error_Msg_N
+                                   ("?`&.&` may be referenced before "
+                                    & "it has a value!", UR);
                               end if;
+                           end if;
+                        end if;
 
-                              Next_Entity (Comp);
-                           end loop;
-                        end;
-                     end if;
+                        --  All other cases of unset reference active
 
-                     Error_Msg_N
-                       ("`&.&` may be referenced before it has a value?",
-                        UR);
-                  else
-                     Error_Msg_N
-                       ("& may be referenced before it has a value?",
-                        UR);
+                     elsif not Warnings_Off_E1 then
+                        Error_Msg_N
+                          ("?& may be referenced before it has a value!",
+                           UR);
+                     end if;
                   end if;
 
                   goto Continue;
@@ -523,20 +1231,40 @@ package body Sem_Warn is
             end if;
 
             --  Then check for unreferenced entities. Note that we are only
-            --  interested in entities which do not have the Referenced flag
-            --  set. The Referenced_As_LHS flag is interesting only if the
-            --  Referenced flag is not set.
+            --  interested in entities whose Referenced flag is not set.
+
+            if not Referenced_Check_Spec (E1)
+
+               --  If Referenced_As_LHS is set, then that's still interesting
+               --  (potential "assigned but never read" case), but not if we
+               --  have pragma Unreferenced, which cancels this error.
 
-            if not Referenced (E1)
+              and then (not Referenced_As_LHS_Check_Spec (E1)
+                          or else not Has_Unreferenced (E1))
 
                --  Check that warnings on unreferenced entities are enabled
 
-              and then ((Check_Unreferenced and then not Is_Formal (E1))
-                           or else
-                        (Check_Unreferenced_Formals and then Is_Formal (E1))
-                           or else
-                        (Warn_On_Modified_Unread
-                          and then Referenced_As_LHS (E1)))
+              and then
+                ((Check_Unreferenced and then not Is_Formal (E1))
+
+                     --  Case of warning on unreferenced formal
+
+                     or else
+                      (Check_Unreferenced_Formals and then Is_Formal (E1))
+
+                     --  Case of warning on unread variables modified by an
+                     --  assignment, or an out parameter if it is the only one.
+
+                     or else
+                       (Warn_On_Modified_Unread
+                          and then Referenced_As_LHS_Check_Spec (E1))
+
+                     --  Case of warning on any unread out parameter (note
+                     --  such indications are only set if the appropriate
+                     --  warning options were set, so no need to recheck here.
+
+                     or else
+                       Referenced_As_Out_Parameter_Check_Spec (E1))
 
                --  Labels, and enumeration literals, and exceptions. The
                --  warnings are also placed on local packages that cannot be
@@ -556,14 +1284,21 @@ package body Sem_Warn is
                          Ekind (E1) = E_Named_Real
                            or else
                          Is_Overloadable (E1)
+
+                           --  Package case, if the main unit is a package
+                           --  spec or generic package spec, then there may
+                           --  be a corresponding body that references this
+                           --  package in some other file. Otherwise we can
+                           --  be sure that there is no other reference.
+
                            or else
                              (Ekind (E1) = E_Package
-                               and then
-                                (Ekind (E) = E_Function
-                                  or else Ekind (E) = E_Package_Body
-                                  or else Ekind (E) = E_Procedure
-                                  or else Ekind (E) = E_Subprogram_Body
-                                  or else Ekind (E) = E_Block)))
+                                and then
+                                  Ekind (Cunit_Entity (Current_Sem_Unit)) /=
+                                                          E_Package
+                                and then
+                                  Ekind (Cunit_Entity (Current_Sem_Unit)) /=
+                                                          E_Generic_Package))
 
                --  Exclude instantiations, since there is no reason why every
                --  entity in an instantiation should be referenced.
@@ -621,7 +1356,7 @@ package body Sem_Warn is
                and then ((Ekind (E1) /= E_Variable
                              and then Ekind (E1) /= E_Constant
                              and then Ekind (E1) /= E_Component)
-                           or else not Is_Task_Type (Etype (E1)))
+                           or else not Is_Task_Type (E1T))
 
                --  For subunits, only place warnings on the main unit itself,
                --  since parent units are not completely compiled
@@ -629,6 +1364,14 @@ package body Sem_Warn is
                and then (Nkind (Unit (Cunit (Main_Unit))) /= N_Subunit
                            or else
                          Get_Source_Unit (E1) = Main_Unit)
+
+               --  No warning on a return object, because these are often
+               --  created with a single expression and an implicit return.
+               --  If the object is a variable there will be a warning
+               --  indicating that it could be declared constant.
+
+               and then not
+                 (Ekind (E1) = E_Constant and then Is_Return_Object (E1))
             then
                --  Suppress warnings in internal units if not in -gnatg mode
                --  (these would be junk warnings for an applications program,
@@ -642,17 +1385,25 @@ package body Sem_Warn is
                   --  We do not immediately flag the error. This is because we
                   --  have not expanded generic bodies yet, and they may have
                   --  the missing reference. So instead we park the entity on a
-                  --  list, for later processing. However, for the accept case,
-                  --  post the error right here, since we have the information
-                  --  now in this case.
+                  --  list, for later processing. However for the case of an
+                  --  accept statement we want to output messages now, since
+                  --  we know we already have all information at hand, and we
+                  --  also want to have separate warnings for each accept
+                  --  statement for the same entry.
 
                   if Present (Anod) then
-                     Output_Reference_Error ("& is not referenced?");
+                     pragma Assert (Is_Formal (E1));
 
-                  else
-                     Unreferenced_Entities.Increment_Last;
-                     Unreferenced_Entities.Table
-                       (Unreferenced_Entities.Last) := E1;
+                     --  The unreferenced entity is E1, but post the warning
+                     --  on the body entity for this accept statement.
+
+                     if not Warnings_Off_E1 then
+                        Warn_On_Unreferenced_Entity
+                          (E1, Body_Formal (E1, Accept_Statement => Anod));
+                     end if;
+
+                  elsif not Warnings_Off_E1 then
+                     Unreferenced_Entities.Append (E1);
                   end if;
                end if;
 
@@ -667,17 +1418,18 @@ package body Sem_Warn is
               and then Instantiation_Depth (Sloc (E1)) = 0
               and then Warn_On_Redundant_Constructs
             then
-               Unreferenced_Entities.Increment_Last;
-               Unreferenced_Entities.Table (Unreferenced_Entities.Last) := E1;
+               if not Warnings_Off_E1 then
+                  Unreferenced_Entities.Append (E1);
 
                --  Force warning on entity
 
-               Set_Referenced (E1, False);
+                  Set_Referenced (E1, False);
+               end if;
             end if;
          end if;
 
          --  Recurse into nested package or block. Do not recurse into a
-         --  formal package, because the correponding body is not analyzed.
+         --  formal package, because the corresponding body is not analyzed.
 
          <<Continue>>
             if ((Ekind (E1) = E_Package or else Ekind (E1) = E_Generic_Package)
@@ -700,6 +1452,68 @@ package body Sem_Warn is
    ---------------------------
 
    procedure Check_Unset_Reference (N : Node_Id) is
+      Typ : constant Entity_Id := Etype (N);
+
+      function Is_OK_Fully_Initialized return Boolean;
+      --  This function returns true if the given node N is fully initialized
+      --  so that the reference is safe as far as this routine is concerned.
+      --  Safe generally means that the type of N is a fully initialized type.
+      --  The one special case is that for access types, which are always fully
+      --  initialized, we don't consider a dereference OK since it will surely
+      --  be dereferencing a null value, which won't do.
+
+      function Prefix_Has_Dereference (Pref : Node_Id) return Boolean;
+      --  Used to test indexed or selected component or slice to see if the
+      --  evaluation of the prefix depends on a dereference, and if so, returns
+      --  True, in which case we always check the prefix, even if we know that
+      --  the referenced component is initialized. Pref is the prefix to test.
+
+      -----------------------------
+      -- Is_OK_Fully_Initialized --
+      -----------------------------
+
+      function Is_OK_Fully_Initialized return Boolean is
+      begin
+         if Is_Access_Type (Typ) and then Is_Dereferenced (N) then
+            return False;
+         else
+            return Is_Fully_Initialized_Type (Typ);
+         end if;
+      end Is_OK_Fully_Initialized;
+
+      ----------------------------
+      -- Prefix_Has_Dereference --
+      ----------------------------
+
+      function Prefix_Has_Dereference (Pref : Node_Id) return Boolean is
+      begin
+         --  If prefix is of an access type, certainly need a dereference
+
+         if Is_Access_Type (Etype (Pref)) then
+            return True;
+
+         --  If prefix is explicit dereference, that's a dereference for sure
+
+         elsif Nkind (Pref) = N_Explicit_Dereference then
+            return True;
+
+            --  If prefix is itself a component reference or slice check prefix
+
+         elsif Nkind (Pref) = N_Slice
+           or else Nkind (Pref) = N_Indexed_Component
+           or else Nkind (Pref) = N_Selected_Component
+         then
+            return Prefix_Has_Dereference (Prefix (Pref));
+
+         --  All other cases do not involve a dereference
+
+         else
+            return False;
+         end if;
+      end Prefix_Has_Dereference;
+
+   --  Start of processing for Check_Unset_Reference
+
    begin
       --  Nothing to do if warnings suppressed
 
@@ -707,13 +1521,11 @@ package body Sem_Warn is
          return;
       end if;
 
-      --  Ignore reference to non-scalar if not from source. Almost always such
-      --  references are bogus (e.g. calls to init procs to set default
-      --  discriminant values).
+      --  Ignore reference unless it comes from source. Almost always if we
+      --  have a reference from generated code, it is bogus (e.g. calls to init
+      --  procs to set default discriminant values).
 
-      if not Comes_From_Source (N)
-        and then not Is_Scalar_Type (Etype (N))
-      then
+      if not Comes_From_Source (N) then
          return;
       end if;
 
@@ -726,21 +1538,28 @@ package body Sem_Warn is
       --  unset reference, we check whether N is earlier before proceeding.
 
       case Nkind (N) is
+
+         --  For identifier or expanded name, examine the entity involved
+
          when N_Identifier | N_Expanded_Name =>
             declare
                E : constant Entity_Id := Entity (N);
 
             begin
                if (Ekind (E) = E_Variable
-                    or else Ekind (E) = E_Out_Parameter)
-                 and then Never_Set_In_Source (E)
+                     or else
+                   Ekind (E) = E_Out_Parameter)
+                 and then Never_Set_In_Source_Check_Spec (E)
+                 and then not Has_Initial_Value (E)
                  and then (No (Unset_Reference (E))
-                             or else Earlier_In_Extended_Unit
-                               (Sloc (N),  Sloc (Unset_Reference (E))))
-                 and then not Warnings_Off (E)
+                            or else
+                              Earlier_In_Extended_Unit
+                                (Sloc (N),  Sloc (Unset_Reference (E))))
+                 and then not Has_Pragma_Unmodified_Check_Spec (E)
+                 and then not Warnings_Off_Check_Spec (E)
                then
                   --  We may have an unset reference. The first test is whether
-                  --  we are accessing a discriminant of a record or a
+                  --  this is an access to a discriminant of a record or a
                   --  component with default initialization. Both of these
                   --  cases can be ignored, since the actual object that is
                   --  referenced is definitely initialized. Note that this
@@ -753,21 +1572,29 @@ package body Sem_Warn is
                   --  not the record, and still deserves an unset reference.
 
                   if Nkind (Parent (N)) = N_Selected_Component
-                    and not Is_Access_Type (Etype (N))
+                    and not Is_Access_Type (Typ)
                   then
                      declare
                         ES : constant Entity_Id :=
                                Entity (Selector_Name (Parent (N)));
-
                      begin
                         if Ekind (ES) = E_Discriminant
-                          or else Present (Expression (Declaration_Node (ES)))
+                          or else
+                            (Present (Declaration_Node (ES))
+                               and then
+                             Present (Expression (Declaration_Node (ES))))
                         then
                            return;
                         end if;
                      end;
                   end if;
 
+                  --  Exclude fully initialized types
+
+                  if Is_OK_Fully_Initialized then
+                     return;
+                  end if;
+
                   --  Here we have a potential unset reference. But before we
                   --  get worried about it, we have to make sure that the
                   --  entity declaration is in the same procedure as the
@@ -803,14 +1630,13 @@ package body Sem_Warn is
                      --  cannot be truly uninitialized, but we still want to
                      --  warn about cases of obvious null dereference.
 
-                     if Is_Access_Type (Etype (N)) then
+                     if Is_Access_Type (Typ) then
                         Access_Type_Case : declare
                            P : Node_Id;
 
                            function Process
-                             (N    : Node_Id)
-                              return Traverse_Result;
-                           --  Process function for instantation of Traverse
+                             (N : Node_Id) return Traverse_Result;
+                           --  Process function for instantiation of Traverse
                            --  below. Checks if N contains reference to other
                            --  than a dereference.
 
@@ -823,8 +1649,7 @@ package body Sem_Warn is
                            -------------
 
                            function Process
-                             (N    : Node_Id)
-                              return Traverse_Result
+                             (N : Node_Id) return Traverse_Result
                            is
                            begin
                               if Is_Entity_Name (N)
@@ -850,18 +1675,18 @@ package body Sem_Warn is
                         --  Start of processing for Access_Type_Case
 
                         begin
-                           --  Don't bother if we are inside an instance,
-                           --  since the compilation of the generic template
-                           --  is where the warning should be issued.
+                           --  Don't bother if we are inside an instance, since
+                           --  the compilation of the generic template is where
+                           --  the warning should be issued.
 
                            if In_Instance then
                               return;
                            end if;
 
-                           --  Don't bother if this is not the main unit.
-                           --  If we try to give this warning for with'ed
-                           --  units, we get some false positives, since
-                           --  we do not record references in other units.
+                           --  Don't bother if this is not the main unit. If we
+                           --  try to give this warning for with'ed units, we
+                           --  get some false positives, since we do not record
+                           --  references in other units.
 
                            if not In_Extended_Main_Source_Unit (E)
                                 or else
@@ -870,7 +1695,7 @@ package body Sem_Warn is
                               return;
                            end if;
 
-                           --  We are only interested in deferences
+                           --  We are only interested in dereferences
 
                            if not Is_Dereferenced (N) then
                               return;
@@ -917,8 +1742,8 @@ package body Sem_Warn is
                      if Nkind (N) = N_Identifier then
                         Set_Unset_Reference (E, N);
 
-                     --  Otherwise it is an expanded name, so set the field
-                     --  of the actual identifier for the reference.
+                     --  Otherwise it is an expanded name, so set the field of
+                     --  the actual identifier for the reference.
 
                      else
                         Set_Unset_Reference (E, Selector_Name (N));
@@ -927,25 +1752,90 @@ package body Sem_Warn is
                end if;
             end;
 
+         --  Indexed component or slice
+
          when N_Indexed_Component | N_Slice =>
-            Check_Unset_Reference (Prefix (N));
 
-         when N_Selected_Component =>
+            --  If prefix does not involve dereferencing an access type, then
+            --  we know we are OK if the component type is fully initialized,
+            --  since the component will have been set as part of the default
+            --  initialization.
 
-            if Present (Entity (Selector_Name (N)))
-              and then Ekind (Entity (Selector_Name (N))) = E_Discriminant
+            if not Prefix_Has_Dereference (Prefix (N))
+              and then Is_OK_Fully_Initialized
             then
-               --   A discriminant is always initialized
+               return;
 
-               null;
+            --  Look at prefix in access type case, or if the component is not
+            --  fully initialized.
 
             else
                Check_Unset_Reference (Prefix (N));
             end if;
 
+         --  Record component
+
+         when N_Selected_Component =>
+            declare
+               Pref : constant Node_Id   := Prefix (N);
+               Ent  : constant Entity_Id := Entity (Selector_Name (N));
+
+            begin
+               --  If prefix involves dereferencing an access type, always
+               --  check the prefix, since the issue then is whether this
+               --  access value is null.
+
+               if Prefix_Has_Dereference (Pref) then
+                  null;
+
+               --  Always go to prefix if no selector entity is set. Can this
+               --  happen in the normal case? Not clear, but it definitely can
+               --  happen in error cases.
+
+               elsif No (Ent) then
+                  null;
+
+               --  For a record component, check some cases where we have
+               --  reasonable cause to consider that the component is known to
+               --  be or probably is initialized. In this case, we don't care
+               --  if the prefix itself was explicitly initialized.
+
+               --  Discriminants are always considered initialized
+
+               elsif Ekind (Ent) = E_Discriminant then
+                  return;
+
+               --  An explicitly initialized component is certainly initialized
+
+               elsif Nkind (Parent (Ent)) = N_Component_Declaration
+                 and then Present (Expression (Parent (Ent)))
+               then
+                  return;
+
+               --  A fully initialized component is initialized
+
+               elsif Is_OK_Fully_Initialized then
+                  return;
+               end if;
+
+               --  If none of those cases apply, check the record type prefix
+
+               Check_Unset_Reference (Pref);
+            end;
+
+         --  For type conversions or qualifications examine the expression
+
          when N_Type_Conversion | N_Qualified_Expression =>
             Check_Unset_Reference (Expression (N));
 
+         --  For explicit dereference, always check prefix, which will generate
+         --  an unset reference (since this is a case of dereferencing null).
+
+         when N_Explicit_Dereference =>
+            Check_Unset_Reference (Prefix (N));
+
+         --  All other cases are not cases of an unset reference
+
          when others =>
             null;
 
@@ -992,7 +1882,7 @@ package body Sem_Warn is
          --  The only reference to a context unit may be in a renaming
          --  declaration. If this renaming declares a visible entity, do
          --  not warn that the context clause could be moved to the body,
-         --  because the renaming may be intented to re-export the unit.
+         --  because the renaming may be intended to re-export the unit.
 
          -------------------------
          -- Check_Inner_Package --
@@ -1022,7 +1912,7 @@ package body Sem_Warn is
                      if Entity (Nam) = Pack then
                         Error_Msg_Qual_Level := 1;
                         Error_Msg_NE
-                          ("no entities of package& are referenced?",
+                          ("?no entities of package& are referenced!",
                              Nam, Pack);
                         Error_Msg_Qual_Level := 0;
                      end if;
@@ -1039,7 +1929,7 @@ package body Sem_Warn is
          begin
             E := First_Entity (Pack);
             while Present (E) loop
-               if Referenced (E) then
+               if Referenced_Check_Spec (E) then
                   return;
                end if;
 
@@ -1067,7 +1957,7 @@ package body Sem_Warn is
             then
                Ent := First_Entity (System_Aux_Id);
                while Present (Ent) loop
-                  if Referenced (Ent) then
+                  if Referenced_Check_Spec (Ent) then
                      return True;
                   end if;
 
@@ -1155,7 +2045,7 @@ package body Sem_Warn is
                --  is explicitly marked by a pragma Unreferenced).
 
                if not Referenced (Lunit)
-                 and then not Has_Pragma_Unreferenced (Lunit)
+                 and then not Has_Unreferenced (Lunit)
                then
                   --  Suppress warnings in internal units if not in -gnatg mode
                   --  (these would be junk warnings for an application program,
@@ -1175,7 +2065,7 @@ package body Sem_Warn is
 
                      else
                         Error_Msg_N
-                          ("unit& is not referenced?", Name (Item));
+                          ("?unit& is not referenced!", Name (Item));
                      end if;
                   end if;
 
@@ -1197,10 +2087,13 @@ package body Sem_Warn is
                --  are referenced. If none of the entities are referenced, we
                --  still post a warning. This occurs if the only use of the
                --  package is in a use clause, or in a package renaming
-               --  declaration.
-
-               elsif Ekind (Lunit) = E_Package then
+               --  declaration. This check is skipped for packages that are
+               --  renamed in a spec, since the entities in such a package are
+               --  visible to clients via the renaming.
 
+               elsif Ekind (Lunit) = E_Package
+                 and then not Renamed_In_Spec (Lunit)
+               then
                   --  If Is_Instantiated is set, it means that the package is
                   --  implicitly instantiated (this is the case of parent
                   --  instance or an actual for a generic package formal), and
@@ -1245,9 +2138,13 @@ package body Sem_Warn is
                            --  Else give the warning
 
                            else
-                              Error_Msg_N
-                                ("no entities of & are referenced?",
-                                 Name (Item));
+                              if not
+                                Has_Unreferenced (Entity (Name (Item)))
+                              then
+                                 Error_Msg_N
+                                   ("?no entities of & are referenced!",
+                                    Name (Item));
+                              end if;
 
                               --  Look for renamings of this package, and flag
                               --  them as well. If the original package has
@@ -1257,27 +2154,36 @@ package body Sem_Warn is
                               Pack := Find_Package_Renaming (Munite, Lunit);
 
                               if Present (Pack)
-                                and then not Warnings_Off (Lunit)
+                                and then not Has_Warnings_Off (Lunit)
+                                and then not Has_Unreferenced (Pack)
                               then
                                  Error_Msg_NE
-                                   ("no entities of & are referenced?",
+                                   ("?no entities of & are referenced!",
                                      Unit_Declaration_Node (Pack),
-                                       Pack);
+                                     Pack);
                               end if;
                            end if;
 
                            exit;
 
-                        --  Case of next entity is referenced
-
-                        elsif Referenced (Ent)
-                          or else Referenced_As_LHS (Ent)
+                        --  Case of entity being referenced. The reference may
+                        --  come from a limited_with_clause, in which case the
+                        --  limited view of the entity carries the flag.
+
+                        elsif Referenced_Check_Spec (Ent)
+                          or else Referenced_As_LHS_Check_Spec (Ent)
+                          or else Referenced_As_Out_Parameter_Check_Spec (Ent)
+                          or else
+                            (From_With_Type (Ent)
+                              and then Is_Incomplete_Type (Ent)
+                              and then Present (Non_Limited_View (Ent))
+                              and then Referenced (Non_Limited_View (Ent)))
                         then
                            --  This means that the with is indeed fine, in that
                            --  it is definitely needed somewhere, and we can
-                           --  quit worrying about this one.
+                           --  quit worrying about this one...
 
-                           --  Except for one little detail, if either of the
+                           --  Except for one little detail: if either of the
                            --  flags was set during spec processing, this is
                            --  where we complain that the with could be moved
                            --  from the spec. If the spec contains a visible
@@ -1292,12 +2198,12 @@ package body Sem_Warn is
 
                            if Unreferenced_In_Spec (Item) then
                               Error_Msg_N
-                                ("unit& is not referenced in spec?",
+                                ("?unit& is not referenced in spec!",
                                  Name (Item));
 
                            elsif No_Entities_Ref_In_Spec (Item) then
                               Error_Msg_N
-                                ("no entities of & are referenced in spec?",
+                                ("?no entities of & are referenced in spec!",
                                  Name (Item));
 
                            else
@@ -1310,7 +2216,7 @@ package body Sem_Warn is
 
                            if not Is_Visible_Renaming then
                               Error_Msg_N
-                                ("\with clause might be moved to body?",
+                                ("\?with clause might be moved to body!",
                                  Name (Item));
                            end if;
 
@@ -1338,7 +2244,7 @@ package body Sem_Warn is
                         Set_Unreferenced_In_Spec (Item);
                      else
                         Error_Msg_N
-                          ("unit& is never instantiated?", Name (Item));
+                          ("?unit& is never instantiated!", Name (Item));
                      end if;
 
                   --  If unit was indeed instantiated, make sure that flag is
@@ -1347,16 +2253,15 @@ package body Sem_Warn is
 
                   elsif Unreferenced_In_Spec (Item) then
                      Error_Msg_N
-                       ("unit& is not instantiated in spec?", Name (Item));
+                       ("?unit& is not instantiated in spec!", Name (Item));
                      Error_Msg_N
-                       ("\with clause can be moved to body?", Name (Item));
+                       ("\?with clause can be moved to body!", Name (Item));
                   end if;
                end if;
             end if;
 
             Next (Item);
          end loop;
-
       end Check_One_Unit;
 
    --  Start of processing for Check_Unused_Withs
@@ -1425,6 +2330,91 @@ package body Sem_Warn is
       end if;
    end Generic_Package_Spec_Entity;
 
+   ----------------------
+   -- Goto_Spec_Entity --
+   ----------------------
+
+   function Goto_Spec_Entity (E : Entity_Id) return Entity_Id is
+   begin
+      if Is_Formal (E)
+        and then Present (Spec_Entity (E))
+      then
+         return Spec_Entity (E);
+      else
+         return E;
+      end if;
+   end Goto_Spec_Entity;
+
+   --------------------------------------
+   -- Has_Pragma_Unmodified_Check_Spec --
+   --------------------------------------
+
+   function Has_Pragma_Unmodified_Check_Spec
+     (E : Entity_Id) return Boolean
+   is
+   begin
+      if Is_Formal (E) and then Present (Spec_Entity (E)) then
+
+         --  Note: use of OR instead of OR ELSE here is deliberate, we want
+         --  to mess with Unmodified flags on both body and spec entities.
+
+         return Has_Unmodified (E)
+                  or
+                Has_Unmodified (Spec_Entity (E));
+
+      else
+         return Has_Unmodified (E);
+      end if;
+   end Has_Pragma_Unmodified_Check_Spec;
+
+   ----------------------------------------
+   -- Has_Pragma_Unreferenced_Check_Spec --
+   ----------------------------------------
+
+   function Has_Pragma_Unreferenced_Check_Spec
+     (E : Entity_Id) return Boolean
+   is
+   begin
+      if Is_Formal (E) and then Present (Spec_Entity (E)) then
+
+         --  Note: use of OR here instead of OR ELSE is deliberate, we want
+         --  to mess with flags on both entities.
+
+         return Has_Unreferenced (E)
+                  or
+                Has_Unreferenced (Spec_Entity (E));
+
+      else
+         return Has_Unreferenced (E);
+      end if;
+   end Has_Pragma_Unreferenced_Check_Spec;
+
+   ----------------
+   -- Initialize --
+   ----------------
+
+   procedure Initialize is
+   begin
+      Warnings_Off_Pragmas.Init;
+      Unreferenced_Entities.Init;
+      In_Out_Warnings.Init;
+   end Initialize;
+
+   ------------------------------------
+   -- Never_Set_In_Source_Check_Spec --
+   ------------------------------------
+
+   function Never_Set_In_Source_Check_Spec (E : Entity_Id) return Boolean is
+   begin
+      if Is_Formal (E) and then Present (Spec_Entity (E)) then
+         return Never_Set_In_Source (E)
+                  and then
+                Never_Set_In_Source (Spec_Entity (E));
+      else
+         return Never_Set_In_Source (E);
+      end if;
+   end Never_Set_In_Source_Check_Spec;
+
    -------------------------------------
    -- Operand_Has_Warnings_Suppressed --
    -------------------------------------
@@ -1450,27 +2440,130 @@ package body Sem_Warn is
       begin
          if Nkind (R) in N_Has_Entity
            and then Present (Entity (R))
-           and then Warnings_Off (Entity (R))
+           and then Has_Warnings_Off (Entity (R))
          then
             return Abandon;
          else
-            return OK_Orig;
+            return OK_Orig;
+         end if;
+      end Check_For_Warnings;
+
+   --  Start of processing for Operand_Has_Warnings_Suppressed
+
+   begin
+      return Traverse (N) = Abandon;
+
+   --  If any exception occurs, then something has gone wrong, and this is
+   --  only a minor aesthetic issue anyway, so just say we did not find what
+   --  we are looking for, rather than blow up.
+
+   exception
+      when others =>
+         return False;
+   end Operand_Has_Warnings_Suppressed;
+
+   -----------------------------------------
+   -- Output_Non_Modified_In_Out_Warnings --
+   -----------------------------------------
+
+   procedure Output_Non_Modifed_In_Out_Warnings is
+
+      function No_Warn_On_In_Out (E : Entity_Id) return Boolean;
+      --  Given a formal parameter entity E, determines if there is a reason to
+      --  suppress IN OUT warnings (not modified, could be IN) for formals of
+      --  the subprogram. We suppress these warnings if Warnings Off is set, or
+      --  if we have seen the address of the subprogram being taken, or if the
+      --  subprogram is used as a generic actual (in the latter cases the
+      --  context may force use of IN OUT, even if the parameter is not
+      --  modifies for this particular case.
+
+      -----------------------
+      -- No_Warn_On_In_Out --
+      -----------------------
+
+      function No_Warn_On_In_Out (E : Entity_Id) return Boolean is
+         S  : constant Entity_Id := Scope (E);
+         SE : constant Entity_Id := Spec_Entity (E);
+
+      begin
+         --  Do not warn if address is taken, since funny business may be going
+         --  on in treating the parameter indirectly as IN OUT.
+
+         if Address_Taken (S)
+           or else (Present (SE) and then Address_Taken (Scope (SE)))
+         then
+            return True;
+
+         --  Do not warn if used as a generic actual, since the generic may be
+         --  what is forcing the use of an "unnecessary" IN OUT.
+
+         elsif Used_As_Generic_Actual (S)
+           or else (Present (SE) and then Used_As_Generic_Actual (Scope (SE)))
+         then
+            return True;
+
+         --  Else test warnings off
+
+         elsif Warnings_Off_Check_Spec (S) then
+            return True;
+
+         --  All tests for suppressing warning failed
+
+         else
+            return False;
          end if;
-      end Check_For_Warnings;
+      end No_Warn_On_In_Out;
 
-   --  Start of processing for Operand_Has_Warnings_Suppressed
+   --  Start of processing for Output_Non_Modified_In_Out_Warnings
 
    begin
-      return Traverse (N) = Abandon;
+      --  Loop through entities for which a warning may be needed
 
-   --  If any exception occurs, then something has gone wrong, and this is
-   --  only a minor aesthetic issue anyway, so just say we did not find what
-   --  we are looking for, rather than blow up.
+      for J in In_Out_Warnings.First .. In_Out_Warnings.Last loop
+         declare
+            E1 : constant Entity_Id := In_Out_Warnings.Table (J);
 
-   exception
-      when others =>
-         return False;
-   end Operand_Has_Warnings_Suppressed;
+         begin
+            --  Suppress warning in specific cases (see details in comments for
+            --  No_Warn_On_In_Out), or if there is a pragma Unmodified.
+
+            if Has_Pragma_Unmodified_Check_Spec (E1)
+              or else No_Warn_On_In_Out (E1)
+            then
+               null;
+
+            --  Here we generate the warning
+
+            else
+               --  If -gnatwc is set then output message that we could be IN
+
+               if not Is_Trivial_Subprogram (Scope (E1)) then
+                  if Warn_On_Constant then
+                     Error_Msg_N
+                       ("?formal parameter & is not modified!", E1);
+                     Error_Msg_N
+                       ("\?mode could be IN instead of `IN OUT`!", E1);
+
+                     --  We do not generate warnings for IN OUT parameters
+                     --  unless we have at least -gnatwu. This is deliberately
+                     --  inconsistent with the treatment of variables, but
+                     --  otherwise we get too many unexpected warnings in
+                     --  default mode.
+
+                  elsif Check_Unreferenced then
+                     Error_Msg_N ("?formal parameter& is read but "
+                                  & "never assigned!", E1);
+                  end if;
+               end if;
+
+               --  Kill any other warnings on this entity, since this is the
+               --  one that should dominate any other unreferenced warning.
+
+               Set_Warnings_Off (E1);
+            end if;
+         end;
+      end loop;
+   end Output_Non_Modifed_In_Out_Warnings;
 
    ----------------------------------------
    -- Output_Obsolescent_Entity_Warnings --
@@ -1588,31 +2681,15 @@ package body Sem_Warn is
 
       --  Output additional warning if present
 
-      declare
-         W : constant Node_Id := Obsolescent_Warning (E);
-
-      begin
-         if Present (W) then
-
-            --  This is a warning continuation to start on a new line
-            Name_Buffer (1) := '\';
-            Name_Buffer (2) := '\';
-            Name_Buffer (3) := '?';
-            Name_Len := 3;
-
-            --  Add characters to message, and output message. Note that
-            --  we quote every character of the message since we don't
-            --  want to process any insertions.
-
-            for J in 1 .. String_Length (Strval (W)) loop
-               Add_Char_To_Name_Buffer (''');
-               Add_Char_To_Name_Buffer
-                 (Get_Character (Get_String_Char (Strval (W), J)));
-            end loop;
-
-            Error_Msg_N (Name_Buffer (1 .. Name_Len), N);
+      for J in Obsolescent_Warnings.First .. Obsolescent_Warnings.Last loop
+         if Obsolescent_Warnings.Table (J).Ent = E then
+            String_To_Name_Buffer (Obsolescent_Warnings.Table (J).Msg);
+            Error_Msg_Strlen := Name_Len;
+            Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
+            Error_Msg_N ("\\?~", N);
+            exit;
          end if;
-      end;
+      end loop;
    end Output_Obsolescent_Entity_Warnings;
 
    ----------------------------------
@@ -1620,119 +2697,112 @@ package body Sem_Warn is
    ----------------------------------
 
    procedure Output_Unreferenced_Messages is
-      E : Entity_Id;
-
    begin
       for J in Unreferenced_Entities.First ..
                Unreferenced_Entities.Last
       loop
-         E := Unreferenced_Entities.Table (J);
-
-         if not Referenced (E) and then not Warnings_Off (E) then
-            case Ekind (E) is
-               when E_Variable =>
-
-                  --  Case of variable that is assigned but not read. We
-                  --  suppress the message if the variable is volatile, has an
-                  --  address clause, or is imported.
-
-                  if Referenced_As_LHS (E)
-                    and then No (Address_Clause (E))
-                    and then not Is_Volatile (E)
-                  then
-                     if Warn_On_Modified_Unread
-                       and then not Is_Imported (E)
-                       and then not Is_Return_Object (E)
-
-                        --  Suppress message for aliased or renamed variables,
-                        --  since there may be other entities that read the
-                        --  same memory location.
+         Warn_On_Unreferenced_Entity (Unreferenced_Entities.Table (J));
+      end loop;
+   end Output_Unreferenced_Messages;
 
-                       and then not Is_Aliased (E)
-                       and then No (Renamed_Object (E))
+   -----------------------------------------
+   -- Output_Unused_Warnings_Off_Warnings --
+   -----------------------------------------
 
-                     then
-                        Error_Msg_N
-                          ("variable & is assigned but never read?", E);
-                        Set_Last_Assignment (E, Empty);
-                     end if;
+   procedure Output_Unused_Warnings_Off_Warnings is
+   begin
+      for J in Warnings_Off_Pragmas.First .. Warnings_Off_Pragmas.Last loop
+         declare
+            Wentry : Warnings_Off_Entry renames Warnings_Off_Pragmas.Table (J);
+            N      : Node_Id renames Wentry.N;
+            E      : Node_Id renames Wentry.E;
 
-                  --  Normal case of neither assigned nor read
+         begin
+            --  Turn off Warnings_Off, or we won't get the warning!
 
-                  else
-                     --  We suppress the message for types for which a valid
-                     --  pragma Unreferenced_Objects has been given, otherwise
-                     --  we go ahead and give the message.
+            Set_Warnings_Off (E, False);
 
-                     if not Has_Pragma_Unreferenced_Objects (Etype (E)) then
+            --  Nothing to do if pragma was used to suppress a general warning
 
-                        --  Distinguish renamed case in message
+            if Warnings_Off_Used (E) then
+               null;
 
-                        if Present (Renamed_Object (E))
-                          and then Comes_From_Source (Renamed_Object (E))
-                        then
-                           Error_Msg_N
-                             ("renamed variable & is not referenced?", E);
-                        else
-                           Error_Msg_N
-                             ("variable & is not referenced?", E);
-                        end if;
-                     end if;
-                  end if;
+            --  If pragma was used both in unmodified and unreferenced contexts
+            --  then that's as good as the general case, no warning.
 
-               when E_Constant =>
-                  if Present (Renamed_Object (E))
-                    and then Comes_From_Source (Renamed_Object (E))
-                  then
-                     Error_Msg_N ("renamed constant & is not referenced?", E);
-                  else
-                     Error_Msg_N ("constant & is not referenced?", E);
-                  end if;
+            elsif Warnings_Off_Used_Unmodified (E)
+                    and
+                  Warnings_Off_Used_Unreferenced (E)
+            then
+               null;
 
-               when E_In_Parameter     |
-                    E_Out_Parameter    |
-                    E_In_Out_Parameter =>
+            --  Used only in context where Unmodified would have worked
 
-                  --  Do not emit message for formals of a renaming, because
-                  --  they are never referenced explicitly.
+            elsif Warnings_Off_Used_Unmodified (E) then
+               Error_Msg_NE
+                 ("?could use Unmodified instead of "
+                  & "Warnings Off for &", Pragma_Identifier (N), E);
 
-                  if Nkind (Original_Node (Unit_Declaration_Node (Scope (E))))
-                    /= N_Subprogram_Renaming_Declaration
-                  then
-                     Error_Msg_N ("formal parameter & is not referenced?", E);
-                  end if;
+            --  Used only in context where Unreferenced would have worked
 
-               when E_Named_Integer    |
-                    E_Named_Real       =>
-                  Error_Msg_N ("named number & is not referenced?", E);
+            elsif Warnings_Off_Used_Unreferenced (E) then
+               Error_Msg_NE
+                 ("?could use Unreferenced instead of "
+                  & "Warnings Off for &", Pragma_Identifier (N), E);
 
-               when E_Enumeration_Literal =>
-                  Error_Msg_N ("literal & is not referenced?", E);
+            --  Not used at all
 
-               when E_Function         =>
-                  Error_Msg_N ("function & is not referenced?", E);
+            else
+               Error_Msg_NE
+                 ("?pragma Warnings Off for & unused, "
+                  & "could be omitted", N, E);
+            end if;
+         end;
+      end loop;
+   end Output_Unused_Warnings_Off_Warnings;
 
-               when E_Procedure         =>
-                  Error_Msg_N ("procedure & is not referenced?", E);
+   ---------------------------
+   -- Referenced_Check_Spec --
+   ---------------------------
 
-               when E_Generic_Procedure =>
-                  Error_Msg_N
-                    ("generic procedure & is never instantiated?", E);
+   function Referenced_Check_Spec (E : Entity_Id) return Boolean is
+   begin
+      if Is_Formal (E) and then Present (Spec_Entity (E)) then
+         return Referenced (E) or else Referenced (Spec_Entity (E));
+      else
+         return Referenced (E);
+      end if;
+   end Referenced_Check_Spec;
 
-               when E_Generic_Function  =>
-                  Error_Msg_N ("generic function & is never instantiated?", E);
+   ----------------------------------
+   -- Referenced_As_LHS_Check_Spec --
+   ----------------------------------
 
-               when Type_Kind          =>
-                  Error_Msg_N ("type & is not referenced?", E);
+   function Referenced_As_LHS_Check_Spec (E : Entity_Id) return Boolean is
+   begin
+      if Is_Formal (E) and then Present (Spec_Entity (E)) then
+         return Referenced_As_LHS (E)
+           or else Referenced_As_LHS (Spec_Entity (E));
+      else
+         return Referenced_As_LHS (E);
+      end if;
+   end Referenced_As_LHS_Check_Spec;
 
-               when others =>
-                  Error_Msg_N ("& is not referenced?", E);
-            end case;
+   --------------------------------------------
+   -- Referenced_As_Out_Parameter_Check_Spec --
+   --------------------------------------------
 
-            Set_Warnings_Off (E);
-         end if;
-      end loop;
-   end Output_Unreferenced_Messages;
+   function Referenced_As_Out_Parameter_Check_Spec
+     (E : Entity_Id) return Boolean
+   is
+   begin
+      if Is_Formal (E) and then Present (Spec_Entity (E)) then
+         return Referenced_As_Out_Parameter (E)
+           or else Referenced_As_Out_Parameter (Spec_Entity (E));
+      else
+         return Referenced_As_Out_Parameter (E);
+      end if;
+   end Referenced_As_Out_Parameter_Check_Spec;
 
    ----------------------------
    -- Set_Dot_Warning_Switch --
@@ -1741,6 +2811,74 @@ package body Sem_Warn is
    function Set_Dot_Warning_Switch (C : Character) return Boolean is
    begin
       case C is
+         when 'a' =>
+            Warn_On_Assertion_Failure           := True;
+
+         when 'A' =>
+            Warn_On_Assertion_Failure           := False;
+
+         when 'c' =>
+            Warn_On_Unrepped_Components         := True;
+
+         when 'C' =>
+            Warn_On_Unrepped_Components         := False;
+
+         when 'e' =>
+            Address_Clause_Overlay_Warnings     := True;
+            Check_Unreferenced                  := True;
+            Check_Unreferenced_Formals          := True;
+            Check_Withs                         := True;
+            Constant_Condition_Warnings         := True;
+            Elab_Warnings                       := True;
+            Implementation_Unit_Warnings        := True;
+            Ineffective_Inline_Warnings         := True;
+            Warn_On_Ada_2005_Compatibility      := True;
+            Warn_On_All_Unread_Out_Parameters   := True;
+            Warn_On_Assertion_Failure           := True;
+            Warn_On_Assumed_Low_Bound           := True;
+            Warn_On_Bad_Fixed_Value             := True;
+            Warn_On_Constant                    := True;
+            Warn_On_Deleted_Code                := True;
+            Warn_On_Dereference                 := True;
+            Warn_On_Export_Import               := True;
+            Warn_On_Hiding                      := True;
+            Ineffective_Inline_Warnings         := True;
+            Warn_On_Modified_Unread             := True;
+            Warn_On_No_Value_Assigned           := True;
+            Warn_On_Non_Local_Exception         := True;
+            Warn_On_Object_Renames_Function     := True;
+            Warn_On_Obsolescent_Feature         := True;
+            Warn_On_Questionable_Missing_Parens := True;
+            Warn_On_Redundant_Constructs        := True;
+            Warn_On_Unchecked_Conversion        := True;
+            Warn_On_Unrecognized_Pragma         := True;
+            Warn_On_Unrepped_Components         := True;
+            Warn_On_Warnings_Off                := True;
+
+         when 'o' =>
+            Warn_On_All_Unread_Out_Parameters   := True;
+
+         when 'O' =>
+            Warn_On_All_Unread_Out_Parameters   := False;
+
+         when 'p' =>
+            Warn_On_Parameter_Order             := True;
+
+         when 'P' =>
+            Warn_On_Parameter_Order             := False;
+
+         when 'r' =>
+            Warn_On_Object_Renames_Function     := True;
+
+         when 'R' =>
+            Warn_On_Object_Renames_Function     := False;
+
+         when 'w' =>
+            Warn_On_Warnings_Off                := True;
+
+         when 'W' =>
+            Warn_On_Warnings_Off                := False;
+
          when 'x' =>
             Warn_On_Non_Local_Exception         := True;
 
@@ -1769,6 +2907,7 @@ package body Sem_Warn is
             Implementation_Unit_Warnings        := True;
             Ineffective_Inline_Warnings         := True;
             Warn_On_Ada_2005_Compatibility      := True;
+            Warn_On_Assertion_Failure           := True;
             Warn_On_Assumed_Low_Bound           := True;
             Warn_On_Bad_Fixed_Value             := True;
             Warn_On_Constant                    := True;
@@ -1776,11 +2915,14 @@ package body Sem_Warn is
             Warn_On_Modified_Unread             := True;
             Warn_On_No_Value_Assigned           := True;
             Warn_On_Non_Local_Exception         := True;
+            Warn_On_Object_Renames_Function     := True;
             Warn_On_Obsolescent_Feature         := True;
+            Warn_On_Parameter_Order             := True;
             Warn_On_Questionable_Missing_Parens := True;
             Warn_On_Redundant_Constructs        := True;
             Warn_On_Unchecked_Conversion        := True;
             Warn_On_Unrecognized_Pragma         := True;
+            Warn_On_Unrepped_Components         := True;
 
          when 'A' =>
             Check_Unreferenced                  := False;
@@ -1791,6 +2933,8 @@ package body Sem_Warn is
             Implementation_Unit_Warnings        := False;
             Ineffective_Inline_Warnings         := False;
             Warn_On_Ada_2005_Compatibility      := False;
+            Warn_On_Assertion_Failure           := False;
+            Warn_On_Assumed_Low_Bound           := False;
             Warn_On_Bad_Fixed_Value             := False;
             Warn_On_Constant                    := False;
             Warn_On_Deleted_Code                := False;
@@ -1801,10 +2945,15 @@ package body Sem_Warn is
             Warn_On_No_Value_Assigned           := False;
             Warn_On_Non_Local_Exception         := False;
             Warn_On_Obsolescent_Feature         := False;
+            Warn_On_All_Unread_Out_Parameters   := False;
+            Warn_On_Parameter_Order             := False;
             Warn_On_Questionable_Missing_Parens := False;
             Warn_On_Redundant_Constructs        := False;
+            Warn_On_Object_Renames_Function     := False;
             Warn_On_Unchecked_Conversion        := False;
             Warn_On_Unrecognized_Pragma         := False;
+            Warn_On_Unrepped_Components         := False;
+            Warn_On_Warnings_Off                := False;
 
          when 'b' =>
             Warn_On_Bad_Fixed_Value             := True;
@@ -2050,13 +3199,15 @@ package body Sem_Warn is
             then
                return;
 
-            --  Don't warn in assert pragma, since presumably tests in such
-            --  a context are very definitely intended, and might well be
+            --  Don't warn in assert or check pragma, since presumably tests in
+            --  such a context are very definitely intended, and might well be
             --  known at compile time. Note that we have to test the original
             --  node, since assert pragmas get rewritten at analysis time.
 
             elsif Nkind (Original_Node (P)) = N_Pragma
-              and then Chars (Original_Node (P)) = Name_Assert
+              and then (Pragma_Name (Original_Node (P)) = Name_Assert
+                          or else
+                        Pragma_Name (Original_Node (P)) = Name_Check)
             then
                return;
             end if;
@@ -2108,6 +3259,17 @@ package body Sem_Warn is
       end if;
    end Warn_On_Known_Condition;
 
+   ---------------------------------------
+   -- Warn_On_Modified_As_Out_Parameter --
+   ---------------------------------------
+
+   function Warn_On_Modified_As_Out_Parameter (E : Entity_Id) return Boolean is
+   begin
+      return
+        (Warn_On_Modified_Unread and then Is_Only_Out_Parameter (E))
+           or else Warn_On_All_Unread_Out_Parameters;
+   end Warn_On_Modified_As_Out_Parameter;
+
    ------------------------------
    -- Warn_On_Suspicious_Index --
    ------------------------------
@@ -2130,7 +3292,7 @@ package body Sem_Warn is
       --  to this lower bound. If not, False is returned, and Low_Bound is
       --  undefined on return.
       --
-      --  For now, we limite this to standard string types, so any other
+      --  For now, we limit this to standard string types, so any other
       --  unconstrained types return False. We may change our minds on this
       --  later on, but strings seem the most important case.
 
@@ -2148,12 +3310,12 @@ package body Sem_Warn is
          if Is_Array_Type (Typ)
            and then not Is_Constrained (Typ)
            and then Number_Dimensions (Typ) = 1
-           and then not Warnings_Off (Typ)
            and then (Root_Type (Typ) = Standard_String
                        or else
                      Root_Type (Typ) = Standard_Wide_String
                        or else
                      Root_Type (Typ) = Standard_Wide_Wide_String)
+           and then not Has_Warnings_Off (Typ)
          then
             LB := Type_Low_Bound (Etype (First_Index (Typ)));
 
@@ -2207,7 +3369,7 @@ package body Sem_Warn is
       begin
          --  Nothing to do if subscript does not come from source (we don't
          --  want to give garbage warnings on compiler expanded code, e.g. the
-         --  loops generated for slice assignments. Sucb junk warnings would
+         --  loops generated for slice assignments. Such junk warnings would
          --  be placed on source constructs with no subscript in sight!)
 
          if not Comes_From_Source (Original_Node (X)) then
@@ -2249,7 +3411,7 @@ package body Sem_Warn is
                   --  Tref (Sref) is used to scan the subscript
 
                   Pctr : Natural;
-                  --  Paretheses counter when scanning subscript
+                  --  Parentheses counter when scanning subscript
 
                begin
                   --  Tref (Sref) points to start of subscript
@@ -2385,16 +3547,222 @@ package body Sem_Warn is
       end if;
    end Warn_On_Suspicious_Index;
 
+   --------------------------------------
+   -- Warn_On_Unassigned_Out_Parameter --
+   --------------------------------------
+
+   procedure Warn_On_Unassigned_Out_Parameter
+     (Return_Node : Node_Id;
+      Scope_Id    : Entity_Id)
+   is
+      Form  : Entity_Id;
+      Form2 : Entity_Id;
+
+   begin
+      --  Ignore if procedure or return statement does not come from source
+
+      if not Comes_From_Source (Scope_Id)
+        or else not Comes_From_Source (Return_Node)
+      then
+         return;
+      end if;
+
+      --  Loop through formals
+
+      Form := First_Formal (Scope_Id);
+      while Present (Form) loop
+
+         --  We are only interested in OUT parameters that come from source
+         --  and are never set in the source, and furthermore only in scalars
+         --  since non-scalars generate too many false positives.
+
+         if Ekind (Form) = E_Out_Parameter
+           and then Never_Set_In_Source_Check_Spec (Form)
+           and then Is_Scalar_Type (Etype (Form))
+           and then not Present (Unset_Reference (Form))
+         then
+            --  Before we issue the warning, an add ad hoc defence against the
+            --  most common case of false positives with this warning which is
+            --  the case where there is a Boolean OUT parameter that has been
+            --  set, and whose meaning is "ignore the values of the other
+            --  parameters". We can't of course reliably tell this case at
+            --  compile time, but the following test kills a lot of false
+            --  positives, without generating a significant number of false
+            --  negatives (missed real warnings).
+
+            Form2 := First_Formal (Scope_Id);
+            while Present (Form2) loop
+               if Ekind (Form2) = E_Out_Parameter
+                 and then Root_Type (Etype (Form2)) = Standard_Boolean
+                 and then not Never_Set_In_Source_Check_Spec (Form2)
+               then
+                  return;
+               end if;
+
+               Next_Formal (Form2);
+            end loop;
+
+            --  Here all conditions are met, record possible unset reference
+
+            Set_Unset_Reference (Form, Return_Node);
+         end if;
+
+         Next_Formal (Form);
+      end loop;
+   end Warn_On_Unassigned_Out_Parameter;
+
+   ---------------------------------
+   -- Warn_On_Unreferenced_Entity --
+   ---------------------------------
+
+   procedure Warn_On_Unreferenced_Entity
+     (Spec_E : Entity_Id;
+      Body_E : Entity_Id := Empty)
+   is
+      E : Entity_Id := Spec_E;
+
+   begin
+      if not Referenced_Check_Spec (E)
+        and then not Has_Pragma_Unreferenced_Check_Spec (E)
+        and then not Warnings_Off_Check_Spec (E)
+      then
+         case Ekind (E) is
+            when E_Variable =>
+
+               --  Case of variable that is assigned but not read. We suppress
+               --  the message if the variable is volatile, has an address
+               --  clause, is aliased, or is a renaming, or is imported.
+
+               if Referenced_As_LHS_Check_Spec (E)
+                 and then No (Address_Clause (E))
+                 and then not Is_Volatile (E)
+               then
+                  if Warn_On_Modified_Unread
+                    and then not Is_Imported (E)
+                    and then not Is_Return_Object (E)
+                    and then not Is_Aliased (E)
+                    and then No (Renamed_Object (E))
+                  then
+                     if not Has_Pragma_Unmodified_Check_Spec (E) then
+                        Error_Msg_N
+                          ("?variable & is assigned but never read!", E);
+                     end if;
+
+                     Set_Last_Assignment (E, Empty);
+                  end if;
+
+               --  Normal case of neither assigned nor read (exclude variables
+               --  referenced as out parameters, since we already generated
+               --  appropriate warnings at the call point in this case).
+
+               elsif not Referenced_As_Out_Parameter (E) then
+
+                  --  We suppress the message for types for which a valid
+                  --  pragma Unreferenced_Objects has been given, otherwise
+                  --  we go ahead and give the message.
+
+                  if not Has_Pragma_Unreferenced_Objects (Etype (E)) then
+
+                     --  Distinguish renamed case in message
+
+                     if Present (Renamed_Object (E))
+                       and then Comes_From_Source (Renamed_Object (E))
+                     then
+                        Error_Msg_N
+                          ("?renamed variable & is not referenced!", E);
+                     else
+                        Error_Msg_N
+                          ("?variable & is not referenced!", E);
+                     end if;
+                  end if;
+               end if;
+
+            when E_Constant =>
+               if Present (Renamed_Object (E))
+                 and then Comes_From_Source (Renamed_Object (E))
+               then
+                  Error_Msg_N
+                    ("?renamed constant & is not referenced!", E);
+               else
+                  Error_Msg_N ("?constant & is not referenced!", E);
+               end if;
+
+            when E_In_Parameter     |
+                 E_In_Out_Parameter =>
+
+               --  Do not emit message for formals of a renaming, because
+               --  they are never referenced explicitly.
+
+               if Nkind (Original_Node (Unit_Declaration_Node (Scope (E))))
+                 /= N_Subprogram_Renaming_Declaration
+               then
+                  --  Suppress this message for an IN OUT parameter of a
+                  --  non-scalar type, since it is normal to have only an
+                  --  assignment in such a case.
+
+                  if Ekind (E) = E_In_Parameter
+                    or else not Referenced_As_LHS_Check_Spec (E)
+                    or else Is_Scalar_Type (E)
+                  then
+                     if Present (Body_E) then
+                        E := Body_E;
+                     end if;
+
+                     if not Is_Trivial_Subprogram (Scope (E)) then
+                        Error_Msg_NE
+                          ("?formal parameter & is not referenced!",
+                           E, Spec_E);
+                     end if;
+                  end if;
+               end if;
+
+            when E_Out_Parameter    =>
+               null;
+
+            when E_Named_Integer    |
+                 E_Named_Real       =>
+               Error_Msg_N ("?named number & is not referenced!", E);
+
+            when E_Enumeration_Literal =>
+               Error_Msg_N ("?literal & is not referenced!", E);
+
+            when E_Function         =>
+               Error_Msg_N ("?function & is not referenced!", E);
+
+            when E_Procedure         =>
+               Error_Msg_N ("?procedure & is not referenced!", E);
+
+            when E_Generic_Procedure =>
+               Error_Msg_N
+                 ("?generic procedure & is never instantiated!", E);
+
+            when E_Generic_Function  =>
+               Error_Msg_N
+                 ("?generic function & is never instantiated!", E);
+
+            when Type_Kind          =>
+               Error_Msg_N ("?type & is not referenced!", E);
+
+            when others =>
+               Error_Msg_N ("?& is not referenced!", E);
+         end case;
+
+         --  Kill warnings on the entity on which the message has been posted
+
+         Set_Warnings_Off (E);
+      end if;
+   end Warn_On_Unreferenced_Entity;
+
    --------------------------------
    -- Warn_On_Useless_Assignment --
    --------------------------------
 
    procedure Warn_On_Useless_Assignment
      (Ent : Entity_Id;
-      Loc : Source_Ptr := No_Location)
+      N   : Node_Id := Empty)
    is
-      P : Node_Id;
-      X : Node_Id;
+      P    : Node_Id;
+      X    : Node_Id;
 
       function Check_Ref (N : Node_Id) return Traverse_Result;
       --  Used to instantiate Traverse_Func. Returns Abandon if
@@ -2425,26 +3793,28 @@ package body Sem_Warn is
    --  Start of processing for Warn_On_Useless_Assignment
 
    begin
-      --  Check if this is a case we want to warn on, a variable with the
-      --  last assignment field set, with warnings enabled, and which is
-      --  not imported or exported.
+      --  Check if this is a case we want to warn on, a scalar or access
+      --  variable with the last assignment field set, with warnings enabled,
+      --  and which is not imported or exported. We also check that it is OK
+      --  to capture the value. We are not going to capture any value, but
+      --  the warning messages depends on the same kind of conditions.
 
-      if Ekind (Ent) = E_Variable
+      if Is_Assignable (Ent)
         and then not Is_Return_Object (Ent)
         and then Present (Last_Assignment (Ent))
-        and then not Warnings_Off (Ent)
-        and then not Has_Pragma_Unreferenced (Ent)
         and then not Is_Imported (Ent)
         and then not Is_Exported (Ent)
+        and then Safe_To_Capture_Value (N, Ent)
+        and then not Has_Pragma_Unreferenced_Check_Spec (Ent)
       then
          --  Before we issue the message, check covering exception handlers.
-         --  Search up tree for enclosing statement sequences and handlers
+         --  Search up tree for enclosing statement sequences and handlers.
 
          P := Parent (Last_Assignment (Ent));
          while Present (P) loop
 
-            --  Something is really wrong if we don't find a handled
-            --  statement sequence, so just suppress the warning.
+            --  Something is really wrong if we don't find a handled statement
+            --  sequence, so just suppress the warning.
 
             if No (P) then
                Set_Last_Assignment (Ent, Empty);
@@ -2455,17 +3825,46 @@ package body Sem_Warn is
             elsif Nkind (P) = N_Subprogram_Body
               or else Nkind (P) = N_Package_Body
             then
-               if Loc = No_Location then
-                  Error_Msg_NE
-                    ("?useless assignment to&, value never referenced",
-                     Last_Assignment (Ent), Ent);
+               --  Case of assigned value never referenced
+
+               if No (N) then
+
+                  --  Don't give this for OUT and IN OUT formals, since
+                  --  clearly caller may reference the assigned value. Also
+                  --  never give such warnings for internal variables.
+
+                  if Ekind (Ent) = E_Variable
+                    and then not Is_Internal_Name (Chars (Ent))
+                  then
+                     if Referenced_As_Out_Parameter (Ent) then
+                        Error_Msg_NE
+                          ("?& modified by call, but value never referenced",
+                           Last_Assignment (Ent), Ent);
+                     else
+                        Error_Msg_NE
+                          ("?useless assignment to&, value never referenced!",
+                           Last_Assignment (Ent), Ent);
+                     end if;
+                  end if;
+
+               --  Case of assigned value overwritten
+
                else
-                  Error_Msg_Sloc := Loc;
-                  Error_Msg_NE
-                    ("?useless assignment to&, value overwritten #",
-                     Last_Assignment (Ent), Ent);
+                  Error_Msg_Sloc := Sloc (N);
+
+                  if Referenced_As_Out_Parameter (Ent) then
+                     Error_Msg_NE
+                       ("?& modified by call, but value overwritten #!",
+                        Last_Assignment (Ent), Ent);
+                  else
+                     Error_Msg_NE
+                       ("?useless assignment to&, value overwritten #!",
+                        Last_Assignment (Ent), Ent);
+                  end if;
                end if;
 
+               --  Clear last assignment indication and we are done
+
                Set_Last_Assignment (Ent, Empty);
                return;
 
@@ -2480,7 +3879,7 @@ package body Sem_Warn is
                   --  If we are not at the top level, we regard an inner
                   --  exception handler as a decisive indicator that we should
                   --  not generate the warning, since the variable in question
-                  --  may be acceessed after an exception in the outer block.
+                  --  may be accessed after an exception in the outer block.
 
                   if Nkind (Parent (P)) /= N_Subprogram_Body
                     and then Nkind (Parent (P)) /= N_Package_Body
@@ -2529,4 +3928,24 @@ package body Sem_Warn is
       end if;
    end Warn_On_Useless_Assignments;
 
+   -----------------------------
+   -- Warnings_Off_Check_Spec --
+   -----------------------------
+
+   function Warnings_Off_Check_Spec (E : Entity_Id) return Boolean is
+   begin
+      if Is_Formal (E) and then Present (Spec_Entity (E)) then
+
+         --  Note: use of OR here instead of OR ELSE is deliberate, we want
+         --  to mess with flags on both entities.
+
+         return Has_Warnings_Off (E)
+                  or
+                Has_Warnings_Off (Spec_Entity (E));
+
+      else
+         return Has_Warnings_Off (E);
+      end if;
+   end Warnings_Off_Check_Spec;
+
 end Sem_Warn;