1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1999-2004 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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 Einfo
; use Einfo
;
30 with Errout
; use Errout
;
31 with Fname
; use Fname
;
33 with Nlists
; use Nlists
;
36 with Sem_Ch8
; use Sem_Ch8
;
37 with Sem_Util
; use Sem_Util
;
38 with Sinfo
; use Sinfo
;
39 with Sinput
; use Sinput
;
40 with Snames
; use Snames
;
41 with Stand
; use Stand
;
44 package body Sem_Warn
is
46 -- The following table collects Id's of entities that are potentially
47 -- unreferenced. See Check_Unset_Reference for further details.
49 package Unreferenced_Entities
is new Table
.Table
(
50 Table_Component_Type
=> Entity_Id
,
51 Table_Index_Type
=> Nat
,
53 Table_Initial
=> Alloc
.Unreferenced_Entities_Initial
,
54 Table_Increment
=> Alloc
.Unreferenced_Entities_Increment
,
55 Table_Name
=> "Unreferenced_Entities");
57 ------------------------------
58 -- Handling of Conditionals --
59 ------------------------------
61 -- Note: this is work in progress, the data structures and general
62 -- approach are defined, but are not in use yet. ???
64 -- One entry is made in the following table for each branch of
65 -- a conditional, e.g. an if-then-elsif-else-endif structure
66 -- creates three entries in this table.
68 type Branch_Entry
is record
70 -- Location for warnings associated with this branch
73 -- List of entities defined for the first time in this branch. On
74 -- exit from a conditional structure, any entity that is in the
75 -- list of all branches is removed (and the entity flagged as
76 -- defined by the conditional as a whole). Thus after processing
77 -- a conditional, Defs contains a list of entities defined in this
78 -- branch for the first time, but not defined at all in some other
79 -- branch of the same conditional. A value of No_Elist is used to
80 -- represent the initial empty list.
83 -- Index of next branch for this conditional, zero = last branch
86 package Branch_Table
is new Table
.Table
(
87 Table_Component_Type
=> Branch_Entry
,
88 Table_Index_Type
=> Nat
,
90 Table_Initial
=> Alloc
.Branches_Initial
,
91 Table_Increment
=> Alloc
.Branches_Increment
,
92 Table_Name
=> "Branches");
94 -- The following table is used to represent conditionals, there is
95 -- one entry in this table for each conditional structure.
97 type Conditional_Entry
is record
99 -- True for IF statement, False for CASE statement
102 -- Index in Branch table of first branch, zero = none yet
104 Current_Branch
: Nat
;
105 -- Index in Branch table of current branch, zero = none yet
108 package Conditional_Table
is new Table
.Table
(
109 Table_Component_Type
=> Conditional_Entry
,
110 Table_Index_Type
=> Nat
,
111 Table_Low_Bound
=> 1,
112 Table_Initial
=> Alloc
.Conditionals_Initial
,
113 Table_Increment
=> Alloc
.Conditionals_Increment
,
114 Table_Name
=> "Conditionals");
116 -- The following table is a stack that keeps track of the current
117 -- conditional. The Last entry is the top of the stack. An Empty
118 -- entry represents the start of a compilation unit. Non-zero
119 -- entries in the stack are indexes into the conditional table.
121 package Conditional_Stack
is new Table
.Table
(
122 Table_Component_Type
=> Nat
,
123 Table_Index_Type
=> Nat
,
124 Table_Low_Bound
=> 1,
125 Table_Initial
=> Alloc
.Conditional_Stack_Initial
,
126 Table_Increment
=> Alloc
.Conditional_Stack_Increment
,
127 Table_Name
=> "Conditional_Stack");
129 pragma Warnings
(Off
, Branch_Table
);
130 pragma Warnings
(Off
, Conditional_Table
);
131 pragma Warnings
(Off
, Conditional_Stack
);
132 -- Not yet referenced, see note above ???
134 -----------------------
135 -- Local Subprograms --
136 -----------------------
138 function Generic_Package_Spec_Entity
(E
: Entity_Id
) return Boolean;
139 -- This returns true if the entity E is declared within a generic package.
140 -- The point of this is to detect variables which are not assigned within
141 -- the generic, but might be assigned outside the package for any given
142 -- instance. These are cases where we leave the warnings to be posted
143 -- for the instance, when we will know more.
145 function Operand_Has_Warnings_Suppressed
(N
: Node_Id
) return Boolean;
146 -- This function traverses the expression tree represented by the node
147 -- N and determines if any sub-operand is a reference to an entity for
148 -- which the Warnings_Off flag is set. True is returned if such an
149 -- entity is encountered, and False otherwise.
151 ----------------------
152 -- Check_References --
153 ----------------------
155 procedure Check_References
(E
: Entity_Id
; Anod
: Node_Id
:= Empty
) is
159 function Missing_Subunits
return Boolean;
160 -- We suppress warnings when there are missing subunits, because this
161 -- may generate too many false positives: entities in a parent may
162 -- only be referenced in one of the subunits. We make an exception
163 -- for subunits that contain no other stubs.
165 procedure Output_Reference_Error
(M
: String);
166 -- Used to output an error message. Deals with posting the error on
167 -- the body formal in the accept case.
169 function Publicly_Referenceable
(Ent
: Entity_Id
) return Boolean;
170 -- This is true if the entity in question is potentially referenceable
171 -- from another unit. This is true for entities in packages that are
172 -- at the library level.
174 -----------------------
175 -- Missing_Subunits --
176 -----------------------
178 function Missing_Subunits
return Boolean is
182 if not Unloaded_Subunits
then
184 -- Normal compilation, all subunits are present
188 elsif E
/= Main_Unit_Entity
then
190 -- No warnings on a stub that is not the main unit
194 elsif Nkind
(Unit_Declaration_Node
(E
)) in N_Proper_Body
then
195 D
:= First
(Declarations
(Unit_Declaration_Node
(E
)));
197 while Present
(D
) loop
199 -- No warnings if the proper body contains nested stubs
201 if Nkind
(D
) in N_Body_Stub
then
211 -- Missing stubs elsewhere
215 end Missing_Subunits
;
217 ----------------------------
218 -- Output_Reference_Error --
219 ----------------------------
221 procedure Output_Reference_Error
(M
: String) is
223 -- Other than accept case, post error on defining identifier
228 -- Accept case, find body formal to post the message
239 if Present
(Parameter_Specifications
(Anod
)) then
240 Parm
:= First
(Parameter_Specifications
(Anod
));
242 while Present
(Parm
) loop
243 Defid
:= Defining_Identifier
(Parm
);
245 if Chars
(E1
) = Chars
(Defid
) then
254 Error_Msg_NE
(M
, Enod
, E1
);
257 end Output_Reference_Error
;
259 ----------------------------
260 -- Publicly_Referenceable --
261 ----------------------------
263 function Publicly_Referenceable
(Ent
: Entity_Id
) return Boolean is
268 -- Examine parents to look for a library level package spec
269 -- But if we find a body or block or other similar construct
270 -- along the way, we cannot be referenced.
277 -- If we get to top of tree, then publicly referenceable
282 -- If we reach a generic package declaration, then always
283 -- consider this referenceable, since any instantiation will
284 -- have access to the entities in the generic package. Note
285 -- that the package itself may not be instantiated, but then
286 -- we will get a warning for the package entity
287 -- Note that generic formal parameters are themselves not
288 -- publicly referenceable in an instance, and warnings on
291 when N_Generic_Package_Declaration
=>
293 not Is_List_Member
(Prev
)
294 or else List_Containing
(Prev
)
295 /= Generic_Formal_Declarations
(P
);
297 -- if we reach a subprogram body, entity is not referenceable
298 -- unless it is the defining entity of the body. This will
299 -- happen, e.g. when a function is an attribute renaming that
300 -- is rewritten as a body.
302 when N_Subprogram_Body
=>
303 if Ent
/= Defining_Entity
(P
) then
309 -- If we reach any other body, definitely not referenceable
311 when N_Package_Body |
319 -- For all other cases, keep looking up tree
326 end Publicly_Referenceable
;
328 -- Start of processing for Check_References
331 -- No messages if warnings are suppressed, or if we have detected
332 -- any real errors so far (this last check avoids junk messages
333 -- resulting from errors, e.g. a subunit that is not loaded).
335 if Warning_Mode
= Suppress
336 or else Serious_Errors_Detected
/= 0
341 -- We also skip the messages if any subunits were not loaded (see
342 -- comment in Sem_Ch10 to understand how this is set, and why it is
343 -- necessary to suppress the warnings in this case).
345 if Missing_Subunits
then
349 -- Otherwise loop through entities, looking for suspicious stuff
351 E1
:= First_Entity
(E
);
352 while Present
(E1
) loop
354 -- We only look at source entities with warning flag on
356 if Comes_From_Source
(E1
) and then not Warnings_Off
(E1
) then
358 -- We are interested in variables and out parameters, but we
359 -- exclude protected types, too complicated to worry about.
361 if Ekind
(E1
) = E_Variable
363 (Ekind
(E1
) = E_Out_Parameter
364 and then not Is_Protected_Type
(Current_Scope
))
366 -- Post warning if this object not assigned. Note that we
367 -- do not consider the implicit initialization of an access
368 -- type to be the assignment of a value for this purpose.
370 if Ekind
(E1
) = E_Out_Parameter
371 and then Present
(Spec_Entity
(E1
))
373 UR
:= Unset_Reference
(Spec_Entity
(E1
));
375 UR
:= Unset_Reference
(E1
);
378 -- If the entity is an out parameter of the current subprogram
379 -- body, check the warning status of the parameter in the spec.
381 if Ekind
(E1
) = E_Out_Parameter
382 and then Present
(Spec_Entity
(E1
))
383 and then Warnings_Off
(Spec_Entity
(E1
))
388 and then Is_Access_Type
(Etype
(E1
))
391 -- For access types, the only time we made a UR
392 -- entry was for a dereference, and so we post
393 -- the appropriate warning here (note that the
394 -- dereference may not be explicit in the source,
395 -- for example in the case of a dispatching call
396 -- with an anonymous access controlling formal, or
397 -- of an assignment of a pointer involving a
398 -- discriminant check on the designated object).
400 Error_Msg_NE
("& may be null?", UR
, E1
);
403 elsif Never_Set_In_Source
(E1
)
404 and then not Generic_Package_Spec_Entity
(E1
)
406 if Warn_On_No_Value_Assigned
then
408 -- Do not output complaint about never being assigned a
409 -- value if a pragma Unreferenced applies to the variable
410 -- or if it is a parameter, to the corresponding spec.
412 if Has_Pragma_Unreferenced
(E1
)
413 or else (Is_Formal
(E1
)
414 and then Present
(Spec_Entity
(E1
))
416 Has_Pragma_Unreferenced
(Spec_Entity
(E1
)))
420 -- Pragma Unreferenced not set, so output message
423 Output_Reference_Error
424 ("& is never assigned a value?");
426 -- Deal with special case where this variable is
427 -- hidden by a loop variable
429 if Ekind
(E1
) = E_Variable
430 and then Present
(Hiding_Loop_Variable
(E1
))
432 Error_Msg_Sloc
:= Sloc
(E1
);
434 ("declaration hides &#?",
435 Hiding_Loop_Variable
(E1
));
437 ("for loop implicitly declares loop variable?",
438 Hiding_Loop_Variable
(E1
));
444 -- Case of variable that could be a constant. Note that we
445 -- never signal such messages for generic package entities,
446 -- since a given instance could have modifications outside
449 elsif Warn_On_Constant
450 and then Ekind
(E1
) = E_Variable
451 and then Is_True_Constant
(E1
)
452 and then not Generic_Package_Spec_Entity
(E1
)
455 ("& is not modified, could be declared constant?", E1
);
458 -- Check for unset reference, note that we exclude access
459 -- types from this check, since access types do always have
460 -- a null value, and that seems legitimate in this case.
462 if Warn_On_No_Value_Assigned
and then Present
(UR
) then
464 -- For other than access type, go back to original node
465 -- to deal with case where original unset reference
466 -- has been rewritten during expansion.
468 UR
:= Original_Node
(UR
);
470 -- In some cases, the original node may be a type
471 -- conversion or qualification, and in this case
472 -- we want the object entity inside.
474 while Nkind
(UR
) = N_Type_Conversion
475 or else Nkind
(UR
) = N_Qualified_Expression
477 UR
:= Expression
(UR
);
480 -- Here we issue the warning, all checks completed
481 -- If the unset reference is prefix of a selected
482 -- component that comes from source, mention the
483 -- component as well. If the selected component comes
484 -- from expansion, all we know is that the entity is
485 -- not fully initialized at the point of the reference.
486 -- Locate an unintialized component to get a better
489 if Nkind
(Parent
(UR
)) = N_Selected_Component
then
490 Error_Msg_Node_2
:= Selector_Name
(Parent
(UR
));
492 if not Comes_From_Source
(Parent
(UR
)) then
497 Comp
:= First_Entity
(Etype
(E1
));
498 while Present
(Comp
) loop
499 if Ekind
(Comp
) = E_Component
500 and then Nkind
(Parent
(Comp
)) =
501 N_Component_Declaration
502 and then No
(Expression
(Parent
(Comp
)))
504 Error_Msg_Node_2
:= Comp
;
514 ("`&.&` may be referenced before it has a value?",
518 ("& may be referenced before it has a value?",
526 -- Then check for unreferenced entities. Note that we are only
527 -- interested in entities which do not have the Referenced flag
528 -- set. The Referenced_As_LHS flag is interesting only if the
529 -- Referenced flag is not set.
531 if not Referenced
(E1
)
533 -- Check that warnings on unreferenced entities are enabled
535 and then ((Check_Unreferenced
and then not Is_Formal
(E1
))
537 (Check_Unreferenced_Formals
and then Is_Formal
(E1
))
539 (Warn_On_Modified_Unread
540 and then Referenced_As_LHS
(E1
)))
542 -- Labels, and enumeration literals, and exceptions. The
543 -- warnings are also placed on local packages that cannot
544 -- be referenced from elsewhere, including those declared
545 -- within a package body.
547 and then (Is_Object
(E1
)
553 Ekind
(E1
) = E_Exception
555 Ekind
(E1
) = E_Named_Integer
557 Ekind
(E1
) = E_Named_Real
561 (Ekind
(E1
) = E_Package
563 (Ekind
(E
) = E_Function
564 or else Ekind
(E
) = E_Package_Body
565 or else Ekind
(E
) = E_Procedure
566 or else Ekind
(E
) = E_Block
)))
568 -- Exclude instantiations, since there is no reason why
569 -- every entity in an instantiation should be referenced.
571 and then Instantiation_Location
(Sloc
(E1
)) = No_Location
573 -- Exclude formal parameters from bodies if the corresponding
574 -- spec entity has been referenced in the case where there is
577 and then not (Is_Formal
(E1
)
579 Ekind
(Scope
(E1
)) = E_Subprogram_Body
581 Present
(Spec_Entity
(E1
))
583 Referenced
(Spec_Entity
(E1
)))
585 -- Consider private type referenced if full view is referenced
586 -- If there is not full view, this is a generic type on which
587 -- warnings are also useful.
590 not (Is_Private_Type
(E1
)
592 Present
(Full_View
(E1
))
593 and then Referenced
(Full_View
(E1
)))
595 -- Don't worry about full view, only about private type
597 and then not Has_Private_Declaration
(E1
)
599 -- Eliminate dispatching operations from consideration, we
600 -- cannot tell if these are referenced or not in any easy
601 -- manner (note this also catches Adjust/Finalize/Initialize)
603 and then not Is_Dispatching_Operation
(E1
)
605 -- Check entity that can be publicly referenced (we do not
606 -- give messages for such entities, since there could be
607 -- other units, not involved in this compilation, that
608 -- contain relevant references.
610 and then not Publicly_Referenceable
(E1
)
612 -- Class wide types are marked as source entities, but
613 -- they are not really source entities, and are always
614 -- created, so we do not care if they are not referenced.
616 and then Ekind
(E1
) /= E_Class_Wide_Type
618 -- Objects other than parameters of task types are allowed
619 -- to be non-referenced, since they start up tasks!
621 and then ((Ekind
(E1
) /= E_Variable
622 and then Ekind
(E1
) /= E_Constant
623 and then Ekind
(E1
) /= E_Component
)
624 or else not Is_Task_Type
(Etype
(E1
)))
626 -- For subunits, only place warnings on the main unit
627 -- itself, since parent units are not completely compiled
629 and then (Nkind
(Unit
(Cunit
(Main_Unit
))) /= N_Subunit
631 Get_Source_Unit
(E1
) = Main_Unit
)
633 -- Suppress warnings in internal units if not in -gnatg
634 -- mode (these would be junk warnings for an applications
635 -- program, since they refer to problems in internal units)
639 Is_Internal_File_Name
640 (Unit_File_Name
(Get_Source_Unit
(E1
)))
642 -- We do not immediately flag the error. This is because
643 -- we have not expanded generic bodies yet, and they may
644 -- have the missing reference. So instead we park the
645 -- entity on a list, for later processing. However, for
646 -- the accept case, post the error right here, since we
647 -- have the information now in this case.
649 if Present
(Anod
) then
650 Output_Reference_Error
("& is not referenced?");
653 Unreferenced_Entities
.Increment_Last
;
654 Unreferenced_Entities
.Table
655 (Unreferenced_Entities
.Last
) := E1
;
659 -- Generic units are referenced in the generic body,
660 -- but if they are not public and never instantiated
661 -- we want to force a warning on them. We treat them
662 -- as redundant constructs to minimize noise.
664 elsif Is_Generic_Subprogram
(E1
)
665 and then not Is_Instantiated
(E1
)
666 and then not Publicly_Referenceable
(E1
)
667 and then Instantiation_Depth
(Sloc
(E1
)) = 0
668 and then Warn_On_Redundant_Constructs
670 Unreferenced_Entities
.Increment_Last
;
671 Unreferenced_Entities
.Table
(Unreferenced_Entities
.Last
) := E1
;
673 -- Force warning on entity.
675 Set_Referenced
(E1
, False);
679 -- Recurse into nested package or block. Do not recurse into a
680 -- formal package, because the correponding body is not analyzed.
683 if ((Ekind
(E1
) = E_Package
or else Ekind
(E1
) = E_Generic_Package
)
684 and then Nkind
(Parent
(E1
)) = N_Package_Specification
686 Nkind
(Original_Node
(Unit_Declaration_Node
(E1
)))
687 /= N_Formal_Package_Declaration
)
689 or else Ekind
(E1
) = E_Block
691 Check_References
(E1
);
696 end Check_References
;
698 ---------------------------
699 -- Check_Unset_Reference --
700 ---------------------------
702 procedure Check_Unset_Reference
(N
: Node_Id
) is
704 -- Nothing to do if warnings suppressed
706 if Warning_Mode
= Suppress
then
710 -- Ignore reference to non-scalar if not from source. Almost always
711 -- such references are bogus (e.g. calls to init procs to set
712 -- default discriminant values).
714 if not Comes_From_Source
(N
)
715 and then not Is_Scalar_Type
(Etype
(N
))
720 -- Otherwise see what kind of node we have. If the entity already
721 -- has an unset reference, it is not necessarily the earliest in
722 -- the text, because resolution of the prefix of selected components
723 -- is completed before the resolution of the selected component itself.
724 -- as a result, given (R /= null and then R.X > 0), the occurrences
725 -- of R are examined in right-to-left order. If there is already an
726 -- unset reference, we check whether N is earlier before proceeding.
729 when N_Identifier | N_Expanded_Name
=>
731 E
: constant Entity_Id
:= Entity
(N
);
734 if (Ekind
(E
) = E_Variable
735 or else Ekind
(E
) = E_Out_Parameter
)
736 and then Never_Set_In_Source
(E
)
737 and then (No
(Unset_Reference
(E
))
738 or else Earlier_In_Extended_Unit
739 (Sloc
(N
), Sloc
(Unset_Reference
(E
))))
740 and then not Warnings_Off
(E
)
742 -- We may have an unset reference. The first test is
743 -- whether we are accessing a discriminant of a record
744 -- or a component with default initialization. Both of
745 -- these cases can be ignored, since the actual object
746 -- that is referenced is definitely initialized. Note
747 -- that this covers the case of reading discriminants
748 -- of an out parameter, which is OK even in Ada 83.
750 -- Note that we are only interested in a direct reference
751 -- to a record component here. If the reference is via an
752 -- access type, then the access object is being referenced,
753 -- not the record, and still deserves an unset reference.
755 if Nkind
(Parent
(N
)) = N_Selected_Component
756 and not Is_Access_Type
(Etype
(N
))
759 ES
: constant Entity_Id
:=
760 Entity
(Selector_Name
(Parent
(N
)));
763 if Ekind
(ES
) = E_Discriminant
764 or else Present
(Expression
(Declaration_Node
(ES
)))
771 -- Here we have a potential unset reference. But before we
772 -- get worried about it, we have to make sure that the
773 -- entity declaration is in the same procedure as the
774 -- reference, since if they are in separate procedures,
775 -- then we have no idea about sequential execution.
777 -- The tests in the loop below catch all such cases, but
778 -- do allow the reference to appear in a loop, block, or
779 -- package spec that is nested within the declaring scope.
780 -- As always, it is possible to construct cases where the
781 -- warning is wrong, that is why it is a warning!
785 SE
: constant Entity_Id
:= Scope
(E
);
790 if SR
= Standard_Standard
791 or else Is_Subprogram
(SR
)
792 or else Is_Concurrent_Body
(SR
)
793 or else Is_Concurrent_Type
(SR
)
801 -- Case of reference has an access type. This is a
802 -- special case since access types are always set to
803 -- null so cannot be truly uninitialized, but we still
804 -- want to warn about cases of obvious null dereference.
806 if Is_Access_Type
(Etype
(N
)) then
812 return Traverse_Result
;
813 -- Process function for instantation of Traverse
814 -- below. Checks if N contains reference to E
815 -- other than a dereference.
817 function Ref_In
(Nod
: Node_Id
) return Boolean;
818 -- Determines whether Nod contains a reference
819 -- to the entity E that is not a dereference.
823 return Traverse_Result
826 if Is_Entity_Name
(N
)
827 and then Entity
(N
) = E
828 and then not Is_Dereferenced
(N
)
836 function Ref_In
(Nod
: Node_Id
) return Boolean is
837 function Traverse
is new Traverse_Func
(Process
);
840 return Traverse
(Nod
) = Abandon
;
844 -- Don't bother if we are inside an instance,
845 -- since the compilation of the generic template
846 -- is where the warning should be issued.
852 -- Don't bother if this is not the main unit.
853 -- If we try to give this warning for with'ed
854 -- units, we get some false positives, since
855 -- we do not record references in other units.
857 if not In_Extended_Main_Source_Unit
(E
)
859 not In_Extended_Main_Source_Unit
(N
)
864 -- We are only interested in deferences
866 if not Is_Dereferenced
(N
) then
870 -- One more check, don't bother with references
871 -- that are inside conditional statements or while
872 -- loops if the condition references the entity in
873 -- question. This avoids most false positives.
880 if (Nkind
(P
) = N_If_Statement
882 Nkind
(P
) = N_Elsif_Part
)
883 and then Ref_In
(Condition
(P
))
887 elsif Nkind
(P
) = N_Loop_Statement
888 and then Present
(Iteration_Scheme
(P
))
890 Ref_In
(Condition
(Iteration_Scheme
(P
)))
898 -- Here we definitely have a case for giving a warning
899 -- for a reference to an unset value. But we don't give
900 -- the warning now. Instead we set the Unset_Reference
901 -- field of the identifier involved. The reason for this
902 -- is that if we find the variable is never ever assigned
903 -- a value then that warning is more important and there
904 -- is no point in giving the reference warning.
906 -- If this is an identifier, set the field directly
908 if Nkind
(N
) = N_Identifier
then
909 Set_Unset_Reference
(E
, N
);
911 -- Otherwise it is an expanded name, so set the field
912 -- of the actual identifier for the reference.
915 Set_Unset_Reference
(E
, Selector_Name
(N
));
921 when N_Indexed_Component | N_Slice
=>
922 Check_Unset_Reference
(Prefix
(N
));
924 when N_Selected_Component
=>
926 if Present
(Entity
(Selector_Name
(N
)))
927 and then Ekind
(Entity
(Selector_Name
(N
))) = E_Discriminant
929 -- A discriminant is always initialized
934 Check_Unset_Reference
(Prefix
(N
));
937 when N_Type_Conversion | N_Qualified_Expression
=>
938 Check_Unset_Reference
(Expression
(N
));
944 end Check_Unset_Reference
;
946 ------------------------
947 -- Check_Unused_Withs --
948 ------------------------
950 procedure Check_Unused_Withs
(Spec_Unit
: Unit_Number_Type
:= No_Unit
) is
956 Munite
: constant Entity_Id
:= Cunit_Entity
(Main_Unit
);
957 -- This is needed for checking the special renaming case
959 procedure Check_One_Unit
(Unit
: Unit_Number_Type
);
960 -- Subsidiary procedure, performs checks for specified unit
966 procedure Check_One_Unit
(Unit
: Unit_Number_Type
) is
967 Is_Visible_Renaming
: Boolean := False;
970 procedure Check_Inner_Package
(Pack
: Entity_Id
);
971 -- Pack is a package local to a unit in a with_clause. Both the
972 -- unit and Pack are referenced. If none of the entities in Pack
973 -- are referenced, then the only occurrence of Pack is in a use
974 -- clause or a pragma, and a warning is worthwhile as well.
976 function Check_System_Aux
return Boolean;
977 -- Before giving a warning on a with_clause for System, check
978 -- whether a system extension is present.
980 function Find_Package_Renaming
982 L
: Entity_Id
) return Entity_Id
;
983 -- The only reference to a context unit may be in a renaming
984 -- declaration. If this renaming declares a visible entity, do
985 -- not warn that the context clause could be moved to the body,
986 -- because the renaming may be intented to re-export the unit.
988 -------------------------
989 -- Check_Inner_Package --
990 -------------------------
992 procedure Check_Inner_Package
(Pack
: Entity_Id
) is
994 Un
: constant Node_Id
:= Sinfo
.Unit
(Cnode
);
996 function Check_Use_Clause
(N
: Node_Id
) return Traverse_Result
;
997 -- If N is a use_clause for Pack, emit warning.
999 procedure Check_Use_Clauses
is new
1000 Traverse_Proc
(Check_Use_Clause
);
1002 ----------------------
1003 -- Check_Use_Clause --
1004 ----------------------
1006 function Check_Use_Clause
(N
: Node_Id
) return Traverse_Result
is
1010 if Nkind
(N
) = N_Use_Package_Clause
then
1011 Nam
:= First
(Names
(N
));
1013 while Present
(Nam
) loop
1014 if Entity
(Nam
) = Pack
then
1015 Error_Msg_Qual_Level
:= 1;
1017 ("no entities of package& are referenced?",
1019 Error_Msg_Qual_Level
:= 0;
1027 end Check_Use_Clause
;
1029 -- Start of processing for Check_Inner_Package
1032 E
:= First_Entity
(Pack
);
1034 while Present
(E
) loop
1035 if Referenced
(E
) then
1042 -- No entities of the package are referenced. Check whether
1043 -- the reference to the package itself is a use clause, and
1044 -- if so place a warning on it.
1046 Check_Use_Clauses
(Un
);
1047 end Check_Inner_Package
;
1049 ----------------------
1050 -- Check_System_Aux --
1051 ----------------------
1053 function Check_System_Aux
return Boolean is
1057 if Chars
(Lunit
) = Name_System
1058 and then Scope
(Lunit
) = Standard_Standard
1059 and then Present_System_Aux
1061 Ent
:= First_Entity
(System_Aux_Id
);
1063 while Present
(Ent
) loop
1064 if Referenced
(Ent
) then
1073 end Check_System_Aux
;
1075 ---------------------------
1076 -- Find_Package_Renaming --
1077 ---------------------------
1079 function Find_Package_Renaming
1081 L
: Entity_Id
) return Entity_Id
1087 Is_Visible_Renaming
:= False;
1088 E1
:= First_Entity
(P
);
1090 while Present
(E1
) loop
1091 if Ekind
(E1
) = E_Package
1092 and then Renamed_Object
(E1
) = L
1094 Is_Visible_Renaming
:= not Is_Hidden
(E1
);
1097 elsif Ekind
(E1
) = E_Package
1098 and then No
(Renamed_Object
(E1
))
1099 and then not Is_Generic_Instance
(E1
)
1101 R
:= Find_Package_Renaming
(E1
, L
);
1104 Is_Visible_Renaming
:= not Is_Hidden
(R
);
1113 end Find_Package_Renaming
;
1115 -- Start of processing for Check_One_Unit
1118 Cnode
:= Cunit
(Unit
);
1120 -- Only do check in units that are part of the extended main
1121 -- unit. This is actually a necessary restriction, because in
1122 -- the case of subprogram acting as its own specification,
1123 -- there can be with's in subunits that we will not see.
1125 if not In_Extended_Main_Source_Unit
(Cnode
) then
1128 -- In configurable run time mode, we remove the bodies of
1129 -- non-inlined subprograms, which may lead to spurious warnings,
1130 -- which are clearly undesirable.
1132 elsif Configurable_Run_Time_Mode
1133 and then Is_Predefined_File_Name
(Unit_File_Name
(Unit
))
1138 -- Loop through context items in this unit
1140 Item
:= First
(Context_Items
(Cnode
));
1141 while Present
(Item
) loop
1142 if Nkind
(Item
) = N_With_Clause
1143 and then not Implicit_With
(Item
)
1144 and then In_Extended_Main_Source_Unit
(Item
)
1146 Lunit
:= Entity
(Name
(Item
));
1148 -- Check if this unit is referenced
1150 if not Referenced
(Lunit
) then
1152 -- Suppress warnings in internal units if not in -gnatg
1153 -- mode (these would be junk warnings for an applications
1154 -- program, since they refer to problems in internal units)
1157 or else not Is_Internal_File_Name
(Unit_File_Name
(Unit
))
1159 -- Here we definitely have a non-referenced unit. If
1160 -- it is the special call for a spec unit, then just
1161 -- set the flag to be read later.
1163 if Unit
= Spec_Unit
then
1164 Set_Unreferenced_In_Spec
(Item
);
1166 -- Otherwise simple unreferenced message
1170 ("unit& is not referenced?", Name
(Item
));
1174 -- If main unit is a renaming of this unit, then we consider
1175 -- the with to be OK (obviously it is needed in this case!)
1177 elsif Present
(Renamed_Entity
(Munite
))
1178 and then Renamed_Entity
(Munite
) = Lunit
1182 -- If this unit is referenced, and it is a package, we
1183 -- do another test, to see if any of the entities in the
1184 -- package are referenced. If none of the entities are
1185 -- referenced, we still post a warning. This occurs if
1186 -- the only use of the package is in a use clause, or
1187 -- in a package renaming declaration.
1189 elsif Ekind
(Lunit
) = E_Package
then
1191 -- If Is_Instantiated is set, it means that the package
1192 -- is implicitly instantiated (this is the case of a
1193 -- parent instance or an actual for a generic package
1194 -- formal), and this counts as a reference.
1196 if Is_Instantiated
(Lunit
) then
1199 -- If no entities in package, and there is a pragma
1200 -- Elaborate_Body present, then assume that this with
1201 -- is done for purposes of this elaboration.
1203 elsif No
(First_Entity
(Lunit
))
1204 and then Has_Pragma_Elaborate_Body
(Lunit
)
1208 -- Otherwise see if any entities have been referenced
1211 Ent
:= First_Entity
(Lunit
);
1213 -- No more entities, and we did not find one
1214 -- that was referenced. Means we have a definite
1215 -- case of a with none of whose entities was
1220 -- If in spec, just set the flag
1222 if Unit
= Spec_Unit
then
1223 Set_No_Entities_Ref_In_Spec
(Item
);
1225 elsif Check_System_Aux
then
1228 -- Else give the warning
1232 ("no entities of & are referenced?",
1235 -- Look for renamings of this package, and
1236 -- flag them as well. If the original package
1237 -- has warnings off, we suppress the warning
1238 -- on the renaming as well.
1240 Pack
:= Find_Package_Renaming
(Munite
, Lunit
);
1243 and then not Warnings_Off
(Lunit
)
1246 ("no entities of & are referenced?",
1247 Unit_Declaration_Node
(Pack
),
1254 -- Case of next entity is referenced
1256 elsif Referenced
(Ent
)
1257 or else Referenced_As_LHS
(Ent
)
1259 -- This means that the with is indeed fine, in
1260 -- that it is definitely needed somewhere, and
1261 -- we can quite worrying about this one.
1263 -- Except for one little detail, if either of
1264 -- the flags was set during spec processing,
1265 -- this is where we complain that the with
1266 -- could be moved from the spec. If the spec
1267 -- contains a visible renaming of the package,
1268 -- inhibit warning to move with_clause to body.
1270 if Ekind
(Munite
) = E_Package_Body
then
1272 Find_Package_Renaming
1273 (Spec_Entity
(Munite
), Lunit
);
1276 if Unreferenced_In_Spec
(Item
) then
1278 ("unit& is not referenced in spec?",
1281 elsif No_Entities_Ref_In_Spec
(Item
) then
1283 ("no entities of & are referenced in spec?",
1287 if Ekind
(Ent
) = E_Package
then
1288 Check_Inner_Package
(Ent
);
1294 if not Is_Visible_Renaming
then
1296 ("\with clause might be moved to body?",
1302 -- Move to next entity to continue search
1310 -- For a generic package, the only interesting kind of
1311 -- reference is an instantiation, since entities cannot
1312 -- be referenced directly.
1314 elsif Is_Generic_Unit
(Lunit
) then
1316 -- Unit was never instantiated, set flag for case of spec
1317 -- call, or give warning for normal call.
1319 if not Is_Instantiated
(Lunit
) then
1320 if Unit
= Spec_Unit
then
1321 Set_Unreferenced_In_Spec
(Item
);
1324 ("unit& is never instantiated?", Name
(Item
));
1327 -- If unit was indeed instantiated, make sure that
1328 -- flag is not set showing it was uninstantiated in
1329 -- the spec, and if so, give warning.
1331 elsif Unreferenced_In_Spec
(Item
) then
1333 ("unit& is not instantiated in spec?", Name
(Item
));
1335 ("\with clause can be moved to body?", Name
(Item
));
1345 -- Start of processing for Check_Unused_Withs
1348 if not Opt
.Check_Withs
1349 or else Operating_Mode
= Check_Syntax
1354 -- Flag any unused with clauses, but skip this step if we are
1355 -- compiling a subunit on its own, since we do not have enough
1356 -- information to determine whether with's are used. We will get
1357 -- the relevant warnings when we compile the parent. This is the
1358 -- normal style of GNAT compilation in any case.
1360 if Nkind
(Unit
(Cunit
(Main_Unit
))) = N_Subunit
then
1364 -- Process specified units
1366 if Spec_Unit
= No_Unit
then
1368 -- For main call, check all units
1370 for Unit
in Main_Unit
.. Last_Unit
loop
1371 Check_One_Unit
(Unit
);
1375 -- For call for spec, check only the spec
1377 Check_One_Unit
(Spec_Unit
);
1379 end Check_Unused_Withs
;
1381 ---------------------------------
1382 -- Generic_Package_Spec_Entity --
1383 ---------------------------------
1385 function Generic_Package_Spec_Entity
(E
: Entity_Id
) return Boolean is
1389 if Is_Package_Body_Entity
(E
) then
1396 if S
= Standard_Standard
then
1399 elsif Ekind
(S
) = E_Generic_Package
then
1402 elsif Ekind
(S
) = E_Package
then
1410 end Generic_Package_Spec_Entity
;
1412 -------------------------------------
1413 -- Operand_Has_Warnings_Suppressed --
1414 -------------------------------------
1416 function Operand_Has_Warnings_Suppressed
(N
: Node_Id
) return Boolean is
1418 function Check_For_Warnings
(N
: Node_Id
) return Traverse_Result
;
1419 -- Function used to check one node to see if it is or was originally
1420 -- a reference to an entity for which Warnings are off. If so, Abandon
1421 -- is returned, otherwise OK_Orig is returned to continue the traversal
1422 -- of the original expression.
1424 function Traverse
is new Traverse_Func
(Check_For_Warnings
);
1425 -- Function used to traverse tree looking for warnings
1427 ------------------------
1428 -- Check_For_Warnings --
1429 ------------------------
1431 function Check_For_Warnings
(N
: Node_Id
) return Traverse_Result
is
1432 R
: constant Node_Id
:= Original_Node
(N
);
1435 if Nkind
(R
) in N_Has_Entity
1436 and then Present
(Entity
(R
))
1437 and then Warnings_Off
(Entity
(R
))
1443 end Check_For_Warnings
;
1445 -- Start of processing for Operand_Has_Warnings_Suppressed
1448 return Traverse
(N
) = Abandon
;
1450 -- If any exception occurs, then something has gone wrong, and this is
1451 -- only a minor aesthetic issue anyway, so just say we did not find what
1452 -- we are looking for, rather than blow up.
1457 end Operand_Has_Warnings_Suppressed
;
1459 ----------------------------------
1460 -- Output_Unreferenced_Messages --
1461 ----------------------------------
1463 procedure Output_Unreferenced_Messages
is
1467 for J
in Unreferenced_Entities
.First
..
1468 Unreferenced_Entities
.Last
1470 E
:= Unreferenced_Entities
.Table
(J
);
1472 if not Referenced
(E
) and then not Warnings_Off
(E
) then
1476 -- Case of variable that is assigned but not read. We
1477 -- suppress the message if the variable is volatile,
1478 -- has an address clause, or is imported.
1480 if Referenced_As_LHS
(E
)
1481 and then No
(Address_Clause
(E
))
1482 and then not Is_Volatile
(E
)
1484 if Warn_On_Modified_Unread
1485 and then not Is_Imported
(E
)
1488 ("variable & is assigned but never read?", E
);
1491 -- Normal case of neither assigned nor read
1494 if Present
(Renamed_Object
(E
))
1495 and then Comes_From_Source
(Renamed_Object
(E
))
1498 ("renamed variable & is not referenced?", E
);
1501 ("variable & is not referenced?", E
);
1506 if Present
(Renamed_Object
(E
))
1507 and then Comes_From_Source
(Renamed_Object
(E
))
1509 Error_Msg_N
("renamed constant & is not referenced?", E
);
1511 Error_Msg_N
("constant & is not referenced?", E
);
1514 when E_In_Parameter |
1516 E_In_Out_Parameter
=>
1518 -- Do not emit message for formals of a renaming, because
1519 -- they are never referenced explicitly.
1521 if Nkind
(Original_Node
(Unit_Declaration_Node
(Scope
(E
))))
1522 /= N_Subprogram_Renaming_Declaration
1524 Error_Msg_N
("formal parameter & is not referenced?", E
);
1527 when E_Named_Integer |
1529 Error_Msg_N
("named number & is not referenced?", E
);
1531 when E_Enumeration_Literal
=>
1532 Error_Msg_N
("literal & is not referenced?", E
);
1535 Error_Msg_N
("function & is not referenced?", E
);
1538 Error_Msg_N
("procedure & is not referenced?", E
);
1540 when E_Generic_Procedure
=>
1542 ("generic procedure & is never instantiated?", E
);
1544 when E_Generic_Function
=>
1545 Error_Msg_N
("generic function & is never instantiated?", E
);
1548 Error_Msg_N
("type & is not referenced?", E
);
1551 Error_Msg_N
("& is not referenced?", E
);
1554 Set_Warnings_Off
(E
);
1557 end Output_Unreferenced_Messages
;
1559 -----------------------------
1560 -- Warn_On_Known_Condition --
1561 -----------------------------
1563 procedure Warn_On_Known_Condition
(C
: Node_Id
) is
1567 -- Argument replacement in an inlined body can make conditions
1568 -- static. Do not emit warnings in this case.
1570 if In_Inlined_Body
then
1574 if Constant_Condition_Warnings
1575 and then Nkind
(C
) = N_Identifier
1577 (Entity
(C
) = Standard_False
or else Entity
(C
) = Standard_True
)
1578 and then Comes_From_Source
(Original_Node
(C
))
1579 and then not In_Instance
1581 -- See if this is in a statement or a declaration
1585 -- If tree is not attached, do not issue warning (this is very
1586 -- peculiar, and probably arises from some other error condition)
1591 -- If we are in a declaration, then no warning, since in practice
1592 -- conditionals in declarations are used for intended tests which
1593 -- may be known at compile time, e.g. things like
1595 -- x : constant Integer := 2 + (Word'Size = 32);
1597 -- And a warning is annoying in such cases
1599 elsif Nkind
(P
) in N_Declaration
1601 Nkind
(P
) in N_Later_Decl_Item
1605 -- Don't warn in assert pragma, since presumably tests in such
1606 -- a context are very definitely intended, and might well be
1607 -- known at compile time. Note that we have to test the original
1608 -- node, since assert pragmas get rewritten at analysis time.
1610 elsif Nkind
(Original_Node
(P
)) = N_Pragma
1611 and then Chars
(Original_Node
(P
)) = Name_Assert
1616 exit when Is_Statement
(P
);
1620 -- Here we issue the warning unless some sub-operand has warnings
1621 -- set off, in which case we suppress the warning for the node.
1623 if not Operand_Has_Warnings_Suppressed
(C
) then
1624 if Entity
(C
) = Standard_True
then
1625 Error_Msg_N
("condition is always True?", C
);
1627 Error_Msg_N
("condition is always False?", C
);
1631 end Warn_On_Known_Condition
;