1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1999-2007, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
28 with Atree; use Atree;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Errout; use Errout;
32 with Exp_Code; use Exp_Code;
33 with Fname; use Fname;
35 with Namet; use Namet;
36 with Nlists; use Nlists;
38 with Rtsfind; use Rtsfind;
40 with Sem_Ch8; use Sem_Ch8;
41 with Sem_Eval; use Sem_Eval;
42 with Sem_Util; use Sem_Util;
43 with Sinfo; use Sinfo;
44 with Sinput; use Sinput;
45 with Snames; use Snames;
46 with Stand; use Stand;
47 with Stringt; use Stringt;
49 with Uintp; use Uintp;
51 package body Sem_Warn is
53 -- The following table collects Id's of entities that are potentially
54 -- unreferenced. See Check_Unset_Reference for further details.
55 -- ??? Check_Unset_Reference has zero information about this table.
57 package Unreferenced_Entities is new Table.Table (
58 Table_Component_Type => Entity_Id,
59 Table_Index_Type => Nat,
61 Table_Initial => Alloc.Unreferenced_Entities_Initial,
62 Table_Increment => Alloc.Unreferenced_Entities_Increment,
63 Table_Name => "Unreferenced_Entities");
65 package In_Out_Warnings is new Table.Table (
66 Table_Component_Type => Entity_Id,
67 Table_Index_Type => Nat,
69 Table_Initial => Alloc.In_Out_Warnings_Initial,
70 Table_Increment => Alloc.In_Out_Warnings_Increment,
71 Table_Name => "In_Out_Warnings");
73 -----------------------
74 -- Local Subprograms --
75 -----------------------
77 function Generic_Package_Spec_Entity (E : Entity_Id) return Boolean;
78 -- This returns true if the entity E is declared within a generic package.
79 -- The point of this is to detect variables which are not assigned within
80 -- the generic, but might be assigned outside the package for any given
81 -- instance. These are cases where we leave the warnings to be posted for
82 -- the instance, when we will know more.
84 function Goto_Spec_Entity (E : Entity_Id) return Entity_Id;
85 -- If E is a parameter entity for a subprogram body, then this function
86 -- returns the corresponding spec entity, if not, E is returned unchanged.
88 function Has_Pragma_Unreferenced_Check_Spec (E : Entity_Id) return Boolean;
89 -- Tests Has_Pragma_Unreferenced flag for entity E. If E is not a formal,
90 -- this is simply the setting of the flag Has_Pragma_Unreferenced. If E is
91 -- a body formal, the setting of the flag in the corresponding spec is
92 -- also checked (and True returned if either flag is True).
94 function Never_Set_In_Source_Check_Spec (E : Entity_Id) return Boolean;
95 -- Tests Never_Set_In_Source status for entity E. If E is not a formal,
96 -- this is simply the setting of the flag Never_Set_In_Source. If E is
97 -- a body formal, the setting of the flag in the corresponding spec is
98 -- also checked (and False returned if either flag is False).
100 function Operand_Has_Warnings_Suppressed (N : Node_Id) return Boolean;
101 -- This function traverses the expression tree represented by the node N
102 -- and determines if any sub-operand is a reference to an entity for which
103 -- the Warnings_Off flag is set. True is returned if such an entity is
104 -- encountered, and False otherwise.
106 function Referenced_Check_Spec (E : Entity_Id) return Boolean;
107 -- Tests Referenced status for entity E. If E is not a formal, this is
108 -- simply the setting of the flag Referenced. If E is a body formal, the
109 -- setting of the flag in the corresponding spec is also checked (and True
110 -- returned if either flag is True).
112 function Referenced_As_LHS_Check_Spec (E : Entity_Id) return Boolean;
113 -- Tests Referenced_As_LHS status for entity E. If E is not a formal, this
114 -- is simply the setting of the flag Referenced_As_LHS. If E is a body
115 -- formal, the setting of the flag in the corresponding spec is also
116 -- checked (and True returned if either flag is True).
118 procedure Warn_On_Unreferenced_Entity
120 Body_E : Entity_Id := Empty);
121 -- Output warnings for unreferenced entity E. For the case of an entry
122 -- formal, Body_E is the corresponding body entity for a particular
123 -- accept statement, and the message is posted on Body_E. In all other
124 -- cases, Body_E is ignored and must be Empty.
126 --------------------------
127 -- Check_Code_Statement --
128 --------------------------
130 procedure Check_Code_Statement (N : Node_Id) is
132 -- If volatile, nothing to worry about
134 if Is_Asm_Volatile (N) then
138 -- Warn if no input or no output
140 Setup_Asm_Inputs (N);
142 if No (Asm_Input_Value) then
144 ("?code statement with no inputs should usually be Volatile!", N);
148 Setup_Asm_Outputs (N);
150 if No (Asm_Output_Variable) then
152 ("?code statement with no outputs should usually be Volatile!", N);
156 -- Check multiple code statements in a row
158 if Is_List_Member (N)
159 and then Present (Prev (N))
160 and then Nkind (Prev (N)) = N_Code_Statement
163 ("?code statements in sequence should usually be Volatile!", N);
165 ("\?(suggest using template with multiple instructions)!", N);
167 end Check_Code_Statement;
169 ---------------------------------
170 -- Check_Infinite_Loop_Warning --
171 ---------------------------------
173 -- The case we look for is a while loop which tests a local variable, where
174 -- there is no obvious direct or possible indirect update of the variable
175 -- within the body of the loop.
177 procedure Check_Infinite_Loop_Warning (Loop_Statement : Node_Id) is
178 Iter : constant Node_Id := Iteration_Scheme (Loop_Statement);
180 Ref : Node_Id := Empty;
181 -- Reference in iteration scheme to variable that may not be modified in
182 -- loop, indicating a possible infinite loop.
184 Var : Entity_Id := Empty;
185 -- Corresponding entity (entity of Ref)
187 procedure Find_Var (N : Node_Id);
188 -- Inspect condition to see if it depends on a single entity reference.
189 -- If so, Ref is set to point to the reference node, and Var is set to
190 -- the referenced Entity.
192 function Has_Indirection (T : Entity_Id) return Boolean;
193 -- If the controlling variable is an access type, or is a record type
194 -- with access components, assume that it is changed indirectly and
195 -- suppress the warning. As a concession to low-level programming, in
196 -- particular within Declib, we also suppress warnings on a record
197 -- type that contains components of type Address or Short_Address.
199 function Is_Suspicious_Function_Name (E : Entity_Id) return Boolean;
200 -- Given an entity name, see if the name appears to have something to
201 -- do with I/O or network stuff, and if so, return True. Used to kill
202 -- some false positives on a heuristic basis that such functions will
203 -- likely have some strange side effect dependencies. A rather funny
204 -- kludge, but warning messages are in the heuristics business.
206 function Test_Ref (N : Node_Id) return Traverse_Result;
207 -- Test for reference to variable in question. Returns Abandon if
208 -- matching reference found.
210 function Find_Ref is new Traverse_Func (Test_Ref);
211 -- Function to traverse body of procedure. Returns Abandon if matching
218 procedure Find_Var (N : Node_Id) is
220 -- Condition is a direct variable reference
222 if Is_Entity_Name (N) then
226 -- Case of condition is a comparison with compile time known value
228 elsif Nkind (N) in N_Op_Compare then
229 if Compile_Time_Known_Value (Right_Opnd (N)) then
230 Find_Var (Left_Opnd (N));
232 elsif Compile_Time_Known_Value (Left_Opnd (N)) then
233 Find_Var (Right_Opnd (N));
235 -- Ignore any other comparison
241 -- If condition is a negation, check its operand
243 elsif Nkind (N) = N_Op_Not then
244 Find_Var (Right_Opnd (N));
246 -- Case of condition is function call
248 elsif Nkind (N) = N_Function_Call then
250 -- Forget it if function name is not entity, who knows what
251 -- we might be calling?
253 if not Is_Entity_Name (Name (N)) then
256 -- Forget it if warnings are suppressed on function entity
258 elsif Warnings_Off (Entity (Name (N))) then
261 -- Forget it if function name is suspicious. A strange test
262 -- but warning generation is in the heuristics business!
264 elsif Is_Suspicious_Function_Name (Entity (Name (N))) then
268 -- OK, see if we have one argument
271 PA : constant List_Id := Parameter_Associations (N);
274 -- One argument, so check the argument
277 and then List_Length (PA) = 1
279 if Nkind (First (PA)) = N_Parameter_Association then
280 Find_Var (Explicit_Actual_Parameter (First (PA)));
282 Find_Var (First (PA));
292 -- Any other kind of node is not something we warn for
299 ---------------------
300 -- Has_Indirection --
301 ---------------------
303 function Has_Indirection (T : Entity_Id) return Boolean is
308 if Is_Access_Type (T) then
311 elsif Is_Private_Type (T)
312 and then Present (Full_View (T))
313 and then Is_Access_Type (Full_View (T))
317 elsif Is_Record_Type (T) then
320 elsif Is_Private_Type (T)
321 and then Present (Full_View (T))
322 and then Is_Record_Type (Full_View (T))
324 Rec := Full_View (T);
329 Comp := First_Component (Rec);
330 while Present (Comp) loop
331 if Is_Access_Type (Etype (Comp))
332 or else Is_Descendent_Of_Address (Etype (Comp))
337 Next_Component (Comp);
343 ---------------------------------
344 -- Is_Suspicious_Function_Name --
345 ---------------------------------
347 function Is_Suspicious_Function_Name (E : Entity_Id) return Boolean is
350 function Substring_Present (S : String) return Boolean;
351 -- Returns True if name buffer has given string delimited by non-
352 -- alphabetic characters or by end of string. S is lower case.
354 -----------------------
355 -- Substring_Present --
356 -----------------------
358 function Substring_Present (S : String) return Boolean is
359 Len : constant Natural := S'Length;
362 for J in 1 .. Name_Len - (Len - 1) loop
363 if Name_Buffer (J .. J + (Len - 1)) = S
366 or else Name_Buffer (J - 1) not in 'a' .. 'z')
369 or else Name_Buffer (J + Len) not in 'a' .. 'z')
376 end Substring_Present;
378 -- Start of processing for Is_Suspicious_Function_Name
382 while Present (S) and then S /= Standard_Standard loop
383 Get_Name_String (Chars (S));
385 if Substring_Present ("io")
386 or else Substring_Present ("file")
387 or else Substring_Present ("network")
396 end Is_Suspicious_Function_Name;
402 function Test_Ref (N : Node_Id) return Traverse_Result is
404 -- Waste of time to look at iteration scheme
409 -- Direct reference to variable in question
411 elsif Is_Entity_Name (N)
412 and then Present (Entity (N))
413 and then Entity (N) = Var
415 -- If this is an Lvalue, then definitely abandon, since
416 -- this could be a direct modification of the variable.
418 if May_Be_Lvalue (N) then
422 -- If we appear in the context of a procedure call, then also
423 -- abandon, since there may be issues of non-visible side
424 -- effects going on in the call.
432 exit when P = Loop_Statement;
434 if Nkind (P) = N_Procedure_Call_Statement then
440 -- Reference to variable renaming variable in question
442 elsif Is_Entity_Name (N)
443 and then Present (Entity (N))
444 and then Ekind (Entity (N)) = E_Variable
445 and then Present (Renamed_Object (Entity (N)))
446 and then Is_Entity_Name (Renamed_Object (Entity (N)))
447 and then Entity (Renamed_Object (Entity (N))) = Var
448 and then May_Be_Lvalue (N)
452 -- Call to subprogram
454 elsif Nkind (N) = N_Procedure_Call_Statement
455 or else Nkind (N) = N_Function_Call
457 -- If subprogram is within the scope of the entity we are dealing
458 -- with as the loop variable, then it could modify this parameter,
459 -- so we abandon in this case. In the case of a subprogram that is
460 -- not an entity we also abandon. The check for no entity being
461 -- present is a defense against previous errors.
463 if not Is_Entity_Name (Name (N))
464 or else No (Entity (Name (N)))
465 or else Scope_Within (Entity (Name (N)), Scope (Var))
471 -- All OK, continue scan
476 -- Start of processing for Check_Infinite_Loop_Warning
479 -- We need a while iteration with no condition actions. Conditions
480 -- actions just make things too complicated to get the warning right.
483 or else No (Condition (Iter))
484 or else Present (Condition_Actions (Iter))
485 or else Debug_Flag_Dot_W
490 -- Initial conditions met, see if condition is of right form
492 Find_Var (Condition (Iter));
494 -- Nothing to do if local variable from source not found
497 or else Ekind (Var) /= E_Variable
498 or else Is_Library_Level_Entity (Var)
499 or else not Comes_From_Source (Var)
503 -- Nothing to do if there is some indirection involved (assume that the
504 -- designated variable might be modified in some way we don't see).
506 elsif Has_Indirection (Etype (Var)) then
509 -- Same sort of thing for volatile variable, might be modified by
510 -- some other task or by the operating system in some way.
512 elsif Is_Volatile (Var) then
516 -- Filter out case of original statement sequence starting with delay.
517 -- We assume this is a multi-tasking program and that the condition
518 -- is affected by other threads (some kind of busy wait).
521 Fstm : constant Node_Id :=
522 Original_Node (First (Statements (Loop_Statement)));
524 if Nkind (Fstm) = N_Delay_Relative_Statement
525 or else Nkind (Fstm) = N_Delay_Until_Statement
531 -- We have a variable reference of the right form, now we scan the loop
532 -- body to see if it looks like it might not be modified
534 if Find_Ref (Loop_Statement) = OK then
536 ("?variable& is not modified in loop body!", Ref, Var);
538 ("\?possible infinite loop!", Ref);
540 end Check_Infinite_Loop_Warning;
542 ----------------------
543 -- Check_References --
544 ----------------------
546 procedure Check_References (E : Entity_Id; Anod : Node_Id := Empty) is
552 Accept_Statement : Node_Id) return Entity_Id;
553 -- For an entry formal entity from an entry declaration, find the
554 -- corrsesponding body formal from the given accept statement.
556 function Missing_Subunits return Boolean;
557 -- We suppress warnings when there are missing subunits, because this
558 -- may generate too many false positives: entities in a parent may only
559 -- be referenced in one of the subunits. We make an exception for
560 -- subunits that contain no other stubs.
562 procedure Output_Reference_Error (M : String);
563 -- Used to output an error message. Deals with posting the error on the
564 -- body formal in the accept case.
566 function Publicly_Referenceable (Ent : Entity_Id) return Boolean;
567 -- This is true if the entity in question is potentially referenceable
568 -- from another unit. This is true for entities in packages that are at
569 -- the library level.
571 ----------------------
572 -- Missing_Subunits --
573 ----------------------
575 function Missing_Subunits return Boolean is
579 if not Unloaded_Subunits then
581 -- Normal compilation, all subunits are present
585 elsif E /= Main_Unit_Entity then
587 -- No warnings on a stub that is not the main unit
591 elsif Nkind (Unit_Declaration_Node (E)) in N_Proper_Body then
592 D := First (Declarations (Unit_Declaration_Node (E)));
593 while Present (D) loop
595 -- No warnings if the proper body contains nested stubs
597 if Nkind (D) in N_Body_Stub then
607 -- Missing stubs elsewhere
611 end Missing_Subunits;
619 Accept_Statement : Node_Id) return Entity_Id
621 Body_Param : Node_Id;
625 -- Loop to find matching parameter in accept statement
627 Body_Param := First (Parameter_Specifications (Accept_Statement));
628 while Present (Body_Param) loop
629 Body_E := Defining_Identifier (Body_Param);
631 if Chars (Body_E) = Chars (E) then
638 -- Should never fall through, should always find a match
643 ----------------------------
644 -- Output_Reference_Error --
645 ----------------------------
647 procedure Output_Reference_Error (M : String) is
649 -- Don't output message for IN OUT formal unless we have the warning
650 -- flag specifically set. It is a bit odd to distinguish IN OUT
651 -- formals from other cases. This distinction is historical in
652 -- nature. Warnings for IN OUT formals were added fairly late.
654 if Ekind (E1) = E_In_Out_Parameter
655 and then not Check_Unreferenced_Formals
660 -- Other than accept case, post error on defining identifier
665 -- Accept case, find body formal to post the message
668 Error_Msg_NE (M, Body_Formal (E1, Accept_Statement => Anod), E1);
671 end Output_Reference_Error;
673 ----------------------------
674 -- Publicly_Referenceable --
675 ----------------------------
677 function Publicly_Referenceable (Ent : Entity_Id) return Boolean is
682 -- A formal parameter is never referenceable outside the body of its
683 -- subprogram or entry.
685 if Is_Formal (Ent) then
689 -- Examine parents to look for a library level package spec. But if
690 -- we find a body or block or other similar construct along the way,
691 -- we cannot be referenced.
698 -- If we get to top of tree, then publicly referenceable
703 -- If we reach a generic package declaration, then always
704 -- consider this referenceable, since any instantiation will
705 -- have access to the entities in the generic package. Note
706 -- that the package itself may not be instantiated, but then
707 -- we will get a warning for the package entity.
709 -- Note that generic formal parameters are themselves not
710 -- publicly referenceable in an instance, and warnings on them
713 when N_Generic_Package_Declaration =>
715 not Is_List_Member (Prev)
716 or else List_Containing (Prev)
717 /= Generic_Formal_Declarations (P);
719 -- Similarly, the generic formals of a generic subprogram are
722 when N_Generic_Subprogram_Declaration =>
723 if Is_List_Member (Prev)
724 and then List_Containing (Prev) =
725 Generic_Formal_Declarations (P)
732 -- If we reach a subprogram body, entity is not referenceable
733 -- unless it is the defining entity of the body. This will
734 -- happen, e.g. when a function is an attribute renaming that
735 -- is rewritten as a body.
737 when N_Subprogram_Body =>
738 if Ent /= Defining_Entity (P) then
744 -- If we reach any other body, definitely not referenceable
746 when N_Package_Body |
754 -- For all other cases, keep looking up tree
761 end Publicly_Referenceable;
763 -- Start of processing for Check_References
766 -- No messages if warnings are suppressed, or if we have detected any
767 -- real errors so far (this last check avoids junk messages resulting
768 -- from errors, e.g. a subunit that is not loaded).
770 if Warning_Mode = Suppress
771 or else Serious_Errors_Detected /= 0
776 -- We also skip the messages if any subunits were not loaded (see
777 -- comment in Sem_Ch10 to understand how this is set, and why it is
778 -- necessary to suppress the warnings in this case).
780 if Missing_Subunits then
784 -- Otherwise loop through entities, looking for suspicious stuff
786 E1 := First_Entity (E);
787 while Present (E1) loop
789 -- We only look at source entities with warning flag on. We also
790 -- ignore objects whose type or base type has warnings suppressed.
792 if Comes_From_Source (E1)
793 and then not Warnings_Off (E1)
794 and then not Warnings_Off (Etype (E1))
795 and then not Warnings_Off (Base_Type (Etype (E1)))
797 -- We are interested in variables and out/in-out parameters, but
798 -- we exclude protected types, too complicated to worry about.
800 if Ekind (E1) = E_Variable
802 ((Ekind (E1) = E_Out_Parameter
803 or else Ekind (E1) = E_In_Out_Parameter)
804 and then not Is_Protected_Type (Current_Scope))
806 -- Case of an unassigned variable
808 -- First gather any Unset_Reference indication for E1. In the
809 -- case of a parameter, it is the Spec_Entity that is relevant.
811 if Ekind (E1) = E_Out_Parameter
812 and then Present (Spec_Entity (E1))
814 UR := Unset_Reference (Spec_Entity (E1));
816 UR := Unset_Reference (E1);
819 -- If the entity is an out parameter of the current subprogram
820 -- body, check the warning status of the parameter in the spec.
823 and then Present (Spec_Entity (E1))
824 and then Warnings_Off (Spec_Entity (E1))
829 and then Is_Access_Type (Etype (E1))
831 -- For access types, the only time we made a UR entry was
832 -- for a dereference, and so we post the appropriate warning
833 -- here (note that the dereference may not be explicit in
834 -- the source, for example in the case of a dispatching call
835 -- with an anonymous access controlling formal, or of an
836 -- assignment of a pointer involving discriminant check
837 -- on the designated object).
839 Error_Msg_NE ("?& may be null!", UR, E1);
842 -- Case of variable that could be a constant. Note that we
843 -- never signal such messages for generic package entities,
844 -- since a given instance could have modifications outside
847 elsif Warn_On_Constant
848 and then ((Ekind (E1) = E_Variable
849 and then Has_Initial_Value (E1))
851 Ekind (E1) = E_In_Out_Parameter)
852 and then Never_Set_In_Source_Check_Spec (E1)
853 and then not Address_Taken (E1)
854 and then not Generic_Package_Spec_Entity (E1)
856 -- A special case, if this variable is volatile and not
857 -- imported, it is not helpful to tell the programmer
858 -- to mark the variable as constant, since this would be
859 -- illegal by virtue of RM C.6(13).
861 if (Is_Volatile (E1) or else Has_Volatile_Components (E1))
862 and then not Is_Imported (E1)
865 ("?& is not modified, volatile has no effect!", E1);
867 -- Another special case, Exception_Occurrence, this catches
868 -- the case of exception choice (and a bit more too, but not
869 -- worth doing more investigation here).
871 elsif Is_RTE (Etype (E1), RE_Exception_Occurrence) then
874 -- Here we give the warning if referenced and no pragma
875 -- Unreferenced is present.
878 if Ekind (E1) = E_Variable then
879 if Referenced_Check_Spec (E1)
880 and then not Has_Pragma_Unreferenced_Check_Spec (E1)
883 ("?& is not modified, "
884 & "could be declared constant!",
888 else pragma Assert (Ekind (E1) = E_In_Out_Parameter);
889 if Referenced_Check_Spec (E1)
891 not Has_Pragma_Unreferenced_Check_Spec (E1)
893 -- Suppress warning if private type, since in this
894 -- case it may be quite reasonable for the logical
895 -- view to be in out, even if the implementation
896 -- ends up using access types.
898 if Has_Private_Declaration (Etype (E1)) then
901 -- Suppress warning for any composite type, since
902 -- for composites it seems quite reasonable to pass
903 -- a value of the composite type and then modify
906 elsif Is_Composite_Type (Etype (E1)) then
909 -- Suppress warning for parameter of dispatching
910 -- operation, since it is quite reasonable to have
911 -- an operation that is overridden, and for some
912 -- subclasses needs to be IN OUT and for others
913 -- the parameter does not happen to be assigned.
915 elsif Is_Dispatching_Operation
916 (Scope (Goto_Spec_Entity (E1)))
920 -- OK, looks like warning for an IN OUT parameter
921 -- that could be IN makes sense, but we delay the
922 -- output of the warning, pending possibly finding
923 -- out later on that the associated subprogram is
924 -- used as a generic actual, or its address/access
925 -- is taken. In these two cases, we suppress the
926 -- warning because the context may force use of IN
927 -- OUT, even if in this particular case the formal
931 In_Out_Warnings.Append (E1);
937 -- Other cases of a variable never set in source
939 elsif Never_Set_In_Source_Check_Spec (E1)
941 -- No warning if warning for this case turned off
943 and then Warn_On_No_Value_Assigned
945 -- No warning if address taken somewhere
947 and then not Address_Taken (E1)
949 -- No warning if explicit initial value
951 and then not Has_Initial_Value (E1)
953 -- No warning for generic package spec entities, since we
954 -- might set them in a child unit or something like that
956 and then not Generic_Package_Spec_Entity (E1)
958 -- No warning if fully initialized type, except that for
959 -- this purpose we do not consider access types to qualify
960 -- as fully initialized types (relying on an access type
961 -- variable being null when it is never set is a bit odd!)
963 -- Also we generate warning for an out parameter that is
964 -- never referenced, since again it seems odd to rely on
965 -- default initialization to set an out parameter value.
967 and then (Is_Access_Type (Etype (E1))
968 or else Ekind (E1) = E_Out_Parameter
969 or else not Is_Fully_Initialized_Type (Etype (E1)))
971 -- Do not output complaint about never being assigned a
972 -- value if a pragma Unreferenced applies to the variable
973 -- we are examining, or if it is a parameter, if there is
974 -- a pragma Unreferenced for the corresponding spec.
976 if Has_Pragma_Unreferenced_Check_Spec (E1)
977 or else Has_Pragma_Unreferenced_Objects (Etype (E1))
981 -- Case of unreferenced formal
983 elsif Is_Formal (E1) then
984 if Referenced_Check_Spec (E1) then
985 Output_Reference_Error
986 ("?formal parameter& is read but never assigned!");
988 Output_Reference_Error
989 ("?formal parameter& is not referenced!");
995 if Referenced (E1) then
996 Output_Reference_Error
997 ("?variable& is read but never assigned!");
999 Output_Reference_Error
1000 ("?variable& is never read and never assigned!");
1003 -- Deal with special case where this variable is
1004 -- hidden by a loop variable
1006 if Ekind (E1) = E_Variable
1007 and then Present (Hiding_Loop_Variable (E1))
1010 ("?for loop implicitly declares loop variable!",
1011 Hiding_Loop_Variable (E1));
1013 Error_Msg_Sloc := Sloc (E1);
1015 ("\?declaration hides & declared#!",
1016 Hiding_Loop_Variable (E1));
1023 -- Check for unset reference
1025 if Warn_On_No_Value_Assigned and then Present (UR) then
1027 -- For other than access type, go back to original node to
1028 -- deal with case where original unset reference has been
1029 -- rewritten during expansion.
1031 -- In some cases, the original node may be a type conversion
1032 -- or qualification, and in this case we want the object
1035 UR := Original_Node (UR);
1036 while Nkind (UR) = N_Type_Conversion
1037 or else Nkind (UR) = N_Qualified_Expression
1039 UR := Expression (UR);
1042 -- Here we issue the warning, all checks completed
1044 -- If we have a return statement, this was a case of an OUT
1045 -- parameter not being set at the time of the return. (Note:
1046 -- it can't be N_Extended_Return_Statement, because those
1047 -- are only for functions, and functions do not allow OUT
1050 if Nkind (UR) = N_Simple_Return_Statement then
1052 ("?OUT parameter& not set before return", UR, E1);
1054 -- If the unset reference is prefix of a selected component
1055 -- that comes from source, mention the component as well. If
1056 -- the selected component comes from expansion, all we know
1057 -- is that the entity is not fully initialized at the point
1058 -- of the reference. Locate an unintialized component to get
1059 -- a better error message.
1061 elsif Nkind (Parent (UR)) = N_Selected_Component then
1062 Error_Msg_Node_2 := Selector_Name (Parent (UR));
1064 if not Comes_From_Source (Parent (UR)) then
1069 Comp := First_Entity (Etype (E1));
1070 while Present (Comp) loop
1071 if Ekind (Comp) = E_Component
1072 and then Nkind (Parent (Comp)) =
1073 N_Component_Declaration
1074 and then No (Expression (Parent (Comp)))
1076 Error_Msg_Node_2 := Comp;
1085 -- Issue proper warning. This is a case of referencing
1086 -- a variable before it has been explicitly assigned.
1087 -- For access types, UR was only set for dereferences,
1088 -- so the issue is that the value may be null.
1090 if Is_Access_Type (Etype (Parent (UR))) then
1091 Error_Msg_N ("?`&.&` may be null!", UR);
1094 ("?`&.&` may be referenced before it has a value!",
1098 -- All other cases of unset reference active
1102 ("?& may be referenced before it has a value!",
1110 -- Then check for unreferenced entities. Note that we are only
1111 -- interested in entities which do not have the Referenced flag
1112 -- set. The Referenced_As_LHS flag is interesting only if the
1113 -- Referenced flag is not set.
1115 if not Referenced_Check_Spec (E1)
1117 -- Check that warnings on unreferenced entities are enabled
1119 and then ((Check_Unreferenced and then not Is_Formal (E1))
1121 (Check_Unreferenced_Formals and then Is_Formal (E1))
1123 (Warn_On_Modified_Unread
1124 and then Referenced_As_LHS_Check_Spec (E1)))
1126 -- Labels, and enumeration literals, and exceptions. The
1127 -- warnings are also placed on local packages that cannot be
1128 -- referenced from elsewhere, including those declared within a
1131 and then (Is_Object (E1)
1135 Ekind (E1) = E_Label
1137 Ekind (E1) = E_Exception
1139 Ekind (E1) = E_Named_Integer
1141 Ekind (E1) = E_Named_Real
1143 Is_Overloadable (E1)
1145 (Ekind (E1) = E_Package
1147 (Ekind (E) = E_Function
1148 or else Ekind (E) = E_Package_Body
1149 or else Ekind (E) = E_Procedure
1150 or else Ekind (E) = E_Subprogram_Body
1151 or else Ekind (E) = E_Block)))
1153 -- Exclude instantiations, since there is no reason why every
1154 -- entity in an instantiation should be referenced.
1156 and then Instantiation_Location (Sloc (E1)) = No_Location
1158 -- Exclude formal parameters from bodies if the corresponding
1159 -- spec entity has been referenced in the case where there is
1162 and then not (Is_Formal (E1)
1164 Ekind (Scope (E1)) = E_Subprogram_Body
1166 Present (Spec_Entity (E1))
1168 Referenced (Spec_Entity (E1)))
1170 -- Consider private type referenced if full view is referenced
1171 -- If there is not full view, this is a generic type on which
1172 -- warnings are also useful.
1175 not (Is_Private_Type (E1)
1177 Present (Full_View (E1))
1178 and then Referenced (Full_View (E1)))
1180 -- Don't worry about full view, only about private type
1182 and then not Has_Private_Declaration (E1)
1184 -- Eliminate dispatching operations from consideration, we
1185 -- cannot tell if these are referenced or not in any easy
1186 -- manner (note this also catches Adjust/Finalize/Initialize)
1188 and then not Is_Dispatching_Operation (E1)
1190 -- Check entity that can be publicly referenced (we do not give
1191 -- messages for such entities, since there could be other
1192 -- units, not involved in this compilation, that contain
1193 -- relevant references.
1195 and then not Publicly_Referenceable (E1)
1197 -- Class wide types are marked as source entities, but they are
1198 -- not really source entities, and are always created, so we do
1199 -- not care if they are not referenced.
1201 and then Ekind (E1) /= E_Class_Wide_Type
1203 -- Objects other than parameters of task types are allowed to
1204 -- be non-referenced, since they start up tasks!
1206 and then ((Ekind (E1) /= E_Variable
1207 and then Ekind (E1) /= E_Constant
1208 and then Ekind (E1) /= E_Component)
1209 or else not Is_Task_Type (Etype (E1)))
1211 -- For subunits, only place warnings on the main unit itself,
1212 -- since parent units are not completely compiled
1214 and then (Nkind (Unit (Cunit (Main_Unit))) /= N_Subunit
1216 Get_Source_Unit (E1) = Main_Unit)
1218 -- No warning on a return object, because these are often
1219 -- created with a single expression and an implicit return.
1220 -- If the object is a variable there will be a warning
1221 -- indicating that it could be declared constant.
1224 (Ekind (E1) = E_Constant and then Is_Return_Object (E1))
1226 -- Suppress warnings in internal units if not in -gnatg mode
1227 -- (these would be junk warnings for an applications program,
1228 -- since they refer to problems in internal units)
1232 Is_Internal_File_Name
1233 (Unit_File_Name (Get_Source_Unit (E1)))
1235 -- We do not immediately flag the error. This is because we
1236 -- have not expanded generic bodies yet, and they may have
1237 -- the missing reference. So instead we park the entity on a
1238 -- list, for later processing. However for the case of an
1239 -- accept statement we want to output messages now, since
1240 -- we know we already have all information at hand, and we
1241 -- also want to have separate warnings for each accept
1242 -- statement for the same entry.
1244 if Present (Anod) then
1245 pragma Assert (Is_Formal (E1));
1247 -- The unreferenced entity is E1, but post the warning
1248 -- on the body entity for this accept statement.
1250 Warn_On_Unreferenced_Entity
1251 (E1, Body_Formal (E1, Accept_Statement => Anod));
1254 Unreferenced_Entities.Append (E1);
1258 -- Generic units are referenced in the generic body, but if they
1259 -- are not public and never instantiated we want to force a
1260 -- warning on them. We treat them as redundant constructs to
1263 elsif Is_Generic_Subprogram (E1)
1264 and then not Is_Instantiated (E1)
1265 and then not Publicly_Referenceable (E1)
1266 and then Instantiation_Depth (Sloc (E1)) = 0
1267 and then Warn_On_Redundant_Constructs
1269 Unreferenced_Entities.Append (E1);
1271 -- Force warning on entity
1273 Set_Referenced (E1, False);
1277 -- Recurse into nested package or block. Do not recurse into a
1278 -- formal package, because the correponding body is not analyzed.
1281 if ((Ekind (E1) = E_Package or else Ekind (E1) = E_Generic_Package)
1282 and then Nkind (Parent (E1)) = N_Package_Specification
1284 Nkind (Original_Node (Unit_Declaration_Node (E1)))
1285 /= N_Formal_Package_Declaration)
1287 or else Ekind (E1) = E_Block
1289 Check_References (E1);
1294 end Check_References;
1296 ---------------------------
1297 -- Check_Unset_Reference --
1298 ---------------------------
1300 procedure Check_Unset_Reference (N : Node_Id) is
1301 Typ : constant Entity_Id := Etype (N);
1303 function Is_OK_Fully_Initialized return Boolean;
1304 -- This function returns true if the given node N is fully initialized
1305 -- so that the reference is safe as far as this routine is concerned.
1306 -- Safe generally means that the type of N is a fully initialized type.
1307 -- The one special case is that for access types, which are always fully
1308 -- initialized, we don't consider a dereference OK since it will surely
1309 -- be dereferencing a null value, which won't do.
1311 function Prefix_Has_Dereference (Pref : Node_Id) return Boolean;
1312 -- Used to test indexed or selected component or slice to see if the
1313 -- evaluation of the prefix depends on a dereference, and if so, returns
1314 -- True, in which case we always check the prefix, even if we know that
1315 -- the referenced component is initialized. Pref is the prefix to test.
1317 -----------------------------
1318 -- Is_OK_Fully_Initialized --
1319 -----------------------------
1321 function Is_OK_Fully_Initialized return Boolean is
1323 if Is_Access_Type (Typ) and then Is_Dereferenced (N) then
1326 return Is_Fully_Initialized_Type (Typ);
1328 end Is_OK_Fully_Initialized;
1330 ----------------------------
1331 -- Prefix_Has_Dereference --
1332 ----------------------------
1334 function Prefix_Has_Dereference (Pref : Node_Id) return Boolean is
1336 -- If prefix is of an access type, certainly need a dereference
1338 if Is_Access_Type (Etype (Pref)) then
1341 -- If prefix is explicit dereference, that's a dereference for sure
1343 elsif Nkind (Pref) = N_Explicit_Dereference then
1346 -- If prefix is itself a component reference or slice check prefix
1348 elsif Nkind (Pref) = N_Slice
1349 or else Nkind (Pref) = N_Indexed_Component
1350 or else Nkind (Pref) = N_Selected_Component
1352 return Prefix_Has_Dereference (Prefix (Pref));
1354 -- All other cases do not involve a dereference
1359 end Prefix_Has_Dereference;
1361 -- Start of processing for Check_Unset_Reference
1364 -- Nothing to do if warnings suppressed
1366 if Warning_Mode = Suppress then
1370 -- Ignore reference unless it comes from source. Almost always if we
1371 -- have a reference from generated code, it is bogus (e.g. calls to init
1372 -- procs to set default discriminant values).
1374 if not Comes_From_Source (N) then
1378 -- Otherwise see what kind of node we have. If the entity already
1379 -- has an unset reference, it is not necessarily the earliest in
1380 -- the text, because resolution of the prefix of selected components
1381 -- is completed before the resolution of the selected component itself.
1382 -- as a result, given (R /= null and then R.X > 0), the occurrences
1383 -- of R are examined in right-to-left order. If there is already an
1384 -- unset reference, we check whether N is earlier before proceeding.
1388 -- For identifier or exanded name, examine the entity involved
1390 when N_Identifier | N_Expanded_Name =>
1392 E : constant Entity_Id := Entity (N);
1395 if (Ekind (E) = E_Variable
1397 Ekind (E) = E_Out_Parameter)
1398 and then Never_Set_In_Source_Check_Spec (E)
1399 and then not Has_Initial_Value (E)
1400 and then (No (Unset_Reference (E))
1402 Earlier_In_Extended_Unit
1403 (Sloc (N), Sloc (Unset_Reference (E))))
1404 and then not Warnings_Off (E)
1406 -- We may have an unset reference. The first test is whether
1407 -- this is an access to a discriminant of a record or a
1408 -- component with default initialization. Both of these
1409 -- cases can be ignored, since the actual object that is
1410 -- referenced is definitely initialized. Note that this
1411 -- covers the case of reading discriminants of an out
1412 -- parameter, which is OK even in Ada 83.
1414 -- Note that we are only interested in a direct reference to
1415 -- a record component here. If the reference is via an
1416 -- access type, then the access object is being referenced,
1417 -- not the record, and still deserves an unset reference.
1419 if Nkind (Parent (N)) = N_Selected_Component
1420 and not Is_Access_Type (Typ)
1423 ES : constant Entity_Id :=
1424 Entity (Selector_Name (Parent (N)));
1426 if Ekind (ES) = E_Discriminant
1428 (Present (Declaration_Node (ES))
1430 Present (Expression (Declaration_Node (ES))))
1437 -- Exclude fully initialized types
1439 if Is_OK_Fully_Initialized then
1443 -- Here we have a potential unset reference. But before we
1444 -- get worried about it, we have to make sure that the
1445 -- entity declaration is in the same procedure as the
1446 -- reference, since if they are in separate procedures, then
1447 -- we have no idea about sequential execution.
1449 -- The tests in the loop below catch all such cases, but do
1450 -- allow the reference to appear in a loop, block, or
1451 -- package spec that is nested within the declaring scope.
1452 -- As always, it is possible to construct cases where the
1453 -- warning is wrong, that is why it is a warning!
1457 SE : constant Entity_Id := Scope (E);
1460 SR := Current_Scope;
1462 if SR = Standard_Standard
1463 or else Is_Subprogram (SR)
1464 or else Is_Concurrent_Body (SR)
1465 or else Is_Concurrent_Type (SR)
1473 -- Case of reference has an access type. This is special
1474 -- case since access types are always set to null so
1475 -- cannot be truly uninitialized, but we still want to
1476 -- warn about cases of obvious null dereference.
1478 if Is_Access_Type (Typ) then
1479 Access_Type_Case : declare
1483 (N : Node_Id) return Traverse_Result;
1484 -- Process function for instantation of Traverse
1485 -- below. Checks if N contains reference to other
1486 -- than a dereference.
1488 function Ref_In (Nod : Node_Id) return Boolean;
1489 -- Determines whether Nod contains a reference to
1490 -- the entity E that is not a dereference.
1497 (N : Node_Id) return Traverse_Result
1500 if Is_Entity_Name (N)
1501 and then Entity (N) = E
1502 and then not Is_Dereferenced (N)
1514 function Ref_In (Nod : Node_Id) return Boolean is
1515 function Traverse is new Traverse_Func (Process);
1517 return Traverse (Nod) = Abandon;
1520 -- Start of processing for Access_Type_Case
1523 -- Don't bother if we are inside an instance, since
1524 -- the compilation of the generic template is where
1525 -- the warning should be issued.
1531 -- Don't bother if this is not the main unit. If we
1532 -- try to give this warning for with'ed units, we
1533 -- get some false positives, since we do not record
1534 -- references in other units.
1536 if not In_Extended_Main_Source_Unit (E)
1538 not In_Extended_Main_Source_Unit (N)
1543 -- We are only interested in dereferences
1545 if not Is_Dereferenced (N) then
1549 -- One more check, don't bother with references
1550 -- that are inside conditional statements or while
1551 -- loops if the condition references the entity in
1552 -- question. This avoids most false positives.
1559 if (Nkind (P) = N_If_Statement
1561 Nkind (P) = N_Elsif_Part)
1562 and then Ref_In (Condition (P))
1566 elsif Nkind (P) = N_Loop_Statement
1567 and then Present (Iteration_Scheme (P))
1569 Ref_In (Condition (Iteration_Scheme (P)))
1574 end Access_Type_Case;
1577 -- Here we definitely have a case for giving a warning
1578 -- for a reference to an unset value. But we don't give
1579 -- the warning now. Instead we set the Unset_Reference
1580 -- field of the identifier involved. The reason for this
1581 -- is that if we find the variable is never ever assigned
1582 -- a value then that warning is more important and there
1583 -- is no point in giving the reference warning.
1585 -- If this is an identifier, set the field directly
1587 if Nkind (N) = N_Identifier then
1588 Set_Unset_Reference (E, N);
1590 -- Otherwise it is an expanded name, so set the field of
1591 -- the actual identifier for the reference.
1594 Set_Unset_Reference (E, Selector_Name (N));
1600 -- Indexed component or slice
1602 when N_Indexed_Component | N_Slice =>
1604 -- If prefix does not involve dereferencing an access type, then
1605 -- we know we are OK if the component type is fully initialized,
1606 -- since the component will have been set as part of the default
1609 if not Prefix_Has_Dereference (Prefix (N))
1610 and then Is_OK_Fully_Initialized
1614 -- Look at prefix in access type case, or if the component is not
1615 -- fully initialized.
1618 Check_Unset_Reference (Prefix (N));
1623 when N_Selected_Component =>
1625 Pref : constant Node_Id := Prefix (N);
1626 Ent : constant Entity_Id := Entity (Selector_Name (N));
1629 -- If prefix involves dereferencing an access type, always
1630 -- check the prefix, since the issue then is whether this
1631 -- access value is null.
1633 if Prefix_Has_Dereference (Pref) then
1636 -- Always go to prefix if no selector entity is set. Can this
1637 -- happen in the normal case? Not clear, but it definitely can
1638 -- happen in error cases.
1643 -- For a record component, check some cases where we have
1644 -- reasonable cause to consider that the component is known to
1645 -- be or probably is initialized. In this case, we don't care
1646 -- if the prefix itself was explicitly initialized.
1648 -- Discriminants are always considered initialized
1650 elsif Ekind (Ent) = E_Discriminant then
1653 -- An explicitly initialized component is certainly initialized
1655 elsif Nkind (Parent (Ent)) = N_Component_Declaration
1656 and then Present (Expression (Parent (Ent)))
1660 -- A fully initialized component is initialized
1662 elsif Is_OK_Fully_Initialized then
1666 -- If none of those cases apply, check the record type prefix
1668 Check_Unset_Reference (Pref);
1671 -- For type conversions or qualifications examine the expression
1673 when N_Type_Conversion | N_Qualified_Expression =>
1674 Check_Unset_Reference (Expression (N));
1676 -- For explicit dereference, always check prefix, which will generate
1677 -- an unset reference (since this is a case of dereferencing null).
1679 when N_Explicit_Dereference =>
1680 Check_Unset_Reference (Prefix (N));
1682 -- All other cases are not cases of an unset reference
1688 end Check_Unset_Reference;
1690 ------------------------
1691 -- Check_Unused_Withs --
1692 ------------------------
1694 procedure Check_Unused_Withs (Spec_Unit : Unit_Number_Type := No_Unit) is
1700 Munite : constant Entity_Id := Cunit_Entity (Main_Unit);
1701 -- This is needed for checking the special renaming case
1703 procedure Check_One_Unit (Unit : Unit_Number_Type);
1704 -- Subsidiary procedure, performs checks for specified unit
1706 --------------------
1707 -- Check_One_Unit --
1708 --------------------
1710 procedure Check_One_Unit (Unit : Unit_Number_Type) is
1711 Is_Visible_Renaming : Boolean := False;
1714 procedure Check_Inner_Package (Pack : Entity_Id);
1715 -- Pack is a package local to a unit in a with_clause. Both the
1716 -- unit and Pack are referenced. If none of the entities in Pack
1717 -- are referenced, then the only occurrence of Pack is in a use
1718 -- clause or a pragma, and a warning is worthwhile as well.
1720 function Check_System_Aux return Boolean;
1721 -- Before giving a warning on a with_clause for System, check
1722 -- whether a system extension is present.
1724 function Find_Package_Renaming
1726 L : Entity_Id) return Entity_Id;
1727 -- The only reference to a context unit may be in a renaming
1728 -- declaration. If this renaming declares a visible entity, do
1729 -- not warn that the context clause could be moved to the body,
1730 -- because the renaming may be intented to re-export the unit.
1732 -------------------------
1733 -- Check_Inner_Package --
1734 -------------------------
1736 procedure Check_Inner_Package (Pack : Entity_Id) is
1738 Un : constant Node_Id := Sinfo.Unit (Cnode);
1740 function Check_Use_Clause (N : Node_Id) return Traverse_Result;
1741 -- If N is a use_clause for Pack, emit warning
1743 procedure Check_Use_Clauses is new
1744 Traverse_Proc (Check_Use_Clause);
1746 ----------------------
1747 -- Check_Use_Clause --
1748 ----------------------
1750 function Check_Use_Clause (N : Node_Id) return Traverse_Result is
1754 if Nkind (N) = N_Use_Package_Clause then
1755 Nam := First (Names (N));
1756 while Present (Nam) loop
1757 if Entity (Nam) = Pack then
1758 Error_Msg_Qual_Level := 1;
1760 ("?no entities of package& are referenced!",
1762 Error_Msg_Qual_Level := 0;
1770 end Check_Use_Clause;
1772 -- Start of processing for Check_Inner_Package
1775 E := First_Entity (Pack);
1776 while Present (E) loop
1777 if Referenced_Check_Spec (E) then
1784 -- No entities of the package are referenced. Check whether the
1785 -- reference to the package itself is a use clause, and if so
1786 -- place a warning on it.
1788 Check_Use_Clauses (Un);
1789 end Check_Inner_Package;
1791 ----------------------
1792 -- Check_System_Aux --
1793 ----------------------
1795 function Check_System_Aux return Boolean is
1799 if Chars (Lunit) = Name_System
1800 and then Scope (Lunit) = Standard_Standard
1801 and then Present_System_Aux
1803 Ent := First_Entity (System_Aux_Id);
1804 while Present (Ent) loop
1805 if Referenced_Check_Spec (Ent) then
1814 end Check_System_Aux;
1816 ---------------------------
1817 -- Find_Package_Renaming --
1818 ---------------------------
1820 function Find_Package_Renaming
1822 L : Entity_Id) return Entity_Id
1828 Is_Visible_Renaming := False;
1830 E1 := First_Entity (P);
1831 while Present (E1) loop
1832 if Ekind (E1) = E_Package
1833 and then Renamed_Object (E1) = L
1835 Is_Visible_Renaming := not Is_Hidden (E1);
1838 elsif Ekind (E1) = E_Package
1839 and then No (Renamed_Object (E1))
1840 and then not Is_Generic_Instance (E1)
1842 R := Find_Package_Renaming (E1, L);
1845 Is_Visible_Renaming := not Is_Hidden (R);
1854 end Find_Package_Renaming;
1856 -- Start of processing for Check_One_Unit
1859 Cnode := Cunit (Unit);
1861 -- Only do check in units that are part of the extended main unit.
1862 -- This is actually a necessary restriction, because in the case of
1863 -- subprogram acting as its own specification, there can be with's in
1864 -- subunits that we will not see.
1866 if not In_Extended_Main_Source_Unit (Cnode) then
1869 -- In configurable run time mode, we remove the bodies of non-inlined
1870 -- subprograms, which may lead to spurious warnings, which are
1871 -- clearly undesirable.
1873 elsif Configurable_Run_Time_Mode
1874 and then Is_Predefined_File_Name (Unit_File_Name (Unit))
1879 -- Loop through context items in this unit
1881 Item := First (Context_Items (Cnode));
1882 while Present (Item) loop
1883 if Nkind (Item) = N_With_Clause
1884 and then not Implicit_With (Item)
1885 and then In_Extended_Main_Source_Unit (Item)
1887 Lunit := Entity (Name (Item));
1889 -- Check if this unit is referenced (skip the check if this
1890 -- is explicitly marked by a pragma Unreferenced).
1892 if not Referenced (Lunit)
1893 and then not Has_Pragma_Unreferenced (Lunit)
1895 -- Suppress warnings in internal units if not in -gnatg mode
1896 -- (these would be junk warnings for an application program,
1897 -- since they refer to problems in internal units).
1900 or else not Is_Internal_File_Name (Unit_File_Name (Unit))
1902 -- Here we definitely have a non-referenced unit. If it
1903 -- is the special call for a spec unit, then just set the
1904 -- flag to be read later.
1906 if Unit = Spec_Unit then
1907 Set_Unreferenced_In_Spec (Item);
1909 -- Otherwise simple unreferenced message
1913 ("?unit& is not referenced!", Name (Item));
1917 -- If main unit is a renaming of this unit, then we consider
1918 -- the with to be OK (obviously it is needed in this case!)
1919 -- This may be transitive: the unit in the with_clause may
1920 -- itself be a renaming, in which case both it and the main
1921 -- unit rename the same ultimate package.
1923 elsif Present (Renamed_Entity (Munite))
1925 (Renamed_Entity (Munite) = Lunit
1926 or else Renamed_Entity (Munite) = Renamed_Entity (Lunit))
1930 -- If this unit is referenced, and it is a package, we do
1931 -- another test, to see if any of the entities in the package
1932 -- are referenced. If none of the entities are referenced, we
1933 -- still post a warning. This occurs if the only use of the
1934 -- package is in a use clause, or in a package renaming
1937 elsif Ekind (Lunit) = E_Package then
1939 -- If Is_Instantiated is set, it means that the package is
1940 -- implicitly instantiated (this is the case of parent
1941 -- instance or an actual for a generic package formal), and
1942 -- this counts as a reference.
1944 if Is_Instantiated (Lunit) then
1947 -- If no entities in package, and there is a pragma
1948 -- Elaborate_Body present, then assume that this with is
1949 -- done for purposes of this elaboration.
1951 elsif No (First_Entity (Lunit))
1952 and then Has_Pragma_Elaborate_Body (Lunit)
1956 -- Otherwise see if any entities have been referenced
1959 if Limited_Present (Item) then
1960 Ent := First_Entity (Limited_View (Lunit));
1962 Ent := First_Entity (Lunit);
1966 -- No more entities, and we did not find one that was
1967 -- referenced. Means we have a definite case of a with
1968 -- none of whose entities was referenced.
1972 -- If in spec, just set the flag
1974 if Unit = Spec_Unit then
1975 Set_No_Entities_Ref_In_Spec (Item);
1977 elsif Check_System_Aux then
1980 -- Else give the warning
1984 ("?no entities of & are referenced!",
1987 -- Look for renamings of this package, and flag
1988 -- them as well. If the original package has
1989 -- warnings off, we suppress the warning on the
1990 -- renaming as well.
1992 Pack := Find_Package_Renaming (Munite, Lunit);
1995 and then not Warnings_Off (Lunit)
1998 ("?no entities of & are referenced!",
1999 Unit_Declaration_Node (Pack),
2006 -- Case of entity being referenced. The reference may
2007 -- come from a limited_with_clause, in which case the
2008 -- limited view of the entity carries the flag.
2010 elsif Referenced_Check_Spec (Ent)
2011 or else Referenced_As_LHS_Check_Spec (Ent)
2013 (From_With_Type (Ent)
2014 and then Is_Incomplete_Type (Ent)
2015 and then Present (Non_Limited_View (Ent))
2016 and then Referenced (Non_Limited_View (Ent)))
2018 -- This means that the with is indeed fine, in that
2019 -- it is definitely needed somewhere, and we can
2020 -- quit worrying about this one...
2022 -- Except for one little detail: if either of the
2023 -- flags was set during spec processing, this is
2024 -- where we complain that the with could be moved
2025 -- from the spec. If the spec contains a visible
2026 -- renaming of the package, inhibit warning to move
2027 -- with_clause to body.
2029 if Ekind (Munite) = E_Package_Body then
2031 Find_Package_Renaming
2032 (Spec_Entity (Munite), Lunit);
2035 if Unreferenced_In_Spec (Item) then
2037 ("?unit& is not referenced in spec!",
2040 elsif No_Entities_Ref_In_Spec (Item) then
2042 ("?no entities of & are referenced in spec!",
2046 if Ekind (Ent) = E_Package then
2047 Check_Inner_Package (Ent);
2053 if not Is_Visible_Renaming then
2055 ("\?with clause might be moved to body!",
2061 -- Move to next entity to continue search
2069 -- For a generic package, the only interesting kind of
2070 -- reference is an instantiation, since entities cannot be
2071 -- referenced directly.
2073 elsif Is_Generic_Unit (Lunit) then
2075 -- Unit was never instantiated, set flag for case of spec
2076 -- call, or give warning for normal call.
2078 if not Is_Instantiated (Lunit) then
2079 if Unit = Spec_Unit then
2080 Set_Unreferenced_In_Spec (Item);
2083 ("?unit& is never instantiated!", Name (Item));
2086 -- If unit was indeed instantiated, make sure that flag is
2087 -- not set showing it was uninstantiated in the spec, and if
2088 -- so, give warning.
2090 elsif Unreferenced_In_Spec (Item) then
2092 ("?unit& is not instantiated in spec!", Name (Item));
2094 ("\?with clause can be moved to body!", Name (Item));
2104 -- Start of processing for Check_Unused_Withs
2107 if not Opt.Check_Withs
2108 or else Operating_Mode = Check_Syntax
2113 -- Flag any unused with clauses, but skip this step if we are compiling
2114 -- a subunit on its own, since we do not have enough information to
2115 -- determine whether with's are used. We will get the relevant warnings
2116 -- when we compile the parent. This is the normal style of GNAT
2117 -- compilation in any case.
2119 if Nkind (Unit (Cunit (Main_Unit))) = N_Subunit then
2123 -- Process specified units
2125 if Spec_Unit = No_Unit then
2127 -- For main call, check all units
2129 for Unit in Main_Unit .. Last_Unit loop
2130 Check_One_Unit (Unit);
2134 -- For call for spec, check only the spec
2136 Check_One_Unit (Spec_Unit);
2138 end Check_Unused_Withs;
2140 ---------------------------------
2141 -- Generic_Package_Spec_Entity --
2142 ---------------------------------
2144 function Generic_Package_Spec_Entity (E : Entity_Id) return Boolean is
2148 if Is_Package_Body_Entity (E) then
2154 if S = Standard_Standard then
2157 elsif Ekind (S) = E_Generic_Package then
2160 elsif Ekind (S) = E_Package then
2168 end Generic_Package_Spec_Entity;
2170 ----------------------
2171 -- Goto_Spec_Entity --
2172 ----------------------
2174 function Goto_Spec_Entity (E : Entity_Id) return Entity_Id is
2177 and then Present (Spec_Entity (E))
2179 return Spec_Entity (E);
2183 end Goto_Spec_Entity;
2185 ----------------------------------------
2186 -- Has_Pragma_Unreferenced_Check_Spec --
2187 ----------------------------------------
2189 function Has_Pragma_Unreferenced_Check_Spec
2190 (E : Entity_Id) return Boolean
2193 if Is_Formal (E) and then Present (Spec_Entity (E)) then
2194 return Has_Pragma_Unreferenced (E)
2196 Has_Pragma_Unreferenced (Spec_Entity (E));
2198 return Has_Pragma_Unreferenced (E);
2200 end Has_Pragma_Unreferenced_Check_Spec;
2202 ------------------------------------
2203 -- Never_Set_In_Source_Check_Spec --
2204 ------------------------------------
2206 function Never_Set_In_Source_Check_Spec (E : Entity_Id) return Boolean is
2208 if Is_Formal (E) and then Present (Spec_Entity (E)) then
2209 return Never_Set_In_Source (E)
2211 Never_Set_In_Source (Spec_Entity (E));
2213 return Never_Set_In_Source (E);
2215 end Never_Set_In_Source_Check_Spec;
2217 -------------------------------------
2218 -- Operand_Has_Warnings_Suppressed --
2219 -------------------------------------
2221 function Operand_Has_Warnings_Suppressed (N : Node_Id) return Boolean is
2223 function Check_For_Warnings (N : Node_Id) return Traverse_Result;
2224 -- Function used to check one node to see if it is or was originally
2225 -- a reference to an entity for which Warnings are off. If so, Abandon
2226 -- is returned, otherwise OK_Orig is returned to continue the traversal
2227 -- of the original expression.
2229 function Traverse is new Traverse_Func (Check_For_Warnings);
2230 -- Function used to traverse tree looking for warnings
2232 ------------------------
2233 -- Check_For_Warnings --
2234 ------------------------
2236 function Check_For_Warnings (N : Node_Id) return Traverse_Result is
2237 R : constant Node_Id := Original_Node (N);
2240 if Nkind (R) in N_Has_Entity
2241 and then Present (Entity (R))
2242 and then Warnings_Off (Entity (R))
2248 end Check_For_Warnings;
2250 -- Start of processing for Operand_Has_Warnings_Suppressed
2253 return Traverse (N) = Abandon;
2255 -- If any exception occurs, then something has gone wrong, and this is
2256 -- only a minor aesthetic issue anyway, so just say we did not find what
2257 -- we are looking for, rather than blow up.
2262 end Operand_Has_Warnings_Suppressed;
2264 -----------------------------------------
2265 -- Output_Non_Modified_In_Out_Warnings --
2266 -----------------------------------------
2268 procedure Output_Non_Modifed_In_Out_Warnings is
2270 function No_Warn_On_In_Out (E : Entity_Id) return Boolean;
2271 -- Given a formal parameter entity E, determines if there is a reason to
2272 -- suppress IN OUT warnings (not modified, could be IN) for formals of
2273 -- the subprogram. We suppress these warnings if Warnings Off is set, or
2274 -- if we have seen the address of the subprogram being taken, or if the
2275 -- subprogram is used as a generic actual (in the latter cases the
2276 -- context may force use of IN OUT, even if the parameter is not
2277 -- modifies for this particular case.
2279 -----------------------
2280 -- No_Warn_On_In_Out --
2281 -----------------------
2283 function No_Warn_On_In_Out (E : Entity_Id) return Boolean is
2284 S : constant Entity_Id := Scope (E);
2286 if Warnings_Off (S) then
2288 elsif Address_Taken (S) then
2290 elsif Used_As_Generic_Actual (S) then
2292 elsif Present (Spec_Entity (E)) then
2293 return No_Warn_On_In_Out (Spec_Entity (E));
2297 end No_Warn_On_In_Out;
2299 -- Start of processing for Output_Non_Modifed_In_Out_Warnings
2302 -- Loop through entities for which a warning may be needed
2304 for J in In_Out_Warnings.First .. In_Out_Warnings.Last loop
2306 E1 : constant Entity_Id := In_Out_Warnings.Table (J);
2309 -- Suppress warning in specific cases (see details in comments for
2310 -- No_Warn_On_In_Out).
2312 if No_Warn_On_In_Out (E1) then
2315 -- Here we generate the warning
2318 Error_Msg_N ("?formal parameter & is not modified!", E1);
2319 Error_Msg_N ("\?mode could be IN instead of `IN OUT`!", E1);
2321 -- Kill any other warnings on this entity, since this is the
2322 -- one that should dominate any other unreferenced warning.
2324 Set_Warnings_Off (E1);
2328 end Output_Non_Modifed_In_Out_Warnings;
2330 ----------------------------------------
2331 -- Output_Obsolescent_Entity_Warnings --
2332 ----------------------------------------
2334 procedure Output_Obsolescent_Entity_Warnings (N : Node_Id; E : Entity_Id) is
2335 P : constant Node_Id := Parent (N);
2341 -- Do not output message if we are the scope of standard. This means
2342 -- we have a reference from a context clause from when it is originally
2343 -- processed, and that's too early to tell whether it is an obsolescent
2344 -- unit doing the with'ing. In Sem_Ch10.Analyze_Compilation_Unit we make
2345 -- sure that we have a later call when the scope is available. This test
2346 -- also eliminates all messages for use clauses, which is fine (we do
2347 -- not want messages for use clauses, since they are always redundant
2348 -- with respect to the associated with clause).
2350 if S = Standard_Standard then
2354 -- Do not output message if we are in scope of an obsolescent package
2358 if Is_Obsolescent (S) then
2363 exit when S = Standard_Standard;
2366 -- Here we will output the message
2368 Error_Msg_Sloc := Sloc (E);
2370 -- Case of with clause
2372 if Nkind (P) = N_With_Clause then
2373 if Ekind (E) = E_Package then
2375 ("?with of obsolescent package& declared#", N, E);
2376 elsif Ekind (E) = E_Procedure then
2378 ("?with of obsolescent procedure& declared#", N, E);
2381 ("?with of obsolescent function& declared#", N, E);
2384 -- If we do not have a with clause, then ignore any reference to an
2385 -- obsolescent package name. We only want to give the one warning of
2386 -- withing the package, not one each time it is used to qualify.
2388 elsif Ekind (E) = E_Package then
2391 -- Procedure call statement
2393 elsif Nkind (P) = N_Procedure_Call_Statement then
2395 ("?call to obsolescent procedure& declared#", N, E);
2399 elsif Nkind (P) = N_Function_Call then
2401 ("?call to obsolescent function& declared#", N, E);
2403 -- Reference to obsolescent type
2405 elsif Is_Type (E) then
2407 ("?reference to obsolescent type& declared#", N, E);
2409 -- Reference to obsolescent component
2411 elsif Ekind (E) = E_Component
2412 or else Ekind (E) = E_Discriminant
2415 ("?reference to obsolescent component& declared#", N, E);
2417 -- Reference to obsolescent variable
2419 elsif Ekind (E) = E_Variable then
2421 ("?reference to obsolescent variable& declared#", N, E);
2423 -- Reference to obsolescent constant
2425 elsif Ekind (E) = E_Constant
2426 or else Ekind (E) in Named_Kind
2429 ("?reference to obsolescent constant& declared#", N, E);
2431 -- Reference to obsolescent enumeration literal
2433 elsif Ekind (E) = E_Enumeration_Literal then
2435 ("?reference to obsolescent enumeration literal& declared#", N, E);
2437 -- Generic message for any other case we missed
2441 ("?reference to obsolescent entity& declared#", N, E);
2444 -- Output additional warning if present
2447 W : constant Node_Id := Obsolescent_Warning (E);
2452 -- This is a warning continuation to start on a new line
2453 Name_Buffer (1) := '\';
2454 Name_Buffer (2) := '\';
2455 Name_Buffer (3) := '?';
2458 -- Add characters to message, and output message. Note that
2459 -- we quote every character of the message since we don't
2460 -- want to process any insertions.
2462 for J in 1 .. String_Length (Strval (W)) loop
2463 Add_Char_To_Name_Buffer (''');
2464 Add_Char_To_Name_Buffer
2465 (Get_Character (Get_String_Char (Strval (W), J)));
2468 Error_Msg_N (Name_Buffer (1 .. Name_Len), N);
2471 end Output_Obsolescent_Entity_Warnings;
2473 ----------------------------------
2474 -- Output_Unreferenced_Messages --
2475 ----------------------------------
2477 procedure Output_Unreferenced_Messages is
2479 for J in Unreferenced_Entities.First ..
2480 Unreferenced_Entities.Last
2482 Warn_On_Unreferenced_Entity (Unreferenced_Entities.Table (J));
2484 end Output_Unreferenced_Messages;
2486 ---------------------------
2487 -- Referenced_Check_Spec --
2488 ---------------------------
2490 function Referenced_Check_Spec (E : Entity_Id) return Boolean is
2492 if Is_Formal (E) and then Present (Spec_Entity (E)) then
2493 return Referenced (E) or else Referenced (Spec_Entity (E));
2495 return Referenced (E);
2497 end Referenced_Check_Spec;
2499 ----------------------------------
2500 -- Referenced_As_LHS_Check_Spec --
2501 ----------------------------------
2503 function Referenced_As_LHS_Check_Spec (E : Entity_Id) return Boolean is
2505 if Is_Formal (E) and then Present (Spec_Entity (E)) then
2506 return Referenced_As_LHS (E)
2507 or else Referenced_As_LHS (Spec_Entity (E));
2509 return Referenced_As_LHS (E);
2511 end Referenced_As_LHS_Check_Spec;
2513 ----------------------------
2514 -- Set_Dot_Warning_Switch --
2515 ----------------------------
2517 function Set_Dot_Warning_Switch (C : Character) return Boolean is
2521 Warn_On_Unrepped_Components := True;
2524 Warn_On_Unrepped_Components := False;
2527 Warn_On_Object_Renames_Function := True;
2530 Warn_On_Object_Renames_Function := False;
2533 Warn_On_Non_Local_Exception := True;
2536 Warn_On_Non_Local_Exception := False;
2543 end Set_Dot_Warning_Switch;
2545 ------------------------
2546 -- Set_Warning_Switch --
2547 ------------------------
2549 function Set_Warning_Switch (C : Character) return Boolean is
2553 Check_Unreferenced := True;
2554 Check_Unreferenced_Formals := True;
2555 Check_Withs := True;
2556 Constant_Condition_Warnings := True;
2557 Implementation_Unit_Warnings := True;
2558 Ineffective_Inline_Warnings := True;
2559 Warn_On_Ada_2005_Compatibility := True;
2560 Warn_On_Assumed_Low_Bound := True;
2561 Warn_On_Bad_Fixed_Value := True;
2562 Warn_On_Constant := True;
2563 Warn_On_Export_Import := True;
2564 Warn_On_Modified_Unread := True;
2565 Warn_On_No_Value_Assigned := True;
2566 Warn_On_Non_Local_Exception := True;
2567 Warn_On_Obsolescent_Feature := True;
2568 Warn_On_Questionable_Missing_Parens := True;
2569 Warn_On_Redundant_Constructs := True;
2570 Warn_On_Object_Renames_Function := True;
2571 Warn_On_Unchecked_Conversion := True;
2572 Warn_On_Unrecognized_Pragma := True;
2573 Warn_On_Unrepped_Components := True;
2576 Check_Unreferenced := False;
2577 Check_Unreferenced_Formals := False;
2578 Check_Withs := False;
2579 Constant_Condition_Warnings := False;
2580 Elab_Warnings := False;
2581 Implementation_Unit_Warnings := False;
2582 Ineffective_Inline_Warnings := False;
2583 Warn_On_Ada_2005_Compatibility := False;
2584 Warn_On_Bad_Fixed_Value := False;
2585 Warn_On_Constant := False;
2586 Warn_On_Deleted_Code := False;
2587 Warn_On_Dereference := False;
2588 Warn_On_Export_Import := False;
2589 Warn_On_Hiding := False;
2590 Warn_On_Modified_Unread := False;
2591 Warn_On_No_Value_Assigned := False;
2592 Warn_On_Non_Local_Exception := False;
2593 Warn_On_Obsolescent_Feature := False;
2594 Warn_On_Questionable_Missing_Parens := False;
2595 Warn_On_Redundant_Constructs := False;
2596 Warn_On_Object_Renames_Function := False;
2597 Warn_On_Unchecked_Conversion := False;
2598 Warn_On_Unrecognized_Pragma := False;
2599 Warn_On_Unrepped_Components := False;
2602 Warn_On_Bad_Fixed_Value := True;
2605 Warn_On_Bad_Fixed_Value := False;
2608 Constant_Condition_Warnings := True;
2611 Constant_Condition_Warnings := False;
2614 Warn_On_Dereference := True;
2617 Warn_On_Dereference := False;
2620 Warning_Mode := Treat_As_Error;
2623 Check_Unreferenced_Formals := True;
2626 Check_Unreferenced_Formals := False;
2629 Warn_On_Unrecognized_Pragma := True;
2632 Warn_On_Unrecognized_Pragma := False;
2635 Warn_On_Hiding := True;
2638 Warn_On_Hiding := False;
2641 Implementation_Unit_Warnings := True;
2644 Implementation_Unit_Warnings := False;
2647 Warn_On_Obsolescent_Feature := True;
2650 Warn_On_Obsolescent_Feature := False;
2653 Warn_On_Constant := True;
2656 Warn_On_Constant := False;
2659 Elab_Warnings := True;
2662 Elab_Warnings := False;
2665 Warn_On_Modified_Unread := True;
2668 Warn_On_Modified_Unread := False;
2671 Warning_Mode := Normal;
2674 Address_Clause_Overlay_Warnings := True;
2677 Address_Clause_Overlay_Warnings := False;
2680 Ineffective_Inline_Warnings := True;
2683 Ineffective_Inline_Warnings := False;
2686 Warn_On_Questionable_Missing_Parens := True;
2689 Warn_On_Questionable_Missing_Parens := False;
2692 Warn_On_Redundant_Constructs := True;
2695 Warn_On_Redundant_Constructs := False;
2698 Warning_Mode := Suppress;
2701 Warn_On_Deleted_Code := True;
2704 Warn_On_Deleted_Code := False;
2707 Check_Unreferenced := True;
2708 Check_Withs := True;
2709 Check_Unreferenced_Formals := True;
2712 Check_Unreferenced := False;
2713 Check_Withs := False;
2714 Check_Unreferenced_Formals := False;
2717 Warn_On_No_Value_Assigned := True;
2720 Warn_On_No_Value_Assigned := False;
2723 Warn_On_Assumed_Low_Bound := True;
2726 Warn_On_Assumed_Low_Bound := False;
2729 Warn_On_Export_Import := True;
2732 Warn_On_Export_Import := False;
2735 Warn_On_Ada_2005_Compatibility := True;
2738 Warn_On_Ada_2005_Compatibility := False;
2741 Warn_On_Unchecked_Conversion := True;
2744 Warn_On_Unchecked_Conversion := False;
2751 end Set_Warning_Switch;
2753 -----------------------------
2754 -- Warn_On_Known_Condition --
2755 -----------------------------
2757 procedure Warn_On_Known_Condition (C : Node_Id) is
2760 procedure Track (N : Node_Id; Loc : Node_Id);
2761 -- Adds continuation warning(s) pointing to reason (assignment or test)
2762 -- for the operand of the conditional having a known value (or at least
2763 -- enough is known about the value to issue the warning). N is the node
2764 -- which is judged to have a known value. Loc is the warning location.
2770 procedure Track (N : Node_Id; Loc : Node_Id) is
2771 Nod : constant Node_Id := Original_Node (N);
2774 if Nkind (Nod) in N_Op_Compare then
2775 Track (Left_Opnd (Nod), Loc);
2776 Track (Right_Opnd (Nod), Loc);
2778 elsif Is_Entity_Name (Nod)
2779 and then Is_Object (Entity (Nod))
2782 CV : constant Node_Id := Current_Value (Entity (Nod));
2785 if Present (CV) then
2786 Error_Msg_Sloc := Sloc (CV);
2788 if Nkind (CV) not in N_Subexpr then
2789 Error_Msg_N ("\\?(see test #)", Loc);
2791 elsif Nkind (Parent (CV)) =
2792 N_Case_Statement_Alternative
2794 Error_Msg_N ("\\?(see case alternative #)", Loc);
2797 Error_Msg_N ("\\?(see assignment #)", Loc);
2804 -- Start of processing for Warn_On_Known_Condition
2807 -- Argument replacement in an inlined body can make conditions static.
2808 -- Do not emit warnings in this case.
2810 if In_Inlined_Body then
2814 if Constant_Condition_Warnings
2815 and then Nkind (C) = N_Identifier
2817 (Entity (C) = Standard_False or else Entity (C) = Standard_True)
2818 and then Comes_From_Source (Original_Node (C))
2819 and then not In_Instance
2821 -- See if this is in a statement or a declaration
2825 -- If tree is not attached, do not issue warning (this is very
2826 -- peculiar, and probably arises from some other error condition)
2831 -- If we are in a declaration, then no warning, since in practice
2832 -- conditionals in declarations are used for intended tests which
2833 -- may be known at compile time, e.g. things like
2835 -- x : constant Integer := 2 + (Word'Size = 32);
2837 -- And a warning is annoying in such cases
2839 elsif Nkind (P) in N_Declaration
2841 Nkind (P) in N_Later_Decl_Item
2845 -- Don't warn in assert pragma, since presumably tests in such
2846 -- a context are very definitely intended, and might well be
2847 -- known at compile time. Note that we have to test the original
2848 -- node, since assert pragmas get rewritten at analysis time.
2850 elsif Nkind (Original_Node (P)) = N_Pragma
2851 and then Chars (Original_Node (P)) = Name_Assert
2856 exit when Is_Statement (P);
2860 -- Here we issue the warning unless some sub-operand has warnings
2861 -- set off, in which case we suppress the warning for the node. If
2862 -- the original expression is an inequality, it has been expanded
2863 -- into a negation, and the value of the original expression is the
2864 -- negation of the equality. If the expression is an entity that
2865 -- appears within a negation, it is clearer to flag the negation
2866 -- itself, and report on its constant value.
2868 if not Operand_Has_Warnings_Suppressed (C) then
2870 True_Branch : Boolean := Entity (C) = Standard_True;
2871 Cond : Node_Id := C;
2874 if Present (Parent (C))
2875 and then Nkind (Parent (C)) = N_Op_Not
2877 True_Branch := not True_Branch;
2882 if Is_Entity_Name (Original_Node (C))
2883 and then Nkind (Cond) /= N_Op_Not
2886 ("object & is always True?", Cond, Original_Node (C));
2887 Track (Original_Node (C), Cond);
2890 Error_Msg_N ("condition is always True?", Cond);
2895 Error_Msg_N ("condition is always False?", Cond);
2901 end Warn_On_Known_Condition;
2903 ------------------------------
2904 -- Warn_On_Suspicious_Index --
2905 ------------------------------
2907 procedure Warn_On_Suspicious_Index (Name : Entity_Id; X : Node_Id) is
2910 -- Set to lower bound for a suspicious type
2913 -- Entity for array reference
2918 function Is_Suspicious_Type (Typ : Entity_Id) return Boolean;
2919 -- Tests to see if Typ is a type for which we may have a suspicious
2920 -- index, namely an unconstrained array type, whose lower bound is
2921 -- either zero or one. If so, True is returned, and Low_Bound is set
2922 -- to this lower bound. If not, False is returned, and Low_Bound is
2923 -- undefined on return.
2925 -- For now, we limite this to standard string types, so any other
2926 -- unconstrained types return False. We may change our minds on this
2927 -- later on, but strings seem the most important case.
2929 procedure Test_Suspicious_Index;
2930 -- Test if index is of suspicious type and if so, generate warning
2932 ------------------------
2933 -- Is_Suspicious_Type --
2934 ------------------------
2936 function Is_Suspicious_Type (Typ : Entity_Id) return Boolean is
2940 if Is_Array_Type (Typ)
2941 and then not Is_Constrained (Typ)
2942 and then Number_Dimensions (Typ) = 1
2943 and then not Warnings_Off (Typ)
2944 and then (Root_Type (Typ) = Standard_String
2946 Root_Type (Typ) = Standard_Wide_String
2948 Root_Type (Typ) = Standard_Wide_Wide_String)
2950 LB := Type_Low_Bound (Etype (First_Index (Typ)));
2952 if Compile_Time_Known_Value (LB) then
2953 Low_Bound := Expr_Value (LB);
2954 return Low_Bound = Uint_0 or else Low_Bound = Uint_1;
2959 end Is_Suspicious_Type;
2961 ---------------------------
2962 -- Test_Suspicious_Index --
2963 ---------------------------
2965 procedure Test_Suspicious_Index is
2967 function Length_Reference (N : Node_Id) return Boolean;
2968 -- Check if node N is of the form Name'Length
2971 -- Generate first warning line
2973 ----------------------
2974 -- Length_Reference --
2975 ----------------------
2977 function Length_Reference (N : Node_Id) return Boolean is
2978 R : constant Node_Id := Original_Node (N);
2981 Nkind (R) = N_Attribute_Reference
2982 and then Attribute_Name (R) = Name_Length
2983 and then Is_Entity_Name (Prefix (R))
2984 and then Entity (Prefix (R)) = Ent;
2985 end Length_Reference;
2993 Error_Msg_Uint_1 := Low_Bound;
2994 Error_Msg_FE ("?index for& may assume lower bound of^", X, Ent);
2997 -- Start of processing for Test_Suspicious_Index
3000 -- Nothing to do if subscript does not come from source (we don't
3001 -- want to give garbage warnings on compiler expanded code, e.g. the
3002 -- loops generated for slice assignments. Sucb junk warnings would
3003 -- be placed on source constructs with no subscript in sight!)
3005 if not Comes_From_Source (Original_Node (X)) then
3009 -- Case where subscript is a constant integer
3011 if Nkind (X) = N_Integer_Literal then
3014 -- Case where original form of subscript is an integer literal
3016 if Nkind (Original_Node (X)) = N_Integer_Literal then
3017 if Intval (X) = Low_Bound then
3019 ("\suggested replacement: `&''First`", X, Ent);
3021 Error_Msg_Uint_1 := Intval (X) - Low_Bound;
3023 ("\suggested replacement: `&''First + ^`", X, Ent);
3027 -- Case where original form of subscript is more complex
3030 -- Build string X'First - 1 + expression where the expression
3031 -- is the original subscript. If the expression starts with "1
3032 -- + ", then the "- 1 + 1" is elided.
3034 Error_Msg_String (1 .. 13) := "'First - 1 + ";
3035 Error_Msg_Strlen := 13;
3038 Sref : Source_Ptr := Sloc (First_Node (Original_Node (X)));
3039 Tref : constant Source_Buffer_Ptr :=
3040 Source_Text (Get_Source_File_Index (Sref));
3041 -- Tref (Sref) is used to scan the subscript
3044 -- Paretheses counter when scanning subscript
3047 -- Tref (Sref) points to start of subscript
3049 -- Elide - 1 if subscript starts with 1 +
3051 if Tref (Sref .. Sref + 2) = "1 +" then
3052 Error_Msg_Strlen := Error_Msg_Strlen - 6;
3055 elsif Tref (Sref .. Sref + 1) = "1+" then
3056 Error_Msg_Strlen := Error_Msg_Strlen - 6;
3060 -- Now we will copy the subscript to the string buffer
3064 -- Count parens, exit if terminating right paren. Note
3065 -- check to ignore paren appearing as character literal.
3067 if Tref (Sref + 1) = '''
3069 Tref (Sref - 1) = '''
3073 if Tref (Sref) = '(' then
3075 elsif Tref (Sref) = ')' then
3081 -- Done if terminating double dot (slice case)
3084 and then (Tref (Sref .. Sref + 1) = ".."
3086 Tref (Sref .. Sref + 2) = " ..");
3088 -- Quit if we have hit EOF character, something wrong
3090 if Tref (Sref) = EOF then
3094 -- String literals are too much of a pain to handle
3096 if Tref (Sref) = '"' or else Tref (Sref) = '%' then
3100 -- If we have a 'Range reference, then this is a case
3101 -- where we cannot easily give a replacement. Don't try!
3103 if Tref (Sref .. Sref + 4) = "range"
3104 and then Tref (Sref - 1) < 'A'
3105 and then Tref (Sref + 5) < 'A'
3110 -- Else store next character
3112 Error_Msg_Strlen := Error_Msg_Strlen + 1;
3113 Error_Msg_String (Error_Msg_Strlen) := Tref (Sref);
3116 -- If we get more than 40 characters then the expression
3117 -- is too long to copy, or something has gone wrong. In
3118 -- either case, just skip the attempt at a suggested fix.
3120 if Error_Msg_Strlen > 40 then
3126 -- Replacement subscript is now in string buffer
3129 ("\suggested replacement: `&~`", Original_Node (X), Ent);
3132 -- Case where subscript is of the form X'Length
3134 elsif Length_Reference (X) then
3136 Error_Msg_Node_2 := Ent;
3138 ("\suggest replacement of `&''Length` by `&''Last`",
3141 -- Case where subscript is of the form X'Length - expression
3143 elsif Nkind (X) = N_Op_Subtract
3144 and then Length_Reference (Left_Opnd (X))
3147 Error_Msg_Node_2 := Ent;
3149 ("\suggest replacement of `&''Length` by `&''Last`",
3150 Left_Opnd (X), Ent);
3152 end Test_Suspicious_Index;
3154 -- Start of processing for Warn_On_Suspicious_Index
3157 -- Only process if warnings activated
3159 if Warn_On_Assumed_Low_Bound then
3161 -- Test if array is simple entity name
3163 if Is_Entity_Name (Name) then
3165 -- Test if array is parameter of unconstrained string type
3167 Ent := Entity (Name);
3171 and then Is_Suspicious_Type (Typ)
3172 and then not Low_Bound_Known (Ent)
3174 Test_Suspicious_Index;
3178 end Warn_On_Suspicious_Index;
3180 --------------------------------------
3181 -- Warn_On_Unassigned_Out_Parameter --
3182 --------------------------------------
3184 procedure Warn_On_Unassigned_Out_Parameter
3185 (Return_Node : Node_Id;
3186 Scope_Id : Entity_Id)
3192 -- Ignore if procedure or return statement does not come from source
3194 if not Comes_From_Source (Scope_Id)
3195 or else not Comes_From_Source (Return_Node)
3200 -- Loop through formals
3202 Form := First_Formal (Scope_Id);
3203 while Present (Form) loop
3205 -- We are only interested in OUT parameters that come from source
3206 -- and are never set in the source, and furthermore only in scalars
3207 -- since non-scalars generate too many false positives.
3209 if Ekind (Form) = E_Out_Parameter
3210 and then Never_Set_In_Source_Check_Spec (Form)
3211 and then Is_Scalar_Type (Etype (Form))
3212 and then not Present (Unset_Reference (Form))
3214 -- Before we issue the warning, an add ad hoc defence against the
3215 -- most common case of false positives with this warning which is
3216 -- the case where there is a Boolean OUT parameter that has been
3217 -- set, and whose meaning is "ignore the values of the other
3218 -- parameters". We can't of course reliably tell this case at
3219 -- compile time, but the following test kills a lot of false
3220 -- positives, without generating a significant number of false
3221 -- negatives (missed real warnings).
3223 Form2 := First_Formal (Scope_Id);
3224 while Present (Form2) loop
3225 if Ekind (Form2) = E_Out_Parameter
3226 and then Root_Type (Etype (Form2)) = Standard_Boolean
3227 and then not Never_Set_In_Source_Check_Spec (Form2)
3232 Next_Formal (Form2);
3235 -- Here all conditionas are met, record possible unset reference
3237 Set_Unset_Reference (Form, Return_Node);
3242 end Warn_On_Unassigned_Out_Parameter;
3244 ---------------------------------
3245 -- Warn_On_Unreferenced_Entity --
3246 ---------------------------------
3248 procedure Warn_On_Unreferenced_Entity
3249 (Spec_E : Entity_Id;
3250 Body_E : Entity_Id := Empty)
3252 E : Entity_Id := Spec_E;
3254 if not Referenced_Check_Spec (E) and then not Warnings_Off (E) then
3258 -- Case of variable that is assigned but not read. We
3259 -- suppress the message if the variable is volatile, has an
3260 -- address clause, or is imported.
3262 if Referenced_As_LHS_Check_Spec (E)
3263 and then No (Address_Clause (E))
3264 and then not Is_Volatile (E)
3266 if Warn_On_Modified_Unread
3267 and then not Is_Imported (E)
3268 and then not Is_Return_Object (E)
3270 -- Suppress message for aliased or renamed variables,
3271 -- since there may be other entities that read the
3272 -- same memory location.
3274 and then not Is_Aliased (E)
3275 and then No (Renamed_Object (E))
3279 ("?variable & is assigned but never read!", E);
3280 Set_Last_Assignment (E, Empty);
3283 -- Normal case of neither assigned nor read
3286 -- We suppress the message for types for which a valid
3287 -- pragma Unreferenced_Objects has been given, otherwise
3288 -- we go ahead and give the message.
3290 if not Has_Pragma_Unreferenced_Objects (Etype (E)) then
3292 -- Distinguish renamed case in message
3294 if Present (Renamed_Object (E))
3295 and then Comes_From_Source (Renamed_Object (E))
3298 ("?renamed variable & is not referenced!", E);
3301 ("?variable & is not referenced!", E);
3307 if Present (Renamed_Object (E))
3308 and then Comes_From_Source (Renamed_Object (E))
3311 ("?renamed constant & is not referenced!", E);
3313 Error_Msg_N ("?constant & is not referenced!", E);
3316 when E_In_Parameter |
3317 E_In_Out_Parameter =>
3319 -- Do not emit message for formals of a renaming, because
3320 -- they are never referenced explicitly.
3322 if Nkind (Original_Node (Unit_Declaration_Node (Scope (E))))
3323 /= N_Subprogram_Renaming_Declaration
3325 -- Suppress this message for an IN OUT parameter of a
3326 -- non-scalar type, since it is normal to have only an
3327 -- assignment in such a case.
3329 if Ekind (E) = E_In_Parameter
3330 or else not Referenced_As_LHS_Check_Spec (E)
3331 or else Is_Scalar_Type (E)
3333 if Present (Body_E) then
3337 ("?formal parameter & is not referenced!", E, Spec_E);
3341 when E_Out_Parameter =>
3344 when E_Named_Integer |
3346 Error_Msg_N ("?named number & is not referenced!", E);
3348 when E_Enumeration_Literal =>
3349 Error_Msg_N ("?literal & is not referenced!", E);
3352 Error_Msg_N ("?function & is not referenced!", E);
3355 Error_Msg_N ("?procedure & is not referenced!", E);
3357 when E_Generic_Procedure =>
3359 ("?generic procedure & is never instantiated!", E);
3361 when E_Generic_Function =>
3363 ("?generic function & is never instantiated!", E);
3366 Error_Msg_N ("?type & is not referenced!", E);
3369 Error_Msg_N ("?& is not referenced!", E);
3372 -- Kill warnings on the entity on which the message has been posted
3374 Set_Warnings_Off (E);
3376 end Warn_On_Unreferenced_Entity;
3378 --------------------------------
3379 -- Warn_On_Useless_Assignment --
3380 --------------------------------
3382 procedure Warn_On_Useless_Assignment
3384 Loc : Source_Ptr := No_Location)
3389 function Check_Ref (N : Node_Id) return Traverse_Result;
3390 -- Used to instantiate Traverse_Func. Returns Abandon if
3391 -- a reference to the entity in question is found.
3393 function Test_No_Refs is new Traverse_Func (Check_Ref);
3399 function Check_Ref (N : Node_Id) return Traverse_Result is
3401 -- Check reference to our identifier. We use name equality here
3402 -- because the exception handlers have not yet been analyzed. This
3403 -- is not quite right, but it really does not matter that we fail
3404 -- to output the warning in some obscure cases of name clashes.
3406 if Nkind (N) = N_Identifier
3407 and then Chars (N) = Chars (Ent)
3415 -- Start of processing for Warn_On_Useless_Assignment
3418 -- Check if this is a case we want to warn on, a variable with the
3419 -- last assignment field set, with warnings enabled, and which is
3420 -- not imported or exported.
3422 if Ekind (Ent) = E_Variable
3423 and then not Is_Return_Object (Ent)
3424 and then Present (Last_Assignment (Ent))
3425 and then not Warnings_Off (Ent)
3426 and then not Has_Pragma_Unreferenced_Check_Spec (Ent)
3427 and then not Is_Imported (Ent)
3428 and then not Is_Exported (Ent)
3430 -- Before we issue the message, check covering exception handlers.
3431 -- Search up tree for enclosing statement sequences and handlers
3433 P := Parent (Last_Assignment (Ent));
3434 while Present (P) loop
3436 -- Something is really wrong if we don't find a handled
3437 -- statement sequence, so just suppress the warning.
3440 Set_Last_Assignment (Ent, Empty);
3443 -- When we hit a package/subprogram body, issue warning and exit
3445 elsif Nkind (P) = N_Subprogram_Body
3446 or else Nkind (P) = N_Package_Body
3448 if Loc = No_Location then
3450 ("?useless assignment to&, value never referenced!",
3451 Last_Assignment (Ent), Ent);
3453 Error_Msg_Sloc := Loc;
3455 ("?useless assignment to&, value overwritten #!",
3456 Last_Assignment (Ent), Ent);
3459 Set_Last_Assignment (Ent, Empty);
3462 -- Enclosing handled sequence of statements
3464 elsif Nkind (P) = N_Handled_Sequence_Of_Statements then
3466 -- Check exception handlers present
3468 if Present (Exception_Handlers (P)) then
3470 -- If we are not at the top level, we regard an inner
3471 -- exception handler as a decisive indicator that we should
3472 -- not generate the warning, since the variable in question
3473 -- may be acceessed after an exception in the outer block.
3475 if Nkind (Parent (P)) /= N_Subprogram_Body
3476 and then Nkind (Parent (P)) /= N_Package_Body
3478 Set_Last_Assignment (Ent, Empty);
3481 -- Otherwise we are at the outer level. An exception
3482 -- handler is significant only if it references the
3483 -- variable in question.
3486 X := First (Exception_Handlers (P));
3487 while Present (X) loop
3488 if Test_No_Refs (X) = Abandon then
3489 Set_Last_Assignment (Ent, Empty);
3502 end Warn_On_Useless_Assignment;
3504 ---------------------------------
3505 -- Warn_On_Useless_Assignments --
3506 ---------------------------------
3508 procedure Warn_On_Useless_Assignments (E : Entity_Id) is
3511 if Warn_On_Modified_Unread
3512 and then In_Extended_Main_Source_Unit (E)
3514 Ent := First_Entity (E);
3515 while Present (Ent) loop
3516 Warn_On_Useless_Assignment (Ent);
3520 end Warn_On_Useless_Assignments;