1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1999-2005, 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
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 -- Similarly, the generic formals of a generic subprogram
298 -- are not accessible.
300 when N_Generic_Subprogram_Declaration =>
301 if Is_List_Member (Prev)
302 and then List_Containing (Prev) =
303 Generic_Formal_Declarations (P)
310 -- If we reach a subprogram body, entity is not referenceable
311 -- unless it is the defining entity of the body. This will
312 -- happen, e.g. when a function is an attribute renaming that
313 -- is rewritten as a body.
315 when N_Subprogram_Body =>
316 if Ent /= Defining_Entity (P) then
322 -- If we reach any other body, definitely not referenceable
324 when N_Package_Body |
332 -- For all other cases, keep looking up tree
339 end Publicly_Referenceable;
341 -- Start of processing for Check_References
344 -- No messages if warnings are suppressed, or if we have detected
345 -- any real errors so far (this last check avoids junk messages
346 -- resulting from errors, e.g. a subunit that is not loaded).
348 if Warning_Mode = Suppress
349 or else Serious_Errors_Detected /= 0
354 -- We also skip the messages if any subunits were not loaded (see
355 -- comment in Sem_Ch10 to understand how this is set, and why it is
356 -- necessary to suppress the warnings in this case).
358 if Missing_Subunits then
362 -- Otherwise loop through entities, looking for suspicious stuff
364 E1 := First_Entity (E);
365 while Present (E1) loop
367 -- We only look at source entities with warning flag on
369 if Comes_From_Source (E1) and then not Warnings_Off (E1) then
371 -- We are interested in variables and out parameters, but we
372 -- exclude protected types, too complicated to worry about.
374 if Ekind (E1) = E_Variable
376 (Ekind (E1) = E_Out_Parameter
377 and then not Is_Protected_Type (Current_Scope))
379 -- Post warning if this object not assigned. Note that we
380 -- do not consider the implicit initialization of an access
381 -- type to be the assignment of a value for this purpose.
383 if Ekind (E1) = E_Out_Parameter
384 and then Present (Spec_Entity (E1))
386 UR := Unset_Reference (Spec_Entity (E1));
388 UR := Unset_Reference (E1);
391 -- If the entity is an out parameter of the current subprogram
392 -- body, check the warning status of the parameter in the spec.
394 if Ekind (E1) = E_Out_Parameter
395 and then Present (Spec_Entity (E1))
396 and then Warnings_Off (Spec_Entity (E1))
401 and then Is_Access_Type (Etype (E1))
404 -- For access types, the only time we made a UR
405 -- entry was for a dereference, and so we post
406 -- the appropriate warning here (note that the
407 -- dereference may not be explicit in the source,
408 -- for example in the case of a dispatching call
409 -- with an anonymous access controlling formal, or
410 -- of an assignment of a pointer involving a
411 -- discriminant check on the designated object).
413 Error_Msg_NE ("& may be null?", UR, E1);
416 elsif Never_Set_In_Source (E1)
417 and then not Generic_Package_Spec_Entity (E1)
419 if Warn_On_No_Value_Assigned then
421 -- Do not output complaint about never being assigned a
422 -- value if a pragma Unreferenced applies to the variable
423 -- or if it is a parameter, to the corresponding spec.
425 if Has_Pragma_Unreferenced (E1)
426 or else (Is_Formal (E1)
427 and then Present (Spec_Entity (E1))
429 Has_Pragma_Unreferenced (Spec_Entity (E1)))
433 -- Pragma Unreferenced not set, so output message
436 Output_Reference_Error
437 ("& is never assigned a value?");
439 -- Deal with special case where this variable is
440 -- hidden by a loop variable
442 if Ekind (E1) = E_Variable
443 and then Present (Hiding_Loop_Variable (E1))
445 Error_Msg_Sloc := Sloc (E1);
447 ("declaration hides &#?",
448 Hiding_Loop_Variable (E1));
450 ("for loop implicitly declares loop variable?",
451 Hiding_Loop_Variable (E1));
457 -- Case of variable that could be a constant. Note that we
458 -- never signal such messages for generic package entities,
459 -- since a given instance could have modifications outside
462 elsif Warn_On_Constant
463 and then Ekind (E1) = E_Variable
464 and then Is_True_Constant (E1)
465 and then not Generic_Package_Spec_Entity (E1)
467 -- A special case, if this variable is volatile and not
468 -- imported, it is not helpful to tell the programmer
469 -- to mark the variable as constant, since this would be
470 -- illegal by virtue of RM C.6(13).
472 if (Is_Volatile (E1) or else Has_Volatile_Components (E1))
473 and then not Is_Imported (E1)
476 ("& is not modified, volatile has no effect?", E1);
479 ("& is not modified, could be declared constant?", E1);
483 -- Check for unset reference, note that we exclude access
484 -- types from this check, since access types do always have
485 -- a null value, and that seems legitimate in this case.
487 if Warn_On_No_Value_Assigned and then Present (UR) then
489 -- For other than access type, go back to original node
490 -- to deal with case where original unset reference
491 -- has been rewritten during expansion.
493 UR := Original_Node (UR);
495 -- In some cases, the original node may be a type
496 -- conversion or qualification, and in this case
497 -- we want the object entity inside.
499 while Nkind (UR) = N_Type_Conversion
500 or else Nkind (UR) = N_Qualified_Expression
502 UR := Expression (UR);
505 -- Here we issue the warning, all checks completed
506 -- If the unset reference is prefix of a selected
507 -- component that comes from source, mention the
508 -- component as well. If the selected component comes
509 -- from expansion, all we know is that the entity is
510 -- not fully initialized at the point of the reference.
511 -- Locate an unintialized component to get a better
514 if Nkind (Parent (UR)) = N_Selected_Component then
515 Error_Msg_Node_2 := Selector_Name (Parent (UR));
517 if not Comes_From_Source (Parent (UR)) then
522 Comp := First_Entity (Etype (E1));
523 while Present (Comp) loop
524 if Ekind (Comp) = E_Component
525 and then Nkind (Parent (Comp)) =
526 N_Component_Declaration
527 and then No (Expression (Parent (Comp)))
529 Error_Msg_Node_2 := Comp;
539 ("`&.&` may be referenced before it has a value?",
543 ("& may be referenced before it has a value?",
551 -- Then check for unreferenced entities. Note that we are only
552 -- interested in entities which do not have the Referenced flag
553 -- set. The Referenced_As_LHS flag is interesting only if the
554 -- Referenced flag is not set.
556 if not Referenced (E1)
558 -- Check that warnings on unreferenced entities are enabled
560 and then ((Check_Unreferenced and then not Is_Formal (E1))
562 (Check_Unreferenced_Formals and then Is_Formal (E1))
564 (Warn_On_Modified_Unread
565 and then Referenced_As_LHS (E1)))
567 -- Labels, and enumeration literals, and exceptions. The
568 -- warnings are also placed on local packages that cannot
569 -- be referenced from elsewhere, including those declared
570 -- within a package body.
572 and then (Is_Object (E1)
578 Ekind (E1) = E_Exception
580 Ekind (E1) = E_Named_Integer
582 Ekind (E1) = E_Named_Real
586 (Ekind (E1) = E_Package
588 (Ekind (E) = E_Function
589 or else Ekind (E) = E_Package_Body
590 or else Ekind (E) = E_Procedure
591 or else Ekind (E) = E_Subprogram_Body
592 or else Ekind (E) = E_Block)))
594 -- Exclude instantiations, since there is no reason why
595 -- every entity in an instantiation should be referenced.
597 and then Instantiation_Location (Sloc (E1)) = No_Location
599 -- Exclude formal parameters from bodies if the corresponding
600 -- spec entity has been referenced in the case where there is
603 and then not (Is_Formal (E1)
605 Ekind (Scope (E1)) = E_Subprogram_Body
607 Present (Spec_Entity (E1))
609 Referenced (Spec_Entity (E1)))
611 -- Consider private type referenced if full view is referenced
612 -- If there is not full view, this is a generic type on which
613 -- warnings are also useful.
616 not (Is_Private_Type (E1)
618 Present (Full_View (E1))
619 and then Referenced (Full_View (E1)))
621 -- Don't worry about full view, only about private type
623 and then not Has_Private_Declaration (E1)
625 -- Eliminate dispatching operations from consideration, we
626 -- cannot tell if these are referenced or not in any easy
627 -- manner (note this also catches Adjust/Finalize/Initialize)
629 and then not Is_Dispatching_Operation (E1)
631 -- Check entity that can be publicly referenced (we do not
632 -- give messages for such entities, since there could be
633 -- other units, not involved in this compilation, that
634 -- contain relevant references.
636 and then not Publicly_Referenceable (E1)
638 -- Class wide types are marked as source entities, but
639 -- they are not really source entities, and are always
640 -- created, so we do not care if they are not referenced.
642 and then Ekind (E1) /= E_Class_Wide_Type
644 -- Objects other than parameters of task types are allowed
645 -- to be non-referenced, since they start up tasks!
647 and then ((Ekind (E1) /= E_Variable
648 and then Ekind (E1) /= E_Constant
649 and then Ekind (E1) /= E_Component)
650 or else not Is_Task_Type (Etype (E1)))
652 -- For subunits, only place warnings on the main unit
653 -- itself, since parent units are not completely compiled
655 and then (Nkind (Unit (Cunit (Main_Unit))) /= N_Subunit
657 Get_Source_Unit (E1) = Main_Unit)
659 -- Suppress warnings in internal units if not in -gnatg
660 -- mode (these would be junk warnings for an applications
661 -- program, since they refer to problems in internal units)
665 Is_Internal_File_Name
666 (Unit_File_Name (Get_Source_Unit (E1)))
668 -- We do not immediately flag the error. This is because
669 -- we have not expanded generic bodies yet, and they may
670 -- have the missing reference. So instead we park the
671 -- entity on a list, for later processing. However, for
672 -- the accept case, post the error right here, since we
673 -- have the information now in this case.
675 if Present (Anod) then
676 Output_Reference_Error ("& is not referenced?");
679 Unreferenced_Entities.Increment_Last;
680 Unreferenced_Entities.Table
681 (Unreferenced_Entities.Last) := E1;
685 -- Generic units are referenced in the generic body,
686 -- but if they are not public and never instantiated
687 -- we want to force a warning on them. We treat them
688 -- as redundant constructs to minimize noise.
690 elsif Is_Generic_Subprogram (E1)
691 and then not Is_Instantiated (E1)
692 and then not Publicly_Referenceable (E1)
693 and then Instantiation_Depth (Sloc (E1)) = 0
694 and then Warn_On_Redundant_Constructs
696 Unreferenced_Entities.Increment_Last;
697 Unreferenced_Entities.Table (Unreferenced_Entities.Last) := E1;
699 -- Force warning on entity
701 Set_Referenced (E1, False);
705 -- Recurse into nested package or block. Do not recurse into a
706 -- formal package, because the correponding body is not analyzed.
709 if ((Ekind (E1) = E_Package or else Ekind (E1) = E_Generic_Package)
710 and then Nkind (Parent (E1)) = N_Package_Specification
712 Nkind (Original_Node (Unit_Declaration_Node (E1)))
713 /= N_Formal_Package_Declaration)
715 or else Ekind (E1) = E_Block
717 Check_References (E1);
722 end Check_References;
724 ---------------------------
725 -- Check_Unset_Reference --
726 ---------------------------
728 procedure Check_Unset_Reference (N : Node_Id) is
730 -- Nothing to do if warnings suppressed
732 if Warning_Mode = Suppress then
736 -- Ignore reference to non-scalar if not from source. Almost always
737 -- such references are bogus (e.g. calls to init procs to set
738 -- default discriminant values).
740 if not Comes_From_Source (N)
741 and then not Is_Scalar_Type (Etype (N))
746 -- Otherwise see what kind of node we have. If the entity already
747 -- has an unset reference, it is not necessarily the earliest in
748 -- the text, because resolution of the prefix of selected components
749 -- is completed before the resolution of the selected component itself.
750 -- as a result, given (R /= null and then R.X > 0), the occurrences
751 -- of R are examined in right-to-left order. If there is already an
752 -- unset reference, we check whether N is earlier before proceeding.
755 when N_Identifier | N_Expanded_Name =>
757 E : constant Entity_Id := Entity (N);
760 if (Ekind (E) = E_Variable
761 or else Ekind (E) = E_Out_Parameter)
762 and then Never_Set_In_Source (E)
763 and then (No (Unset_Reference (E))
764 or else Earlier_In_Extended_Unit
765 (Sloc (N), Sloc (Unset_Reference (E))))
766 and then not Warnings_Off (E)
768 -- We may have an unset reference. The first test is
769 -- whether we are accessing a discriminant of a record
770 -- or a component with default initialization. Both of
771 -- these cases can be ignored, since the actual object
772 -- that is referenced is definitely initialized. Note
773 -- that this covers the case of reading discriminants
774 -- of an out parameter, which is OK even in Ada 83.
776 -- Note that we are only interested in a direct reference
777 -- to a record component here. If the reference is via an
778 -- access type, then the access object is being referenced,
779 -- not the record, and still deserves an unset reference.
781 if Nkind (Parent (N)) = N_Selected_Component
782 and not Is_Access_Type (Etype (N))
785 ES : constant Entity_Id :=
786 Entity (Selector_Name (Parent (N)));
789 if Ekind (ES) = E_Discriminant
790 or else Present (Expression (Declaration_Node (ES)))
797 -- Here we have a potential unset reference. But before we
798 -- get worried about it, we have to make sure that the
799 -- entity declaration is in the same procedure as the
800 -- reference, since if they are in separate procedures,
801 -- then we have no idea about sequential execution.
803 -- The tests in the loop below catch all such cases, but
804 -- do allow the reference to appear in a loop, block, or
805 -- package spec that is nested within the declaring scope.
806 -- As always, it is possible to construct cases where the
807 -- warning is wrong, that is why it is a warning!
811 SE : constant Entity_Id := Scope (E);
816 if SR = Standard_Standard
817 or else Is_Subprogram (SR)
818 or else Is_Concurrent_Body (SR)
819 or else Is_Concurrent_Type (SR)
827 -- Case of reference has an access type. This is a
828 -- special case since access types are always set to
829 -- null so cannot be truly uninitialized, but we still
830 -- want to warn about cases of obvious null dereference.
832 if Is_Access_Type (Etype (N)) then
838 return Traverse_Result;
839 -- Process function for instantation of Traverse
840 -- below. Checks if N contains reference to E
841 -- other than a dereference.
843 function Ref_In (Nod : Node_Id) return Boolean;
844 -- Determines whether Nod contains a reference
845 -- to the entity E that is not a dereference.
849 return Traverse_Result
852 if Is_Entity_Name (N)
853 and then Entity (N) = E
854 and then not Is_Dereferenced (N)
862 function Ref_In (Nod : Node_Id) return Boolean is
863 function Traverse is new Traverse_Func (Process);
866 return Traverse (Nod) = Abandon;
870 -- Don't bother if we are inside an instance,
871 -- since the compilation of the generic template
872 -- is where the warning should be issued.
878 -- Don't bother if this is not the main unit.
879 -- If we try to give this warning for with'ed
880 -- units, we get some false positives, since
881 -- we do not record references in other units.
883 if not In_Extended_Main_Source_Unit (E)
885 not In_Extended_Main_Source_Unit (N)
890 -- We are only interested in deferences
892 if not Is_Dereferenced (N) then
896 -- One more check, don't bother with references
897 -- that are inside conditional statements or while
898 -- loops if the condition references the entity in
899 -- question. This avoids most false positives.
906 if (Nkind (P) = N_If_Statement
908 Nkind (P) = N_Elsif_Part)
909 and then Ref_In (Condition (P))
913 elsif Nkind (P) = N_Loop_Statement
914 and then Present (Iteration_Scheme (P))
916 Ref_In (Condition (Iteration_Scheme (P)))
924 -- Here we definitely have a case for giving a warning
925 -- for a reference to an unset value. But we don't give
926 -- the warning now. Instead we set the Unset_Reference
927 -- field of the identifier involved. The reason for this
928 -- is that if we find the variable is never ever assigned
929 -- a value then that warning is more important and there
930 -- is no point in giving the reference warning.
932 -- If this is an identifier, set the field directly
934 if Nkind (N) = N_Identifier then
935 Set_Unset_Reference (E, N);
937 -- Otherwise it is an expanded name, so set the field
938 -- of the actual identifier for the reference.
941 Set_Unset_Reference (E, Selector_Name (N));
947 when N_Indexed_Component | N_Slice =>
948 Check_Unset_Reference (Prefix (N));
950 when N_Selected_Component =>
952 if Present (Entity (Selector_Name (N)))
953 and then Ekind (Entity (Selector_Name (N))) = E_Discriminant
955 -- A discriminant is always initialized
960 Check_Unset_Reference (Prefix (N));
963 when N_Type_Conversion | N_Qualified_Expression =>
964 Check_Unset_Reference (Expression (N));
970 end Check_Unset_Reference;
972 ------------------------
973 -- Check_Unused_Withs --
974 ------------------------
976 procedure Check_Unused_Withs (Spec_Unit : Unit_Number_Type := No_Unit) is
982 Munite : constant Entity_Id := Cunit_Entity (Main_Unit);
983 -- This is needed for checking the special renaming case
985 procedure Check_One_Unit (Unit : Unit_Number_Type);
986 -- Subsidiary procedure, performs checks for specified unit
992 procedure Check_One_Unit (Unit : Unit_Number_Type) is
993 Is_Visible_Renaming : Boolean := False;
996 procedure Check_Inner_Package (Pack : Entity_Id);
997 -- Pack is a package local to a unit in a with_clause. Both the
998 -- unit and Pack are referenced. If none of the entities in Pack
999 -- are referenced, then the only occurrence of Pack is in a use
1000 -- clause or a pragma, and a warning is worthwhile as well.
1002 function Check_System_Aux return Boolean;
1003 -- Before giving a warning on a with_clause for System, check
1004 -- whether a system extension is present.
1006 function Find_Package_Renaming
1008 L : Entity_Id) return Entity_Id;
1009 -- The only reference to a context unit may be in a renaming
1010 -- declaration. If this renaming declares a visible entity, do
1011 -- not warn that the context clause could be moved to the body,
1012 -- because the renaming may be intented to re-export the unit.
1014 -------------------------
1015 -- Check_Inner_Package --
1016 -------------------------
1018 procedure Check_Inner_Package (Pack : Entity_Id) is
1020 Un : constant Node_Id := Sinfo.Unit (Cnode);
1022 function Check_Use_Clause (N : Node_Id) return Traverse_Result;
1023 -- If N is a use_clause for Pack, emit warning
1025 procedure Check_Use_Clauses is new
1026 Traverse_Proc (Check_Use_Clause);
1028 ----------------------
1029 -- Check_Use_Clause --
1030 ----------------------
1032 function Check_Use_Clause (N : Node_Id) return Traverse_Result is
1036 if Nkind (N) = N_Use_Package_Clause then
1037 Nam := First (Names (N));
1039 while Present (Nam) loop
1040 if Entity (Nam) = Pack then
1041 Error_Msg_Qual_Level := 1;
1043 ("no entities of package& are referenced?",
1045 Error_Msg_Qual_Level := 0;
1053 end Check_Use_Clause;
1055 -- Start of processing for Check_Inner_Package
1058 E := First_Entity (Pack);
1060 while Present (E) loop
1061 if Referenced (E) then
1068 -- No entities of the package are referenced. Check whether
1069 -- the reference to the package itself is a use clause, and
1070 -- if so place a warning on it.
1072 Check_Use_Clauses (Un);
1073 end Check_Inner_Package;
1075 ----------------------
1076 -- Check_System_Aux --
1077 ----------------------
1079 function Check_System_Aux return Boolean is
1083 if Chars (Lunit) = Name_System
1084 and then Scope (Lunit) = Standard_Standard
1085 and then Present_System_Aux
1087 Ent := First_Entity (System_Aux_Id);
1089 while Present (Ent) loop
1090 if Referenced (Ent) then
1099 end Check_System_Aux;
1101 ---------------------------
1102 -- Find_Package_Renaming --
1103 ---------------------------
1105 function Find_Package_Renaming
1107 L : Entity_Id) return Entity_Id
1113 Is_Visible_Renaming := False;
1114 E1 := First_Entity (P);
1116 while Present (E1) loop
1117 if Ekind (E1) = E_Package
1118 and then Renamed_Object (E1) = L
1120 Is_Visible_Renaming := not Is_Hidden (E1);
1123 elsif Ekind (E1) = E_Package
1124 and then No (Renamed_Object (E1))
1125 and then not Is_Generic_Instance (E1)
1127 R := Find_Package_Renaming (E1, L);
1130 Is_Visible_Renaming := not Is_Hidden (R);
1139 end Find_Package_Renaming;
1141 -- Start of processing for Check_One_Unit
1144 Cnode := Cunit (Unit);
1146 -- Only do check in units that are part of the extended main
1147 -- unit. This is actually a necessary restriction, because in
1148 -- the case of subprogram acting as its own specification,
1149 -- there can be with's in subunits that we will not see.
1151 if not In_Extended_Main_Source_Unit (Cnode) then
1154 -- In configurable run time mode, we remove the bodies of
1155 -- non-inlined subprograms, which may lead to spurious warnings,
1156 -- which are clearly undesirable.
1158 elsif Configurable_Run_Time_Mode
1159 and then Is_Predefined_File_Name (Unit_File_Name (Unit))
1164 -- Loop through context items in this unit
1166 Item := First (Context_Items (Cnode));
1167 while Present (Item) loop
1168 if Nkind (Item) = N_With_Clause
1169 and then not Implicit_With (Item)
1170 and then In_Extended_Main_Source_Unit (Item)
1172 Lunit := Entity (Name (Item));
1174 -- Check if this unit is referenced
1176 if not Referenced (Lunit) then
1178 -- Suppress warnings in internal units if not in -gnatg
1179 -- mode (these would be junk warnings for an applications
1180 -- program, since they refer to problems in internal units)
1183 or else not Is_Internal_File_Name (Unit_File_Name (Unit))
1185 -- Here we definitely have a non-referenced unit. If
1186 -- it is the special call for a spec unit, then just
1187 -- set the flag to be read later.
1189 if Unit = Spec_Unit then
1190 Set_Unreferenced_In_Spec (Item);
1192 -- Otherwise simple unreferenced message
1196 ("unit& is not referenced?", Name (Item));
1200 -- If main unit is a renaming of this unit, then we consider
1201 -- the with to be OK (obviously it is needed in this case!)
1203 elsif Present (Renamed_Entity (Munite))
1204 and then Renamed_Entity (Munite) = Lunit
1208 -- If this unit is referenced, and it is a package, we
1209 -- do another test, to see if any of the entities in the
1210 -- package are referenced. If none of the entities are
1211 -- referenced, we still post a warning. This occurs if
1212 -- the only use of the package is in a use clause, or
1213 -- in a package renaming declaration.
1215 elsif Ekind (Lunit) = E_Package then
1217 -- If Is_Instantiated is set, it means that the package
1218 -- is implicitly instantiated (this is the case of a
1219 -- parent instance or an actual for a generic package
1220 -- formal), and this counts as a reference.
1222 if Is_Instantiated (Lunit) then
1225 -- If no entities in package, and there is a pragma
1226 -- Elaborate_Body present, then assume that this with
1227 -- is done for purposes of this elaboration.
1229 elsif No (First_Entity (Lunit))
1230 and then Has_Pragma_Elaborate_Body (Lunit)
1234 -- Otherwise see if any entities have been referenced
1237 Ent := First_Entity (Lunit);
1239 -- No more entities, and we did not find one
1240 -- that was referenced. Means we have a definite
1241 -- case of a with none of whose entities was
1246 -- If in spec, just set the flag
1248 if Unit = Spec_Unit then
1249 Set_No_Entities_Ref_In_Spec (Item);
1251 elsif Check_System_Aux then
1254 -- Else give the warning
1258 ("no entities of & are referenced?",
1261 -- Look for renamings of this package, and
1262 -- flag them as well. If the original package
1263 -- has warnings off, we suppress the warning
1264 -- on the renaming as well.
1266 Pack := Find_Package_Renaming (Munite, Lunit);
1269 and then not Warnings_Off (Lunit)
1272 ("no entities of & are referenced?",
1273 Unit_Declaration_Node (Pack),
1280 -- Case of next entity is referenced
1282 elsif Referenced (Ent)
1283 or else Referenced_As_LHS (Ent)
1285 -- This means that the with is indeed fine, in
1286 -- that it is definitely needed somewhere, and
1287 -- we can quite worrying about this one.
1289 -- Except for one little detail, if either of
1290 -- the flags was set during spec processing,
1291 -- this is where we complain that the with
1292 -- could be moved from the spec. If the spec
1293 -- contains a visible renaming of the package,
1294 -- inhibit warning to move with_clause to body.
1296 if Ekind (Munite) = E_Package_Body then
1298 Find_Package_Renaming
1299 (Spec_Entity (Munite), Lunit);
1302 if Unreferenced_In_Spec (Item) then
1304 ("unit& is not referenced in spec?",
1307 elsif No_Entities_Ref_In_Spec (Item) then
1309 ("no entities of & are referenced in spec?",
1313 if Ekind (Ent) = E_Package then
1314 Check_Inner_Package (Ent);
1320 if not Is_Visible_Renaming then
1322 ("\with clause might be moved to body?",
1328 -- Move to next entity to continue search
1336 -- For a generic package, the only interesting kind of
1337 -- reference is an instantiation, since entities cannot
1338 -- be referenced directly.
1340 elsif Is_Generic_Unit (Lunit) then
1342 -- Unit was never instantiated, set flag for case of spec
1343 -- call, or give warning for normal call.
1345 if not Is_Instantiated (Lunit) then
1346 if Unit = Spec_Unit then
1347 Set_Unreferenced_In_Spec (Item);
1350 ("unit& is never instantiated?", Name (Item));
1353 -- If unit was indeed instantiated, make sure that
1354 -- flag is not set showing it was uninstantiated in
1355 -- the spec, and if so, give warning.
1357 elsif Unreferenced_In_Spec (Item) then
1359 ("unit& is not instantiated in spec?", Name (Item));
1361 ("\with clause can be moved to body?", Name (Item));
1371 -- Start of processing for Check_Unused_Withs
1374 if not Opt.Check_Withs
1375 or else Operating_Mode = Check_Syntax
1380 -- Flag any unused with clauses, but skip this step if we are
1381 -- compiling a subunit on its own, since we do not have enough
1382 -- information to determine whether with's are used. We will get
1383 -- the relevant warnings when we compile the parent. This is the
1384 -- normal style of GNAT compilation in any case.
1386 if Nkind (Unit (Cunit (Main_Unit))) = N_Subunit then
1390 -- Process specified units
1392 if Spec_Unit = No_Unit then
1394 -- For main call, check all units
1396 for Unit in Main_Unit .. Last_Unit loop
1397 Check_One_Unit (Unit);
1401 -- For call for spec, check only the spec
1403 Check_One_Unit (Spec_Unit);
1405 end Check_Unused_Withs;
1407 ---------------------------------
1408 -- Generic_Package_Spec_Entity --
1409 ---------------------------------
1411 function Generic_Package_Spec_Entity (E : Entity_Id) return Boolean is
1415 if Is_Package_Body_Entity (E) then
1422 if S = Standard_Standard then
1425 elsif Ekind (S) = E_Generic_Package then
1428 elsif Ekind (S) = E_Package then
1436 end Generic_Package_Spec_Entity;
1438 -------------------------------------
1439 -- Operand_Has_Warnings_Suppressed --
1440 -------------------------------------
1442 function Operand_Has_Warnings_Suppressed (N : Node_Id) return Boolean is
1444 function Check_For_Warnings (N : Node_Id) return Traverse_Result;
1445 -- Function used to check one node to see if it is or was originally
1446 -- a reference to an entity for which Warnings are off. If so, Abandon
1447 -- is returned, otherwise OK_Orig is returned to continue the traversal
1448 -- of the original expression.
1450 function Traverse is new Traverse_Func (Check_For_Warnings);
1451 -- Function used to traverse tree looking for warnings
1453 ------------------------
1454 -- Check_For_Warnings --
1455 ------------------------
1457 function Check_For_Warnings (N : Node_Id) return Traverse_Result is
1458 R : constant Node_Id := Original_Node (N);
1461 if Nkind (R) in N_Has_Entity
1462 and then Present (Entity (R))
1463 and then Warnings_Off (Entity (R))
1469 end Check_For_Warnings;
1471 -- Start of processing for Operand_Has_Warnings_Suppressed
1474 return Traverse (N) = Abandon;
1476 -- If any exception occurs, then something has gone wrong, and this is
1477 -- only a minor aesthetic issue anyway, so just say we did not find what
1478 -- we are looking for, rather than blow up.
1483 end Operand_Has_Warnings_Suppressed;
1485 ----------------------------------
1486 -- Output_Unreferenced_Messages --
1487 ----------------------------------
1489 procedure Output_Unreferenced_Messages is
1493 for J in Unreferenced_Entities.First ..
1494 Unreferenced_Entities.Last
1496 E := Unreferenced_Entities.Table (J);
1498 if not Referenced (E) and then not Warnings_Off (E) then
1502 -- Case of variable that is assigned but not read. We
1503 -- suppress the message if the variable is volatile,
1504 -- has an address clause, or is imported.
1506 if Referenced_As_LHS (E)
1507 and then No (Address_Clause (E))
1508 and then not Is_Volatile (E)
1510 if Warn_On_Modified_Unread
1511 and then not Is_Imported (E)
1513 -- Suppress the message for aliased or renamed
1514 -- variables, since there may be other entities
1515 -- read the same memory location.
1517 and then not Is_Aliased (E)
1518 and then No (Renamed_Object (E))
1522 ("variable & is assigned but never read?", E);
1525 -- Normal case of neither assigned nor read
1528 if Present (Renamed_Object (E))
1529 and then Comes_From_Source (Renamed_Object (E))
1532 ("renamed variable & is not referenced?", E);
1535 ("variable & is not referenced?", E);
1540 if Present (Renamed_Object (E))
1541 and then Comes_From_Source (Renamed_Object (E))
1543 Error_Msg_N ("renamed constant & is not referenced?", E);
1545 Error_Msg_N ("constant & is not referenced?", E);
1548 when E_In_Parameter |
1550 E_In_Out_Parameter =>
1552 -- Do not emit message for formals of a renaming, because
1553 -- they are never referenced explicitly.
1555 if Nkind (Original_Node (Unit_Declaration_Node (Scope (E))))
1556 /= N_Subprogram_Renaming_Declaration
1558 Error_Msg_N ("formal parameter & is not referenced?", E);
1561 when E_Named_Integer |
1563 Error_Msg_N ("named number & is not referenced?", E);
1565 when E_Enumeration_Literal =>
1566 Error_Msg_N ("literal & is not referenced?", E);
1569 Error_Msg_N ("function & is not referenced?", E);
1572 Error_Msg_N ("procedure & is not referenced?", E);
1574 when E_Generic_Procedure =>
1576 ("generic procedure & is never instantiated?", E);
1578 when E_Generic_Function =>
1579 Error_Msg_N ("generic function & is never instantiated?", E);
1582 Error_Msg_N ("type & is not referenced?", E);
1585 Error_Msg_N ("& is not referenced?", E);
1588 Set_Warnings_Off (E);
1591 end Output_Unreferenced_Messages;
1593 ------------------------
1594 -- Set_Warning_Switch --
1595 ------------------------
1597 function Set_Warning_Switch (C : Character) return Boolean is
1601 Check_Unreferenced := True;
1602 Check_Unreferenced_Formals := True;
1603 Check_Withs := True;
1604 Constant_Condition_Warnings := True;
1605 Implementation_Unit_Warnings := True;
1606 Ineffective_Inline_Warnings := True;
1607 Warn_On_Ada_2005_Compatibility := True;
1608 Warn_On_Bad_Fixed_Value := True;
1609 Warn_On_Constant := True;
1610 Warn_On_Export_Import := True;
1611 Warn_On_Modified_Unread := True;
1612 Warn_On_No_Value_Assigned := True;
1613 Warn_On_Obsolescent_Feature := True;
1614 Warn_On_Redundant_Constructs := True;
1615 Warn_On_Unchecked_Conversion := True;
1616 Warn_On_Unrecognized_Pragma := True;
1619 Check_Unreferenced := False;
1620 Check_Unreferenced_Formals := False;
1621 Check_Withs := False;
1622 Constant_Condition_Warnings := False;
1623 Elab_Warnings := False;
1624 Implementation_Unit_Warnings := False;
1625 Ineffective_Inline_Warnings := False;
1626 Warn_On_Ada_2005_Compatibility := False;
1627 Warn_On_Bad_Fixed_Value := False;
1628 Warn_On_Constant := False;
1629 Warn_On_Dereference := False;
1630 Warn_On_Export_Import := False;
1631 Warn_On_Hiding := False;
1632 Warn_On_Modified_Unread := False;
1633 Warn_On_No_Value_Assigned := False;
1634 Warn_On_Obsolescent_Feature := False;
1635 Warn_On_Redundant_Constructs := False;
1636 Warn_On_Unchecked_Conversion := False;
1637 Warn_On_Unrecognized_Pragma := False;
1640 Warn_On_Bad_Fixed_Value := True;
1643 Warn_On_Bad_Fixed_Value := False;
1646 Constant_Condition_Warnings := True;
1649 Constant_Condition_Warnings := False;
1652 Warn_On_Dereference := True;
1655 Warn_On_Dereference := False;
1658 Warning_Mode := Treat_As_Error;
1661 Check_Unreferenced_Formals := True;
1664 Check_Unreferenced_Formals := False;
1667 Warn_On_Unrecognized_Pragma := True;
1670 Warn_On_Unrecognized_Pragma := False;
1673 Warn_On_Hiding := True;
1676 Warn_On_Hiding := False;
1679 Implementation_Unit_Warnings := True;
1682 Implementation_Unit_Warnings := False;
1685 Warn_On_Obsolescent_Feature := True;
1688 Warn_On_Obsolescent_Feature := False;
1691 Warn_On_Constant := True;
1694 Warn_On_Constant := False;
1697 Elab_Warnings := True;
1700 Elab_Warnings := False;
1703 Warn_On_Modified_Unread := True;
1706 Warn_On_Modified_Unread := False;
1709 Warning_Mode := Normal;
1712 Address_Clause_Overlay_Warnings := True;
1715 Address_Clause_Overlay_Warnings := False;
1718 Ineffective_Inline_Warnings := True;
1721 Ineffective_Inline_Warnings := False;
1724 Warn_On_Redundant_Constructs := True;
1727 Warn_On_Redundant_Constructs := False;
1730 Warning_Mode := Suppress;
1733 Check_Unreferenced := True;
1734 Check_Withs := True;
1735 Check_Unreferenced_Formals := True;
1738 Check_Unreferenced := False;
1739 Check_Withs := False;
1740 Check_Unreferenced_Formals := False;
1743 Warn_On_No_Value_Assigned := True;
1746 Warn_On_No_Value_Assigned := False;
1749 Warn_On_Export_Import := True;
1752 Warn_On_Export_Import := False;
1755 Warn_On_Ada_2005_Compatibility := True;
1758 Warn_On_Ada_2005_Compatibility := False;
1761 Warn_On_Unchecked_Conversion := True;
1764 Warn_On_Unchecked_Conversion := False;
1766 -- Allow and ignore 'w' so that the old
1767 -- format (e.g. -gnatwuwl) will work.
1777 end Set_Warning_Switch;
1779 -----------------------------
1780 -- Warn_On_Known_Condition --
1781 -----------------------------
1783 procedure Warn_On_Known_Condition (C : Node_Id) is
1787 -- Argument replacement in an inlined body can make conditions
1788 -- static. Do not emit warnings in this case.
1790 if In_Inlined_Body then
1794 if Constant_Condition_Warnings
1795 and then Nkind (C) = N_Identifier
1797 (Entity (C) = Standard_False or else Entity (C) = Standard_True)
1798 and then Comes_From_Source (Original_Node (C))
1799 and then not In_Instance
1801 -- See if this is in a statement or a declaration
1805 -- If tree is not attached, do not issue warning (this is very
1806 -- peculiar, and probably arises from some other error condition)
1811 -- If we are in a declaration, then no warning, since in practice
1812 -- conditionals in declarations are used for intended tests which
1813 -- may be known at compile time, e.g. things like
1815 -- x : constant Integer := 2 + (Word'Size = 32);
1817 -- And a warning is annoying in such cases
1819 elsif Nkind (P) in N_Declaration
1821 Nkind (P) in N_Later_Decl_Item
1825 -- Don't warn in assert pragma, since presumably tests in such
1826 -- a context are very definitely intended, and might well be
1827 -- known at compile time. Note that we have to test the original
1828 -- node, since assert pragmas get rewritten at analysis time.
1830 elsif Nkind (Original_Node (P)) = N_Pragma
1831 and then Chars (Original_Node (P)) = Name_Assert
1836 exit when Is_Statement (P);
1840 -- Here we issue the warning unless some sub-operand has warnings
1841 -- set off, in which case we suppress the warning for the node. If
1842 -- the original expression is an inequality, it has been expanded
1843 -- into a negation, and the value of the original expression is the
1844 -- negation of the equality. If the expression is an entity that
1845 -- appears within a negation, it is clearer to flag the negation
1846 -- itself, and report on its constant value.
1848 if not Operand_Has_Warnings_Suppressed (C) then
1850 True_Branch : Boolean := Entity (C) = Standard_True;
1851 Cond : Node_Id := C;
1854 if Present (Parent (C))
1855 and then Nkind (Parent (C)) = N_Op_Not
1857 True_Branch := not True_Branch;
1862 if Is_Entity_Name (Original_Node (C))
1863 and then Nkind (Cond) /= N_Op_Not
1866 ("object & is always True?", Cond, Original_Node (C));
1868 Error_Msg_N ("condition is always True?", Cond);
1871 Error_Msg_N ("condition is always False?", Cond);
1876 end Warn_On_Known_Condition;