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
))
387 elsif Warn_On_No_Value_Assigned
388 and then Present
(UR
)
389 and then Is_Access_Type
(Etype
(E1
))
392 -- For access types, the only time we made a UR
393 -- entry was for a dereference, and so we post
394 -- the appropriate warning here (note that the
395 -- dereference may not be explicit in the source,
396 -- for example in the case of a dispatching call
397 -- with an anonymous access controlling formal, or
398 -- of an assignment of a pointer involving a
399 -- discriminant check on the designated object).
401 Error_Msg_NE
("& may be null?", UR
, E1
);
404 elsif Never_Set_In_Source
(E1
)
405 and then not Generic_Package_Spec_Entity
(E1
)
407 if Warn_On_No_Value_Assigned
then
409 -- Do not output complaint about never being assigned a
410 -- value if a pragma Unreferenced applies to the variable
411 -- or if it is a parameter, to the corresponding spec.
413 if Has_Pragma_Unreferenced
(E1
)
414 or else (Is_Formal
(E1
)
415 and then Present
(Spec_Entity
(E1
))
417 Has_Pragma_Unreferenced
(Spec_Entity
(E1
)))
421 -- Pragma Unreferenced not set, so output message
424 Output_Reference_Error
425 ("& is never assigned a value?");
427 -- Deal with special case where this variable is
428 -- hidden by a loop variable
430 if Ekind
(E1
) = E_Variable
431 and then Present
(Hiding_Loop_Variable
(E1
))
433 Error_Msg_Sloc
:= Sloc
(E1
);
435 ("declaration hides &#?",
436 Hiding_Loop_Variable
(E1
));
438 ("for loop implicitly declares loop variable?",
439 Hiding_Loop_Variable
(E1
));
445 -- Case of variable that could be a constant. Note that we
446 -- never signal such messages for generic package entities,
447 -- since a given instance could have modifications outside
450 elsif Warn_On_Constant
451 and then Ekind
(E1
) = E_Variable
452 and then Is_True_Constant
(E1
)
453 and then not Generic_Package_Spec_Entity
(E1
)
456 ("& is not modified, could be declared constant?", E1
);
459 -- Check for unset reference, note that we exclude access
460 -- types from this check, since access types do always have
461 -- a null value, and that seems legitimate in this case.
463 if Warn_On_No_Value_Assigned
and then Present
(UR
) then
465 -- For other than access type, go back to original node
466 -- to deal with case where original unset reference
467 -- has been rewritten during expansion.
469 UR
:= Original_Node
(UR
);
471 -- In some cases, the original node may be a type
472 -- conversion or qualification, and in this case
473 -- we want the object entity inside.
475 while Nkind
(UR
) = N_Type_Conversion
476 or else Nkind
(UR
) = N_Qualified_Expression
478 UR
:= Expression
(UR
);
481 -- Here we issue the warning, all checks completed
482 -- If the unset reference is prefix of a selected
483 -- component that comes from source, mention the
484 -- component as well. If the selected component comes
485 -- from expansion, all we know is that the entity is
486 -- not fully initialized at the point of the reference.
487 -- Locate an unintialized component to get a better
490 if Nkind
(Parent
(UR
)) = N_Selected_Component
then
491 Error_Msg_Node_2
:= Selector_Name
(Parent
(UR
));
493 if not Comes_From_Source
(Parent
(UR
)) then
498 Comp
:= First_Entity
(Etype
(E1
));
499 while Present
(Comp
) loop
500 if Ekind
(Comp
) = E_Component
501 and then Nkind
(Parent
(Comp
)) =
502 N_Component_Declaration
503 and then No
(Expression
(Parent
(Comp
)))
505 Error_Msg_Node_2
:= Comp
;
515 ("`&.&` may be referenced before it has a value?",
519 ("& may be referenced before it has a value?",
527 -- Then check for unreferenced entities. Note that we are only
528 -- interested in entities which do not have the Referenced flag
529 -- set. The Referenced_As_LHS flag is interesting only if the
530 -- Referenced flag is not set.
532 if not Referenced
(E1
)
534 -- Check that warnings on unreferenced entities are enabled
536 and then ((Check_Unreferenced
and then not Is_Formal
(E1
))
538 (Check_Unreferenced_Formals
and then Is_Formal
(E1
))
540 (Warn_On_Modified_Unread
541 and then Referenced_As_LHS
(E1
)))
543 -- Labels, and enumeration literals, and exceptions. The
544 -- warnings are also placed on local packages that cannot
545 -- be referenced from elsewhere, including those declared
546 -- within a package body.
548 and then (Is_Object
(E1
)
554 Ekind
(E1
) = E_Exception
556 Ekind
(E1
) = E_Named_Integer
558 Ekind
(E1
) = E_Named_Real
562 (Ekind
(E1
) = E_Package
564 (Ekind
(E
) = E_Function
565 or else Ekind
(E
) = E_Package_Body
566 or else Ekind
(E
) = E_Procedure
567 or else Ekind
(E
) = E_Block
)))
569 -- Exclude instantiations, since there is no reason why
570 -- every entity in an instantiation should be referenced.
572 and then Instantiation_Location
(Sloc
(E1
)) = No_Location
574 -- Exclude formal parameters from bodies if the corresponding
575 -- spec entity has been referenced in the case where there is
578 and then not (Is_Formal
(E1
)
580 Ekind
(Scope
(E1
)) = E_Subprogram_Body
582 Present
(Spec_Entity
(E1
))
584 Referenced
(Spec_Entity
(E1
)))
586 -- Consider private type referenced if full view is referenced
587 -- If there is not full view, this is a generic type on which
588 -- warnings are also useful.
591 not (Is_Private_Type
(E1
)
593 Present
(Full_View
(E1
))
594 and then Referenced
(Full_View
(E1
)))
596 -- Don't worry about full view, only about private type
598 and then not Has_Private_Declaration
(E1
)
600 -- Eliminate dispatching operations from consideration, we
601 -- cannot tell if these are referenced or not in any easy
602 -- manner (note this also catches Adjust/Finalize/Initialize)
604 and then not Is_Dispatching_Operation
(E1
)
606 -- Check entity that can be publicly referenced (we do not
607 -- give messages for such entities, since there could be
608 -- other units, not involved in this compilation, that
609 -- contain relevant references.
611 and then not Publicly_Referenceable
(E1
)
613 -- Class wide types are marked as source entities, but
614 -- they are not really source entities, and are always
615 -- created, so we do not care if they are not referenced.
617 and then Ekind
(E1
) /= E_Class_Wide_Type
619 -- Objects other than parameters of task types are allowed
620 -- to be non-referenced, since they start up tasks!
622 and then ((Ekind
(E1
) /= E_Variable
623 and then Ekind
(E1
) /= E_Constant
624 and then Ekind
(E1
) /= E_Component
)
625 or else not Is_Task_Type
(Etype
(E1
)))
627 -- For subunits, only place warnings on the main unit
628 -- itself, since parent units are not completely compiled
630 and then (Nkind
(Unit
(Cunit
(Main_Unit
))) /= N_Subunit
632 Get_Source_Unit
(E1
) = Main_Unit
)
634 -- Suppress warnings in internal units if not in -gnatg
635 -- mode (these would be junk warnings for an applications
636 -- program, since they refer to problems in internal units)
640 Is_Internal_File_Name
641 (Unit_File_Name
(Get_Source_Unit
(E1
)))
643 -- We do not immediately flag the error. This is because
644 -- we have not expanded generic bodies yet, and they may
645 -- have the missing reference. So instead we park the
646 -- entity on a list, for later processing. However, for
647 -- the accept case, post the error right here, since we
648 -- have the information now in this case.
650 if Present
(Anod
) then
651 Output_Reference_Error
("& is not referenced?");
654 Unreferenced_Entities
.Increment_Last
;
655 Unreferenced_Entities
.Table
656 (Unreferenced_Entities
.Last
) := E1
;
660 -- Generic units are referenced in the generic body,
661 -- but if they are not public and never instantiated
662 -- we want to force a warning on them. We treat them
663 -- as redundant constructs to minimize noise.
665 elsif Is_Generic_Subprogram
(E1
)
666 and then not Is_Instantiated
(E1
)
667 and then not Publicly_Referenceable
(E1
)
668 and then Instantiation_Depth
(Sloc
(E1
)) = 0
669 and then Warn_On_Redundant_Constructs
671 Unreferenced_Entities
.Increment_Last
;
672 Unreferenced_Entities
.Table
(Unreferenced_Entities
.Last
) := E1
;
674 -- Force warning on entity.
676 Set_Referenced
(E1
, False);
680 -- Recurse into nested package or block. Do not recurse into a
681 -- formal package, because the correponding body is not analyzed.
684 if ((Ekind
(E1
) = E_Package
or else Ekind
(E1
) = E_Generic_Package
)
685 and then Nkind
(Parent
(E1
)) = N_Package_Specification
687 Nkind
(Original_Node
(Unit_Declaration_Node
(E1
)))
688 /= N_Formal_Package_Declaration
)
690 or else Ekind
(E1
) = E_Block
692 Check_References
(E1
);
697 end Check_References
;
699 ---------------------------
700 -- Check_Unset_Reference --
701 ---------------------------
703 procedure Check_Unset_Reference
(N
: Node_Id
) is
705 -- Nothing to do if warnings suppressed
707 if Warning_Mode
= Suppress
then
711 -- Ignore reference to non-scalar if not from source. Almost always
712 -- such references are bogus (e.g. calls to init procs to set
713 -- default discriminant values).
715 if not Comes_From_Source
(N
)
716 and then not Is_Scalar_Type
(Etype
(N
))
721 -- Otherwise see what kind of node we have. If the entity already
722 -- has an unset reference, it is not necessarily the earliest in
723 -- the text, because resolution of the prefix of selected components
724 -- is completed before the resolution of the selected component itself.
725 -- as a result, given (R /= null and then R.X > 0), the occurrences
726 -- of R are examined in right-to-left order. If there is already an
727 -- unset reference, we check whether N is earlier before proceeding.
730 when N_Identifier | N_Expanded_Name
=>
732 E
: constant Entity_Id
:= Entity
(N
);
735 if (Ekind
(E
) = E_Variable
736 or else Ekind
(E
) = E_Out_Parameter
)
737 and then Never_Set_In_Source
(E
)
738 and then (No
(Unset_Reference
(E
))
739 or else Earlier_In_Extended_Unit
740 (Sloc
(N
), Sloc
(Unset_Reference
(E
))))
741 and then not Warnings_Off
(E
)
743 -- We may have an unset reference. The first test is
744 -- whether we are accessing a discriminant of a record
745 -- or a component with default initialization. Both of
746 -- these cases can be ignored, since the actual object
747 -- that is referenced is definitely initialized. Note
748 -- that this covers the case of reading discriminants
749 -- of an out parameter, which is OK even in Ada 83.
751 -- Note that we are only interested in a direct reference
752 -- to a record component here. If the reference is via an
753 -- access type, then the access object is being referenced,
754 -- not the record, and still deserves an unset reference.
756 if Nkind
(Parent
(N
)) = N_Selected_Component
757 and not Is_Access_Type
(Etype
(N
))
760 ES
: constant Entity_Id
:=
761 Entity
(Selector_Name
(Parent
(N
)));
764 if Ekind
(ES
) = E_Discriminant
765 or else Present
(Expression
(Declaration_Node
(ES
)))
772 -- Here we have a potential unset reference. But before we
773 -- get worried about it, we have to make sure that the
774 -- entity declaration is in the same procedure as the
775 -- reference, since if they are in separate procedures,
776 -- then we have no idea about sequential execution.
778 -- The tests in the loop below catch all such cases, but
779 -- do allow the reference to appear in a loop, block, or
780 -- package spec that is nested within the declaring scope.
781 -- As always, it is possible to construct cases where the
782 -- warning is wrong, that is why it is a warning!
786 SE
: constant Entity_Id
:= Scope
(E
);
791 if SR
= Standard_Standard
792 or else Is_Subprogram
(SR
)
793 or else Is_Concurrent_Body
(SR
)
794 or else Is_Concurrent_Type
(SR
)
802 -- Case of reference has an access type. This is a
803 -- special case since access types are always set to
804 -- null so cannot be truly uninitialized, but we still
805 -- want to warn about cases of obvious null dereference.
807 if Is_Access_Type
(Etype
(N
)) then
813 return Traverse_Result
;
814 -- Process function for instantation of Traverse
815 -- below. Checks if N contains reference to E
816 -- other than a dereference.
818 function Ref_In
(Nod
: Node_Id
) return Boolean;
819 -- Determines whether Nod contains a reference
820 -- to the entity E that is not a dereference.
824 return Traverse_Result
827 if Is_Entity_Name
(N
)
828 and then Entity
(N
) = E
829 and then not Is_Dereferenced
(N
)
837 function Ref_In
(Nod
: Node_Id
) return Boolean is
838 function Traverse
is new Traverse_Func
(Process
);
841 return Traverse
(Nod
) = Abandon
;
845 -- Don't bother if we are inside an instance,
846 -- since the compilation of the generic template
847 -- is where the warning should be issued.
853 -- Don't bother if this is not the main unit.
854 -- If we try to give this warning for with'ed
855 -- units, we get some false positives, since
856 -- we do not record references in other units.
858 if not In_Extended_Main_Source_Unit
(E
)
860 not In_Extended_Main_Source_Unit
(N
)
865 -- We are only interested in deferences
867 if not Is_Dereferenced
(N
) then
871 -- One more check, don't bother with references
872 -- that are inside conditional statements or while
873 -- loops if the condition references the entity in
874 -- question. This avoids most false positives.
881 if (Nkind
(P
) = N_If_Statement
883 Nkind
(P
) = N_Elsif_Part
)
884 and then Ref_In
(Condition
(P
))
888 elsif Nkind
(P
) = N_Loop_Statement
889 and then Present
(Iteration_Scheme
(P
))
891 Ref_In
(Condition
(Iteration_Scheme
(P
)))
899 -- Here we definitely have a case for giving a warning
900 -- for a reference to an unset value. But we don't give
901 -- the warning now. Instead we set the Unset_Reference
902 -- field of the identifier involved. The reason for this
903 -- is that if we find the variable is never ever assigned
904 -- a value then that warning is more important and there
905 -- is no point in giving the reference warning.
907 -- If this is an identifier, set the field directly
909 if Nkind
(N
) = N_Identifier
then
910 Set_Unset_Reference
(E
, N
);
912 -- Otherwise it is an expanded name, so set the field
913 -- of the actual identifier for the reference.
916 Set_Unset_Reference
(E
, Selector_Name
(N
));
922 when N_Indexed_Component | N_Slice
=>
923 Check_Unset_Reference
(Prefix
(N
));
925 when N_Selected_Component
=>
927 if Present
(Entity
(Selector_Name
(N
)))
928 and then Ekind
(Entity
(Selector_Name
(N
))) = E_Discriminant
930 -- A discriminant is always initialized
935 Check_Unset_Reference
(Prefix
(N
));
938 when N_Type_Conversion | N_Qualified_Expression
=>
939 Check_Unset_Reference
(Expression
(N
));
945 end Check_Unset_Reference
;
947 ------------------------
948 -- Check_Unused_Withs --
949 ------------------------
951 procedure Check_Unused_Withs
(Spec_Unit
: Unit_Number_Type
:= No_Unit
) is
957 Munite
: constant Entity_Id
:= Cunit_Entity
(Main_Unit
);
958 -- This is needed for checking the special renaming case
960 procedure Check_One_Unit
(Unit
: Unit_Number_Type
);
961 -- Subsidiary procedure, performs checks for specified unit
967 procedure Check_One_Unit
(Unit
: Unit_Number_Type
) is
968 Is_Visible_Renaming
: Boolean := False;
971 procedure Check_Inner_Package
(Pack
: Entity_Id
);
972 -- Pack is a package local to a unit in a with_clause. Both the
973 -- unit and Pack are referenced. If none of the entities in Pack
974 -- are referenced, then the only occurrence of Pack is in a use
975 -- clause or a pragma, and a warning is worthwhile as well.
977 function Check_System_Aux
return Boolean;
978 -- Before giving a warning on a with_clause for System, check
979 -- whether a system extension is present.
981 function Find_Package_Renaming
983 L
: Entity_Id
) return Entity_Id
;
984 -- The only reference to a context unit may be in a renaming
985 -- declaration. If this renaming declares a visible entity, do
986 -- not warn that the context clause could be moved to the body,
987 -- because the renaming may be intented to re-export the unit.
989 -------------------------
990 -- Check_Inner_Package --
991 -------------------------
993 procedure Check_Inner_Package
(Pack
: Entity_Id
) is
995 Un
: constant Node_Id
:= Sinfo
.Unit
(Cnode
);
997 function Check_Use_Clause
(N
: Node_Id
) return Traverse_Result
;
998 -- If N is a use_clause for Pack, emit warning.
1000 procedure Check_Use_Clauses
is new
1001 Traverse_Proc
(Check_Use_Clause
);
1003 ----------------------
1004 -- Check_Use_Clause --
1005 ----------------------
1007 function Check_Use_Clause
(N
: Node_Id
) return Traverse_Result
is
1011 if Nkind
(N
) = N_Use_Package_Clause
then
1012 Nam
:= First
(Names
(N
));
1014 while Present
(Nam
) loop
1015 if Entity
(Nam
) = Pack
then
1016 Error_Msg_Qual_Level
:= 1;
1018 ("no entities of package& are referenced?",
1020 Error_Msg_Qual_Level
:= 0;
1028 end Check_Use_Clause
;
1030 -- Start of processing for Check_Inner_Package
1033 E
:= First_Entity
(Pack
);
1035 while Present
(E
) loop
1036 if Referenced
(E
) then
1043 -- No entities of the package are referenced. Check whether
1044 -- the reference to the package itself is a use clause, and
1045 -- if so place a warning on it.
1047 Check_Use_Clauses
(Un
);
1048 end Check_Inner_Package
;
1050 ----------------------
1051 -- Check_System_Aux --
1052 ----------------------
1054 function Check_System_Aux
return Boolean is
1058 if Chars
(Lunit
) = Name_System
1059 and then Scope
(Lunit
) = Standard_Standard
1060 and then Present_System_Aux
1062 Ent
:= First_Entity
(System_Aux_Id
);
1064 while Present
(Ent
) loop
1065 if Referenced
(Ent
) then
1074 end Check_System_Aux
;
1076 ---------------------------
1077 -- Find_Package_Renaming --
1078 ---------------------------
1080 function Find_Package_Renaming
1082 L
: Entity_Id
) return Entity_Id
1088 Is_Visible_Renaming
:= False;
1089 E1
:= First_Entity
(P
);
1091 while Present
(E1
) loop
1092 if Ekind
(E1
) = E_Package
1093 and then Renamed_Object
(E1
) = L
1095 Is_Visible_Renaming
:= not Is_Hidden
(E1
);
1098 elsif Ekind
(E1
) = E_Package
1099 and then No
(Renamed_Object
(E1
))
1100 and then not Is_Generic_Instance
(E1
)
1102 R
:= Find_Package_Renaming
(E1
, L
);
1105 Is_Visible_Renaming
:= not Is_Hidden
(R
);
1114 end Find_Package_Renaming
;
1116 -- Start of processing for Check_One_Unit
1119 Cnode
:= Cunit
(Unit
);
1121 -- Only do check in units that are part of the extended main
1122 -- unit. This is actually a necessary restriction, because in
1123 -- the case of subprogram acting as its own specification,
1124 -- there can be with's in subunits that we will not see.
1126 if not In_Extended_Main_Source_Unit
(Cnode
) then
1129 -- In configurable run time mode, we remove the bodies of
1130 -- non-inlined subprograms, which may lead to spurious warnings,
1131 -- which are clearly undesirable.
1133 elsif Configurable_Run_Time_Mode
1134 and then Is_Predefined_File_Name
(Unit_File_Name
(Unit
))
1139 -- Loop through context items in this unit
1141 Item
:= First
(Context_Items
(Cnode
));
1142 while Present
(Item
) loop
1143 if Nkind
(Item
) = N_With_Clause
1144 and then not Implicit_With
(Item
)
1145 and then In_Extended_Main_Source_Unit
(Item
)
1147 Lunit
:= Entity
(Name
(Item
));
1149 -- Check if this unit is referenced
1151 if not Referenced
(Lunit
) then
1153 -- Suppress warnings in internal units if not in -gnatg
1154 -- mode (these would be junk warnings for an applications
1155 -- program, since they refer to problems in internal units)
1158 or else not Is_Internal_File_Name
(Unit_File_Name
(Unit
))
1160 -- Here we definitely have a non-referenced unit. If
1161 -- it is the special call for a spec unit, then just
1162 -- set the flag to be read later.
1164 if Unit
= Spec_Unit
then
1165 Set_Unreferenced_In_Spec
(Item
);
1167 -- Otherwise simple unreferenced message
1171 ("unit& is not referenced?", Name
(Item
));
1175 -- If main unit is a renaming of this unit, then we consider
1176 -- the with to be OK (obviously it is needed in this case!)
1178 elsif Present
(Renamed_Entity
(Munite
))
1179 and then Renamed_Entity
(Munite
) = Lunit
1183 -- If this unit is referenced, and it is a package, we
1184 -- do another test, to see if any of the entities in the
1185 -- package are referenced. If none of the entities are
1186 -- referenced, we still post a warning. This occurs if
1187 -- the only use of the package is in a use clause, or
1188 -- in a package renaming declaration.
1190 elsif Ekind
(Lunit
) = E_Package
then
1192 -- If Is_Instantiated is set, it means that the package
1193 -- is implicitly instantiated (this is the case of a
1194 -- parent instance or an actual for a generic package
1195 -- formal), and this counts as a reference.
1197 if Is_Instantiated
(Lunit
) then
1200 -- If no entities in package, and there is a pragma
1201 -- Elaborate_Body present, then assume that this with
1202 -- is done for purposes of this elaboration.
1204 elsif No
(First_Entity
(Lunit
))
1205 and then Has_Pragma_Elaborate_Body
(Lunit
)
1209 -- Otherwise see if any entities have been referenced
1212 Ent
:= First_Entity
(Lunit
);
1214 -- No more entities, and we did not find one
1215 -- that was referenced. Means we have a definite
1216 -- case of a with none of whose entities was
1221 -- If in spec, just set the flag
1223 if Unit
= Spec_Unit
then
1224 Set_No_Entities_Ref_In_Spec
(Item
);
1226 elsif Check_System_Aux
then
1229 -- Else give the warning
1233 ("no entities of & are referenced?",
1236 -- Look for renamings of this package, and
1237 -- flag them as well. If the original package
1238 -- has warnings off, we suppress the warning
1239 -- on the renaming as well.
1241 Pack
:= Find_Package_Renaming
(Munite
, Lunit
);
1244 and then not Warnings_Off
(Lunit
)
1247 ("no entities of & are referenced?",
1248 Unit_Declaration_Node
(Pack
),
1255 -- Case of next entity is referenced
1257 elsif Referenced
(Ent
)
1258 or else Referenced_As_LHS
(Ent
)
1260 -- This means that the with is indeed fine, in
1261 -- that it is definitely needed somewhere, and
1262 -- we can quite worrying about this one.
1264 -- Except for one little detail, if either of
1265 -- the flags was set during spec processing,
1266 -- this is where we complain that the with
1267 -- could be moved from the spec. If the spec
1268 -- contains a visible renaming of the package,
1269 -- inhibit warning to move with_clause to body.
1271 if Ekind
(Munite
) = E_Package_Body
then
1273 Find_Package_Renaming
1274 (Spec_Entity
(Munite
), Lunit
);
1277 if Unreferenced_In_Spec
(Item
) then
1279 ("unit& is not referenced in spec?",
1282 elsif No_Entities_Ref_In_Spec
(Item
) then
1284 ("no entities of & are referenced in spec?",
1288 if Ekind
(Ent
) = E_Package
then
1289 Check_Inner_Package
(Ent
);
1295 if not Is_Visible_Renaming
then
1297 ("\with clause might be moved to body?",
1303 -- Move to next entity to continue search
1311 -- For a generic package, the only interesting kind of
1312 -- reference is an instantiation, since entities cannot
1313 -- be referenced directly.
1315 elsif Is_Generic_Unit
(Lunit
) then
1317 -- Unit was never instantiated, set flag for case of spec
1318 -- call, or give warning for normal call.
1320 if not Is_Instantiated
(Lunit
) then
1321 if Unit
= Spec_Unit
then
1322 Set_Unreferenced_In_Spec
(Item
);
1325 ("unit& is never instantiated?", Name
(Item
));
1328 -- If unit was indeed instantiated, make sure that
1329 -- flag is not set showing it was uninstantiated in
1330 -- the spec, and if so, give warning.
1332 elsif Unreferenced_In_Spec
(Item
) then
1334 ("unit& is not instantiated in spec?", Name
(Item
));
1336 ("\with clause can be moved to body?", Name
(Item
));
1346 -- Start of processing for Check_Unused_Withs
1349 if not Opt
.Check_Withs
1350 or else Operating_Mode
= Check_Syntax
1355 -- Flag any unused with clauses, but skip this step if we are
1356 -- compiling a subunit on its own, since we do not have enough
1357 -- information to determine whether with's are used. We will get
1358 -- the relevant warnings when we compile the parent. This is the
1359 -- normal style of GNAT compilation in any case.
1361 if Nkind
(Unit
(Cunit
(Main_Unit
))) = N_Subunit
then
1365 -- Process specified units
1367 if Spec_Unit
= No_Unit
then
1369 -- For main call, check all units
1371 for Unit
in Main_Unit
.. Last_Unit
loop
1372 Check_One_Unit
(Unit
);
1376 -- For call for spec, check only the spec
1378 Check_One_Unit
(Spec_Unit
);
1380 end Check_Unused_Withs
;
1382 ---------------------------------
1383 -- Generic_Package_Spec_Entity --
1384 ---------------------------------
1386 function Generic_Package_Spec_Entity
(E
: Entity_Id
) return Boolean is
1390 if Is_Package_Body_Entity
(E
) then
1397 if S
= Standard_Standard
then
1400 elsif Ekind
(S
) = E_Generic_Package
then
1403 elsif Ekind
(S
) = E_Package
then
1411 end Generic_Package_Spec_Entity
;
1413 -------------------------------------
1414 -- Operand_Has_Warnings_Suppressed --
1415 -------------------------------------
1417 function Operand_Has_Warnings_Suppressed
(N
: Node_Id
) return Boolean is
1419 function Check_For_Warnings
(N
: Node_Id
) return Traverse_Result
;
1420 -- Function used to check one node to see if it is or was originally
1421 -- a reference to an entity for which Warnings are off. If so, Abandon
1422 -- is returned, otherwise OK_Orig is returned to continue the traversal
1423 -- of the original expression.
1425 function Traverse
is new Traverse_Func
(Check_For_Warnings
);
1426 -- Function used to traverse tree looking for warnings
1428 ------------------------
1429 -- Check_For_Warnings --
1430 ------------------------
1432 function Check_For_Warnings
(N
: Node_Id
) return Traverse_Result
is
1433 R
: constant Node_Id
:= Original_Node
(N
);
1436 if Nkind
(R
) in N_Has_Entity
1437 and then Present
(Entity
(R
))
1438 and then Warnings_Off
(Entity
(R
))
1444 end Check_For_Warnings
;
1446 -- Start of processing for Operand_Has_Warnings_Suppressed
1449 return Traverse
(N
) = Abandon
;
1451 -- If any exception occurs, then something has gone wrong, and this is
1452 -- only a minor aesthetic issue anyway, so just say we did not find what
1453 -- we are looking for, rather than blow up.
1458 end Operand_Has_Warnings_Suppressed
;
1460 ----------------------------------
1461 -- Output_Unreferenced_Messages --
1462 ----------------------------------
1464 procedure Output_Unreferenced_Messages
is
1468 for J
in Unreferenced_Entities
.First
..
1469 Unreferenced_Entities
.Last
1471 E
:= Unreferenced_Entities
.Table
(J
);
1473 if not Referenced
(E
) and then not Warnings_Off
(E
) then
1477 -- Case of variable that is assigned but not read. We
1478 -- suppress the message if the variable is volatile,
1479 -- has an address clause, or is imported.
1481 if Referenced_As_LHS
(E
)
1482 and then No
(Address_Clause
(E
))
1483 and then not Is_Volatile
(E
)
1485 if Warn_On_Modified_Unread
1486 and then not Is_Imported
(E
)
1489 ("variable & is assigned but never read?", E
);
1492 -- Normal case of neither assigned nor read
1495 if Present
(Renamed_Object
(E
))
1496 and then Comes_From_Source
(Renamed_Object
(E
))
1499 ("renamed variable & is not referenced?", E
);
1502 ("variable & is not referenced?", E
);
1507 if Present
(Renamed_Object
(E
))
1508 and then Comes_From_Source
(Renamed_Object
(E
))
1510 Error_Msg_N
("renamed constant & is not referenced?", E
);
1512 Error_Msg_N
("constant & is not referenced?", E
);
1515 when E_In_Parameter |
1517 E_In_Out_Parameter
=>
1519 -- Do not emit message for formals of a renaming, because
1520 -- they are never referenced explicitly.
1522 if Nkind
(Original_Node
(Unit_Declaration_Node
(Scope
(E
))))
1523 /= N_Subprogram_Renaming_Declaration
1525 Error_Msg_N
("formal parameter & is not referenced?", E
);
1528 when E_Named_Integer |
1530 Error_Msg_N
("named number & is not referenced?", E
);
1532 when E_Enumeration_Literal
=>
1533 Error_Msg_N
("literal & is not referenced?", E
);
1536 Error_Msg_N
("function & is not referenced?", E
);
1539 Error_Msg_N
("procedure & is not referenced?", E
);
1541 when E_Generic_Procedure
=>
1543 ("generic procedure & is never instantiated?", E
);
1545 when E_Generic_Function
=>
1546 Error_Msg_N
("generic function & is never instantiated?", E
);
1549 Error_Msg_N
("type & is not referenced?", E
);
1552 Error_Msg_N
("& is not referenced?", E
);
1555 Set_Warnings_Off
(E
);
1558 end Output_Unreferenced_Messages
;
1560 -----------------------------
1561 -- Warn_On_Known_Condition --
1562 -----------------------------
1564 procedure Warn_On_Known_Condition
(C
: Node_Id
) is
1568 -- Argument replacement in an inlined body can make conditions
1569 -- static. Do not emit warnings in this case.
1571 if In_Inlined_Body
then
1575 if Constant_Condition_Warnings
1576 and then Nkind
(C
) = N_Identifier
1578 (Entity
(C
) = Standard_False
or else Entity
(C
) = Standard_True
)
1579 and then Comes_From_Source
(Original_Node
(C
))
1580 and then not In_Instance
1582 -- See if this is in a statement or a declaration
1586 -- If tree is not attached, do not issue warning (this is very
1587 -- peculiar, and probably arises from some other error condition)
1592 -- If we are in a declaration, then no warning, since in practice
1593 -- conditionals in declarations are used for intended tests which
1594 -- may be known at compile time, e.g. things like
1596 -- x : constant Integer := 2 + (Word'Size = 32);
1598 -- And a warning is annoying in such cases
1600 elsif Nkind
(P
) in N_Declaration
1602 Nkind
(P
) in N_Later_Decl_Item
1606 -- Don't warn in assert pragma, since presumably tests in such
1607 -- a context are very definitely intended, and might well be
1608 -- known at compile time. Note that we have to test the original
1609 -- node, since assert pragmas get rewritten at analysis time.
1611 elsif Nkind
(Original_Node
(P
)) = N_Pragma
1612 and then Chars
(Original_Node
(P
)) = Name_Assert
1617 exit when Is_Statement
(P
);
1621 -- Here we issue the warning unless some sub-operand has warnings
1622 -- set off, in which case we suppress the warning for the node.
1624 if not Operand_Has_Warnings_Suppressed
(C
) then
1625 if Entity
(C
) = Standard_True
then
1626 Error_Msg_N
("condition is always True?", C
);
1628 Error_Msg_N
("condition is always False?", C
);
1632 end Warn_On_Known_Condition
;