1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1999-2006, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
28 with Atree
; use Atree
;
29 with 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 approach
62 -- are defined, but are not in use yet. ???
64 -- An entry is made in the following table for each branch of conditional,
65 -- e.g. an if-then-elsif-else-endif structure creates three entries in this
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 exit
74 -- from a conditional structure, any entity that is in the list of all
75 -- branches is removed (and the entity flagged as defined by the
76 -- conditional as a whole). Thus after processing a conditional, Defs
77 -- contains a list of entities defined in this branch for the first
78 -- time, but not defined at all in some other branch of the same
79 -- conditional. A value of No_Elist is used to represent the initial
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 one
95 -- 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 entry
118 -- represents the start of a compilation unit. Non-zero entries in the
119 -- 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 N
147 -- and determines if any sub-operand is a reference to an entity for which
148 -- the Warnings_Off flag is set. True is returned if such an entity is
149 -- 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 only
162 -- be referenced in one of the subunits. We make an exception for
163 -- 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 the
167 -- 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 at
172 -- 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
)));
196 while Present
(D
) loop
198 -- No warnings if the proper body contains nested stubs
200 if Nkind
(D
) in N_Body_Stub
then
210 -- Missing stubs elsewhere
214 end Missing_Subunits
;
216 ----------------------------
217 -- Output_Reference_Error --
218 ----------------------------
220 procedure Output_Reference_Error
(M
: String) is
222 -- Other than accept case, post error on defining identifier
227 -- Accept case, find body formal to post the message
238 if Present
(Parameter_Specifications
(Anod
)) then
239 Parm
:= First
(Parameter_Specifications
(Anod
));
240 while Present
(Parm
) loop
241 Defid
:= Defining_Identifier
(Parm
);
243 if Chars
(E1
) = Chars
(Defid
) then
252 Error_Msg_NE
(M
, Enod
, E1
);
255 end Output_Reference_Error
;
257 ----------------------------
258 -- Publicly_Referenceable --
259 ----------------------------
261 function Publicly_Referenceable
(Ent
: Entity_Id
) return Boolean is
266 -- Examine parents to look for a library level package spec. But if
267 -- we find a body or block or other similar construct along the way,
268 -- we cannot be referenced.
275 -- If we get to top of tree, then publicly referenceable
280 -- If we reach a generic package declaration, then always
281 -- consider this referenceable, since any instantiation will
282 -- have access to the entities in the generic package. Note
283 -- that the package itself may not be instantiated, but then
284 -- we will get a warning for the package entity.
286 -- Note that generic formal parameters are themselves not
287 -- publicly referenceable in an instance, and warnings on
290 when N_Generic_Package_Declaration
=>
292 not Is_List_Member
(Prev
)
293 or else List_Containing
(Prev
)
294 /= Generic_Formal_Declarations
(P
);
296 -- Similarly, the generic formals of a generic subprogram
297 -- are not accessible.
299 when N_Generic_Subprogram_Declaration
=>
300 if Is_List_Member
(Prev
)
301 and then List_Containing
(Prev
) =
302 Generic_Formal_Declarations
(P
)
309 -- If we reach a subprogram body, entity is not referenceable
310 -- unless it is the defining entity of the body. This will
311 -- happen, e.g. when a function is an attribute renaming that
312 -- is rewritten as a body.
314 when N_Subprogram_Body
=>
315 if Ent
/= Defining_Entity
(P
) then
321 -- If we reach any other body, definitely not referenceable
323 when N_Package_Body |
331 -- For all other cases, keep looking up tree
338 end Publicly_Referenceable
;
340 -- Start of processing for Check_References
343 -- No messages if warnings are suppressed, or if we have detected any
344 -- real errors so far (this last check avoids junk messages resulting
345 -- from errors, e.g. a subunit that is not loaded).
347 if Warning_Mode
= Suppress
348 or else Serious_Errors_Detected
/= 0
353 -- We also skip the messages if any subunits were not loaded (see
354 -- comment in Sem_Ch10 to understand how this is set, and why it is
355 -- necessary to suppress the warnings in this case).
357 if Missing_Subunits
then
361 -- Otherwise loop through entities, looking for suspicious stuff
363 E1
:= First_Entity
(E
);
364 while Present
(E1
) loop
366 -- We only look at source entities with warning flag on
368 if Comes_From_Source
(E1
) and then not Warnings_Off
(E1
) then
370 -- We are interested in variables and out parameters, but we
371 -- exclude protected types, too complicated to worry about.
373 if Ekind
(E1
) = E_Variable
375 (Ekind
(E1
) = E_Out_Parameter
376 and then not Is_Protected_Type
(Current_Scope
))
378 -- Post warning if this object not assigned. Note that we do
379 -- not consider the implicit initialization of an access type
380 -- to be the assignment of a value for this purpose.
382 if Ekind
(E1
) = E_Out_Parameter
383 and then Present
(Spec_Entity
(E1
))
385 UR
:= Unset_Reference
(Spec_Entity
(E1
));
387 UR
:= Unset_Reference
(E1
);
390 -- If the entity is an out parameter of the current subprogram
391 -- body, check the warning status of the parameter in the spec.
393 if Ekind
(E1
) = E_Out_Parameter
394 and then Present
(Spec_Entity
(E1
))
395 and then Warnings_Off
(Spec_Entity
(E1
))
400 and then Is_Access_Type
(Etype
(E1
))
403 -- For access types, the only time we made a UR entry was
404 -- for a dereference, and so we post the appropriate warning
405 -- here (note that the dereference may not be explicit in
406 -- the source, for example in the case of a dispatching call
407 -- with an anonymous access controlling formal, or of an
408 -- assignment of a pointer involving discriminant check on
409 -- the designated object).
411 Error_Msg_NE
("& may be null?", UR
, E1
);
414 elsif Never_Set_In_Source
(E1
)
415 and then not Generic_Package_Spec_Entity
(E1
)
417 if Warn_On_No_Value_Assigned
then
419 -- Do not output complaint about never being assigned a
420 -- value if a pragma Unreferenced applies to the variable
421 -- or if it is a parameter, to the corresponding spec.
423 if Has_Pragma_Unreferenced
(E1
)
424 or else (Is_Formal
(E1
)
425 and then Present
(Spec_Entity
(E1
))
427 Has_Pragma_Unreferenced
(Spec_Entity
(E1
)))
431 -- Pragma Unreferenced not set, so output message
434 Output_Reference_Error
435 ("& is never assigned a value?");
437 -- Deal with special case where this variable is
438 -- hidden by a loop variable
440 if Ekind
(E1
) = E_Variable
441 and then Present
(Hiding_Loop_Variable
(E1
))
443 Error_Msg_Sloc
:= Sloc
(E1
);
445 ("declaration hides &#?",
446 Hiding_Loop_Variable
(E1
));
448 ("for loop implicitly declares loop variable?",
449 Hiding_Loop_Variable
(E1
));
455 -- Case of variable that could be a constant. Note that we
456 -- never signal such messages for generic package entities,
457 -- since a given instance could have modifications outside
460 elsif Warn_On_Constant
461 and then Ekind
(E1
) = E_Variable
462 and then Is_True_Constant
(E1
)
463 and then not Generic_Package_Spec_Entity
(E1
)
465 -- A special case, if this variable is volatile and not
466 -- imported, it is not helpful to tell the programmer
467 -- to mark the variable as constant, since this would be
468 -- illegal by virtue of RM C.6(13).
470 if (Is_Volatile
(E1
) or else Has_Volatile_Components
(E1
))
471 and then not Is_Imported
(E1
)
474 ("& is not modified, volatile has no effect?", E1
);
477 ("& is not modified, could be declared constant?", E1
);
481 -- Check for unset reference, note that we exclude access
482 -- types from this check, since access types do always have
483 -- a null value, and that seems legitimate in this case.
485 if Warn_On_No_Value_Assigned
and then Present
(UR
) then
487 -- For other than access type, go back to original node
488 -- to deal with case where original unset reference
489 -- has been rewritten during expansion.
491 UR
:= Original_Node
(UR
);
493 -- In some cases, the original node may be a type
494 -- conversion or qualification, and in this case
495 -- we want the object entity inside.
497 while Nkind
(UR
) = N_Type_Conversion
498 or else Nkind
(UR
) = N_Qualified_Expression
500 UR
:= Expression
(UR
);
503 -- Here we issue the warning, all checks completed If the
504 -- unset reference is prefix of a selected component that
505 -- comes from source, mention the component as well. If the
506 -- selected component comes from expansion, all we know is
507 -- that the entity is not fully initialized at the point of
508 -- the reference. Locate an unintialized component to get a
509 -- better error message.
511 if Nkind
(Parent
(UR
)) = N_Selected_Component
then
512 Error_Msg_Node_2
:= Selector_Name
(Parent
(UR
));
514 if not Comes_From_Source
(Parent
(UR
)) then
519 Comp
:= First_Entity
(Etype
(E1
));
520 while Present
(Comp
) loop
521 if Ekind
(Comp
) = E_Component
522 and then Nkind
(Parent
(Comp
)) =
523 N_Component_Declaration
524 and then No
(Expression
(Parent
(Comp
)))
526 Error_Msg_Node_2
:= Comp
;
536 ("`&.&` may be referenced before it has a value?",
540 ("& may be referenced before it has a value?",
548 -- Then check for unreferenced entities. Note that we are only
549 -- interested in entities which do not have the Referenced flag
550 -- set. The Referenced_As_LHS flag is interesting only if the
551 -- Referenced flag is not set.
553 if not Referenced
(E1
)
555 -- Check that warnings on unreferenced entities are enabled
557 and then ((Check_Unreferenced
and then not Is_Formal
(E1
))
559 (Check_Unreferenced_Formals
and then Is_Formal
(E1
))
561 (Warn_On_Modified_Unread
562 and then Referenced_As_LHS
(E1
)))
564 -- Labels, and enumeration literals, and exceptions. The
565 -- warnings are also placed on local packages that cannot be
566 -- referenced from elsewhere, including those declared within a
569 and then (Is_Object
(E1
)
575 Ekind
(E1
) = E_Exception
577 Ekind
(E1
) = E_Named_Integer
579 Ekind
(E1
) = E_Named_Real
583 (Ekind
(E1
) = E_Package
585 (Ekind
(E
) = E_Function
586 or else Ekind
(E
) = E_Package_Body
587 or else Ekind
(E
) = E_Procedure
588 or else Ekind
(E
) = E_Subprogram_Body
589 or else Ekind
(E
) = E_Block
)))
591 -- Exclude instantiations, since there is no reason why every
592 -- entity in an instantiation should be referenced.
594 and then Instantiation_Location
(Sloc
(E1
)) = No_Location
596 -- Exclude formal parameters from bodies if the corresponding
597 -- spec entity has been referenced in the case where there is
600 and then not (Is_Formal
(E1
)
602 Ekind
(Scope
(E1
)) = E_Subprogram_Body
604 Present
(Spec_Entity
(E1
))
606 Referenced
(Spec_Entity
(E1
)))
608 -- Consider private type referenced if full view is referenced
609 -- If there is not full view, this is a generic type on which
610 -- warnings are also useful.
613 not (Is_Private_Type
(E1
)
615 Present
(Full_View
(E1
))
616 and then Referenced
(Full_View
(E1
)))
618 -- Don't worry about full view, only about private type
620 and then not Has_Private_Declaration
(E1
)
622 -- Eliminate dispatching operations from consideration, we
623 -- cannot tell if these are referenced or not in any easy
624 -- manner (note this also catches Adjust/Finalize/Initialize)
626 and then not Is_Dispatching_Operation
(E1
)
628 -- Check entity that can be publicly referenced (we do not give
629 -- messages for such entities, since there could be other
630 -- units, not involved in this compilation, that contain
631 -- relevant references.
633 and then not Publicly_Referenceable
(E1
)
635 -- Class wide types are marked as source entities, but they are
636 -- not really source entities, and are always created, so we do
637 -- not care if they are not referenced.
639 and then Ekind
(E1
) /= E_Class_Wide_Type
641 -- Objects other than parameters of task types are allowed to
642 -- be non-referenced, since they start up tasks!
644 and then ((Ekind
(E1
) /= E_Variable
645 and then Ekind
(E1
) /= E_Constant
646 and then Ekind
(E1
) /= E_Component
)
647 or else not Is_Task_Type
(Etype
(E1
)))
649 -- For subunits, only place warnings on the main unit itself,
650 -- since parent units are not completely compiled
652 and then (Nkind
(Unit
(Cunit
(Main_Unit
))) /= N_Subunit
654 Get_Source_Unit
(E1
) = Main_Unit
)
656 -- Suppress warnings in internal units if not in -gnatg mode
657 -- (these would be junk warnings for an applications program,
658 -- since they refer to problems in internal units)
662 Is_Internal_File_Name
663 (Unit_File_Name
(Get_Source_Unit
(E1
)))
665 -- We do not immediately flag the error. This is because we
666 -- have not expanded generic bodies yet, and they may have
667 -- the missing reference. So instead we park the entity on a
668 -- list, for later processing. However, for the accept case,
669 -- post the error right here, since we have the information
672 if Present
(Anod
) then
673 Output_Reference_Error
("& is not referenced?");
676 Unreferenced_Entities
.Increment_Last
;
677 Unreferenced_Entities
.Table
678 (Unreferenced_Entities
.Last
) := E1
;
682 -- Generic units are referenced in the generic body, but if they
683 -- are not public and never instantiated we want to force a
684 -- warning on them. We treat them as redundant constructs to
687 elsif Is_Generic_Subprogram
(E1
)
688 and then not Is_Instantiated
(E1
)
689 and then not Publicly_Referenceable
(E1
)
690 and then Instantiation_Depth
(Sloc
(E1
)) = 0
691 and then Warn_On_Redundant_Constructs
693 Unreferenced_Entities
.Increment_Last
;
694 Unreferenced_Entities
.Table
(Unreferenced_Entities
.Last
) := E1
;
696 -- Force warning on entity
698 Set_Referenced
(E1
, False);
702 -- Recurse into nested package or block. Do not recurse into a
703 -- formal package, because the correponding body is not analyzed.
706 if ((Ekind
(E1
) = E_Package
or else Ekind
(E1
) = E_Generic_Package
)
707 and then Nkind
(Parent
(E1
)) = N_Package_Specification
709 Nkind
(Original_Node
(Unit_Declaration_Node
(E1
)))
710 /= N_Formal_Package_Declaration
)
712 or else Ekind
(E1
) = E_Block
714 Check_References
(E1
);
719 end Check_References
;
721 ---------------------------
722 -- Check_Unset_Reference --
723 ---------------------------
725 procedure Check_Unset_Reference
(N
: Node_Id
) is
727 -- Nothing to do if warnings suppressed
729 if Warning_Mode
= Suppress
then
733 -- Ignore reference to non-scalar if not from source. Almost always such
734 -- references are bogus (e.g. calls to init procs to set default
735 -- discriminant values).
737 if not Comes_From_Source
(N
)
738 and then not Is_Scalar_Type
(Etype
(N
))
743 -- Otherwise see what kind of node we have. If the entity already
744 -- has an unset reference, it is not necessarily the earliest in
745 -- the text, because resolution of the prefix of selected components
746 -- is completed before the resolution of the selected component itself.
747 -- as a result, given (R /= null and then R.X > 0), the occurrences
748 -- of R are examined in right-to-left order. If there is already an
749 -- unset reference, we check whether N is earlier before proceeding.
752 when N_Identifier | N_Expanded_Name
=>
754 E
: constant Entity_Id
:= Entity
(N
);
757 if (Ekind
(E
) = E_Variable
758 or else Ekind
(E
) = E_Out_Parameter
)
759 and then Never_Set_In_Source
(E
)
760 and then (No
(Unset_Reference
(E
))
761 or else Earlier_In_Extended_Unit
762 (Sloc
(N
), Sloc
(Unset_Reference
(E
))))
763 and then not Warnings_Off
(E
)
765 -- We may have an unset reference. The first test is whether
766 -- we are accessing a discriminant of a record or a
767 -- component with default initialization. Both of these
768 -- cases can be ignored, since the actual object that is
769 -- referenced is definitely initialized. Note that this
770 -- covers the case of reading discriminants of an out
771 -- parameter, which is OK even in Ada 83.
773 -- Note that we are only interested in a direct reference to
774 -- a record component here. If the reference is via an
775 -- access type, then the access object is being referenced,
776 -- not the record, and still deserves an unset reference.
778 if Nkind
(Parent
(N
)) = N_Selected_Component
779 and not Is_Access_Type
(Etype
(N
))
782 ES
: constant Entity_Id
:=
783 Entity
(Selector_Name
(Parent
(N
)));
786 if Ekind
(ES
) = E_Discriminant
787 or else Present
(Expression
(Declaration_Node
(ES
)))
794 -- Here we have a potential unset reference. But before we
795 -- get worried about it, we have to make sure that the
796 -- entity declaration is in the same procedure as the
797 -- reference, since if they are in separate procedures, then
798 -- we have no idea about sequential execution.
800 -- The tests in the loop below catch all such cases, but do
801 -- allow the reference to appear in a loop, block, or
802 -- package spec that is nested within the declaring scope.
803 -- As always, it is possible to construct cases where the
804 -- warning is wrong, that is why it is a warning!
808 SE
: constant Entity_Id
:= Scope
(E
);
813 if SR
= Standard_Standard
814 or else Is_Subprogram
(SR
)
815 or else Is_Concurrent_Body
(SR
)
816 or else Is_Concurrent_Type
(SR
)
824 -- Case of reference has an access type. This is special
825 -- case since access types are always set to null so
826 -- cannot be truly uninitialized, but we still want to
827 -- warn about cases of obvious null dereference.
829 if Is_Access_Type
(Etype
(N
)) then
830 Access_Type_Case
: declare
835 return Traverse_Result
;
836 -- Process function for instantation of Traverse
837 -- below. Checks if N contains reference to other
838 -- than a dereference.
840 function Ref_In
(Nod
: Node_Id
) return Boolean;
841 -- Determines whether Nod contains a reference to
842 -- the entity E that is not a dereference.
850 return Traverse_Result
853 if Is_Entity_Name
(N
)
854 and then Entity
(N
) = E
855 and then not Is_Dereferenced
(N
)
867 function Ref_In
(Nod
: Node_Id
) return Boolean is
868 function Traverse
is new Traverse_Func
(Process
);
870 return Traverse
(Nod
) = Abandon
;
873 -- Start of processing for Access_Type_Case
876 -- Don't bother if we are inside an instance,
877 -- since the compilation of the generic template
878 -- is where the warning should be issued.
884 -- Don't bother if this is not the main unit.
885 -- If we try to give this warning for with'ed
886 -- units, we get some false positives, since
887 -- we do not record references in other units.
889 if not In_Extended_Main_Source_Unit
(E
)
891 not In_Extended_Main_Source_Unit
(N
)
896 -- We are only interested in deferences
898 if not Is_Dereferenced
(N
) then
902 -- One more check, don't bother with references
903 -- that are inside conditional statements or while
904 -- loops if the condition references the entity in
905 -- question. This avoids most false positives.
912 if (Nkind
(P
) = N_If_Statement
914 Nkind
(P
) = N_Elsif_Part
)
915 and then Ref_In
(Condition
(P
))
919 elsif Nkind
(P
) = N_Loop_Statement
920 and then Present
(Iteration_Scheme
(P
))
922 Ref_In
(Condition
(Iteration_Scheme
(P
)))
927 end Access_Type_Case
;
930 -- Here we definitely have a case for giving a warning
931 -- for a reference to an unset value. But we don't give
932 -- the warning now. Instead we set the Unset_Reference
933 -- field of the identifier involved. The reason for this
934 -- is that if we find the variable is never ever assigned
935 -- a value then that warning is more important and there
936 -- is no point in giving the reference warning.
938 -- If this is an identifier, set the field directly
940 if Nkind
(N
) = N_Identifier
then
941 Set_Unset_Reference
(E
, N
);
943 -- Otherwise it is an expanded name, so set the field
944 -- of the actual identifier for the reference.
947 Set_Unset_Reference
(E
, Selector_Name
(N
));
953 when N_Indexed_Component | N_Slice
=>
954 Check_Unset_Reference
(Prefix
(N
));
956 when N_Selected_Component
=>
958 if Present
(Entity
(Selector_Name
(N
)))
959 and then Ekind
(Entity
(Selector_Name
(N
))) = E_Discriminant
961 -- A discriminant is always initialized
966 Check_Unset_Reference
(Prefix
(N
));
969 when N_Type_Conversion | N_Qualified_Expression
=>
970 Check_Unset_Reference
(Expression
(N
));
976 end Check_Unset_Reference
;
978 ------------------------
979 -- Check_Unused_Withs --
980 ------------------------
982 procedure Check_Unused_Withs
(Spec_Unit
: Unit_Number_Type
:= No_Unit
) is
988 Munite
: constant Entity_Id
:= Cunit_Entity
(Main_Unit
);
989 -- This is needed for checking the special renaming case
991 procedure Check_One_Unit
(Unit
: Unit_Number_Type
);
992 -- Subsidiary procedure, performs checks for specified unit
998 procedure Check_One_Unit
(Unit
: Unit_Number_Type
) is
999 Is_Visible_Renaming
: Boolean := False;
1002 procedure Check_Inner_Package
(Pack
: Entity_Id
);
1003 -- Pack is a package local to a unit in a with_clause. Both the
1004 -- unit and Pack are referenced. If none of the entities in Pack
1005 -- are referenced, then the only occurrence of Pack is in a use
1006 -- clause or a pragma, and a warning is worthwhile as well.
1008 function Check_System_Aux
return Boolean;
1009 -- Before giving a warning on a with_clause for System, check
1010 -- whether a system extension is present.
1012 function Find_Package_Renaming
1014 L
: Entity_Id
) return Entity_Id
;
1015 -- The only reference to a context unit may be in a renaming
1016 -- declaration. If this renaming declares a visible entity, do
1017 -- not warn that the context clause could be moved to the body,
1018 -- because the renaming may be intented to re-export the unit.
1020 -------------------------
1021 -- Check_Inner_Package --
1022 -------------------------
1024 procedure Check_Inner_Package
(Pack
: Entity_Id
) is
1026 Un
: constant Node_Id
:= Sinfo
.Unit
(Cnode
);
1028 function Check_Use_Clause
(N
: Node_Id
) return Traverse_Result
;
1029 -- If N is a use_clause for Pack, emit warning
1031 procedure Check_Use_Clauses
is new
1032 Traverse_Proc
(Check_Use_Clause
);
1034 ----------------------
1035 -- Check_Use_Clause --
1036 ----------------------
1038 function Check_Use_Clause
(N
: Node_Id
) return Traverse_Result
is
1042 if Nkind
(N
) = N_Use_Package_Clause
then
1043 Nam
:= First
(Names
(N
));
1044 while Present
(Nam
) loop
1045 if Entity
(Nam
) = Pack
then
1046 Error_Msg_Qual_Level
:= 1;
1048 ("no entities of package& are referenced?",
1050 Error_Msg_Qual_Level
:= 0;
1058 end Check_Use_Clause
;
1060 -- Start of processing for Check_Inner_Package
1063 E
:= First_Entity
(Pack
);
1064 while Present
(E
) loop
1065 if Referenced
(E
) then
1072 -- No entities of the package are referenced. Check whether the
1073 -- reference to the package itself is a use clause, and if so
1074 -- place a warning on it.
1076 Check_Use_Clauses
(Un
);
1077 end Check_Inner_Package
;
1079 ----------------------
1080 -- Check_System_Aux --
1081 ----------------------
1083 function Check_System_Aux
return Boolean is
1087 if Chars
(Lunit
) = Name_System
1088 and then Scope
(Lunit
) = Standard_Standard
1089 and then Present_System_Aux
1091 Ent
:= First_Entity
(System_Aux_Id
);
1092 while Present
(Ent
) loop
1093 if Referenced
(Ent
) then
1102 end Check_System_Aux
;
1104 ---------------------------
1105 -- Find_Package_Renaming --
1106 ---------------------------
1108 function Find_Package_Renaming
1110 L
: Entity_Id
) return Entity_Id
1116 Is_Visible_Renaming
:= False;
1118 E1
:= First_Entity
(P
);
1119 while Present
(E1
) loop
1120 if Ekind
(E1
) = E_Package
1121 and then Renamed_Object
(E1
) = L
1123 Is_Visible_Renaming
:= not Is_Hidden
(E1
);
1126 elsif Ekind
(E1
) = E_Package
1127 and then No
(Renamed_Object
(E1
))
1128 and then not Is_Generic_Instance
(E1
)
1130 R
:= Find_Package_Renaming
(E1
, L
);
1133 Is_Visible_Renaming
:= not Is_Hidden
(R
);
1142 end Find_Package_Renaming
;
1144 -- Start of processing for Check_One_Unit
1147 Cnode
:= Cunit
(Unit
);
1149 -- Only do check in units that are part of the extended main unit.
1150 -- This is actually a necessary restriction, because in the case of
1151 -- subprogram acting as its own specification, there can be with's in
1152 -- subunits that we will not see.
1154 if not In_Extended_Main_Source_Unit
(Cnode
) then
1157 -- In configurable run time mode, we remove the bodies of non-inlined
1158 -- subprograms, which may lead to spurious warnings, which are
1159 -- clearly undesirable.
1161 elsif Configurable_Run_Time_Mode
1162 and then Is_Predefined_File_Name
(Unit_File_Name
(Unit
))
1167 -- Loop through context items in this unit
1169 Item
:= First
(Context_Items
(Cnode
));
1170 while Present
(Item
) loop
1171 if Nkind
(Item
) = N_With_Clause
1172 and then not Implicit_With
(Item
)
1173 and then In_Extended_Main_Source_Unit
(Item
)
1175 Lunit
:= Entity
(Name
(Item
));
1177 -- Check if this unit is referenced
1179 if not Referenced
(Lunit
) then
1181 -- Suppress warnings in internal units if not in -gnatg mode
1182 -- (these would be junk warnings for an application program,
1183 -- since they refer to problems in internal units)
1186 or else not Is_Internal_File_Name
(Unit_File_Name
(Unit
))
1188 -- Here we definitely have a non-referenced unit. If it
1189 -- is the special call for a spec unit, then just set the
1190 -- flag to be read later.
1192 if Unit
= Spec_Unit
then
1193 Set_Unreferenced_In_Spec
(Item
);
1195 -- Otherwise simple unreferenced message
1199 ("unit& is not referenced?", Name
(Item
));
1203 -- If main unit is a renaming of this unit, then we consider
1204 -- the with to be OK (obviously it is needed in this case!)
1206 elsif Present
(Renamed_Entity
(Munite
))
1207 and then Renamed_Entity
(Munite
) = Lunit
1211 -- If this unit is referenced, and it is a package, we do
1212 -- another test, to see if any of the entities in the package
1213 -- are referenced. If none of the entities are referenced, we
1214 -- still post a warning. This occurs if the only use of the
1215 -- package is in a use clause, or in a package renaming
1218 elsif Ekind
(Lunit
) = E_Package
then
1220 -- If Is_Instantiated is set, it means that the package is
1221 -- implicitly instantiated (this is the case of parent
1222 -- instance or an actual for a generic package formal), and
1223 -- this counts as a reference.
1225 if Is_Instantiated
(Lunit
) then
1228 -- If no entities in package, and there is a pragma
1229 -- Elaborate_Body present, then assume that this with is
1230 -- done for purposes of this elaboration.
1232 elsif No
(First_Entity
(Lunit
))
1233 and then Has_Pragma_Elaborate_Body
(Lunit
)
1237 -- Otherwise see if any entities have been referenced
1240 if Limited_Present
(Item
) then
1241 Ent
:= First_Entity
(Limited_View
(Lunit
));
1243 Ent
:= First_Entity
(Lunit
);
1247 -- No more entities, and we did not find one that was
1248 -- referenced. Means we have a definite case of a with
1249 -- none of whose entities was referenced.
1253 -- If in spec, just set the flag
1255 if Unit
= Spec_Unit
then
1256 Set_No_Entities_Ref_In_Spec
(Item
);
1258 elsif Check_System_Aux
then
1261 -- Else give the warning
1265 ("no entities of & are referenced?",
1268 -- Look for renamings of this package, and flag
1269 -- them as well. If the original package has
1270 -- warnings off, we suppress the warning on the
1271 -- renaming as well.
1273 Pack
:= Find_Package_Renaming
(Munite
, Lunit
);
1276 and then not Warnings_Off
(Lunit
)
1279 ("no entities of & are referenced?",
1280 Unit_Declaration_Node
(Pack
),
1287 -- Case of next entity is referenced
1289 elsif Referenced
(Ent
)
1290 or else Referenced_As_LHS
(Ent
)
1292 -- This means that the with is indeed fine, in that
1293 -- it is definitely needed somewhere, and we can
1294 -- quite worrying about this one.
1296 -- Except for one little detail, if either of the
1297 -- flags was set during spec processing, this is
1298 -- where we complain that the with could be moved
1299 -- from the spec. If the spec contains a visible
1300 -- renaming of the package, inhibit warning to move
1301 -- with_clause to body.
1303 if Ekind
(Munite
) = E_Package_Body
then
1305 Find_Package_Renaming
1306 (Spec_Entity
(Munite
), Lunit
);
1309 if Unreferenced_In_Spec
(Item
) then
1311 ("unit& is not referenced in spec?",
1314 elsif No_Entities_Ref_In_Spec
(Item
) then
1316 ("no entities of & are referenced in spec?",
1320 if Ekind
(Ent
) = E_Package
then
1321 Check_Inner_Package
(Ent
);
1327 if not Is_Visible_Renaming
then
1329 ("\with clause might be moved to body?",
1335 -- Move to next entity to continue search
1343 -- For a generic package, the only interesting kind of
1344 -- reference is an instantiation, since entities cannot be
1345 -- referenced directly.
1347 elsif Is_Generic_Unit
(Lunit
) then
1349 -- Unit was never instantiated, set flag for case of spec
1350 -- call, or give warning for normal call.
1352 if not Is_Instantiated
(Lunit
) then
1353 if Unit
= Spec_Unit
then
1354 Set_Unreferenced_In_Spec
(Item
);
1357 ("unit& is never instantiated?", Name
(Item
));
1360 -- If unit was indeed instantiated, make sure that flag is
1361 -- not set showing it was uninstantiated in the spec, and if
1362 -- so, give warning.
1364 elsif Unreferenced_In_Spec
(Item
) then
1366 ("unit& is not instantiated in spec?", Name
(Item
));
1368 ("\with clause can be moved to body?", Name
(Item
));
1378 -- Start of processing for Check_Unused_Withs
1381 if not Opt
.Check_Withs
1382 or else Operating_Mode
= Check_Syntax
1387 -- Flag any unused with clauses, but skip this step if we are compiling
1388 -- a subunit on its own, since we do not have enough information to
1389 -- determine whether with's are used. We will get the relevant warnings
1390 -- when we compile the parent. This is the normal style of GNAT
1391 -- compilation in any case.
1393 if Nkind
(Unit
(Cunit
(Main_Unit
))) = N_Subunit
then
1397 -- Process specified units
1399 if Spec_Unit
= No_Unit
then
1401 -- For main call, check all units
1403 for Unit
in Main_Unit
.. Last_Unit
loop
1404 Check_One_Unit
(Unit
);
1408 -- For call for spec, check only the spec
1410 Check_One_Unit
(Spec_Unit
);
1412 end Check_Unused_Withs
;
1414 ---------------------------------
1415 -- Generic_Package_Spec_Entity --
1416 ---------------------------------
1418 function Generic_Package_Spec_Entity
(E
: Entity_Id
) return Boolean is
1422 if Is_Package_Body_Entity
(E
) then
1428 if S
= Standard_Standard
then
1431 elsif Ekind
(S
) = E_Generic_Package
then
1434 elsif Ekind
(S
) = E_Package
then
1442 end Generic_Package_Spec_Entity
;
1444 -------------------------------------
1445 -- Operand_Has_Warnings_Suppressed --
1446 -------------------------------------
1448 function Operand_Has_Warnings_Suppressed
(N
: Node_Id
) return Boolean is
1450 function Check_For_Warnings
(N
: Node_Id
) return Traverse_Result
;
1451 -- Function used to check one node to see if it is or was originally
1452 -- a reference to an entity for which Warnings are off. If so, Abandon
1453 -- is returned, otherwise OK_Orig is returned to continue the traversal
1454 -- of the original expression.
1456 function Traverse
is new Traverse_Func
(Check_For_Warnings
);
1457 -- Function used to traverse tree looking for warnings
1459 ------------------------
1460 -- Check_For_Warnings --
1461 ------------------------
1463 function Check_For_Warnings
(N
: Node_Id
) return Traverse_Result
is
1464 R
: constant Node_Id
:= Original_Node
(N
);
1467 if Nkind
(R
) in N_Has_Entity
1468 and then Present
(Entity
(R
))
1469 and then Warnings_Off
(Entity
(R
))
1475 end Check_For_Warnings
;
1477 -- Start of processing for Operand_Has_Warnings_Suppressed
1480 return Traverse
(N
) = Abandon
;
1482 -- If any exception occurs, then something has gone wrong, and this is
1483 -- only a minor aesthetic issue anyway, so just say we did not find what
1484 -- we are looking for, rather than blow up.
1489 end Operand_Has_Warnings_Suppressed
;
1491 ----------------------------------
1492 -- Output_Unreferenced_Messages --
1493 ----------------------------------
1495 procedure Output_Unreferenced_Messages
is
1499 for J
in Unreferenced_Entities
.First
..
1500 Unreferenced_Entities
.Last
1502 E
:= Unreferenced_Entities
.Table
(J
);
1504 if not Referenced
(E
) and then not Warnings_Off
(E
) then
1508 -- Case of variable that is assigned but not read. We
1509 -- suppress the message if the variable is volatile, has an
1510 -- address clause, or is imported.
1512 if Referenced_As_LHS
(E
)
1513 and then No
(Address_Clause
(E
))
1514 and then not Is_Volatile
(E
)
1516 if Warn_On_Modified_Unread
1517 and then not Is_Imported
(E
)
1519 -- Suppress the message for aliased or renamed
1520 -- variables, since there may be other entities read
1521 -- the same memory location.
1523 and then not Is_Aliased
(E
)
1524 and then No
(Renamed_Object
(E
))
1528 ("variable & is assigned but never read?", E
);
1531 -- Normal case of neither assigned nor read
1534 if Present
(Renamed_Object
(E
))
1535 and then Comes_From_Source
(Renamed_Object
(E
))
1538 ("renamed variable & is not referenced?", E
);
1541 ("variable & is not referenced?", E
);
1546 if Present
(Renamed_Object
(E
))
1547 and then Comes_From_Source
(Renamed_Object
(E
))
1549 Error_Msg_N
("renamed constant & is not referenced?", E
);
1551 Error_Msg_N
("constant & is not referenced?", E
);
1554 when E_In_Parameter |
1556 E_In_Out_Parameter
=>
1558 -- Do not emit message for formals of a renaming, because
1559 -- they are never referenced explicitly.
1561 if Nkind
(Original_Node
(Unit_Declaration_Node
(Scope
(E
))))
1562 /= N_Subprogram_Renaming_Declaration
1564 Error_Msg_N
("formal parameter & is not referenced?", E
);
1567 when E_Named_Integer |
1569 Error_Msg_N
("named number & is not referenced?", E
);
1571 when E_Enumeration_Literal
=>
1572 Error_Msg_N
("literal & is not referenced?", E
);
1575 Error_Msg_N
("function & is not referenced?", E
);
1578 Error_Msg_N
("procedure & is not referenced?", E
);
1580 when E_Generic_Procedure
=>
1582 ("generic procedure & is never instantiated?", E
);
1584 when E_Generic_Function
=>
1585 Error_Msg_N
("generic function & is never instantiated?", E
);
1588 Error_Msg_N
("type & is not referenced?", E
);
1591 Error_Msg_N
("& is not referenced?", E
);
1594 Set_Warnings_Off
(E
);
1597 end Output_Unreferenced_Messages
;
1599 ------------------------
1600 -- Set_Warning_Switch --
1601 ------------------------
1603 function Set_Warning_Switch
(C
: Character) return Boolean is
1607 Check_Unreferenced
:= True;
1608 Check_Unreferenced_Formals
:= True;
1609 Check_Withs
:= True;
1610 Constant_Condition_Warnings
:= True;
1611 Implementation_Unit_Warnings
:= True;
1612 Ineffective_Inline_Warnings
:= True;
1613 Warn_On_Ada_2005_Compatibility
:= True;
1614 Warn_On_Bad_Fixed_Value
:= True;
1615 Warn_On_Constant
:= True;
1616 Warn_On_Export_Import
:= True;
1617 Warn_On_Modified_Unread
:= True;
1618 Warn_On_No_Value_Assigned
:= True;
1619 Warn_On_Obsolescent_Feature
:= True;
1620 Warn_On_Redundant_Constructs
:= True;
1621 Warn_On_Unchecked_Conversion
:= True;
1622 Warn_On_Unrecognized_Pragma
:= True;
1625 Check_Unreferenced
:= False;
1626 Check_Unreferenced_Formals
:= False;
1627 Check_Withs
:= False;
1628 Constant_Condition_Warnings
:= False;
1629 Elab_Warnings
:= False;
1630 Implementation_Unit_Warnings
:= False;
1631 Ineffective_Inline_Warnings
:= False;
1632 Warn_On_Ada_2005_Compatibility
:= False;
1633 Warn_On_Bad_Fixed_Value
:= False;
1634 Warn_On_Constant
:= False;
1635 Warn_On_Dereference
:= False;
1636 Warn_On_Export_Import
:= False;
1637 Warn_On_Hiding
:= False;
1638 Warn_On_Modified_Unread
:= False;
1639 Warn_On_No_Value_Assigned
:= False;
1640 Warn_On_Obsolescent_Feature
:= False;
1641 Warn_On_Redundant_Constructs
:= False;
1642 Warn_On_Unchecked_Conversion
:= False;
1643 Warn_On_Unrecognized_Pragma
:= False;
1646 Warn_On_Bad_Fixed_Value
:= True;
1649 Warn_On_Bad_Fixed_Value
:= False;
1652 Constant_Condition_Warnings
:= True;
1655 Constant_Condition_Warnings
:= False;
1658 Warn_On_Dereference
:= True;
1661 Warn_On_Dereference
:= False;
1664 Warning_Mode
:= Treat_As_Error
;
1667 Check_Unreferenced_Formals
:= True;
1670 Check_Unreferenced_Formals
:= False;
1673 Warn_On_Unrecognized_Pragma
:= True;
1676 Warn_On_Unrecognized_Pragma
:= False;
1679 Warn_On_Hiding
:= True;
1682 Warn_On_Hiding
:= False;
1685 Implementation_Unit_Warnings
:= True;
1688 Implementation_Unit_Warnings
:= False;
1691 Warn_On_Obsolescent_Feature
:= True;
1694 Warn_On_Obsolescent_Feature
:= False;
1697 Warn_On_Constant
:= True;
1700 Warn_On_Constant
:= False;
1703 Elab_Warnings
:= True;
1706 Elab_Warnings
:= False;
1709 Warn_On_Modified_Unread
:= True;
1712 Warn_On_Modified_Unread
:= False;
1715 Warning_Mode
:= Normal
;
1718 Address_Clause_Overlay_Warnings
:= True;
1721 Address_Clause_Overlay_Warnings
:= False;
1724 Ineffective_Inline_Warnings
:= True;
1727 Ineffective_Inline_Warnings
:= False;
1730 Warn_On_Redundant_Constructs
:= True;
1733 Warn_On_Redundant_Constructs
:= False;
1736 Warning_Mode
:= Suppress
;
1739 Check_Unreferenced
:= True;
1740 Check_Withs
:= True;
1741 Check_Unreferenced_Formals
:= True;
1744 Check_Unreferenced
:= False;
1745 Check_Withs
:= False;
1746 Check_Unreferenced_Formals
:= False;
1749 Warn_On_No_Value_Assigned
:= True;
1752 Warn_On_No_Value_Assigned
:= False;
1755 Warn_On_Export_Import
:= True;
1758 Warn_On_Export_Import
:= False;
1761 Warn_On_Ada_2005_Compatibility
:= True;
1764 Warn_On_Ada_2005_Compatibility
:= False;
1767 Warn_On_Unchecked_Conversion
:= True;
1770 Warn_On_Unchecked_Conversion
:= False;
1772 -- Allow and ignore 'w' so that the old
1773 -- format (e.g. -gnatwuwl) will work.
1783 end Set_Warning_Switch
;
1785 -----------------------------
1786 -- Warn_On_Known_Condition --
1787 -----------------------------
1789 procedure Warn_On_Known_Condition
(C
: Node_Id
) is
1793 -- Argument replacement in an inlined body can make conditions static.
1794 -- Do not emit warnings in this case.
1796 if In_Inlined_Body
then
1800 if Constant_Condition_Warnings
1801 and then Nkind
(C
) = N_Identifier
1803 (Entity
(C
) = Standard_False
or else Entity
(C
) = Standard_True
)
1804 and then Comes_From_Source
(Original_Node
(C
))
1805 and then not In_Instance
1807 -- See if this is in a statement or a declaration
1811 -- If tree is not attached, do not issue warning (this is very
1812 -- peculiar, and probably arises from some other error condition)
1817 -- If we are in a declaration, then no warning, since in practice
1818 -- conditionals in declarations are used for intended tests which
1819 -- may be known at compile time, e.g. things like
1821 -- x : constant Integer := 2 + (Word'Size = 32);
1823 -- And a warning is annoying in such cases
1825 elsif Nkind
(P
) in N_Declaration
1827 Nkind
(P
) in N_Later_Decl_Item
1831 -- Don't warn in assert pragma, since presumably tests in such
1832 -- a context are very definitely intended, and might well be
1833 -- known at compile time. Note that we have to test the original
1834 -- node, since assert pragmas get rewritten at analysis time.
1836 elsif Nkind
(Original_Node
(P
)) = N_Pragma
1837 and then Chars
(Original_Node
(P
)) = Name_Assert
1842 exit when Is_Statement
(P
);
1846 -- Here we issue the warning unless some sub-operand has warnings
1847 -- set off, in which case we suppress the warning for the node. If
1848 -- the original expression is an inequality, it has been expanded
1849 -- into a negation, and the value of the original expression is the
1850 -- negation of the equality. If the expression is an entity that
1851 -- appears within a negation, it is clearer to flag the negation
1852 -- itself, and report on its constant value.
1854 if not Operand_Has_Warnings_Suppressed
(C
) then
1856 True_Branch
: Boolean := Entity
(C
) = Standard_True
;
1857 Cond
: Node_Id
:= C
;
1860 if Present
(Parent
(C
))
1861 and then Nkind
(Parent
(C
)) = N_Op_Not
1863 True_Branch
:= not True_Branch
;
1868 if Is_Entity_Name
(Original_Node
(C
))
1869 and then Nkind
(Cond
) /= N_Op_Not
1872 ("object & is always True?", Cond
, Original_Node
(C
));
1874 Error_Msg_N
("condition is always True?", Cond
);
1877 Error_Msg_N
("condition is always False?", Cond
);
1882 end Warn_On_Known_Condition
;