1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1999-2003 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 off
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 the entity is an out parameter of the current subprogram
371 -- body, check the warning status of the parameter in the spec.
373 if Ekind
(E1
) = E_Out_Parameter
374 and then Present
(Spec_Entity
(E1
))
375 and then Warnings_Off
(Spec_Entity
(E1
))
379 elsif Never_Set_In_Source
(E1
)
380 and then not Generic_Package_Spec_Entity
(E1
)
382 if Warn_On_No_Value_Assigned
then
384 -- Do not output complaint about never being assigned a
385 -- value if a pragma Unreferenced applies to the variable
386 -- or if it is a parameter, to the corresponding spec.
388 if Has_Pragma_Unreferenced
(E1
)
389 or else (Is_Formal
(E1
)
390 and then Present
(Spec_Entity
(E1
))
392 Has_Pragma_Unreferenced
(Spec_Entity
(E1
)))
396 -- Pragma Unreferenced not set, so output message
399 Output_Reference_Error
400 ("& is never assigned a value?");
402 -- Deal with special case where this variable is
403 -- hidden by a loop variable
405 if Ekind
(E1
) = E_Variable
406 and then Present
(Hiding_Loop_Variable
(E1
))
408 Error_Msg_Sloc
:= Sloc
(E1
);
410 ("declaration hides &#?",
411 Hiding_Loop_Variable
(E1
));
413 ("for loop implicitly declares loop variable?",
414 Hiding_Loop_Variable
(E1
));
420 -- Case of variable that could be a constant. Note that we
421 -- never signal such messages for generic package entities,
422 -- since a given instance could have modifications outside
425 elsif Warn_On_Constant
426 and then Ekind
(E1
) = E_Variable
427 and then Is_True_Constant
(E1
)
428 and then not Generic_Package_Spec_Entity
(E1
)
431 ("& is not modified, could be declared constant?", E1
);
434 -- Check for unset reference, note that we exclude access
435 -- types from this check, since access types do always have
436 -- a null value, and that seems legitimate in this case.
438 if Ekind
(E1
) = E_Out_Parameter
439 and then Present
(Spec_Entity
(E1
))
441 UR
:= Unset_Reference
(Spec_Entity
(E1
));
443 UR
:= Unset_Reference
(E1
);
446 if Warn_On_No_Value_Assigned
and then Present
(UR
) then
448 -- For access types, the only time we made a UR entry
449 -- was for a dereference, and so we post the appropriate
450 -- warning here. The issue is not that the value is not
451 -- initialized here, but that it is null.
453 if Is_Access_Type
(Etype
(E1
)) then
454 Error_Msg_NE
("& may be null?", UR
, E1
);
457 -- For other than access type, go back to original node
458 -- to deal with case where original unset reference
459 -- has been rewritten during expansion.
462 UR
:= Original_Node
(UR
);
464 -- In some cases, the original node may be a type
465 -- conversion or qualification, and in this case
466 -- we want the object entity inside.
468 while Nkind
(UR
) = N_Type_Conversion
469 or else Nkind
(UR
) = N_Qualified_Expression
471 UR
:= Expression
(UR
);
474 -- Here we issue the warning, all checks completed
476 if Nkind
(Parent
(UR
)) = N_Selected_Component
then
477 Error_Msg_Node_2
:= Selector_Name
(Parent
(UR
));
479 ("`&.&` may be referenced before it has a value?",
483 ("& may be referenced before it has a value?",
492 -- Then check for unreferenced entities. Note that we are only
493 -- interested in entities which do not have the Referenced flag
494 -- set. The Referenced_As_LHS flag is interesting only if the
495 -- Referenced flag is not set.
497 if not Referenced
(E1
)
499 -- Check that warnings on unreferenced entities are enabled
501 and then ((Check_Unreferenced
and then not Is_Formal
(E1
))
503 (Check_Unreferenced_Formals
and then Is_Formal
(E1
))
505 (Warn_On_Modified_Unread
506 and then Referenced_As_LHS
(E1
)))
508 -- Labels, and enumeration literals, and exceptions. The
509 -- warnings are also placed on local packages that cannot
510 -- be referenced from elsewhere, including those declared
511 -- within a package body.
513 and then (Is_Object
(E1
)
519 Ekind
(E1
) = E_Exception
521 Ekind
(E1
) = E_Named_Integer
523 Ekind
(E1
) = E_Named_Real
527 (Ekind
(E1
) = E_Package
529 (Ekind
(E
) = E_Function
530 or else Ekind
(E
) = E_Package_Body
531 or else Ekind
(E
) = E_Procedure
532 or else Ekind
(E
) = E_Block
)))
534 -- Exclude instantiations, since there is no reason why
535 -- every entity in an instantiation should be referenced.
537 and then Instantiation_Location
(Sloc
(E1
)) = No_Location
539 -- Exclude formal parameters from bodies if the corresponding
540 -- spec entity has been referenced in the case where there is
543 and then not (Is_Formal
(E1
)
545 Ekind
(Scope
(E1
)) = E_Subprogram_Body
547 Present
(Spec_Entity
(E1
))
549 Referenced
(Spec_Entity
(E1
)))
551 -- Consider private type referenced if full view is referenced
552 -- If there is not full view, this is a generic type on which
553 -- warnings are also useful.
556 not (Is_Private_Type
(E1
)
558 Present
(Full_View
(E1
))
559 and then Referenced
(Full_View
(E1
)))
561 -- Don't worry about full view, only about private type
563 and then not Has_Private_Declaration
(E1
)
565 -- Eliminate dispatching operations from consideration, we
566 -- cannot tell if these are referenced or not in any easy
567 -- manner (note this also catches Adjust/Finalize/Initialize)
569 and then not Is_Dispatching_Operation
(E1
)
571 -- Check entity that can be publicly referenced (we do not
572 -- give messages for such entities, since there could be
573 -- other units, not involved in this compilation, that
574 -- contain relevant references.
576 and then not Publicly_Referenceable
(E1
)
578 -- Class wide types are marked as source entities, but
579 -- they are not really source entities, and are always
580 -- created, so we do not care if they are not referenced.
582 and then Ekind
(E1
) /= E_Class_Wide_Type
584 -- Objects other than parameters of task types are allowed
585 -- to be non-referenced, since they start up tasks!
587 and then ((Ekind
(E1
) /= E_Variable
588 and then Ekind
(E1
) /= E_Constant
589 and then Ekind
(E1
) /= E_Component
)
590 or else not Is_Task_Type
(Etype
(E1
)))
592 -- For subunits, only place warnings on the main unit
593 -- itself, since parent units are not completely compiled
595 and then (Nkind
(Unit
(Cunit
(Main_Unit
))) /= N_Subunit
597 Get_Source_Unit
(E1
) = Main_Unit
)
599 -- Suppress warnings in internal units if not in -gnatg
600 -- mode (these would be junk warnings for an applications
601 -- program, since they refer to problems in internal units)
605 Is_Internal_File_Name
606 (Unit_File_Name
(Get_Source_Unit
(E1
)))
608 -- We do not immediately flag the error. This is because
609 -- we have not expanded generic bodies yet, and they may
610 -- have the missing reference. So instead we park the
611 -- entity on a list, for later processing. However, for
612 -- the accept case, post the error right here, since we
613 -- have the information now in this case.
615 if Present
(Anod
) then
616 Output_Reference_Error
("& is not referenced?");
619 Unreferenced_Entities
.Increment_Last
;
620 Unreferenced_Entities
.Table
621 (Unreferenced_Entities
.Last
) := E1
;
625 -- Generic units are referenced in the generic body,
626 -- but if they are not public and never instantiated
627 -- we want to force a warning on them. We treat them
628 -- as redundant constructs to minimize noise.
630 elsif Is_Generic_Subprogram
(E1
)
631 and then not Is_Instantiated
(E1
)
632 and then not Publicly_Referenceable
(E1
)
633 and then Instantiation_Depth
(Sloc
(E1
)) = 0
634 and then Warn_On_Redundant_Constructs
636 Unreferenced_Entities
.Increment_Last
;
637 Unreferenced_Entities
.Table
(Unreferenced_Entities
.Last
) := E1
;
639 -- Force warning on entity.
641 Set_Referenced
(E1
, False);
645 -- Recurse into nested package or block. Do not recurse into a
646 -- formal package, because the correponding body is not analyzed.
649 if ((Ekind
(E1
) = E_Package
or else Ekind
(E1
) = E_Generic_Package
)
650 and then Nkind
(Parent
(E1
)) = N_Package_Specification
652 Nkind
(Original_Node
(Unit_Declaration_Node
(E1
)))
653 /= N_Formal_Package_Declaration
)
655 or else Ekind
(E1
) = E_Block
657 Check_References
(E1
);
662 end Check_References
;
664 ---------------------------
665 -- Check_Unset_Reference --
666 ---------------------------
668 procedure Check_Unset_Reference
(N
: Node_Id
) is
670 -- Nothing to do if warnings suppressed
672 if Warning_Mode
= Suppress
then
676 -- Ignore reference to non-scalar if not from source. Almost always
677 -- such references are bogus (e.g. calls to init procs to set
678 -- default discriminant values).
680 if not Comes_From_Source
(N
)
681 and then not Is_Scalar_Type
(Etype
(N
))
686 -- Otherwise see what kind of node we have. If the entity already
687 -- has an unset reference, it is not necessarily the earliest in
688 -- the text, because resolution of the prefix of selected components
689 -- is completed before the resolution of the selected component itself.
690 -- as a result, given (R /= null and then R.X > 0), the occurrences
691 -- of R are examined in right-to-left order. If there is already an
692 -- unset reference, we check whether N is earlier before proceeding.
695 when N_Identifier | N_Expanded_Name
=>
697 E
: constant Entity_Id
:= Entity
(N
);
700 if (Ekind
(E
) = E_Variable
701 or else Ekind
(E
) = E_Out_Parameter
)
702 and then Never_Set_In_Source
(E
)
703 and then (No
(Unset_Reference
(E
))
704 or else Earlier_In_Extended_Unit
705 (Sloc
(N
), Sloc
(Unset_Reference
(E
))))
706 and then not Warnings_Off
(E
)
708 -- We may have an unset reference. The first test is
709 -- whether we are accessing a discriminant of a record
710 -- or a component with default initialization. Both of
711 -- these cases can be ignored, since the actual object
712 -- that is referenced is definitely initialized. Note
713 -- that this covers the case of reading discriminants
714 -- of an out parameter, which is OK even in Ada 83.
716 -- Note that we are only interested in a direct reference
717 -- to a record component here. If the reference is via an
718 -- access type, then the access object is being referenced,
719 -- not the record, and still deserves an unset reference.
721 if Nkind
(Parent
(N
)) = N_Selected_Component
722 and not Is_Access_Type
(Etype
(N
))
725 ES
: constant Entity_Id
:=
726 Entity
(Selector_Name
(Parent
(N
)));
729 if Ekind
(ES
) = E_Discriminant
730 or else Present
(Expression
(Declaration_Node
(ES
)))
737 -- Here we have a potential unset reference. But before we
738 -- get worried about it, we have to make sure that the
739 -- entity declaration is in the same procedure as the
740 -- reference, since if they are in separate procedures,
741 -- then we have no idea about sequential execution.
743 -- The tests in the loop below catch all such cases, but
744 -- do allow the reference to appear in a loop, block, or
745 -- package spec that is nested within the declaring scope.
746 -- As always, it is possible to construct cases where the
747 -- warning is wrong, that is why it is a warning!
751 SE
: constant Entity_Id
:= Scope
(E
);
756 if SR
= Standard_Standard
757 or else Is_Subprogram
(SR
)
758 or else Is_Concurrent_Body
(SR
)
759 or else Is_Concurrent_Type
(SR
)
767 -- Case of reference has an access type. This is a
768 -- special case since access types are always set to
769 -- null so cannot be truly uninitialized, but we still
770 -- want to warn about cases of obvious null dereference.
772 if Is_Access_Type
(Etype
(N
)) then
778 return Traverse_Result
;
779 -- Process function for instantation of Traverse
780 -- below. Checks if N contains reference to E
781 -- other than a dereference.
783 function Ref_In
(Nod
: Node_Id
) return Boolean;
784 -- Determines whether Nod contains a reference
785 -- to the entity E that is not a dereference.
789 return Traverse_Result
792 if Is_Entity_Name
(N
)
793 and then Entity
(N
) = E
794 and then not Is_Dereferenced
(N
)
802 function Ref_In
(Nod
: Node_Id
) return Boolean is
803 function Traverse
is new Traverse_Func
(Process
);
806 return Traverse
(Nod
) = Abandon
;
810 -- Don't bother if we are inside an instance,
811 -- since the compilation of the generic template
812 -- is where the warning should be issued.
818 -- Don't bother if this is not the main unit.
819 -- If we try to give this warning for with'ed
820 -- units, we get some false positives, since
821 -- we do not record references in other units.
823 if not In_Extended_Main_Source_Unit
(E
)
825 not In_Extended_Main_Source_Unit
(N
)
830 -- We are only interested in deferences
832 if not Is_Dereferenced
(N
) then
836 -- One more check, don't bother with references
837 -- that are inside conditional statements or while
838 -- loops if the condition references the entity in
839 -- question. This avoids most false positives.
846 if (Nkind
(P
) = N_If_Statement
848 Nkind
(P
) = N_Elsif_Part
)
849 and then Ref_In
(Condition
(P
))
853 elsif Nkind
(P
) = N_Loop_Statement
854 and then Present
(Iteration_Scheme
(P
))
856 Ref_In
(Condition
(Iteration_Scheme
(P
)))
864 -- Here we definitely have a case for giving a warning
865 -- for a reference to an unset value. But we don't give
866 -- the warning now. Instead we set the Unset_Reference
867 -- field of the identifier involved. The reason for this
868 -- is that if we find the variable is never ever assigned
869 -- a value then that warning is more important and there
870 -- is no point in giving the reference warning.
872 -- If this is an identifier, set the field directly
874 if Nkind
(N
) = N_Identifier
then
875 Set_Unset_Reference
(E
, N
);
877 -- Otherwise it is an expanded name, so set the field
878 -- of the actual identifier for the reference.
881 Set_Unset_Reference
(E
, Selector_Name
(N
));
887 when N_Indexed_Component | N_Slice
=>
888 Check_Unset_Reference
(Prefix
(N
));
890 when N_Selected_Component
=>
892 if Present
(Entity
(Selector_Name
(N
)))
893 and then Ekind
(Entity
(Selector_Name
(N
))) = E_Discriminant
895 -- A discriminant is always initialized
900 Check_Unset_Reference
(Prefix
(N
));
903 when N_Type_Conversion | N_Qualified_Expression
=>
904 Check_Unset_Reference
(Expression
(N
));
910 end Check_Unset_Reference
;
912 ------------------------
913 -- Check_Unused_Withs --
914 ------------------------
916 procedure Check_Unused_Withs
(Spec_Unit
: Unit_Number_Type
:= No_Unit
) is
922 Munite
: constant Entity_Id
:= Cunit_Entity
(Main_Unit
);
923 -- This is needed for checking the special renaming case
925 procedure Check_One_Unit
(Unit
: Unit_Number_Type
);
926 -- Subsidiary procedure, performs checks for specified unit
932 procedure Check_One_Unit
(Unit
: Unit_Number_Type
) is
933 Is_Visible_Renaming
: Boolean := False;
936 procedure Check_Inner_Package
(Pack
: Entity_Id
);
937 -- Pack is a package local to a unit in a with_clause. Both the
938 -- unit and Pack are referenced. If none of the entities in Pack
939 -- are referenced, then the only occurrence of Pack is in a use
940 -- clause or a pragma, and a warning is worthwhile as well.
942 function Check_System_Aux
return Boolean;
943 -- Before giving a warning on a with_clause for System, check
944 -- whether a system extension is present.
946 function Find_Package_Renaming
948 L
: Entity_Id
) return Entity_Id
;
949 -- The only reference to a context unit may be in a renaming
950 -- declaration. If this renaming declares a visible entity, do
951 -- not warn that the context clause could be moved to the body,
952 -- because the renaming may be intented to re-export the unit.
954 -------------------------
955 -- Check_Inner_Package --
956 -------------------------
958 procedure Check_Inner_Package
(Pack
: Entity_Id
) is
960 Un
: constant Node_Id
:= Sinfo
.Unit
(Cnode
);
962 function Check_Use_Clause
(N
: Node_Id
) return Traverse_Result
;
963 -- If N is a use_clause for Pack, emit warning.
965 procedure Check_Use_Clauses
is new
966 Traverse_Proc
(Check_Use_Clause
);
968 ----------------------
969 -- Check_Use_Clause --
970 ----------------------
972 function Check_Use_Clause
(N
: Node_Id
) return Traverse_Result
is
976 if Nkind
(N
) = N_Use_Package_Clause
then
977 Nam
:= First
(Names
(N
));
979 while Present
(Nam
) loop
980 if Entity
(Nam
) = Pack
then
981 Error_Msg_Qual_Level
:= 1;
983 ("no entities of package& are referenced?",
985 Error_Msg_Qual_Level
:= 0;
993 end Check_Use_Clause
;
995 -- Start of processing for Check_Inner_Package
998 E
:= First_Entity
(Pack
);
1000 while Present
(E
) loop
1001 if Referenced
(E
) then
1008 -- No entities of the package are referenced. Check whether
1009 -- the reference to the package itself is a use clause, and
1010 -- if so place a warning on it.
1012 Check_Use_Clauses
(Un
);
1013 end Check_Inner_Package
;
1015 ----------------------
1016 -- Check_System_Aux --
1017 ----------------------
1019 function Check_System_Aux
return Boolean is
1023 if Chars
(Lunit
) = Name_System
1024 and then Scope
(Lunit
) = Standard_Standard
1025 and then Present_System_Aux
1027 Ent
:= First_Entity
(System_Aux_Id
);
1029 while Present
(Ent
) loop
1030 if Referenced
(Ent
) then
1039 end Check_System_Aux
;
1041 ---------------------------
1042 -- Find_Package_Renaming --
1043 ---------------------------
1045 function Find_Package_Renaming
1047 L
: Entity_Id
) return Entity_Id
1053 Is_Visible_Renaming
:= False;
1054 E1
:= First_Entity
(P
);
1056 while Present
(E1
) loop
1057 if Ekind
(E1
) = E_Package
1058 and then Renamed_Object
(E1
) = L
1060 Is_Visible_Renaming
:= not Is_Hidden
(E1
);
1063 elsif Ekind
(E1
) = E_Package
1064 and then No
(Renamed_Object
(E1
))
1065 and then not Is_Generic_Instance
(E1
)
1067 R
:= Find_Package_Renaming
(E1
, L
);
1070 Is_Visible_Renaming
:= not Is_Hidden
(R
);
1079 end Find_Package_Renaming
;
1081 -- Start of processing for Check_One_Unit
1084 Cnode
:= Cunit
(Unit
);
1086 -- Only do check in units that are part of the extended main
1087 -- unit. This is actually a necessary restriction, because in
1088 -- the case of subprogram acting as its own specification,
1089 -- there can be with's in subunits that we will not see.
1091 if not In_Extended_Main_Source_Unit
(Cnode
) then
1094 -- In configurable run time mode, we remove the bodies of
1095 -- non-inlined subprograms, which may lead to spurious warnings,
1096 -- which are clearly undesirable.
1098 elsif Configurable_Run_Time_Mode
1099 and then Is_Predefined_File_Name
(Unit_File_Name
(Unit
))
1104 -- Loop through context items in this unit
1106 Item
:= First
(Context_Items
(Cnode
));
1107 while Present
(Item
) loop
1108 if Nkind
(Item
) = N_With_Clause
1109 and then not Implicit_With
(Item
)
1110 and then In_Extended_Main_Source_Unit
(Item
)
1112 Lunit
:= Entity
(Name
(Item
));
1114 -- Check if this unit is referenced
1116 if not Referenced
(Lunit
) then
1118 -- Suppress warnings in internal units if not in -gnatg
1119 -- mode (these would be junk warnings for an applications
1120 -- program, since they refer to problems in internal units)
1123 or else not Is_Internal_File_Name
(Unit_File_Name
(Unit
))
1125 -- Here we definitely have a non-referenced unit. If
1126 -- it is the special call for a spec unit, then just
1127 -- set the flag to be read later.
1129 if Unit
= Spec_Unit
then
1130 Set_Unreferenced_In_Spec
(Item
);
1132 -- Otherwise simple unreferenced message
1136 ("unit& is not referenced?", Name
(Item
));
1140 -- If main unit is a renaming of this unit, then we consider
1141 -- the with to be OK (obviously it is needed in this case!)
1143 elsif Present
(Renamed_Entity
(Munite
))
1144 and then Renamed_Entity
(Munite
) = Lunit
1148 -- If this unit is referenced, and it is a package, we
1149 -- do another test, to see if any of the entities in the
1150 -- package are referenced. If none of the entities are
1151 -- referenced, we still post a warning. This occurs if
1152 -- the only use of the package is in a use clause, or
1153 -- in a package renaming declaration.
1155 elsif Ekind
(Lunit
) = E_Package
then
1157 -- If Is_Instantiated is set, it means that the package
1158 -- is implicitly instantiated (this is the case of a
1159 -- parent instance or an actual for a generic package
1160 -- formal), and this counts as a reference.
1162 if Is_Instantiated
(Lunit
) then
1165 -- If no entities in package, and there is a pragma
1166 -- Elaborate_Body present, then assume that this with
1167 -- is done for purposes of this elaboration.
1169 elsif No
(First_Entity
(Lunit
))
1170 and then Has_Pragma_Elaborate_Body
(Lunit
)
1174 -- Otherwise see if any entities have been referenced
1177 Ent
:= First_Entity
(Lunit
);
1179 -- No more entities, and we did not find one
1180 -- that was referenced. Means we have a definite
1181 -- case of a with none of whose entities was
1186 -- If in spec, just set the flag
1188 if Unit
= Spec_Unit
then
1189 Set_No_Entities_Ref_In_Spec
(Item
);
1191 elsif Check_System_Aux
then
1194 -- Else give the warning
1198 ("no entities of & are referenced?",
1201 -- Look for renamings of this package, and
1202 -- flag them as well. If the original package
1203 -- has warnings off, we suppress the warning
1204 -- on the renaming as well.
1206 Pack
:= Find_Package_Renaming
(Munite
, Lunit
);
1209 and then not Warnings_Off
(Lunit
)
1212 ("no entities of & are referenced?",
1213 Unit_Declaration_Node
(Pack
),
1220 -- Case of next entity is referenced
1222 elsif Referenced
(Ent
)
1223 or else Referenced_As_LHS
(Ent
)
1225 -- This means that the with is indeed fine, in
1226 -- that it is definitely needed somewhere, and
1227 -- we can quite worrying about this one.
1229 -- Except for one little detail, if either of
1230 -- the flags was set during spec processing,
1231 -- this is where we complain that the with
1232 -- could be moved from the spec. If the spec
1233 -- contains a visible renaming of the package,
1234 -- inhibit warning to move with_clause to body.
1236 if Ekind
(Munite
) = E_Package_Body
then
1238 Find_Package_Renaming
1239 (Spec_Entity
(Munite
), Lunit
);
1242 if Unreferenced_In_Spec
(Item
) then
1244 ("unit& is not referenced in spec?",
1247 elsif No_Entities_Ref_In_Spec
(Item
) then
1249 ("no entities of & are referenced in spec?",
1253 if Ekind
(Ent
) = E_Package
then
1254 Check_Inner_Package
(Ent
);
1260 if not Is_Visible_Renaming
then
1262 ("\with clause might be moved to body?",
1268 -- Move to next entity to continue search
1276 -- For a generic package, the only interesting kind of
1277 -- reference is an instantiation, since entities cannot
1278 -- be referenced directly.
1280 elsif Is_Generic_Unit
(Lunit
) then
1282 -- Unit was never instantiated, set flag for case of spec
1283 -- call, or give warning for normal call.
1285 if not Is_Instantiated
(Lunit
) then
1286 if Unit
= Spec_Unit
then
1287 Set_Unreferenced_In_Spec
(Item
);
1290 ("unit& is never instantiated?", Name
(Item
));
1293 -- If unit was indeed instantiated, make sure that
1294 -- flag is not set showing it was uninstantiated in
1295 -- the spec, and if so, give warning.
1297 elsif Unreferenced_In_Spec
(Item
) then
1299 ("unit& is not instantiated in spec?", Name
(Item
));
1301 ("\with clause can be moved to body?", Name
(Item
));
1311 -- Start of processing for Check_Unused_Withs
1314 if not Opt
.Check_Withs
1315 or else Operating_Mode
= Check_Syntax
1320 -- Flag any unused with clauses, but skip this step if we are
1321 -- compiling a subunit on its own, since we do not have enough
1322 -- information to determine whether with's are used. We will get
1323 -- the relevant warnings when we compile the parent. This is the
1324 -- normal style of GNAT compilation in any case.
1326 if Nkind
(Unit
(Cunit
(Main_Unit
))) = N_Subunit
then
1330 -- Process specified units
1332 if Spec_Unit
= No_Unit
then
1334 -- For main call, check all units
1336 for Unit
in Main_Unit
.. Last_Unit
loop
1337 Check_One_Unit
(Unit
);
1341 -- For call for spec, check only the spec
1343 Check_One_Unit
(Spec_Unit
);
1345 end Check_Unused_Withs
;
1347 ---------------------------------
1348 -- Generic_Package_Spec_Entity --
1349 ---------------------------------
1351 function Generic_Package_Spec_Entity
(E
: Entity_Id
) return Boolean is
1355 if Is_Package_Body_Entity
(E
) then
1362 if S
= Standard_Standard
then
1365 elsif Ekind
(S
) = E_Generic_Package
then
1368 elsif Ekind
(S
) = E_Package
then
1376 end Generic_Package_Spec_Entity
;
1378 -------------------------------------
1379 -- Operand_Has_Warnings_Suppressed --
1380 -------------------------------------
1382 function Operand_Has_Warnings_Suppressed
(N
: Node_Id
) return Boolean is
1384 function Check_For_Warnings
(N
: Node_Id
) return Traverse_Result
;
1385 -- Function used to check one node to see if it is or was originally
1386 -- a reference to an entity for which Warnings are off. If so, Abandon
1387 -- is returned, otherwise OK_Orig is returned to continue the traversal
1388 -- of the original expression.
1390 function Traverse
is new Traverse_Func
(Check_For_Warnings
);
1391 -- Function used to traverse tree looking for warnings
1393 ------------------------
1394 -- Check_For_Warnings --
1395 ------------------------
1397 function Check_For_Warnings
(N
: Node_Id
) return Traverse_Result
is
1398 R
: constant Node_Id
:= Original_Node
(N
);
1401 if Nkind
(R
) in N_Has_Entity
1402 and then Present
(Entity
(R
))
1403 and then Warnings_Off
(Entity
(R
))
1409 end Check_For_Warnings
;
1411 -- Start of processing for Operand_Has_Warnings_Suppressed
1414 return Traverse
(N
) = Abandon
;
1416 -- If any exception occurs, then something has gone wrong, and this is
1417 -- only a minor aesthetic issue anyway, so just say we did not find what
1418 -- we are looking for, rather than blow up.
1423 end Operand_Has_Warnings_Suppressed
;
1425 ----------------------------------
1426 -- Output_Unreferenced_Messages --
1427 ----------------------------------
1429 procedure Output_Unreferenced_Messages
is
1433 for J
in Unreferenced_Entities
.First
..
1434 Unreferenced_Entities
.Last
1436 E
:= Unreferenced_Entities
.Table
(J
);
1438 if not Referenced
(E
) and then not Warnings_Off
(E
) then
1442 -- Case of variable that is assigned but not read. We
1443 -- suppress the message if the variable is volatile or
1444 -- has an address clause.
1446 if Referenced_As_LHS
(E
)
1447 and then No
(Address_Clause
(E
))
1448 and then not Is_Volatile
(E
)
1450 if Warn_On_Modified_Unread
then
1452 ("variable & is assigned but never read?", E
);
1455 -- Normal case of neither assigned nor read
1458 if Present
(Renamed_Object
(E
))
1459 and then Comes_From_Source
(Renamed_Object
(E
))
1462 ("renamed variable & is not referenced?", E
);
1465 ("variable & is not referenced?", E
);
1470 if Present
(Renamed_Object
(E
))
1471 and then Comes_From_Source
(Renamed_Object
(E
))
1473 Error_Msg_N
("renamed constant & is not referenced?", E
);
1475 Error_Msg_N
("constant & is not referenced?", E
);
1478 when E_In_Parameter |
1480 E_In_Out_Parameter
=>
1482 -- Do not emit message for formals of a renaming, because
1483 -- they are never referenced explicitly.
1485 if Nkind
(Original_Node
(Unit_Declaration_Node
(Scope
(E
))))
1486 /= N_Subprogram_Renaming_Declaration
1488 Error_Msg_N
("formal parameter & is not referenced?", E
);
1491 when E_Named_Integer |
1493 Error_Msg_N
("named number & is not referenced?", E
);
1495 when E_Enumeration_Literal
=>
1496 Error_Msg_N
("literal & is not referenced?", E
);
1499 Error_Msg_N
("function & is not referenced?", E
);
1502 Error_Msg_N
("procedure & is not referenced?", E
);
1504 when E_Generic_Procedure
=>
1506 ("generic procedure & is never instantiated?", E
);
1508 when E_Generic_Function
=>
1509 Error_Msg_N
("generic function & is never instantiated?", E
);
1512 Error_Msg_N
("type & is not referenced?", E
);
1515 Error_Msg_N
("& is not referenced?", E
);
1518 Set_Warnings_Off
(E
);
1521 end Output_Unreferenced_Messages
;
1523 -----------------------------
1524 -- Warn_On_Known_Condition --
1525 -----------------------------
1527 procedure Warn_On_Known_Condition
(C
: Node_Id
) is
1531 -- Argument replacement in an inlined body can make conditions
1532 -- static. Do not emit warnings in this case.
1534 if In_Inlined_Body
then
1538 if Constant_Condition_Warnings
1539 and then Nkind
(C
) = N_Identifier
1541 (Entity
(C
) = Standard_False
or else Entity
(C
) = Standard_True
)
1542 and then Comes_From_Source
(Original_Node
(C
))
1543 and then not In_Instance
1545 -- See if this is in a statement or a declaration
1549 -- If tree is not attached, do not issue warning (this is very
1550 -- peculiar, and probably arises from some other error condition)
1555 -- If we are in a declaration, then no warning, since in practice
1556 -- conditionals in declarations are used for intended tests which
1557 -- may be known at compile time, e.g. things like
1559 -- x : constant Integer := 2 + (Word'Size = 32);
1561 -- And a warning is annoying in such cases
1563 elsif Nkind
(P
) in N_Declaration
1565 Nkind
(P
) in N_Later_Decl_Item
1569 -- Don't warn in assert pragma, since presumably tests in such
1570 -- a context are very definitely intended, and might well be
1571 -- known at compile time. Note that we have to test the original
1572 -- node, since assert pragmas get rewritten at analysis time.
1574 elsif Nkind
(Original_Node
(P
)) = N_Pragma
1575 and then Chars
(Original_Node
(P
)) = Name_Assert
1580 exit when Is_Statement
(P
);
1584 -- Here we issue the warning unless some sub-operand has warnings
1585 -- set off, in which case we suppress the warning for the node.
1587 if not Operand_Has_Warnings_Suppressed
(C
) then
1588 if Entity
(C
) = Standard_True
then
1589 Error_Msg_N
("condition is always True?", C
);
1591 Error_Msg_N
("condition is always False?", C
);
1595 end Warn_On_Known_Condition
;