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 3, 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Errout; use Errout;
31 with Exp_Code; use Exp_Code;
32 with Fname; use Fname;
34 with Namet; use Namet;
35 with Nlists; use Nlists;
37 with Rtsfind; use Rtsfind;
39 with Sem_Ch8; use Sem_Ch8;
40 with Sem_Eval; use Sem_Eval;
41 with Sem_Util; use Sem_Util;
42 with Sinfo; use Sinfo;
43 with Sinput; use Sinput;
44 with Snames; use Snames;
45 with Stand; use Stand;
46 with Stringt; use Stringt;
48 with Uintp; use Uintp;
50 package body Sem_Warn is
52 -- The following table collects Id's of entities that are potentially
53 -- unreferenced. See Check_Unset_Reference for further details.
54 -- ??? Check_Unset_Reference has zero information about this table.
56 package Unreferenced_Entities is new Table.Table (
57 Table_Component_Type => Entity_Id,
58 Table_Index_Type => Nat,
60 Table_Initial => Alloc.Unreferenced_Entities_Initial,
61 Table_Increment => Alloc.Unreferenced_Entities_Increment,
62 Table_Name => "Unreferenced_Entities");
64 package In_Out_Warnings is new Table.Table (
65 Table_Component_Type => Entity_Id,
66 Table_Index_Type => Nat,
68 Table_Initial => Alloc.In_Out_Warnings_Initial,
69 Table_Increment => Alloc.In_Out_Warnings_Increment,
70 Table_Name => "In_Out_Warnings");
72 -----------------------
73 -- Local Subprograms --
74 -----------------------
76 function Generic_Package_Spec_Entity (E : Entity_Id) return Boolean;
77 -- This returns true if the entity E is declared within a generic package.
78 -- The point of this is to detect variables which are not assigned within
79 -- the generic, but might be assigned outside the package for any given
80 -- instance. These are cases where we leave the warnings to be posted for
81 -- the instance, when we will know more.
83 function Goto_Spec_Entity (E : Entity_Id) return Entity_Id;
84 -- If E is a parameter entity for a subprogram body, then this function
85 -- returns the corresponding spec entity, if not, E is returned unchanged.
87 function Has_Pragma_Unreferenced_Check_Spec (E : Entity_Id) return Boolean;
88 -- Tests Has_Pragma_Unreferenced flag for entity E. If E is not a formal,
89 -- this is simply the setting of the flag Has_Pragma_Unreferenced. If E is
90 -- a body formal, the setting of the flag in the corresponding spec is
91 -- also checked (and True returned if either flag is True).
93 function Never_Set_In_Source_Check_Spec (E : Entity_Id) return Boolean;
94 -- Tests Never_Set_In_Source status for entity E. If E is not a formal,
95 -- this is simply the setting of the flag Never_Set_In_Source. If E is
96 -- a body formal, the setting of the flag in the corresponding spec is
97 -- also checked (and False returned if either flag is False).
99 function Operand_Has_Warnings_Suppressed (N : Node_Id) return Boolean;
100 -- This function traverses the expression tree represented by the node N
101 -- and determines if any sub-operand is a reference to an entity for which
102 -- the Warnings_Off flag is set. True is returned if such an entity is
103 -- encountered, and False otherwise.
105 function Referenced_Check_Spec (E : Entity_Id) return Boolean;
106 -- Tests Referenced status for entity E. If E is not a formal, this is
107 -- simply the setting of the flag Referenced. If E is a body formal, the
108 -- setting of the flag in the corresponding spec is also checked (and True
109 -- returned if either flag is True).
111 function Referenced_As_LHS_Check_Spec (E : Entity_Id) return Boolean;
112 -- Tests Referenced_As_LHS status for entity E. If E is not a formal, this
113 -- is simply the setting of the flag Referenced_As_LHS. If E is a body
114 -- formal, the setting of the flag in the corresponding spec is also
115 -- checked (and True returned if either flag is True).
117 procedure Warn_On_Unreferenced_Entity
119 Body_E : Entity_Id := Empty);
120 -- Output warnings for unreferenced entity E. For the case of an entry
121 -- formal, Body_E is the corresponding body entity for a particular
122 -- accept statement, and the message is posted on Body_E. In all other
123 -- cases, Body_E is ignored and must be Empty.
125 --------------------------
126 -- Check_Code_Statement --
127 --------------------------
129 procedure Check_Code_Statement (N : Node_Id) is
131 -- If volatile, nothing to worry about
133 if Is_Asm_Volatile (N) then
137 -- Warn if no input or no output
139 Setup_Asm_Inputs (N);
141 if No (Asm_Input_Value) then
143 ("?code statement with no inputs should usually be Volatile!", N);
147 Setup_Asm_Outputs (N);
149 if No (Asm_Output_Variable) then
151 ("?code statement with no outputs should usually be Volatile!", N);
155 -- Check multiple code statements in a row
157 if Is_List_Member (N)
158 and then Present (Prev (N))
159 and then Nkind (Prev (N)) = N_Code_Statement
162 ("?code statements in sequence should usually be Volatile!", N);
164 ("\?(suggest using template with multiple instructions)!", N);
166 end Check_Code_Statement;
168 ---------------------------------
169 -- Check_Infinite_Loop_Warning --
170 ---------------------------------
172 -- The case we look for is a while loop which tests a local variable, where
173 -- there is no obvious direct or possible indirect update of the variable
174 -- within the body of the loop.
176 procedure Check_Infinite_Loop_Warning (Loop_Statement : Node_Id) is
177 Iter : constant Node_Id := Iteration_Scheme (Loop_Statement);
179 Ref : Node_Id := Empty;
180 -- Reference in iteration scheme to variable that may not be modified in
181 -- loop, indicating a possible infinite loop.
183 Var : Entity_Id := Empty;
184 -- Corresponding entity (entity of Ref)
186 procedure Find_Var (N : Node_Id);
187 -- Inspect condition to see if it depends on a single entity reference.
188 -- If so, Ref is set to point to the reference node, and Var is set to
189 -- the referenced Entity.
191 function Has_Indirection (T : Entity_Id) return Boolean;
192 -- If the controlling variable is an access type, or is a record type
193 -- with access components, assume that it is changed indirectly and
194 -- suppress the warning. As a concession to low-level programming, in
195 -- particular within Declib, we also suppress warnings on a record
196 -- type that contains components of type Address or Short_Address.
198 function Is_Suspicious_Function_Name (E : Entity_Id) return Boolean;
199 -- Given an entity name, see if the name appears to have something to
200 -- do with I/O or network stuff, and if so, return True. Used to kill
201 -- some false positives on a heuristic basis that such functions will
202 -- likely have some strange side effect dependencies. A rather funny
203 -- kludge, but warning messages are in the heuristics business.
205 function Test_Ref (N : Node_Id) return Traverse_Result;
206 -- Test for reference to variable in question. Returns Abandon if
207 -- matching reference found.
209 function Find_Ref is new Traverse_Func (Test_Ref);
210 -- Function to traverse body of procedure. Returns Abandon if matching
217 procedure Find_Var (N : Node_Id) is
219 -- Condition is a direct variable reference
221 if Is_Entity_Name (N) then
225 -- Case of condition is a comparison with compile time known value
227 elsif Nkind (N) in N_Op_Compare then
228 if Compile_Time_Known_Value (Right_Opnd (N)) then
229 Find_Var (Left_Opnd (N));
231 elsif Compile_Time_Known_Value (Left_Opnd (N)) then
232 Find_Var (Right_Opnd (N));
234 -- Ignore any other comparison
240 -- If condition is a negation, check its operand
242 elsif Nkind (N) = N_Op_Not then
243 Find_Var (Right_Opnd (N));
245 -- Case of condition is function call
247 elsif Nkind (N) = N_Function_Call then
249 -- Forget it if function name is not entity, who knows what
250 -- we might be calling?
252 if not Is_Entity_Name (Name (N)) then
255 -- Forget it if warnings are suppressed on function entity
257 elsif Warnings_Off (Entity (Name (N))) then
260 -- Forget it if function name is suspicious. A strange test
261 -- but warning generation is in the heuristics business!
263 elsif Is_Suspicious_Function_Name (Entity (Name (N))) then
267 -- OK, see if we have one argument
270 PA : constant List_Id := Parameter_Associations (N);
273 -- One argument, so check the argument
276 and then List_Length (PA) = 1
278 if Nkind (First (PA)) = N_Parameter_Association then
279 Find_Var (Explicit_Actual_Parameter (First (PA)));
281 Find_Var (First (PA));
291 -- Any other kind of node is not something we warn for
298 ---------------------
299 -- Has_Indirection --
300 ---------------------
302 function Has_Indirection (T : Entity_Id) return Boolean is
307 if Is_Access_Type (T) then
310 elsif Is_Private_Type (T)
311 and then Present (Full_View (T))
312 and then Is_Access_Type (Full_View (T))
316 elsif Is_Record_Type (T) then
319 elsif Is_Private_Type (T)
320 and then Present (Full_View (T))
321 and then Is_Record_Type (Full_View (T))
323 Rec := Full_View (T);
328 Comp := First_Component (Rec);
329 while Present (Comp) loop
330 if Is_Access_Type (Etype (Comp))
331 or else Is_Descendent_Of_Address (Etype (Comp))
336 Next_Component (Comp);
342 ---------------------------------
343 -- Is_Suspicious_Function_Name --
344 ---------------------------------
346 function Is_Suspicious_Function_Name (E : Entity_Id) return Boolean is
349 function Substring_Present (S : String) return Boolean;
350 -- Returns True if name buffer has given string delimited by non-
351 -- alphabetic characters or by end of string. S is lower case.
353 -----------------------
354 -- Substring_Present --
355 -----------------------
357 function Substring_Present (S : String) return Boolean is
358 Len : constant Natural := S'Length;
361 for J in 1 .. Name_Len - (Len - 1) loop
362 if Name_Buffer (J .. J + (Len - 1)) = S
365 or else Name_Buffer (J - 1) not in 'a' .. 'z')
368 or else Name_Buffer (J + Len) not in 'a' .. 'z')
375 end Substring_Present;
377 -- Start of processing for Is_Suspicious_Function_Name
381 while Present (S) and then S /= Standard_Standard loop
382 Get_Name_String (Chars (S));
384 if Substring_Present ("io")
385 or else Substring_Present ("file")
386 or else Substring_Present ("network")
395 end Is_Suspicious_Function_Name;
401 function Test_Ref (N : Node_Id) return Traverse_Result is
403 -- Waste of time to look at iteration scheme
408 -- Direct reference to variable in question
410 elsif Is_Entity_Name (N)
411 and then Present (Entity (N))
412 and then Entity (N) = Var
414 -- If this is an Lvalue, then definitely abandon, since
415 -- this could be a direct modification of the variable.
417 if May_Be_Lvalue (N) then
421 -- If we appear in the context of a procedure call, then also
422 -- abandon, since there may be issues of non-visible side
423 -- effects going on in the call.
431 exit when P = Loop_Statement;
433 if Nkind (P) = N_Procedure_Call_Statement then
439 -- Reference to variable renaming variable in question
441 elsif Is_Entity_Name (N)
442 and then Present (Entity (N))
443 and then Ekind (Entity (N)) = E_Variable
444 and then Present (Renamed_Object (Entity (N)))
445 and then Is_Entity_Name (Renamed_Object (Entity (N)))
446 and then Entity (Renamed_Object (Entity (N))) = Var
447 and then May_Be_Lvalue (N)
451 -- Call to subprogram
453 elsif Nkind (N) = N_Procedure_Call_Statement
454 or else Nkind (N) = N_Function_Call
456 -- If subprogram is within the scope of the entity we are dealing
457 -- with as the loop variable, then it could modify this parameter,
458 -- so we abandon in this case. In the case of a subprogram that is
459 -- not an entity we also abandon. The check for no entity being
460 -- present is a defense against previous errors.
462 if not Is_Entity_Name (Name (N))
463 or else No (Entity (Name (N)))
464 or else Scope_Within (Entity (Name (N)), Scope (Var))
470 -- All OK, continue scan
475 -- Start of processing for Check_Infinite_Loop_Warning
478 -- We need a while iteration with no condition actions. Conditions
479 -- actions just make things too complicated to get the warning right.
482 or else No (Condition (Iter))
483 or else Present (Condition_Actions (Iter))
484 or else Debug_Flag_Dot_W
489 -- Initial conditions met, see if condition is of right form
491 Find_Var (Condition (Iter));
493 -- Nothing to do if local variable from source not found
496 or else Ekind (Var) /= E_Variable
497 or else Is_Library_Level_Entity (Var)
498 or else not Comes_From_Source (Var)
502 -- Nothing to do if there is some indirection involved (assume that the
503 -- designated variable might be modified in some way we don't see).
505 elsif Has_Indirection (Etype (Var)) then
508 -- Same sort of thing for volatile variable, might be modified by
509 -- some other task or by the operating system in some way.
511 elsif Is_Volatile (Var) then
515 -- Filter out case of original statement sequence starting with delay.
516 -- We assume this is a multi-tasking program and that the condition
517 -- is affected by other threads (some kind of busy wait).
520 Fstm : constant Node_Id :=
521 Original_Node (First (Statements (Loop_Statement)));
523 if Nkind (Fstm) = N_Delay_Relative_Statement
524 or else Nkind (Fstm) = N_Delay_Until_Statement
530 -- We have a variable reference of the right form, now we scan the loop
531 -- body to see if it looks like it might not be modified
533 if Find_Ref (Loop_Statement) = OK then
535 ("?variable& is not modified in loop body!", Ref, Var);
537 ("\?possible infinite loop!", Ref);
539 end Check_Infinite_Loop_Warning;
541 ----------------------
542 -- Check_References --
543 ----------------------
545 procedure Check_References (E : Entity_Id; Anod : Node_Id := Empty) is
551 Accept_Statement : Node_Id) return Entity_Id;
552 -- For an entry formal entity from an entry declaration, find the
553 -- corrsesponding body formal from the given accept statement.
555 function Missing_Subunits return Boolean;
556 -- We suppress warnings when there are missing subunits, because this
557 -- may generate too many false positives: entities in a parent may only
558 -- be referenced in one of the subunits. We make an exception for
559 -- subunits that contain no other stubs.
561 procedure Output_Reference_Error (M : String);
562 -- Used to output an error message. Deals with posting the error on the
563 -- body formal in the accept case.
565 function Publicly_Referenceable (Ent : Entity_Id) return Boolean;
566 -- This is true if the entity in question is potentially referenceable
567 -- from another unit. This is true for entities in packages that are at
568 -- the library level.
570 ----------------------
571 -- Missing_Subunits --
572 ----------------------
574 function Missing_Subunits return Boolean is
578 if not Unloaded_Subunits then
580 -- Normal compilation, all subunits are present
584 elsif E /= Main_Unit_Entity then
586 -- No warnings on a stub that is not the main unit
590 elsif Nkind (Unit_Declaration_Node (E)) in N_Proper_Body then
591 D := First (Declarations (Unit_Declaration_Node (E)));
592 while Present (D) loop
594 -- No warnings if the proper body contains nested stubs
596 if Nkind (D) in N_Body_Stub then
606 -- Missing stubs elsewhere
610 end Missing_Subunits;
618 Accept_Statement : Node_Id) return Entity_Id
620 Body_Param : Node_Id;
624 -- Loop to find matching parameter in accept statement
626 Body_Param := First (Parameter_Specifications (Accept_Statement));
627 while Present (Body_Param) loop
628 Body_E := Defining_Identifier (Body_Param);
630 if Chars (Body_E) = Chars (E) then
637 -- Should never fall through, should always find a match
642 ----------------------------
643 -- Output_Reference_Error --
644 ----------------------------
646 procedure Output_Reference_Error (M : String) is
648 -- Don't output message for IN OUT formal unless we have the warning
649 -- flag specifically set. It is a bit odd to distinguish IN OUT
650 -- formals from other cases. This distinction is historical in
651 -- nature. Warnings for IN OUT formals were added fairly late.
653 if Ekind (E1) = E_In_Out_Parameter
654 and then not Check_Unreferenced_Formals
659 -- Other than accept case, post error on defining identifier
664 -- Accept case, find body formal to post the message
667 Error_Msg_NE (M, Body_Formal (E1, Accept_Statement => Anod), E1);
670 end Output_Reference_Error;
672 ----------------------------
673 -- Publicly_Referenceable --
674 ----------------------------
676 function Publicly_Referenceable (Ent : Entity_Id) return Boolean is
681 -- A formal parameter is never referenceable outside the body of its
682 -- subprogram or entry.
684 if Is_Formal (Ent) then
688 -- Examine parents to look for a library level package spec. But if
689 -- we find a body or block or other similar construct along the way,
690 -- we cannot be referenced.
697 -- If we get to top of tree, then publicly referenceable
702 -- If we reach a generic package declaration, then always
703 -- consider this referenceable, since any instantiation will
704 -- have access to the entities in the generic package. Note
705 -- that the package itself may not be instantiated, but then
706 -- we will get a warning for the package entity.
708 -- Note that generic formal parameters are themselves not
709 -- publicly referenceable in an instance, and warnings on them
712 when N_Generic_Package_Declaration =>
714 not Is_List_Member (Prev)
715 or else List_Containing (Prev)
716 /= Generic_Formal_Declarations (P);
718 -- Similarly, the generic formals of a generic subprogram are
721 when N_Generic_Subprogram_Declaration =>
722 if Is_List_Member (Prev)
723 and then List_Containing (Prev) =
724 Generic_Formal_Declarations (P)
731 -- If we reach a subprogram body, entity is not referenceable
732 -- unless it is the defining entity of the body. This will
733 -- happen, e.g. when a function is an attribute renaming that
734 -- is rewritten as a body.
736 when N_Subprogram_Body =>
737 if Ent /= Defining_Entity (P) then
743 -- If we reach any other body, definitely not referenceable
745 when N_Package_Body |
753 -- For all other cases, keep looking up tree
760 end Publicly_Referenceable;
762 -- Start of processing for Check_References
765 -- No messages if warnings are suppressed, or if we have detected any
766 -- real errors so far (this last check avoids junk messages resulting
767 -- from errors, e.g. a subunit that is not loaded).
769 if Warning_Mode = Suppress
770 or else Serious_Errors_Detected /= 0
775 -- We also skip the messages if any subunits were not loaded (see
776 -- comment in Sem_Ch10 to understand how this is set, and why it is
777 -- necessary to suppress the warnings in this case).
779 if Missing_Subunits then
783 -- Otherwise loop through entities, looking for suspicious stuff
785 E1 := First_Entity (E);
786 while Present (E1) loop
788 -- We only look at source entities with warning flag on. We also
789 -- ignore objects whose type or base type has warnings suppressed.
791 if Comes_From_Source (E1)
792 and then not Warnings_Off (E1)
793 and then not Warnings_Off (Etype (E1))
794 and then not Warnings_Off (Base_Type (Etype (E1)))
796 -- We are interested in variables and out/in-out parameters, but
797 -- we exclude protected types, too complicated to worry about.
799 if Ekind (E1) = E_Variable
801 ((Ekind (E1) = E_Out_Parameter
802 or else Ekind (E1) = E_In_Out_Parameter)
803 and then not Is_Protected_Type (Current_Scope))
805 -- Case of an unassigned variable
807 -- First gather any Unset_Reference indication for E1. In the
808 -- case of a parameter, it is the Spec_Entity that is relevant.
810 if Ekind (E1) = E_Out_Parameter
811 and then Present (Spec_Entity (E1))
813 UR := Unset_Reference (Spec_Entity (E1));
815 UR := Unset_Reference (E1);
818 -- If the entity is an out parameter of the current subprogram
819 -- body, check the warning status of the parameter in the spec.
822 and then Present (Spec_Entity (E1))
823 and then Warnings_Off (Spec_Entity (E1))
828 and then Is_Access_Type (Etype (E1))
830 -- For access types, the only time we made a UR entry was
831 -- for a dereference, and so we post the appropriate warning
832 -- here (note that the dereference may not be explicit in
833 -- the source, for example in the case of a dispatching call
834 -- with an anonymous access controlling formal, or of an
835 -- assignment of a pointer involving discriminant check
836 -- on the designated object).
838 Error_Msg_NE ("?& may be null!", UR, E1);
841 -- Case of variable that could be a constant. Note that we
842 -- never signal such messages for generic package entities,
843 -- since a given instance could have modifications outside
846 elsif Warn_On_Constant
847 and then ((Ekind (E1) = E_Variable
848 and then Has_Initial_Value (E1))
850 Ekind (E1) = E_In_Out_Parameter)
851 and then Never_Set_In_Source_Check_Spec (E1)
852 and then not Address_Taken (E1)
853 and then not Generic_Package_Spec_Entity (E1)
855 -- A special case, if this variable is volatile and not
856 -- imported, it is not helpful to tell the programmer
857 -- to mark the variable as constant, since this would be
858 -- illegal by virtue of RM C.6(13).
860 if (Is_Volatile (E1) or else Has_Volatile_Components (E1))
861 and then not Is_Imported (E1)
864 ("?& is not modified, volatile has no effect!", E1);
866 -- Another special case, Exception_Occurrence, this catches
867 -- the case of exception choice (and a bit more too, but not
868 -- worth doing more investigation here).
870 elsif Is_RTE (Etype (E1), RE_Exception_Occurrence) then
873 -- Here we give the warning if referenced and no pragma
874 -- Unreferenced is present.
877 if Ekind (E1) = E_Variable then
878 if Referenced_Check_Spec (E1)
879 and then not Has_Pragma_Unreferenced_Check_Spec (E1)
882 ("?& is not modified, "
883 & "could be declared constant!",
887 else pragma Assert (Ekind (E1) = E_In_Out_Parameter);
888 if Referenced_Check_Spec (E1)
890 not Has_Pragma_Unreferenced_Check_Spec (E1)
892 -- Suppress warning if private type, since in this
893 -- case it may be quite reasonable for the logical
894 -- view to be in out, even if the implementation
895 -- ends up using access types.
897 if Has_Private_Declaration (Etype (E1)) then
900 -- Suppress warning for any composite type, since
901 -- for composites it seems quite reasonable to pass
902 -- a value of the composite type and then modify
905 elsif Is_Composite_Type (Etype (E1)) then
908 -- Suppress warning for parameter of dispatching
909 -- operation, since it is quite reasonable to have
910 -- an operation that is overridden, and for some
911 -- subclasses needs to be IN OUT and for others
912 -- the parameter does not happen to be assigned.
914 elsif Is_Dispatching_Operation
915 (Scope (Goto_Spec_Entity (E1)))
919 -- OK, looks like warning for an IN OUT parameter
920 -- that could be IN makes sense, but we delay the
921 -- output of the warning, pending possibly finding
922 -- out later on that the associated subprogram is
923 -- used as a generic actual, or its address/access
924 -- is taken. In these two cases, we suppress the
925 -- warning because the context may force use of IN
926 -- OUT, even if in this particular case the formal
930 In_Out_Warnings.Append (E1);
936 -- Other cases of a variable never set in source
938 elsif Never_Set_In_Source_Check_Spec (E1)
940 -- No warning if warning for this case turned off
942 and then Warn_On_No_Value_Assigned
944 -- No warning if address taken somewhere
946 and then not Address_Taken (E1)
948 -- No warning if explicit initial value
950 and then not Has_Initial_Value (E1)
952 -- No warning for generic package spec entities, since we
953 -- might set them in a child unit or something like that
955 and then not Generic_Package_Spec_Entity (E1)
957 -- No warning if fully initialized type, except that for
958 -- this purpose we do not consider access types to qualify
959 -- as fully initialized types (relying on an access type
960 -- variable being null when it is never set is a bit odd!)
962 -- Also we generate warning for an out parameter that is
963 -- never referenced, since again it seems odd to rely on
964 -- default initialization to set an out parameter value.
966 and then (Is_Access_Type (Etype (E1))
967 or else Ekind (E1) = E_Out_Parameter
968 or else not Is_Fully_Initialized_Type (Etype (E1)))
970 -- Do not output complaint about never being assigned a
971 -- value if a pragma Unreferenced applies to the variable
972 -- we are examining, or if it is a parameter, if there is
973 -- a pragma Unreferenced for the corresponding spec.
975 if Has_Pragma_Unreferenced_Check_Spec (E1)
976 or else Has_Pragma_Unreferenced_Objects (Etype (E1))
980 -- Case of unreferenced formal
982 elsif Is_Formal (E1) then
983 if Referenced_Check_Spec (E1) then
984 Output_Reference_Error
985 ("?formal parameter& is read but never assigned!");
987 Output_Reference_Error
988 ("?formal parameter& is not referenced!");
994 if Referenced (E1) then
995 Output_Reference_Error
996 ("?variable& is read but never assigned!");
998 Output_Reference_Error
999 ("?variable& is never read and never assigned!");
1002 -- Deal with special case where this variable is
1003 -- hidden by a loop variable
1005 if Ekind (E1) = E_Variable
1006 and then Present (Hiding_Loop_Variable (E1))
1009 ("?for loop implicitly declares loop variable!",
1010 Hiding_Loop_Variable (E1));
1012 Error_Msg_Sloc := Sloc (E1);
1014 ("\?declaration hides & declared#!",
1015 Hiding_Loop_Variable (E1));
1022 -- Check for unset reference
1024 if Warn_On_No_Value_Assigned and then Present (UR) then
1026 -- For other than access type, go back to original node to
1027 -- deal with case where original unset reference has been
1028 -- rewritten during expansion.
1030 -- In some cases, the original node may be a type conversion
1031 -- or qualification, and in this case we want the object
1034 UR := Original_Node (UR);
1035 while Nkind (UR) = N_Type_Conversion
1036 or else Nkind (UR) = N_Qualified_Expression
1038 UR := Expression (UR);
1041 -- Here we issue the warning, all checks completed
1043 -- If we have a return statement, this was a case of an OUT
1044 -- parameter not being set at the time of the return. (Note:
1045 -- it can't be N_Extended_Return_Statement, because those
1046 -- are only for functions, and functions do not allow OUT
1049 if Nkind (UR) = N_Simple_Return_Statement then
1051 ("?OUT parameter& not set before return", UR, E1);
1053 -- If the unset reference is prefix of a selected component
1054 -- that comes from source, mention the component as well. If
1055 -- the selected component comes from expansion, all we know
1056 -- is that the entity is not fully initialized at the point
1057 -- of the reference. Locate an unintialized component to get
1058 -- a better error message.
1060 elsif Nkind (Parent (UR)) = N_Selected_Component then
1061 Error_Msg_Node_2 := Selector_Name (Parent (UR));
1063 if not Comes_From_Source (Parent (UR)) then
1068 Comp := First_Entity (Etype (E1));
1069 while Present (Comp) loop
1070 if Ekind (Comp) = E_Component
1071 and then Nkind (Parent (Comp)) =
1072 N_Component_Declaration
1073 and then No (Expression (Parent (Comp)))
1075 Error_Msg_Node_2 := Comp;
1084 -- Issue proper warning. This is a case of referencing
1085 -- a variable before it has been explicitly assigned.
1086 -- For access types, UR was only set for dereferences,
1087 -- so the issue is that the value may be null.
1089 if Is_Access_Type (Etype (Parent (UR))) then
1090 Error_Msg_N ("?`&.&` may be null!", UR);
1093 ("?`&.&` may be referenced before it has a value!",
1097 -- All other cases of unset reference active
1101 ("?& may be referenced before it has a value!",
1109 -- Then check for unreferenced entities. Note that we are only
1110 -- interested in entities which do not have the Referenced flag
1111 -- set. The Referenced_As_LHS flag is interesting only if the
1112 -- Referenced flag is not set.
1114 if not Referenced_Check_Spec (E1)
1116 -- Check that warnings on unreferenced entities are enabled
1118 and then ((Check_Unreferenced and then not Is_Formal (E1))
1120 (Check_Unreferenced_Formals and then Is_Formal (E1))
1122 (Warn_On_Modified_Unread
1123 and then Referenced_As_LHS_Check_Spec (E1)))
1125 -- Labels, and enumeration literals, and exceptions. The
1126 -- warnings are also placed on local packages that cannot be
1127 -- referenced from elsewhere, including those declared within a
1130 and then (Is_Object (E1)
1134 Ekind (E1) = E_Label
1136 Ekind (E1) = E_Exception
1138 Ekind (E1) = E_Named_Integer
1140 Ekind (E1) = E_Named_Real
1142 Is_Overloadable (E1)
1144 (Ekind (E1) = E_Package
1146 (Ekind (E) = E_Function
1147 or else Ekind (E) = E_Package_Body
1148 or else Ekind (E) = E_Procedure
1149 or else Ekind (E) = E_Subprogram_Body
1150 or else Ekind (E) = E_Block)))
1152 -- Exclude instantiations, since there is no reason why every
1153 -- entity in an instantiation should be referenced.
1155 and then Instantiation_Location (Sloc (E1)) = No_Location
1157 -- Exclude formal parameters from bodies if the corresponding
1158 -- spec entity has been referenced in the case where there is
1161 and then not (Is_Formal (E1)
1163 Ekind (Scope (E1)) = E_Subprogram_Body
1165 Present (Spec_Entity (E1))
1167 Referenced (Spec_Entity (E1)))
1169 -- Consider private type referenced if full view is referenced
1170 -- If there is not full view, this is a generic type on which
1171 -- warnings are also useful.
1174 not (Is_Private_Type (E1)
1176 Present (Full_View (E1))
1177 and then Referenced (Full_View (E1)))
1179 -- Don't worry about full view, only about private type
1181 and then not Has_Private_Declaration (E1)
1183 -- Eliminate dispatching operations from consideration, we
1184 -- cannot tell if these are referenced or not in any easy
1185 -- manner (note this also catches Adjust/Finalize/Initialize)
1187 and then not Is_Dispatching_Operation (E1)
1189 -- Check entity that can be publicly referenced (we do not give
1190 -- messages for such entities, since there could be other
1191 -- units, not involved in this compilation, that contain
1192 -- relevant references.
1194 and then not Publicly_Referenceable (E1)
1196 -- Class wide types are marked as source entities, but they are
1197 -- not really source entities, and are always created, so we do
1198 -- not care if they are not referenced.
1200 and then Ekind (E1) /= E_Class_Wide_Type
1202 -- Objects other than parameters of task types are allowed to
1203 -- be non-referenced, since they start up tasks!
1205 and then ((Ekind (E1) /= E_Variable
1206 and then Ekind (E1) /= E_Constant
1207 and then Ekind (E1) /= E_Component)
1208 or else not Is_Task_Type (Etype (E1)))
1210 -- For subunits, only place warnings on the main unit itself,
1211 -- since parent units are not completely compiled
1213 and then (Nkind (Unit (Cunit (Main_Unit))) /= N_Subunit
1215 Get_Source_Unit (E1) = Main_Unit)
1217 -- No warning on a return object, because these are often
1218 -- created with a single expression and an implicit return.
1219 -- If the object is a variable there will be a warning
1220 -- indicating that it could be declared constant.
1223 (Ekind (E1) = E_Constant and then Is_Return_Object (E1))
1225 -- Suppress warnings in internal units if not in -gnatg mode
1226 -- (these would be junk warnings for an applications program,
1227 -- since they refer to problems in internal units)
1231 Is_Internal_File_Name
1232 (Unit_File_Name (Get_Source_Unit (E1)))
1234 -- We do not immediately flag the error. This is because we
1235 -- have not expanded generic bodies yet, and they may have
1236 -- the missing reference. So instead we park the entity on a
1237 -- list, for later processing. However for the case of an
1238 -- accept statement we want to output messages now, since
1239 -- we know we already have all information at hand, and we
1240 -- also want to have separate warnings for each accept
1241 -- statement for the same entry.
1243 if Present (Anod) then
1244 pragma Assert (Is_Formal (E1));
1246 -- The unreferenced entity is E1, but post the warning
1247 -- on the body entity for this accept statement.
1249 Warn_On_Unreferenced_Entity
1250 (E1, Body_Formal (E1, Accept_Statement => Anod));
1253 Unreferenced_Entities.Append (E1);
1257 -- Generic units are referenced in the generic body, but if they
1258 -- are not public and never instantiated we want to force a
1259 -- warning on them. We treat them as redundant constructs to
1262 elsif Is_Generic_Subprogram (E1)
1263 and then not Is_Instantiated (E1)
1264 and then not Publicly_Referenceable (E1)
1265 and then Instantiation_Depth (Sloc (E1)) = 0
1266 and then Warn_On_Redundant_Constructs
1268 Unreferenced_Entities.Append (E1);
1270 -- Force warning on entity
1272 Set_Referenced (E1, False);
1276 -- Recurse into nested package or block. Do not recurse into a
1277 -- formal package, because the correponding body is not analyzed.
1280 if ((Ekind (E1) = E_Package or else Ekind (E1) = E_Generic_Package)
1281 and then Nkind (Parent (E1)) = N_Package_Specification
1283 Nkind (Original_Node (Unit_Declaration_Node (E1)))
1284 /= N_Formal_Package_Declaration)
1286 or else Ekind (E1) = E_Block
1288 Check_References (E1);
1293 end Check_References;
1295 ---------------------------
1296 -- Check_Unset_Reference --
1297 ---------------------------
1299 procedure Check_Unset_Reference (N : Node_Id) is
1300 Typ : constant Entity_Id := Etype (N);
1302 function Is_OK_Fully_Initialized return Boolean;
1303 -- This function returns true if the given node N is fully initialized
1304 -- so that the reference is safe as far as this routine is concerned.
1305 -- Safe generally means that the type of N is a fully initialized type.
1306 -- The one special case is that for access types, which are always fully
1307 -- initialized, we don't consider a dereference OK since it will surely
1308 -- be dereferencing a null value, which won't do.
1310 function Prefix_Has_Dereference (Pref : Node_Id) return Boolean;
1311 -- Used to test indexed or selected component or slice to see if the
1312 -- evaluation of the prefix depends on a dereference, and if so, returns
1313 -- True, in which case we always check the prefix, even if we know that
1314 -- the referenced component is initialized. Pref is the prefix to test.
1316 -----------------------------
1317 -- Is_OK_Fully_Initialized --
1318 -----------------------------
1320 function Is_OK_Fully_Initialized return Boolean is
1322 if Is_Access_Type (Typ) and then Is_Dereferenced (N) then
1325 return Is_Fully_Initialized_Type (Typ);
1327 end Is_OK_Fully_Initialized;
1329 ----------------------------
1330 -- Prefix_Has_Dereference --
1331 ----------------------------
1333 function Prefix_Has_Dereference (Pref : Node_Id) return Boolean is
1335 -- If prefix is of an access type, certainly need a dereference
1337 if Is_Access_Type (Etype (Pref)) then
1340 -- If prefix is explicit dereference, that's a dereference for sure
1342 elsif Nkind (Pref) = N_Explicit_Dereference then
1345 -- If prefix is itself a component reference or slice check prefix
1347 elsif Nkind (Pref) = N_Slice
1348 or else Nkind (Pref) = N_Indexed_Component
1349 or else Nkind (Pref) = N_Selected_Component
1351 return Prefix_Has_Dereference (Prefix (Pref));
1353 -- All other cases do not involve a dereference
1358 end Prefix_Has_Dereference;
1360 -- Start of processing for Check_Unset_Reference
1363 -- Nothing to do if warnings suppressed
1365 if Warning_Mode = Suppress then
1369 -- Ignore reference unless it comes from source. Almost always if we
1370 -- have a reference from generated code, it is bogus (e.g. calls to init
1371 -- procs to set default discriminant values).
1373 if not Comes_From_Source (N) then
1377 -- Otherwise see what kind of node we have. If the entity already
1378 -- has an unset reference, it is not necessarily the earliest in
1379 -- the text, because resolution of the prefix of selected components
1380 -- is completed before the resolution of the selected component itself.
1381 -- as a result, given (R /= null and then R.X > 0), the occurrences
1382 -- of R are examined in right-to-left order. If there is already an
1383 -- unset reference, we check whether N is earlier before proceeding.
1387 -- For identifier or exanded name, examine the entity involved
1389 when N_Identifier | N_Expanded_Name =>
1391 E : constant Entity_Id := Entity (N);
1394 if (Ekind (E) = E_Variable
1396 Ekind (E) = E_Out_Parameter)
1397 and then Never_Set_In_Source_Check_Spec (E)
1398 and then not Has_Initial_Value (E)
1399 and then (No (Unset_Reference (E))
1401 Earlier_In_Extended_Unit
1402 (Sloc (N), Sloc (Unset_Reference (E))))
1403 and then not Warnings_Off (E)
1405 -- We may have an unset reference. The first test is whether
1406 -- this is an access to a discriminant of a record or a
1407 -- component with default initialization. Both of these
1408 -- cases can be ignored, since the actual object that is
1409 -- referenced is definitely initialized. Note that this
1410 -- covers the case of reading discriminants of an out
1411 -- parameter, which is OK even in Ada 83.
1413 -- Note that we are only interested in a direct reference to
1414 -- a record component here. If the reference is via an
1415 -- access type, then the access object is being referenced,
1416 -- not the record, and still deserves an unset reference.
1418 if Nkind (Parent (N)) = N_Selected_Component
1419 and not Is_Access_Type (Typ)
1422 ES : constant Entity_Id :=
1423 Entity (Selector_Name (Parent (N)));
1425 if Ekind (ES) = E_Discriminant
1427 (Present (Declaration_Node (ES))
1429 Present (Expression (Declaration_Node (ES))))
1436 -- Exclude fully initialized types
1438 if Is_OK_Fully_Initialized then
1442 -- Here we have a potential unset reference. But before we
1443 -- get worried about it, we have to make sure that the
1444 -- entity declaration is in the same procedure as the
1445 -- reference, since if they are in separate procedures, then
1446 -- we have no idea about sequential execution.
1448 -- The tests in the loop below catch all such cases, but do
1449 -- allow the reference to appear in a loop, block, or
1450 -- package spec that is nested within the declaring scope.
1451 -- As always, it is possible to construct cases where the
1452 -- warning is wrong, that is why it is a warning!
1456 SE : constant Entity_Id := Scope (E);
1459 SR := Current_Scope;
1461 if SR = Standard_Standard
1462 or else Is_Subprogram (SR)
1463 or else Is_Concurrent_Body (SR)
1464 or else Is_Concurrent_Type (SR)
1472 -- Case of reference has an access type. This is special
1473 -- case since access types are always set to null so
1474 -- cannot be truly uninitialized, but we still want to
1475 -- warn about cases of obvious null dereference.
1477 if Is_Access_Type (Typ) then
1478 Access_Type_Case : declare
1482 (N : Node_Id) return Traverse_Result;
1483 -- Process function for instantation of Traverse
1484 -- below. Checks if N contains reference to other
1485 -- than a dereference.
1487 function Ref_In (Nod : Node_Id) return Boolean;
1488 -- Determines whether Nod contains a reference to
1489 -- the entity E that is not a dereference.
1496 (N : Node_Id) return Traverse_Result
1499 if Is_Entity_Name (N)
1500 and then Entity (N) = E
1501 and then not Is_Dereferenced (N)
1513 function Ref_In (Nod : Node_Id) return Boolean is
1514 function Traverse is new Traverse_Func (Process);
1516 return Traverse (Nod) = Abandon;
1519 -- Start of processing for Access_Type_Case
1522 -- Don't bother if we are inside an instance, since
1523 -- the compilation of the generic template is where
1524 -- the warning should be issued.
1530 -- Don't bother if this is not the main unit. If we
1531 -- try to give this warning for with'ed units, we
1532 -- get some false positives, since we do not record
1533 -- references in other units.
1535 if not In_Extended_Main_Source_Unit (E)
1537 not In_Extended_Main_Source_Unit (N)
1542 -- We are only interested in dereferences
1544 if not Is_Dereferenced (N) then
1548 -- One more check, don't bother with references
1549 -- that are inside conditional statements or while
1550 -- loops if the condition references the entity in
1551 -- question. This avoids most false positives.
1558 if (Nkind (P) = N_If_Statement
1560 Nkind (P) = N_Elsif_Part)
1561 and then Ref_In (Condition (P))
1565 elsif Nkind (P) = N_Loop_Statement
1566 and then Present (Iteration_Scheme (P))
1568 Ref_In (Condition (Iteration_Scheme (P)))
1573 end Access_Type_Case;
1576 -- Here we definitely have a case for giving a warning
1577 -- for a reference to an unset value. But we don't give
1578 -- the warning now. Instead we set the Unset_Reference
1579 -- field of the identifier involved. The reason for this
1580 -- is that if we find the variable is never ever assigned
1581 -- a value then that warning is more important and there
1582 -- is no point in giving the reference warning.
1584 -- If this is an identifier, set the field directly
1586 if Nkind (N) = N_Identifier then
1587 Set_Unset_Reference (E, N);
1589 -- Otherwise it is an expanded name, so set the field of
1590 -- the actual identifier for the reference.
1593 Set_Unset_Reference (E, Selector_Name (N));
1599 -- Indexed component or slice
1601 when N_Indexed_Component | N_Slice =>
1603 -- If prefix does not involve dereferencing an access type, then
1604 -- we know we are OK if the component type is fully initialized,
1605 -- since the component will have been set as part of the default
1608 if not Prefix_Has_Dereference (Prefix (N))
1609 and then Is_OK_Fully_Initialized
1613 -- Look at prefix in access type case, or if the component is not
1614 -- fully initialized.
1617 Check_Unset_Reference (Prefix (N));
1622 when N_Selected_Component =>
1624 Pref : constant Node_Id := Prefix (N);
1625 Ent : constant Entity_Id := Entity (Selector_Name (N));
1628 -- If prefix involves dereferencing an access type, always
1629 -- check the prefix, since the issue then is whether this
1630 -- access value is null.
1632 if Prefix_Has_Dereference (Pref) then
1635 -- Always go to prefix if no selector entity is set. Can this
1636 -- happen in the normal case? Not clear, but it definitely can
1637 -- happen in error cases.
1642 -- For a record component, check some cases where we have
1643 -- reasonable cause to consider that the component is known to
1644 -- be or probably is initialized. In this case, we don't care
1645 -- if the prefix itself was explicitly initialized.
1647 -- Discriminants are always considered initialized
1649 elsif Ekind (Ent) = E_Discriminant then
1652 -- An explicitly initialized component is certainly initialized
1654 elsif Nkind (Parent (Ent)) = N_Component_Declaration
1655 and then Present (Expression (Parent (Ent)))
1659 -- A fully initialized component is initialized
1661 elsif Is_OK_Fully_Initialized then
1665 -- If none of those cases apply, check the record type prefix
1667 Check_Unset_Reference (Pref);
1670 -- For type conversions or qualifications examine the expression
1672 when N_Type_Conversion | N_Qualified_Expression =>
1673 Check_Unset_Reference (Expression (N));
1675 -- For explicit dereference, always check prefix, which will generate
1676 -- an unset reference (since this is a case of dereferencing null).
1678 when N_Explicit_Dereference =>
1679 Check_Unset_Reference (Prefix (N));
1681 -- All other cases are not cases of an unset reference
1687 end Check_Unset_Reference;
1689 ------------------------
1690 -- Check_Unused_Withs --
1691 ------------------------
1693 procedure Check_Unused_Withs (Spec_Unit : Unit_Number_Type := No_Unit) is
1699 Munite : constant Entity_Id := Cunit_Entity (Main_Unit);
1700 -- This is needed for checking the special renaming case
1702 procedure Check_One_Unit (Unit : Unit_Number_Type);
1703 -- Subsidiary procedure, performs checks for specified unit
1705 --------------------
1706 -- Check_One_Unit --
1707 --------------------
1709 procedure Check_One_Unit (Unit : Unit_Number_Type) is
1710 Is_Visible_Renaming : Boolean := False;
1713 procedure Check_Inner_Package (Pack : Entity_Id);
1714 -- Pack is a package local to a unit in a with_clause. Both the
1715 -- unit and Pack are referenced. If none of the entities in Pack
1716 -- are referenced, then the only occurrence of Pack is in a use
1717 -- clause or a pragma, and a warning is worthwhile as well.
1719 function Check_System_Aux return Boolean;
1720 -- Before giving a warning on a with_clause for System, check
1721 -- whether a system extension is present.
1723 function Find_Package_Renaming
1725 L : Entity_Id) return Entity_Id;
1726 -- The only reference to a context unit may be in a renaming
1727 -- declaration. If this renaming declares a visible entity, do
1728 -- not warn that the context clause could be moved to the body,
1729 -- because the renaming may be intented to re-export the unit.
1731 -------------------------
1732 -- Check_Inner_Package --
1733 -------------------------
1735 procedure Check_Inner_Package (Pack : Entity_Id) is
1737 Un : constant Node_Id := Sinfo.Unit (Cnode);
1739 function Check_Use_Clause (N : Node_Id) return Traverse_Result;
1740 -- If N is a use_clause for Pack, emit warning
1742 procedure Check_Use_Clauses is new
1743 Traverse_Proc (Check_Use_Clause);
1745 ----------------------
1746 -- Check_Use_Clause --
1747 ----------------------
1749 function Check_Use_Clause (N : Node_Id) return Traverse_Result is
1753 if Nkind (N) = N_Use_Package_Clause then
1754 Nam := First (Names (N));
1755 while Present (Nam) loop
1756 if Entity (Nam) = Pack then
1757 Error_Msg_Qual_Level := 1;
1759 ("?no entities of package& are referenced!",
1761 Error_Msg_Qual_Level := 0;
1769 end Check_Use_Clause;
1771 -- Start of processing for Check_Inner_Package
1774 E := First_Entity (Pack);
1775 while Present (E) loop
1776 if Referenced_Check_Spec (E) then
1783 -- No entities of the package are referenced. Check whether the
1784 -- reference to the package itself is a use clause, and if so
1785 -- place a warning on it.
1787 Check_Use_Clauses (Un);
1788 end Check_Inner_Package;
1790 ----------------------
1791 -- Check_System_Aux --
1792 ----------------------
1794 function Check_System_Aux return Boolean is
1798 if Chars (Lunit) = Name_System
1799 and then Scope (Lunit) = Standard_Standard
1800 and then Present_System_Aux
1802 Ent := First_Entity (System_Aux_Id);
1803 while Present (Ent) loop
1804 if Referenced_Check_Spec (Ent) then
1813 end Check_System_Aux;
1815 ---------------------------
1816 -- Find_Package_Renaming --
1817 ---------------------------
1819 function Find_Package_Renaming
1821 L : Entity_Id) return Entity_Id
1827 Is_Visible_Renaming := False;
1829 E1 := First_Entity (P);
1830 while Present (E1) loop
1831 if Ekind (E1) = E_Package
1832 and then Renamed_Object (E1) = L
1834 Is_Visible_Renaming := not Is_Hidden (E1);
1837 elsif Ekind (E1) = E_Package
1838 and then No (Renamed_Object (E1))
1839 and then not Is_Generic_Instance (E1)
1841 R := Find_Package_Renaming (E1, L);
1844 Is_Visible_Renaming := not Is_Hidden (R);
1853 end Find_Package_Renaming;
1855 -- Start of processing for Check_One_Unit
1858 Cnode := Cunit (Unit);
1860 -- Only do check in units that are part of the extended main unit.
1861 -- This is actually a necessary restriction, because in the case of
1862 -- subprogram acting as its own specification, there can be with's in
1863 -- subunits that we will not see.
1865 if not In_Extended_Main_Source_Unit (Cnode) then
1868 -- In configurable run time mode, we remove the bodies of non-inlined
1869 -- subprograms, which may lead to spurious warnings, which are
1870 -- clearly undesirable.
1872 elsif Configurable_Run_Time_Mode
1873 and then Is_Predefined_File_Name (Unit_File_Name (Unit))
1878 -- Loop through context items in this unit
1880 Item := First (Context_Items (Cnode));
1881 while Present (Item) loop
1882 if Nkind (Item) = N_With_Clause
1883 and then not Implicit_With (Item)
1884 and then In_Extended_Main_Source_Unit (Item)
1886 Lunit := Entity (Name (Item));
1888 -- Check if this unit is referenced (skip the check if this
1889 -- is explicitly marked by a pragma Unreferenced).
1891 if not Referenced (Lunit)
1892 and then not Has_Pragma_Unreferenced (Lunit)
1894 -- Suppress warnings in internal units if not in -gnatg mode
1895 -- (these would be junk warnings for an application program,
1896 -- since they refer to problems in internal units).
1899 or else not Is_Internal_File_Name (Unit_File_Name (Unit))
1901 -- Here we definitely have a non-referenced unit. If it
1902 -- is the special call for a spec unit, then just set the
1903 -- flag to be read later.
1905 if Unit = Spec_Unit then
1906 Set_Unreferenced_In_Spec (Item);
1908 -- Otherwise simple unreferenced message
1912 ("?unit& is not referenced!", Name (Item));
1916 -- If main unit is a renaming of this unit, then we consider
1917 -- the with to be OK (obviously it is needed in this case!)
1918 -- This may be transitive: the unit in the with_clause may
1919 -- itself be a renaming, in which case both it and the main
1920 -- unit rename the same ultimate package.
1922 elsif Present (Renamed_Entity (Munite))
1924 (Renamed_Entity (Munite) = Lunit
1925 or else Renamed_Entity (Munite) = Renamed_Entity (Lunit))
1929 -- If this unit is referenced, and it is a package, we do
1930 -- another test, to see if any of the entities in the package
1931 -- are referenced. If none of the entities are referenced, we
1932 -- still post a warning. This occurs if the only use of the
1933 -- package is in a use clause, or in a package renaming
1936 elsif Ekind (Lunit) = E_Package then
1938 -- If Is_Instantiated is set, it means that the package is
1939 -- implicitly instantiated (this is the case of parent
1940 -- instance or an actual for a generic package formal), and
1941 -- this counts as a reference.
1943 if Is_Instantiated (Lunit) then
1946 -- If no entities in package, and there is a pragma
1947 -- Elaborate_Body present, then assume that this with is
1948 -- done for purposes of this elaboration.
1950 elsif No (First_Entity (Lunit))
1951 and then Has_Pragma_Elaborate_Body (Lunit)
1955 -- Otherwise see if any entities have been referenced
1958 if Limited_Present (Item) then
1959 Ent := First_Entity (Limited_View (Lunit));
1961 Ent := First_Entity (Lunit);
1965 -- No more entities, and we did not find one that was
1966 -- referenced. Means we have a definite case of a with
1967 -- none of whose entities was referenced.
1971 -- If in spec, just set the flag
1973 if Unit = Spec_Unit then
1974 Set_No_Entities_Ref_In_Spec (Item);
1976 elsif Check_System_Aux then
1979 -- Else give the warning
1983 ("?no entities of & are referenced!",
1986 -- Look for renamings of this package, and flag
1987 -- them as well. If the original package has
1988 -- warnings off, we suppress the warning on the
1989 -- renaming as well.
1991 Pack := Find_Package_Renaming (Munite, Lunit);
1994 and then not Warnings_Off (Lunit)
1997 ("?no entities of & are referenced!",
1998 Unit_Declaration_Node (Pack),
2005 -- Case of entity being referenced. The reference may
2006 -- come from a limited_with_clause, in which case the
2007 -- limited view of the entity carries the flag.
2009 elsif Referenced_Check_Spec (Ent)
2010 or else Referenced_As_LHS_Check_Spec (Ent)
2012 (From_With_Type (Ent)
2013 and then Is_Incomplete_Type (Ent)
2014 and then Present (Non_Limited_View (Ent))
2015 and then Referenced (Non_Limited_View (Ent)))
2017 -- This means that the with is indeed fine, in that
2018 -- it is definitely needed somewhere, and we can
2019 -- quit worrying about this one...
2021 -- Except for one little detail: if either of the
2022 -- flags was set during spec processing, this is
2023 -- where we complain that the with could be moved
2024 -- from the spec. If the spec contains a visible
2025 -- renaming of the package, inhibit warning to move
2026 -- with_clause to body.
2028 if Ekind (Munite) = E_Package_Body then
2030 Find_Package_Renaming
2031 (Spec_Entity (Munite), Lunit);
2034 if Unreferenced_In_Spec (Item) then
2036 ("?unit& is not referenced in spec!",
2039 elsif No_Entities_Ref_In_Spec (Item) then
2041 ("?no entities of & are referenced in spec!",
2045 if Ekind (Ent) = E_Package then
2046 Check_Inner_Package (Ent);
2052 if not Is_Visible_Renaming then
2054 ("\?with clause might be moved to body!",
2060 -- Move to next entity to continue search
2068 -- For a generic package, the only interesting kind of
2069 -- reference is an instantiation, since entities cannot be
2070 -- referenced directly.
2072 elsif Is_Generic_Unit (Lunit) then
2074 -- Unit was never instantiated, set flag for case of spec
2075 -- call, or give warning for normal call.
2077 if not Is_Instantiated (Lunit) then
2078 if Unit = Spec_Unit then
2079 Set_Unreferenced_In_Spec (Item);
2082 ("?unit& is never instantiated!", Name (Item));
2085 -- If unit was indeed instantiated, make sure that flag is
2086 -- not set showing it was uninstantiated in the spec, and if
2087 -- so, give warning.
2089 elsif Unreferenced_In_Spec (Item) then
2091 ("?unit& is not instantiated in spec!", Name (Item));
2093 ("\?with clause can be moved to body!", Name (Item));
2103 -- Start of processing for Check_Unused_Withs
2106 if not Opt.Check_Withs
2107 or else Operating_Mode = Check_Syntax
2112 -- Flag any unused with clauses, but skip this step if we are compiling
2113 -- a subunit on its own, since we do not have enough information to
2114 -- determine whether with's are used. We will get the relevant warnings
2115 -- when we compile the parent. This is the normal style of GNAT
2116 -- compilation in any case.
2118 if Nkind (Unit (Cunit (Main_Unit))) = N_Subunit then
2122 -- Process specified units
2124 if Spec_Unit = No_Unit then
2126 -- For main call, check all units
2128 for Unit in Main_Unit .. Last_Unit loop
2129 Check_One_Unit (Unit);
2133 -- For call for spec, check only the spec
2135 Check_One_Unit (Spec_Unit);
2137 end Check_Unused_Withs;
2139 ---------------------------------
2140 -- Generic_Package_Spec_Entity --
2141 ---------------------------------
2143 function Generic_Package_Spec_Entity (E : Entity_Id) return Boolean is
2147 if Is_Package_Body_Entity (E) then
2153 if S = Standard_Standard then
2156 elsif Ekind (S) = E_Generic_Package then
2159 elsif Ekind (S) = E_Package then
2167 end Generic_Package_Spec_Entity;
2169 ----------------------
2170 -- Goto_Spec_Entity --
2171 ----------------------
2173 function Goto_Spec_Entity (E : Entity_Id) return Entity_Id is
2176 and then Present (Spec_Entity (E))
2178 return Spec_Entity (E);
2182 end Goto_Spec_Entity;
2184 ----------------------------------------
2185 -- Has_Pragma_Unreferenced_Check_Spec --
2186 ----------------------------------------
2188 function Has_Pragma_Unreferenced_Check_Spec
2189 (E : Entity_Id) return Boolean
2192 if Is_Formal (E) and then Present (Spec_Entity (E)) then
2193 return Has_Pragma_Unreferenced (E)
2195 Has_Pragma_Unreferenced (Spec_Entity (E));
2197 return Has_Pragma_Unreferenced (E);
2199 end Has_Pragma_Unreferenced_Check_Spec;
2201 ------------------------------------
2202 -- Never_Set_In_Source_Check_Spec --
2203 ------------------------------------
2205 function Never_Set_In_Source_Check_Spec (E : Entity_Id) return Boolean is
2207 if Is_Formal (E) and then Present (Spec_Entity (E)) then
2208 return Never_Set_In_Source (E)
2210 Never_Set_In_Source (Spec_Entity (E));
2212 return Never_Set_In_Source (E);
2214 end Never_Set_In_Source_Check_Spec;
2216 -------------------------------------
2217 -- Operand_Has_Warnings_Suppressed --
2218 -------------------------------------
2220 function Operand_Has_Warnings_Suppressed (N : Node_Id) return Boolean is
2222 function Check_For_Warnings (N : Node_Id) return Traverse_Result;
2223 -- Function used to check one node to see if it is or was originally
2224 -- a reference to an entity for which Warnings are off. If so, Abandon
2225 -- is returned, otherwise OK_Orig is returned to continue the traversal
2226 -- of the original expression.
2228 function Traverse is new Traverse_Func (Check_For_Warnings);
2229 -- Function used to traverse tree looking for warnings
2231 ------------------------
2232 -- Check_For_Warnings --
2233 ------------------------
2235 function Check_For_Warnings (N : Node_Id) return Traverse_Result is
2236 R : constant Node_Id := Original_Node (N);
2239 if Nkind (R) in N_Has_Entity
2240 and then Present (Entity (R))
2241 and then Warnings_Off (Entity (R))
2247 end Check_For_Warnings;
2249 -- Start of processing for Operand_Has_Warnings_Suppressed
2252 return Traverse (N) = Abandon;
2254 -- If any exception occurs, then something has gone wrong, and this is
2255 -- only a minor aesthetic issue anyway, so just say we did not find what
2256 -- we are looking for, rather than blow up.
2261 end Operand_Has_Warnings_Suppressed;
2263 -----------------------------------------
2264 -- Output_Non_Modified_In_Out_Warnings --
2265 -----------------------------------------
2267 procedure Output_Non_Modifed_In_Out_Warnings is
2269 function No_Warn_On_In_Out (E : Entity_Id) return Boolean;
2270 -- Given a formal parameter entity E, determines if there is a reason to
2271 -- suppress IN OUT warnings (not modified, could be IN) for formals of
2272 -- the subprogram. We suppress these warnings if Warnings Off is set, or
2273 -- if we have seen the address of the subprogram being taken, or if the
2274 -- subprogram is used as a generic actual (in the latter cases the
2275 -- context may force use of IN OUT, even if the parameter is not
2276 -- modifies for this particular case.
2278 -----------------------
2279 -- No_Warn_On_In_Out --
2280 -----------------------
2282 function No_Warn_On_In_Out (E : Entity_Id) return Boolean is
2283 S : constant Entity_Id := Scope (E);
2285 if Warnings_Off (S) then
2287 elsif Address_Taken (S) then
2289 elsif Used_As_Generic_Actual (S) then
2291 elsif Present (Spec_Entity (E)) then
2292 return No_Warn_On_In_Out (Spec_Entity (E));
2296 end No_Warn_On_In_Out;
2298 -- Start of processing for Output_Non_Modifed_In_Out_Warnings
2301 -- Loop through entities for which a warning may be needed
2303 for J in In_Out_Warnings.First .. In_Out_Warnings.Last loop
2305 E1 : constant Entity_Id := In_Out_Warnings.Table (J);
2308 -- Suppress warning in specific cases (see details in comments for
2309 -- No_Warn_On_In_Out).
2311 if No_Warn_On_In_Out (E1) then
2314 -- Here we generate the warning
2317 Error_Msg_N ("?formal parameter & is not modified!", E1);
2318 Error_Msg_N ("\?mode could be IN instead of `IN OUT`!", E1);
2320 -- Kill any other warnings on this entity, since this is the
2321 -- one that should dominate any other unreferenced warning.
2323 Set_Warnings_Off (E1);
2327 end Output_Non_Modifed_In_Out_Warnings;
2329 ----------------------------------------
2330 -- Output_Obsolescent_Entity_Warnings --
2331 ----------------------------------------
2333 procedure Output_Obsolescent_Entity_Warnings (N : Node_Id; E : Entity_Id) is
2334 P : constant Node_Id := Parent (N);
2340 -- Do not output message if we are the scope of standard. This means
2341 -- we have a reference from a context clause from when it is originally
2342 -- processed, and that's too early to tell whether it is an obsolescent
2343 -- unit doing the with'ing. In Sem_Ch10.Analyze_Compilation_Unit we make
2344 -- sure that we have a later call when the scope is available. This test
2345 -- also eliminates all messages for use clauses, which is fine (we do
2346 -- not want messages for use clauses, since they are always redundant
2347 -- with respect to the associated with clause).
2349 if S = Standard_Standard then
2353 -- Do not output message if we are in scope of an obsolescent package
2357 if Is_Obsolescent (S) then
2362 exit when S = Standard_Standard;
2365 -- Here we will output the message
2367 Error_Msg_Sloc := Sloc (E);
2369 -- Case of with clause
2371 if Nkind (P) = N_With_Clause then
2372 if Ekind (E) = E_Package then
2374 ("?with of obsolescent package& declared#", N, E);
2375 elsif Ekind (E) = E_Procedure then
2377 ("?with of obsolescent procedure& declared#", N, E);
2380 ("?with of obsolescent function& declared#", N, E);
2383 -- If we do not have a with clause, then ignore any reference to an
2384 -- obsolescent package name. We only want to give the one warning of
2385 -- withing the package, not one each time it is used to qualify.
2387 elsif Ekind (E) = E_Package then
2390 -- Procedure call statement
2392 elsif Nkind (P) = N_Procedure_Call_Statement then
2394 ("?call to obsolescent procedure& declared#", N, E);
2398 elsif Nkind (P) = N_Function_Call then
2400 ("?call to obsolescent function& declared#", N, E);
2402 -- Reference to obsolescent type
2404 elsif Is_Type (E) then
2406 ("?reference to obsolescent type& declared#", N, E);
2408 -- Reference to obsolescent component
2410 elsif Ekind (E) = E_Component
2411 or else Ekind (E) = E_Discriminant
2414 ("?reference to obsolescent component& declared#", N, E);
2416 -- Reference to obsolescent variable
2418 elsif Ekind (E) = E_Variable then
2420 ("?reference to obsolescent variable& declared#", N, E);
2422 -- Reference to obsolescent constant
2424 elsif Ekind (E) = E_Constant
2425 or else Ekind (E) in Named_Kind
2428 ("?reference to obsolescent constant& declared#", N, E);
2430 -- Reference to obsolescent enumeration literal
2432 elsif Ekind (E) = E_Enumeration_Literal then
2434 ("?reference to obsolescent enumeration literal& declared#", N, E);
2436 -- Generic message for any other case we missed
2440 ("?reference to obsolescent entity& declared#", N, E);
2443 -- Output additional warning if present
2446 W : constant Node_Id := Obsolescent_Warning (E);
2451 -- This is a warning continuation to start on a new line
2452 Name_Buffer (1) := '\';
2453 Name_Buffer (2) := '\';
2454 Name_Buffer (3) := '?';
2457 -- Add characters to message, and output message. Note that
2458 -- we quote every character of the message since we don't
2459 -- want to process any insertions.
2461 for J in 1 .. String_Length (Strval (W)) loop
2462 Add_Char_To_Name_Buffer (''');
2463 Add_Char_To_Name_Buffer
2464 (Get_Character (Get_String_Char (Strval (W), J)));
2467 Error_Msg_N (Name_Buffer (1 .. Name_Len), N);
2470 end Output_Obsolescent_Entity_Warnings;
2472 ----------------------------------
2473 -- Output_Unreferenced_Messages --
2474 ----------------------------------
2476 procedure Output_Unreferenced_Messages is
2478 for J in Unreferenced_Entities.First ..
2479 Unreferenced_Entities.Last
2481 Warn_On_Unreferenced_Entity (Unreferenced_Entities.Table (J));
2483 end Output_Unreferenced_Messages;
2485 ---------------------------
2486 -- Referenced_Check_Spec --
2487 ---------------------------
2489 function Referenced_Check_Spec (E : Entity_Id) return Boolean is
2491 if Is_Formal (E) and then Present (Spec_Entity (E)) then
2492 return Referenced (E) or else Referenced (Spec_Entity (E));
2494 return Referenced (E);
2496 end Referenced_Check_Spec;
2498 ----------------------------------
2499 -- Referenced_As_LHS_Check_Spec --
2500 ----------------------------------
2502 function Referenced_As_LHS_Check_Spec (E : Entity_Id) return Boolean is
2504 if Is_Formal (E) and then Present (Spec_Entity (E)) then
2505 return Referenced_As_LHS (E)
2506 or else Referenced_As_LHS (Spec_Entity (E));
2508 return Referenced_As_LHS (E);
2510 end Referenced_As_LHS_Check_Spec;
2512 ----------------------------
2513 -- Set_Dot_Warning_Switch --
2514 ----------------------------
2516 function Set_Dot_Warning_Switch (C : Character) return Boolean is
2520 Warn_On_Unrepped_Components := True;
2523 Warn_On_Unrepped_Components := False;
2526 Warn_On_Object_Renames_Function := True;
2529 Warn_On_Object_Renames_Function := False;
2532 Warn_On_Non_Local_Exception := True;
2535 Warn_On_Non_Local_Exception := False;
2542 end Set_Dot_Warning_Switch;
2544 ------------------------
2545 -- Set_Warning_Switch --
2546 ------------------------
2548 function Set_Warning_Switch (C : Character) return Boolean is
2552 Check_Unreferenced := True;
2553 Check_Unreferenced_Formals := True;
2554 Check_Withs := True;
2555 Constant_Condition_Warnings := True;
2556 Implementation_Unit_Warnings := True;
2557 Ineffective_Inline_Warnings := True;
2558 Warn_On_Ada_2005_Compatibility := True;
2559 Warn_On_Assumed_Low_Bound := True;
2560 Warn_On_Bad_Fixed_Value := True;
2561 Warn_On_Constant := True;
2562 Warn_On_Export_Import := True;
2563 Warn_On_Modified_Unread := True;
2564 Warn_On_No_Value_Assigned := True;
2565 Warn_On_Non_Local_Exception := True;
2566 Warn_On_Obsolescent_Feature := True;
2567 Warn_On_Questionable_Missing_Parens := True;
2568 Warn_On_Redundant_Constructs := True;
2569 Warn_On_Object_Renames_Function := True;
2570 Warn_On_Unchecked_Conversion := True;
2571 Warn_On_Unrecognized_Pragma := True;
2572 Warn_On_Unrepped_Components := True;
2575 Check_Unreferenced := False;
2576 Check_Unreferenced_Formals := False;
2577 Check_Withs := False;
2578 Constant_Condition_Warnings := False;
2579 Elab_Warnings := False;
2580 Implementation_Unit_Warnings := False;
2581 Ineffective_Inline_Warnings := False;
2582 Warn_On_Ada_2005_Compatibility := False;
2583 Warn_On_Bad_Fixed_Value := False;
2584 Warn_On_Constant := False;
2585 Warn_On_Deleted_Code := False;
2586 Warn_On_Dereference := False;
2587 Warn_On_Export_Import := False;
2588 Warn_On_Hiding := False;
2589 Warn_On_Modified_Unread := False;
2590 Warn_On_No_Value_Assigned := False;
2591 Warn_On_Non_Local_Exception := False;
2592 Warn_On_Obsolescent_Feature := False;
2593 Warn_On_Questionable_Missing_Parens := False;
2594 Warn_On_Redundant_Constructs := False;
2595 Warn_On_Object_Renames_Function := False;
2596 Warn_On_Unchecked_Conversion := False;
2597 Warn_On_Unrecognized_Pragma := False;
2598 Warn_On_Unrepped_Components := False;
2601 Warn_On_Bad_Fixed_Value := True;
2604 Warn_On_Bad_Fixed_Value := False;
2607 Constant_Condition_Warnings := True;
2610 Constant_Condition_Warnings := False;
2613 Warn_On_Dereference := True;
2616 Warn_On_Dereference := False;
2619 Warning_Mode := Treat_As_Error;
2622 Check_Unreferenced_Formals := True;
2625 Check_Unreferenced_Formals := False;
2628 Warn_On_Unrecognized_Pragma := True;
2631 Warn_On_Unrecognized_Pragma := False;
2634 Warn_On_Hiding := True;
2637 Warn_On_Hiding := False;
2640 Implementation_Unit_Warnings := True;
2643 Implementation_Unit_Warnings := False;
2646 Warn_On_Obsolescent_Feature := True;
2649 Warn_On_Obsolescent_Feature := False;
2652 Warn_On_Constant := True;
2655 Warn_On_Constant := False;
2658 Elab_Warnings := True;
2661 Elab_Warnings := False;
2664 Warn_On_Modified_Unread := True;
2667 Warn_On_Modified_Unread := False;
2670 Warning_Mode := Normal;
2673 Address_Clause_Overlay_Warnings := True;
2676 Address_Clause_Overlay_Warnings := False;
2679 Ineffective_Inline_Warnings := True;
2682 Ineffective_Inline_Warnings := False;
2685 Warn_On_Questionable_Missing_Parens := True;
2688 Warn_On_Questionable_Missing_Parens := False;
2691 Warn_On_Redundant_Constructs := True;
2694 Warn_On_Redundant_Constructs := False;
2697 Warning_Mode := Suppress;
2700 Warn_On_Deleted_Code := True;
2703 Warn_On_Deleted_Code := False;
2706 Check_Unreferenced := True;
2707 Check_Withs := True;
2708 Check_Unreferenced_Formals := True;
2711 Check_Unreferenced := False;
2712 Check_Withs := False;
2713 Check_Unreferenced_Formals := False;
2716 Warn_On_No_Value_Assigned := True;
2719 Warn_On_No_Value_Assigned := False;
2722 Warn_On_Assumed_Low_Bound := True;
2725 Warn_On_Assumed_Low_Bound := False;
2728 Warn_On_Export_Import := True;
2731 Warn_On_Export_Import := False;
2734 Warn_On_Ada_2005_Compatibility := True;
2737 Warn_On_Ada_2005_Compatibility := False;
2740 Warn_On_Unchecked_Conversion := True;
2743 Warn_On_Unchecked_Conversion := False;
2750 end Set_Warning_Switch;
2752 -----------------------------
2753 -- Warn_On_Known_Condition --
2754 -----------------------------
2756 procedure Warn_On_Known_Condition (C : Node_Id) is
2759 procedure Track (N : Node_Id; Loc : Node_Id);
2760 -- Adds continuation warning(s) pointing to reason (assignment or test)
2761 -- for the operand of the conditional having a known value (or at least
2762 -- enough is known about the value to issue the warning). N is the node
2763 -- which is judged to have a known value. Loc is the warning location.
2769 procedure Track (N : Node_Id; Loc : Node_Id) is
2770 Nod : constant Node_Id := Original_Node (N);
2773 if Nkind (Nod) in N_Op_Compare then
2774 Track (Left_Opnd (Nod), Loc);
2775 Track (Right_Opnd (Nod), Loc);
2777 elsif Is_Entity_Name (Nod)
2778 and then Is_Object (Entity (Nod))
2781 CV : constant Node_Id := Current_Value (Entity (Nod));
2784 if Present (CV) then
2785 Error_Msg_Sloc := Sloc (CV);
2787 if Nkind (CV) not in N_Subexpr then
2788 Error_Msg_N ("\\?(see test #)", Loc);
2790 elsif Nkind (Parent (CV)) =
2791 N_Case_Statement_Alternative
2793 Error_Msg_N ("\\?(see case alternative #)", Loc);
2796 Error_Msg_N ("\\?(see assignment #)", Loc);
2803 -- Start of processing for Warn_On_Known_Condition
2806 -- Argument replacement in an inlined body can make conditions static.
2807 -- Do not emit warnings in this case.
2809 if In_Inlined_Body then
2813 if Constant_Condition_Warnings
2814 and then Nkind (C) = N_Identifier
2816 (Entity (C) = Standard_False or else Entity (C) = Standard_True)
2817 and then Comes_From_Source (Original_Node (C))
2818 and then not In_Instance
2820 -- See if this is in a statement or a declaration
2824 -- If tree is not attached, do not issue warning (this is very
2825 -- peculiar, and probably arises from some other error condition)
2830 -- If we are in a declaration, then no warning, since in practice
2831 -- conditionals in declarations are used for intended tests which
2832 -- may be known at compile time, e.g. things like
2834 -- x : constant Integer := 2 + (Word'Size = 32);
2836 -- And a warning is annoying in such cases
2838 elsif Nkind (P) in N_Declaration
2840 Nkind (P) in N_Later_Decl_Item
2844 -- Don't warn in assert pragma, since presumably tests in such
2845 -- a context are very definitely intended, and might well be
2846 -- known at compile time. Note that we have to test the original
2847 -- node, since assert pragmas get rewritten at analysis time.
2849 elsif Nkind (Original_Node (P)) = N_Pragma
2850 and then Chars (Original_Node (P)) = Name_Assert
2855 exit when Is_Statement (P);
2859 -- Here we issue the warning unless some sub-operand has warnings
2860 -- set off, in which case we suppress the warning for the node. If
2861 -- the original expression is an inequality, it has been expanded
2862 -- into a negation, and the value of the original expression is the
2863 -- negation of the equality. If the expression is an entity that
2864 -- appears within a negation, it is clearer to flag the negation
2865 -- itself, and report on its constant value.
2867 if not Operand_Has_Warnings_Suppressed (C) then
2869 True_Branch : Boolean := Entity (C) = Standard_True;
2870 Cond : Node_Id := C;
2873 if Present (Parent (C))
2874 and then Nkind (Parent (C)) = N_Op_Not
2876 True_Branch := not True_Branch;
2881 if Is_Entity_Name (Original_Node (C))
2882 and then Nkind (Cond) /= N_Op_Not
2885 ("object & is always True?", Cond, Original_Node (C));
2886 Track (Original_Node (C), Cond);
2889 Error_Msg_N ("condition is always True?", Cond);
2894 Error_Msg_N ("condition is always False?", Cond);
2900 end Warn_On_Known_Condition;
2902 ------------------------------
2903 -- Warn_On_Suspicious_Index --
2904 ------------------------------
2906 procedure Warn_On_Suspicious_Index (Name : Entity_Id; X : Node_Id) is
2909 -- Set to lower bound for a suspicious type
2912 -- Entity for array reference
2917 function Is_Suspicious_Type (Typ : Entity_Id) return Boolean;
2918 -- Tests to see if Typ is a type for which we may have a suspicious
2919 -- index, namely an unconstrained array type, whose lower bound is
2920 -- either zero or one. If so, True is returned, and Low_Bound is set
2921 -- to this lower bound. If not, False is returned, and Low_Bound is
2922 -- undefined on return.
2924 -- For now, we limite this to standard string types, so any other
2925 -- unconstrained types return False. We may change our minds on this
2926 -- later on, but strings seem the most important case.
2928 procedure Test_Suspicious_Index;
2929 -- Test if index is of suspicious type and if so, generate warning
2931 ------------------------
2932 -- Is_Suspicious_Type --
2933 ------------------------
2935 function Is_Suspicious_Type (Typ : Entity_Id) return Boolean is
2939 if Is_Array_Type (Typ)
2940 and then not Is_Constrained (Typ)
2941 and then Number_Dimensions (Typ) = 1
2942 and then not Warnings_Off (Typ)
2943 and then (Root_Type (Typ) = Standard_String
2945 Root_Type (Typ) = Standard_Wide_String
2947 Root_Type (Typ) = Standard_Wide_Wide_String)
2949 LB := Type_Low_Bound (Etype (First_Index (Typ)));
2951 if Compile_Time_Known_Value (LB) then
2952 Low_Bound := Expr_Value (LB);
2953 return Low_Bound = Uint_0 or else Low_Bound = Uint_1;
2958 end Is_Suspicious_Type;
2960 ---------------------------
2961 -- Test_Suspicious_Index --
2962 ---------------------------
2964 procedure Test_Suspicious_Index is
2966 function Length_Reference (N : Node_Id) return Boolean;
2967 -- Check if node N is of the form Name'Length
2970 -- Generate first warning line
2972 ----------------------
2973 -- Length_Reference --
2974 ----------------------
2976 function Length_Reference (N : Node_Id) return Boolean is
2977 R : constant Node_Id := Original_Node (N);
2980 Nkind (R) = N_Attribute_Reference
2981 and then Attribute_Name (R) = Name_Length
2982 and then Is_Entity_Name (Prefix (R))
2983 and then Entity (Prefix (R)) = Ent;
2984 end Length_Reference;
2992 Error_Msg_Uint_1 := Low_Bound;
2993 Error_Msg_FE ("?index for& may assume lower bound of^", X, Ent);
2996 -- Start of processing for Test_Suspicious_Index
2999 -- Nothing to do if subscript does not come from source (we don't
3000 -- want to give garbage warnings on compiler expanded code, e.g. the
3001 -- loops generated for slice assignments. Sucb junk warnings would
3002 -- be placed on source constructs with no subscript in sight!)
3004 if not Comes_From_Source (Original_Node (X)) then
3008 -- Case where subscript is a constant integer
3010 if Nkind (X) = N_Integer_Literal then
3013 -- Case where original form of subscript is an integer literal
3015 if Nkind (Original_Node (X)) = N_Integer_Literal then
3016 if Intval (X) = Low_Bound then
3018 ("\suggested replacement: `&''First`", X, Ent);
3020 Error_Msg_Uint_1 := Intval (X) - Low_Bound;
3022 ("\suggested replacement: `&''First + ^`", X, Ent);
3026 -- Case where original form of subscript is more complex
3029 -- Build string X'First - 1 + expression where the expression
3030 -- is the original subscript. If the expression starts with "1
3031 -- + ", then the "- 1 + 1" is elided.
3033 Error_Msg_String (1 .. 13) := "'First - 1 + ";
3034 Error_Msg_Strlen := 13;
3037 Sref : Source_Ptr := Sloc (First_Node (Original_Node (X)));
3038 Tref : constant Source_Buffer_Ptr :=
3039 Source_Text (Get_Source_File_Index (Sref));
3040 -- Tref (Sref) is used to scan the subscript
3043 -- Paretheses counter when scanning subscript
3046 -- Tref (Sref) points to start of subscript
3048 -- Elide - 1 if subscript starts with 1 +
3050 if Tref (Sref .. Sref + 2) = "1 +" then
3051 Error_Msg_Strlen := Error_Msg_Strlen - 6;
3054 elsif Tref (Sref .. Sref + 1) = "1+" then
3055 Error_Msg_Strlen := Error_Msg_Strlen - 6;
3059 -- Now we will copy the subscript to the string buffer
3063 -- Count parens, exit if terminating right paren. Note
3064 -- check to ignore paren appearing as character literal.
3066 if Tref (Sref + 1) = '''
3068 Tref (Sref - 1) = '''
3072 if Tref (Sref) = '(' then
3074 elsif Tref (Sref) = ')' then
3080 -- Done if terminating double dot (slice case)
3083 and then (Tref (Sref .. Sref + 1) = ".."
3085 Tref (Sref .. Sref + 2) = " ..");
3087 -- Quit if we have hit EOF character, something wrong
3089 if Tref (Sref) = EOF then
3093 -- String literals are too much of a pain to handle
3095 if Tref (Sref) = '"' or else Tref (Sref) = '%' then
3099 -- If we have a 'Range reference, then this is a case
3100 -- where we cannot easily give a replacement. Don't try!
3102 if Tref (Sref .. Sref + 4) = "range"
3103 and then Tref (Sref - 1) < 'A'
3104 and then Tref (Sref + 5) < 'A'
3109 -- Else store next character
3111 Error_Msg_Strlen := Error_Msg_Strlen + 1;
3112 Error_Msg_String (Error_Msg_Strlen) := Tref (Sref);
3115 -- If we get more than 40 characters then the expression
3116 -- is too long to copy, or something has gone wrong. In
3117 -- either case, just skip the attempt at a suggested fix.
3119 if Error_Msg_Strlen > 40 then
3125 -- Replacement subscript is now in string buffer
3128 ("\suggested replacement: `&~`", Original_Node (X), Ent);
3131 -- Case where subscript is of the form X'Length
3133 elsif Length_Reference (X) then
3135 Error_Msg_Node_2 := Ent;
3137 ("\suggest replacement of `&''Length` by `&''Last`",
3140 -- Case where subscript is of the form X'Length - expression
3142 elsif Nkind (X) = N_Op_Subtract
3143 and then Length_Reference (Left_Opnd (X))
3146 Error_Msg_Node_2 := Ent;
3148 ("\suggest replacement of `&''Length` by `&''Last`",
3149 Left_Opnd (X), Ent);
3151 end Test_Suspicious_Index;
3153 -- Start of processing for Warn_On_Suspicious_Index
3156 -- Only process if warnings activated
3158 if Warn_On_Assumed_Low_Bound then
3160 -- Test if array is simple entity name
3162 if Is_Entity_Name (Name) then
3164 -- Test if array is parameter of unconstrained string type
3166 Ent := Entity (Name);
3170 and then Is_Suspicious_Type (Typ)
3171 and then not Low_Bound_Known (Ent)
3173 Test_Suspicious_Index;
3177 end Warn_On_Suspicious_Index;
3179 --------------------------------------
3180 -- Warn_On_Unassigned_Out_Parameter --
3181 --------------------------------------
3183 procedure Warn_On_Unassigned_Out_Parameter
3184 (Return_Node : Node_Id;
3185 Scope_Id : Entity_Id)
3191 -- Ignore if procedure or return statement does not come from source
3193 if not Comes_From_Source (Scope_Id)
3194 or else not Comes_From_Source (Return_Node)
3199 -- Loop through formals
3201 Form := First_Formal (Scope_Id);
3202 while Present (Form) loop
3204 -- We are only interested in OUT parameters that come from source
3205 -- and are never set in the source, and furthermore only in scalars
3206 -- since non-scalars generate too many false positives.
3208 if Ekind (Form) = E_Out_Parameter
3209 and then Never_Set_In_Source_Check_Spec (Form)
3210 and then Is_Scalar_Type (Etype (Form))
3211 and then not Present (Unset_Reference (Form))
3213 -- Before we issue the warning, an add ad hoc defence against the
3214 -- most common case of false positives with this warning which is
3215 -- the case where there is a Boolean OUT parameter that has been
3216 -- set, and whose meaning is "ignore the values of the other
3217 -- parameters". We can't of course reliably tell this case at
3218 -- compile time, but the following test kills a lot of false
3219 -- positives, without generating a significant number of false
3220 -- negatives (missed real warnings).
3222 Form2 := First_Formal (Scope_Id);
3223 while Present (Form2) loop
3224 if Ekind (Form2) = E_Out_Parameter
3225 and then Root_Type (Etype (Form2)) = Standard_Boolean
3226 and then not Never_Set_In_Source_Check_Spec (Form2)
3231 Next_Formal (Form2);
3234 -- Here all conditionas are met, record possible unset reference
3236 Set_Unset_Reference (Form, Return_Node);
3241 end Warn_On_Unassigned_Out_Parameter;
3243 ---------------------------------
3244 -- Warn_On_Unreferenced_Entity --
3245 ---------------------------------
3247 procedure Warn_On_Unreferenced_Entity
3248 (Spec_E : Entity_Id;
3249 Body_E : Entity_Id := Empty)
3251 E : Entity_Id := Spec_E;
3253 if not Referenced_Check_Spec (E) and then not Warnings_Off (E) then
3257 -- Case of variable that is assigned but not read. We
3258 -- suppress the message if the variable is volatile, has an
3259 -- address clause, or is imported.
3261 if Referenced_As_LHS_Check_Spec (E)
3262 and then No (Address_Clause (E))
3263 and then not Is_Volatile (E)
3265 if Warn_On_Modified_Unread
3266 and then not Is_Imported (E)
3267 and then not Is_Return_Object (E)
3269 -- Suppress message for aliased or renamed variables,
3270 -- since there may be other entities that read the
3271 -- same memory location.
3273 and then not Is_Aliased (E)
3274 and then No (Renamed_Object (E))
3278 ("?variable & is assigned but never read!", E);
3279 Set_Last_Assignment (E, Empty);
3282 -- Normal case of neither assigned nor read
3285 -- We suppress the message for types for which a valid
3286 -- pragma Unreferenced_Objects has been given, otherwise
3287 -- we go ahead and give the message.
3289 if not Has_Pragma_Unreferenced_Objects (Etype (E)) then
3291 -- Distinguish renamed case in message
3293 if Present (Renamed_Object (E))
3294 and then Comes_From_Source (Renamed_Object (E))
3297 ("?renamed variable & is not referenced!", E);
3300 ("?variable & is not referenced!", E);
3306 if Present (Renamed_Object (E))
3307 and then Comes_From_Source (Renamed_Object (E))
3310 ("?renamed constant & is not referenced!", E);
3312 Error_Msg_N ("?constant & is not referenced!", E);
3315 when E_In_Parameter |
3316 E_In_Out_Parameter =>
3318 -- Do not emit message for formals of a renaming, because
3319 -- they are never referenced explicitly.
3321 if Nkind (Original_Node (Unit_Declaration_Node (Scope (E))))
3322 /= N_Subprogram_Renaming_Declaration
3324 -- Suppress this message for an IN OUT parameter of a
3325 -- non-scalar type, since it is normal to have only an
3326 -- assignment in such a case.
3328 if Ekind (E) = E_In_Parameter
3329 or else not Referenced_As_LHS_Check_Spec (E)
3330 or else Is_Scalar_Type (E)
3332 if Present (Body_E) then
3336 ("?formal parameter & is not referenced!", E, Spec_E);
3340 when E_Out_Parameter =>
3343 when E_Named_Integer |
3345 Error_Msg_N ("?named number & is not referenced!", E);
3347 when E_Enumeration_Literal =>
3348 Error_Msg_N ("?literal & is not referenced!", E);
3351 Error_Msg_N ("?function & is not referenced!", E);
3354 Error_Msg_N ("?procedure & is not referenced!", E);
3356 when E_Generic_Procedure =>
3358 ("?generic procedure & is never instantiated!", E);
3360 when E_Generic_Function =>
3362 ("?generic function & is never instantiated!", E);
3365 Error_Msg_N ("?type & is not referenced!", E);
3368 Error_Msg_N ("?& is not referenced!", E);
3371 -- Kill warnings on the entity on which the message has been posted
3373 Set_Warnings_Off (E);
3375 end Warn_On_Unreferenced_Entity;
3377 --------------------------------
3378 -- Warn_On_Useless_Assignment --
3379 --------------------------------
3381 procedure Warn_On_Useless_Assignment
3383 Loc : Source_Ptr := No_Location)
3388 function Check_Ref (N : Node_Id) return Traverse_Result;
3389 -- Used to instantiate Traverse_Func. Returns Abandon if
3390 -- a reference to the entity in question is found.
3392 function Test_No_Refs is new Traverse_Func (Check_Ref);
3398 function Check_Ref (N : Node_Id) return Traverse_Result is
3400 -- Check reference to our identifier. We use name equality here
3401 -- because the exception handlers have not yet been analyzed. This
3402 -- is not quite right, but it really does not matter that we fail
3403 -- to output the warning in some obscure cases of name clashes.
3405 if Nkind (N) = N_Identifier
3406 and then Chars (N) = Chars (Ent)
3414 -- Start of processing for Warn_On_Useless_Assignment
3417 -- Check if this is a case we want to warn on, a variable with the
3418 -- last assignment field set, with warnings enabled, and which is
3419 -- not imported or exported.
3421 if Ekind (Ent) = E_Variable
3422 and then not Is_Return_Object (Ent)
3423 and then Present (Last_Assignment (Ent))
3424 and then not Warnings_Off (Ent)
3425 and then not Has_Pragma_Unreferenced_Check_Spec (Ent)
3426 and then not Is_Imported (Ent)
3427 and then not Is_Exported (Ent)
3429 -- Before we issue the message, check covering exception handlers.
3430 -- Search up tree for enclosing statement sequences and handlers
3432 P := Parent (Last_Assignment (Ent));
3433 while Present (P) loop
3435 -- Something is really wrong if we don't find a handled
3436 -- statement sequence, so just suppress the warning.
3439 Set_Last_Assignment (Ent, Empty);
3442 -- When we hit a package/subprogram body, issue warning and exit
3444 elsif Nkind (P) = N_Subprogram_Body
3445 or else Nkind (P) = N_Package_Body
3447 if Loc = No_Location then
3449 ("?useless assignment to&, value never referenced!",
3450 Last_Assignment (Ent), Ent);
3452 Error_Msg_Sloc := Loc;
3454 ("?useless assignment to&, value overwritten #!",
3455 Last_Assignment (Ent), Ent);
3458 Set_Last_Assignment (Ent, Empty);
3461 -- Enclosing handled sequence of statements
3463 elsif Nkind (P) = N_Handled_Sequence_Of_Statements then
3465 -- Check exception handlers present
3467 if Present (Exception_Handlers (P)) then
3469 -- If we are not at the top level, we regard an inner
3470 -- exception handler as a decisive indicator that we should
3471 -- not generate the warning, since the variable in question
3472 -- may be acceessed after an exception in the outer block.
3474 if Nkind (Parent (P)) /= N_Subprogram_Body
3475 and then Nkind (Parent (P)) /= N_Package_Body
3477 Set_Last_Assignment (Ent, Empty);
3480 -- Otherwise we are at the outer level. An exception
3481 -- handler is significant only if it references the
3482 -- variable in question.
3485 X := First (Exception_Handlers (P));
3486 while Present (X) loop
3487 if Test_No_Refs (X) = Abandon then
3488 Set_Last_Assignment (Ent, Empty);
3501 end Warn_On_Useless_Assignment;
3503 ---------------------------------
3504 -- Warn_On_Useless_Assignments --
3505 ---------------------------------
3507 procedure Warn_On_Useless_Assignments (E : Entity_Id) is
3510 if Warn_On_Modified_Unread
3511 and then In_Extended_Main_Source_Unit (E)
3513 Ent := First_Entity (E);
3514 while Present (Ent) loop
3515 Warn_On_Useless_Assignment (Ent);
3519 end Warn_On_Useless_Assignments;