Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / gcc / ada / sem_warn.adb
blob371060669cbf81bc219bcacc7720b6260b152655
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ W A R N --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1999-2004 Free Software Foundation, Inc. --
10 -- --
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. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with Alloc;
28 with Atree; use Atree;
29 with Einfo; use Einfo;
30 with Errout; use Errout;
31 with Fname; use Fname;
32 with Lib; use Lib;
33 with Nlists; use Nlists;
34 with Opt; use Opt;
35 with Sem; use Sem;
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;
42 with Table;
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,
52 Table_Low_Bound => 1,
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
69 Sloc : Source_Ptr;
70 -- Location for warnings associated with this branch
72 Defs : Elist_Id;
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.
82 Next : Nat;
83 -- Index of next branch for this conditional, zero = last branch
84 end record;
86 package Branch_Table is new Table.Table (
87 Table_Component_Type => Branch_Entry,
88 Table_Index_Type => Nat,
89 Table_Low_Bound => 1,
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
98 If_Stmt : Boolean;
99 -- True for IF statement, False for CASE statement
101 First_Branch : Nat;
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
106 end record;
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
156 E1 : Entity_Id;
157 UR : Node_Id;
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
179 D : Node_Id;
181 begin
182 if not Unloaded_Subunits then
184 -- Normal compilation, all subunits are present
186 return False;
188 elsif E /= Main_Unit_Entity then
190 -- No warnings on a stub that is not the main unit
192 return True;
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
202 return True;
203 end if;
205 Next (D);
206 end loop;
208 return False;
210 else
211 -- Missing stubs elsewhere
213 return True;
214 end if;
215 end Missing_Subunits;
217 ----------------------------
218 -- Output_Reference_Error --
219 ----------------------------
221 procedure Output_Reference_Error (M : String) is
222 begin
223 -- Other than accept case, post error on defining identifier
225 if No (Anod) then
226 Error_Msg_N (M, E1);
228 -- Accept case, find body formal to post the message
230 else
231 declare
232 Parm : Node_Id;
233 Enod : Node_Id;
234 Defid : Entity_Id;
236 begin
237 Enod := Anod;
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
246 Enod := Defid;
247 exit;
248 end if;
250 Next (Parm);
251 end loop;
252 end if;
254 Error_Msg_NE (M, Enod, E1);
255 end;
256 end if;
257 end Output_Reference_Error;
259 ----------------------------
260 -- Publicly_Referenceable --
261 ----------------------------
263 function Publicly_Referenceable (Ent : Entity_Id) return Boolean is
264 P : Node_Id;
265 Prev : Node_Id;
267 begin
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.
272 Prev := Ent;
273 P := Parent (Ent);
274 loop
275 case Nkind (P) is
277 -- If we get to top of tree, then publicly referenceable
279 when N_Empty =>
280 return True;
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
289 -- them are useful.
291 when N_Generic_Package_Declaration =>
292 return
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
304 return False;
305 else
306 P := Parent (P);
307 end if;
309 -- If we reach any other body, definitely not referenceable
311 when N_Package_Body |
312 N_Task_Body |
313 N_Entry_Body |
314 N_Protected_Body |
315 N_Block_Statement |
316 N_Subunit =>
317 return False;
319 -- For all other cases, keep looking up tree
321 when others =>
322 Prev := P;
323 P := Parent (P);
324 end case;
325 end loop;
326 end Publicly_Referenceable;
328 -- Start of processing for Check_References
330 begin
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
337 then
338 return;
339 end if;
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
346 return;
347 end if;
349 -- Otherwise loop through entities, looking for suspicious stuff
351 E1 := First_Entity (E);
352 while Present (E1) loop
354 -- We only look at source entities with warning flag on
356 if Comes_From_Source (E1) and then not Warnings_Off (E1) then
358 -- We are interested in variables and out parameters, but we
359 -- exclude protected types, too complicated to worry about.
361 if Ekind (E1) = E_Variable
362 or else
363 (Ekind (E1) = E_Out_Parameter
364 and then not Is_Protected_Type (Current_Scope))
365 then
366 -- Post warning if this object not assigned. Note that we
367 -- do not consider the implicit initialization of an access
368 -- type to be the assignment of a value for this purpose.
370 if Ekind (E1) = E_Out_Parameter
371 and then Present (Spec_Entity (E1))
372 then
373 UR := Unset_Reference (Spec_Entity (E1));
374 else
375 UR := Unset_Reference (E1);
376 end if;
378 -- If the entity is an out parameter of the current subprogram
379 -- body, check the warning status of the parameter in the spec.
381 if Ekind (E1) = E_Out_Parameter
382 and then Present (Spec_Entity (E1))
383 and then Warnings_Off (Spec_Entity (E1))
384 then
385 null;
387 elsif Present (UR)
388 and then Is_Access_Type (Etype (E1))
389 then
391 -- For access types, the only time we made a UR
392 -- entry was for a dereference, and so we post
393 -- the appropriate warning here (note that the
394 -- dereference may not be explicit in the source,
395 -- for example in the case of a dispatching call
396 -- with an anonymous access controlling formal, or
397 -- of an assignment of a pointer involving a
398 -- discriminant check on the designated object).
400 Error_Msg_NE ("& may be null?", UR, E1);
401 goto Continue;
403 elsif Never_Set_In_Source (E1)
404 and then not Generic_Package_Spec_Entity (E1)
405 then
406 if Warn_On_No_Value_Assigned then
408 -- Do not output complaint about never being assigned a
409 -- value if a pragma Unreferenced applies to the variable
410 -- or if it is a parameter, to the corresponding spec.
412 if Has_Pragma_Unreferenced (E1)
413 or else (Is_Formal (E1)
414 and then Present (Spec_Entity (E1))
415 and then
416 Has_Pragma_Unreferenced (Spec_Entity (E1)))
417 then
418 null;
420 -- Pragma Unreferenced not set, so output message
422 else
423 Output_Reference_Error
424 ("& is never assigned a value?");
426 -- Deal with special case where this variable is
427 -- hidden by a loop variable
429 if Ekind (E1) = E_Variable
430 and then Present (Hiding_Loop_Variable (E1))
431 then
432 Error_Msg_Sloc := Sloc (E1);
433 Error_Msg_N
434 ("declaration hides &#?",
435 Hiding_Loop_Variable (E1));
436 Error_Msg_N
437 ("for loop implicitly declares loop variable?",
438 Hiding_Loop_Variable (E1));
439 end if;
440 end if;
441 end if;
442 goto Continue;
444 -- Case of variable that could be a constant. Note that we
445 -- never signal such messages for generic package entities,
446 -- since a given instance could have modifications outside
447 -- the package.
449 elsif Warn_On_Constant
450 and then Ekind (E1) = E_Variable
451 and then Is_True_Constant (E1)
452 and then not Generic_Package_Spec_Entity (E1)
453 then
454 Error_Msg_N
455 ("& is not modified, could be declared constant?", E1);
456 end if;
458 -- Check for unset reference, note that we exclude access
459 -- types from this check, since access types do always have
460 -- a null value, and that seems legitimate in this case.
462 if Warn_On_No_Value_Assigned and then Present (UR) then
464 -- For other than access type, go back to original node
465 -- to deal with case where original unset reference
466 -- has been rewritten during expansion.
468 UR := Original_Node (UR);
470 -- In some cases, the original node may be a type
471 -- conversion or qualification, and in this case
472 -- we want the object entity inside.
474 while Nkind (UR) = N_Type_Conversion
475 or else Nkind (UR) = N_Qualified_Expression
476 loop
477 UR := Expression (UR);
478 end loop;
480 -- Here we issue the warning, all checks completed
481 -- If the unset reference is prefix of a selected
482 -- component that comes from source, mention the
483 -- component as well. If the selected component comes
484 -- from expansion, all we know is that the entity is
485 -- not fully initialized at the point of the reference.
486 -- Locate an unintialized component to get a better
487 -- error message.
489 if Nkind (Parent (UR)) = N_Selected_Component then
490 Error_Msg_Node_2 := Selector_Name (Parent (UR));
492 if not Comes_From_Source (Parent (UR)) then
493 declare
494 Comp : Entity_Id;
496 begin
497 Comp := First_Entity (Etype (E1));
498 while Present (Comp) loop
499 if Ekind (Comp) = E_Component
500 and then Nkind (Parent (Comp)) =
501 N_Component_Declaration
502 and then No (Expression (Parent (Comp)))
503 then
504 Error_Msg_Node_2 := Comp;
505 exit;
506 end if;
508 Next_Entity (Comp);
509 end loop;
510 end;
511 end if;
513 Error_Msg_N
514 ("`&.&` may be referenced before it has a value?",
515 UR);
516 else
517 Error_Msg_N
518 ("& may be referenced before it has a value?",
519 UR);
520 end if;
522 goto Continue;
523 end if;
524 end if;
526 -- Then check for unreferenced entities. Note that we are only
527 -- interested in entities which do not have the Referenced flag
528 -- set. The Referenced_As_LHS flag is interesting only if the
529 -- Referenced flag is not set.
531 if not Referenced (E1)
533 -- Check that warnings on unreferenced entities are enabled
535 and then ((Check_Unreferenced and then not Is_Formal (E1))
536 or else
537 (Check_Unreferenced_Formals and then Is_Formal (E1))
538 or else
539 (Warn_On_Modified_Unread
540 and then Referenced_As_LHS (E1)))
542 -- Labels, and enumeration literals, and exceptions. The
543 -- warnings are also placed on local packages that cannot
544 -- be referenced from elsewhere, including those declared
545 -- within a package body.
547 and then (Is_Object (E1)
548 or else
549 Is_Type (E1)
550 or else
551 Ekind (E1) = E_Label
552 or else
553 Ekind (E1) = E_Exception
554 or else
555 Ekind (E1) = E_Named_Integer
556 or else
557 Ekind (E1) = E_Named_Real
558 or else
559 Is_Overloadable (E1)
560 or else
561 (Ekind (E1) = E_Package
562 and then
563 (Ekind (E) = E_Function
564 or else Ekind (E) = E_Package_Body
565 or else Ekind (E) = E_Procedure
566 or else Ekind (E) = E_Subprogram_Body
567 or else Ekind (E) = E_Block)))
569 -- Exclude instantiations, since there is no reason why
570 -- every entity in an instantiation should be referenced.
572 and then Instantiation_Location (Sloc (E1)) = No_Location
574 -- Exclude formal parameters from bodies if the corresponding
575 -- spec entity has been referenced in the case where there is
576 -- a separate spec.
578 and then not (Is_Formal (E1)
579 and then
580 Ekind (Scope (E1)) = E_Subprogram_Body
581 and then
582 Present (Spec_Entity (E1))
583 and then
584 Referenced (Spec_Entity (E1)))
586 -- Consider private type referenced if full view is referenced
587 -- If there is not full view, this is a generic type on which
588 -- warnings are also useful.
590 and then
591 not (Is_Private_Type (E1)
592 and then
593 Present (Full_View (E1))
594 and then Referenced (Full_View (E1)))
596 -- Don't worry about full view, only about private type
598 and then not Has_Private_Declaration (E1)
600 -- Eliminate dispatching operations from consideration, we
601 -- cannot tell if these are referenced or not in any easy
602 -- manner (note this also catches Adjust/Finalize/Initialize)
604 and then not Is_Dispatching_Operation (E1)
606 -- Check entity that can be publicly referenced (we do not
607 -- give messages for such entities, since there could be
608 -- other units, not involved in this compilation, that
609 -- contain relevant references.
611 and then not Publicly_Referenceable (E1)
613 -- Class wide types are marked as source entities, but
614 -- they are not really source entities, and are always
615 -- created, so we do not care if they are not referenced.
617 and then Ekind (E1) /= E_Class_Wide_Type
619 -- Objects other than parameters of task types are allowed
620 -- to be non-referenced, since they start up tasks!
622 and then ((Ekind (E1) /= E_Variable
623 and then Ekind (E1) /= E_Constant
624 and then Ekind (E1) /= E_Component)
625 or else not Is_Task_Type (Etype (E1)))
627 -- For subunits, only place warnings on the main unit
628 -- itself, since parent units are not completely compiled
630 and then (Nkind (Unit (Cunit (Main_Unit))) /= N_Subunit
631 or else
632 Get_Source_Unit (E1) = Main_Unit)
633 then
634 -- Suppress warnings in internal units if not in -gnatg
635 -- mode (these would be junk warnings for an applications
636 -- program, since they refer to problems in internal units)
638 if GNAT_Mode
639 or else not
640 Is_Internal_File_Name
641 (Unit_File_Name (Get_Source_Unit (E1)))
642 then
643 -- We do not immediately flag the error. This is because
644 -- we have not expanded generic bodies yet, and they may
645 -- have the missing reference. So instead we park the
646 -- entity on a list, for later processing. However, for
647 -- the accept case, post the error right here, since we
648 -- have the information now in this case.
650 if Present (Anod) then
651 Output_Reference_Error ("& is not referenced?");
653 else
654 Unreferenced_Entities.Increment_Last;
655 Unreferenced_Entities.Table
656 (Unreferenced_Entities.Last) := E1;
657 end if;
658 end if;
660 -- Generic units are referenced in the generic body,
661 -- but if they are not public and never instantiated
662 -- we want to force a warning on them. We treat them
663 -- as redundant constructs to minimize noise.
665 elsif Is_Generic_Subprogram (E1)
666 and then not Is_Instantiated (E1)
667 and then not Publicly_Referenceable (E1)
668 and then Instantiation_Depth (Sloc (E1)) = 0
669 and then Warn_On_Redundant_Constructs
670 then
671 Unreferenced_Entities.Increment_Last;
672 Unreferenced_Entities.Table (Unreferenced_Entities.Last) := E1;
674 -- Force warning on entity
676 Set_Referenced (E1, False);
677 end if;
678 end if;
680 -- Recurse into nested package or block. Do not recurse into a
681 -- formal package, because the correponding body is not analyzed.
683 <<Continue>>
684 if ((Ekind (E1) = E_Package or else Ekind (E1) = E_Generic_Package)
685 and then Nkind (Parent (E1)) = N_Package_Specification
686 and then
687 Nkind (Original_Node (Unit_Declaration_Node (E1)))
688 /= N_Formal_Package_Declaration)
690 or else Ekind (E1) = E_Block
691 then
692 Check_References (E1);
693 end if;
695 Next_Entity (E1);
696 end loop;
697 end Check_References;
699 ---------------------------
700 -- Check_Unset_Reference --
701 ---------------------------
703 procedure Check_Unset_Reference (N : Node_Id) is
704 begin
705 -- Nothing to do if warnings suppressed
707 if Warning_Mode = Suppress then
708 return;
709 end if;
711 -- Ignore reference to non-scalar if not from source. Almost always
712 -- such references are bogus (e.g. calls to init procs to set
713 -- default discriminant values).
715 if not Comes_From_Source (N)
716 and then not Is_Scalar_Type (Etype (N))
717 then
718 return;
719 end if;
721 -- Otherwise see what kind of node we have. If the entity already
722 -- has an unset reference, it is not necessarily the earliest in
723 -- the text, because resolution of the prefix of selected components
724 -- is completed before the resolution of the selected component itself.
725 -- as a result, given (R /= null and then R.X > 0), the occurrences
726 -- of R are examined in right-to-left order. If there is already an
727 -- unset reference, we check whether N is earlier before proceeding.
729 case Nkind (N) is
730 when N_Identifier | N_Expanded_Name =>
731 declare
732 E : constant Entity_Id := Entity (N);
734 begin
735 if (Ekind (E) = E_Variable
736 or else Ekind (E) = E_Out_Parameter)
737 and then Never_Set_In_Source (E)
738 and then (No (Unset_Reference (E))
739 or else Earlier_In_Extended_Unit
740 (Sloc (N), Sloc (Unset_Reference (E))))
741 and then not Warnings_Off (E)
742 then
743 -- We may have an unset reference. The first test is
744 -- whether we are accessing a discriminant of a record
745 -- or a component with default initialization. Both of
746 -- these cases can be ignored, since the actual object
747 -- that is referenced is definitely initialized. Note
748 -- that this covers the case of reading discriminants
749 -- of an out parameter, which is OK even in Ada 83.
751 -- Note that we are only interested in a direct reference
752 -- to a record component here. If the reference is via an
753 -- access type, then the access object is being referenced,
754 -- not the record, and still deserves an unset reference.
756 if Nkind (Parent (N)) = N_Selected_Component
757 and not Is_Access_Type (Etype (N))
758 then
759 declare
760 ES : constant Entity_Id :=
761 Entity (Selector_Name (Parent (N)));
763 begin
764 if Ekind (ES) = E_Discriminant
765 or else Present (Expression (Declaration_Node (ES)))
766 then
767 return;
768 end if;
769 end;
770 end if;
772 -- Here we have a potential unset reference. But before we
773 -- get worried about it, we have to make sure that the
774 -- entity declaration is in the same procedure as the
775 -- reference, since if they are in separate procedures,
776 -- then we have no idea about sequential execution.
778 -- The tests in the loop below catch all such cases, but
779 -- do allow the reference to appear in a loop, block, or
780 -- package spec that is nested within the declaring scope.
781 -- As always, it is possible to construct cases where the
782 -- warning is wrong, that is why it is a warning!
784 declare
785 SR : Entity_Id;
786 SE : constant Entity_Id := Scope (E);
788 begin
789 SR := Current_Scope;
790 while SR /= SE loop
791 if SR = Standard_Standard
792 or else Is_Subprogram (SR)
793 or else Is_Concurrent_Body (SR)
794 or else Is_Concurrent_Type (SR)
795 then
796 return;
797 end if;
799 SR := Scope (SR);
800 end loop;
802 -- Case of reference has an access type. This is a
803 -- special case since access types are always set to
804 -- null so cannot be truly uninitialized, but we still
805 -- want to warn about cases of obvious null dereference.
807 if Is_Access_Type (Etype (N)) then
808 declare
809 P : Node_Id;
811 function Process
812 (N : Node_Id)
813 return Traverse_Result;
814 -- Process function for instantation of Traverse
815 -- below. Checks if N contains reference to E
816 -- other than a dereference.
818 function Ref_In (Nod : Node_Id) return Boolean;
819 -- Determines whether Nod contains a reference
820 -- to the entity E that is not a dereference.
822 function Process
823 (N : Node_Id)
824 return Traverse_Result
826 begin
827 if Is_Entity_Name (N)
828 and then Entity (N) = E
829 and then not Is_Dereferenced (N)
830 then
831 return Abandon;
832 else
833 return OK;
834 end if;
835 end Process;
837 function Ref_In (Nod : Node_Id) return Boolean is
838 function Traverse is new Traverse_Func (Process);
840 begin
841 return Traverse (Nod) = Abandon;
842 end Ref_In;
844 begin
845 -- Don't bother if we are inside an instance,
846 -- since the compilation of the generic template
847 -- is where the warning should be issued.
849 if In_Instance then
850 return;
851 end if;
853 -- Don't bother if this is not the main unit.
854 -- If we try to give this warning for with'ed
855 -- units, we get some false positives, since
856 -- we do not record references in other units.
858 if not In_Extended_Main_Source_Unit (E)
859 or else
860 not In_Extended_Main_Source_Unit (N)
861 then
862 return;
863 end if;
865 -- We are only interested in deferences
867 if not Is_Dereferenced (N) then
868 return;
869 end if;
871 -- One more check, don't bother with references
872 -- that are inside conditional statements or while
873 -- loops if the condition references the entity in
874 -- question. This avoids most false positives.
876 P := Parent (N);
877 loop
878 P := Parent (P);
879 exit when No (P);
881 if (Nkind (P) = N_If_Statement
882 or else
883 Nkind (P) = N_Elsif_Part)
884 and then Ref_In (Condition (P))
885 then
886 return;
888 elsif Nkind (P) = N_Loop_Statement
889 and then Present (Iteration_Scheme (P))
890 and then
891 Ref_In (Condition (Iteration_Scheme (P)))
892 then
893 return;
894 end if;
895 end loop;
896 end;
897 end if;
899 -- Here we definitely have a case for giving a warning
900 -- for a reference to an unset value. But we don't give
901 -- the warning now. Instead we set the Unset_Reference
902 -- field of the identifier involved. The reason for this
903 -- is that if we find the variable is never ever assigned
904 -- a value then that warning is more important and there
905 -- is no point in giving the reference warning.
907 -- If this is an identifier, set the field directly
909 if Nkind (N) = N_Identifier then
910 Set_Unset_Reference (E, N);
912 -- Otherwise it is an expanded name, so set the field
913 -- of the actual identifier for the reference.
915 else
916 Set_Unset_Reference (E, Selector_Name (N));
917 end if;
918 end;
919 end if;
920 end;
922 when N_Indexed_Component | N_Slice =>
923 Check_Unset_Reference (Prefix (N));
925 when N_Selected_Component =>
927 if Present (Entity (Selector_Name (N)))
928 and then Ekind (Entity (Selector_Name (N))) = E_Discriminant
929 then
930 -- A discriminant is always initialized
932 null;
934 else
935 Check_Unset_Reference (Prefix (N));
936 end if;
938 when N_Type_Conversion | N_Qualified_Expression =>
939 Check_Unset_Reference (Expression (N));
941 when others =>
942 null;
944 end case;
945 end Check_Unset_Reference;
947 ------------------------
948 -- Check_Unused_Withs --
949 ------------------------
951 procedure Check_Unused_Withs (Spec_Unit : Unit_Number_Type := No_Unit) is
952 Cnode : Node_Id;
953 Item : Node_Id;
954 Lunit : Node_Id;
955 Ent : Entity_Id;
957 Munite : constant Entity_Id := Cunit_Entity (Main_Unit);
958 -- This is needed for checking the special renaming case
960 procedure Check_One_Unit (Unit : Unit_Number_Type);
961 -- Subsidiary procedure, performs checks for specified unit
963 --------------------
964 -- Check_One_Unit --
965 --------------------
967 procedure Check_One_Unit (Unit : Unit_Number_Type) is
968 Is_Visible_Renaming : Boolean := False;
969 Pack : Entity_Id;
971 procedure Check_Inner_Package (Pack : Entity_Id);
972 -- Pack is a package local to a unit in a with_clause. Both the
973 -- unit and Pack are referenced. If none of the entities in Pack
974 -- are referenced, then the only occurrence of Pack is in a use
975 -- clause or a pragma, and a warning is worthwhile as well.
977 function Check_System_Aux return Boolean;
978 -- Before giving a warning on a with_clause for System, check
979 -- whether a system extension is present.
981 function Find_Package_Renaming
982 (P : Entity_Id;
983 L : Entity_Id) return Entity_Id;
984 -- The only reference to a context unit may be in a renaming
985 -- declaration. If this renaming declares a visible entity, do
986 -- not warn that the context clause could be moved to the body,
987 -- because the renaming may be intented to re-export the unit.
989 -------------------------
990 -- Check_Inner_Package --
991 -------------------------
993 procedure Check_Inner_Package (Pack : Entity_Id) is
994 E : Entity_Id;
995 Un : constant Node_Id := Sinfo.Unit (Cnode);
997 function Check_Use_Clause (N : Node_Id) return Traverse_Result;
998 -- If N is a use_clause for Pack, emit warning
1000 procedure Check_Use_Clauses is new
1001 Traverse_Proc (Check_Use_Clause);
1003 ----------------------
1004 -- Check_Use_Clause --
1005 ----------------------
1007 function Check_Use_Clause (N : Node_Id) return Traverse_Result is
1008 Nam : Node_Id;
1010 begin
1011 if Nkind (N) = N_Use_Package_Clause then
1012 Nam := First (Names (N));
1014 while Present (Nam) loop
1015 if Entity (Nam) = Pack then
1016 Error_Msg_Qual_Level := 1;
1017 Error_Msg_NE
1018 ("no entities of package& are referenced?",
1019 Nam, Pack);
1020 Error_Msg_Qual_Level := 0;
1021 end if;
1023 Next (Nam);
1024 end loop;
1025 end if;
1027 return OK;
1028 end Check_Use_Clause;
1030 -- Start of processing for Check_Inner_Package
1032 begin
1033 E := First_Entity (Pack);
1035 while Present (E) loop
1036 if Referenced (E) then
1037 return;
1038 end if;
1040 Next_Entity (E);
1041 end loop;
1043 -- No entities of the package are referenced. Check whether
1044 -- the reference to the package itself is a use clause, and
1045 -- if so place a warning on it.
1047 Check_Use_Clauses (Un);
1048 end Check_Inner_Package;
1050 ----------------------
1051 -- Check_System_Aux --
1052 ----------------------
1054 function Check_System_Aux return Boolean is
1055 Ent : Entity_Id;
1057 begin
1058 if Chars (Lunit) = Name_System
1059 and then Scope (Lunit) = Standard_Standard
1060 and then Present_System_Aux
1061 then
1062 Ent := First_Entity (System_Aux_Id);
1064 while Present (Ent) loop
1065 if Referenced (Ent) then
1066 return True;
1067 end if;
1069 Next_Entity (Ent);
1070 end loop;
1071 end if;
1073 return False;
1074 end Check_System_Aux;
1076 ---------------------------
1077 -- Find_Package_Renaming --
1078 ---------------------------
1080 function Find_Package_Renaming
1081 (P : Entity_Id;
1082 L : Entity_Id) return Entity_Id
1084 E1 : Entity_Id;
1085 R : Entity_Id;
1087 begin
1088 Is_Visible_Renaming := False;
1089 E1 := First_Entity (P);
1091 while Present (E1) loop
1092 if Ekind (E1) = E_Package
1093 and then Renamed_Object (E1) = L
1094 then
1095 Is_Visible_Renaming := not Is_Hidden (E1);
1096 return E1;
1098 elsif Ekind (E1) = E_Package
1099 and then No (Renamed_Object (E1))
1100 and then not Is_Generic_Instance (E1)
1101 then
1102 R := Find_Package_Renaming (E1, L);
1104 if Present (R) then
1105 Is_Visible_Renaming := not Is_Hidden (R);
1106 return R;
1107 end if;
1108 end if;
1110 Next_Entity (E1);
1111 end loop;
1113 return Empty;
1114 end Find_Package_Renaming;
1116 -- Start of processing for Check_One_Unit
1118 begin
1119 Cnode := Cunit (Unit);
1121 -- Only do check in units that are part of the extended main
1122 -- unit. This is actually a necessary restriction, because in
1123 -- the case of subprogram acting as its own specification,
1124 -- there can be with's in subunits that we will not see.
1126 if not In_Extended_Main_Source_Unit (Cnode) then
1127 return;
1129 -- In configurable run time mode, we remove the bodies of
1130 -- non-inlined subprograms, which may lead to spurious warnings,
1131 -- which are clearly undesirable.
1133 elsif Configurable_Run_Time_Mode
1134 and then Is_Predefined_File_Name (Unit_File_Name (Unit))
1135 then
1136 return;
1137 end if;
1139 -- Loop through context items in this unit
1141 Item := First (Context_Items (Cnode));
1142 while Present (Item) loop
1143 if Nkind (Item) = N_With_Clause
1144 and then not Implicit_With (Item)
1145 and then In_Extended_Main_Source_Unit (Item)
1146 then
1147 Lunit := Entity (Name (Item));
1149 -- Check if this unit is referenced
1151 if not Referenced (Lunit) then
1153 -- Suppress warnings in internal units if not in -gnatg
1154 -- mode (these would be junk warnings for an applications
1155 -- program, since they refer to problems in internal units)
1157 if GNAT_Mode
1158 or else not Is_Internal_File_Name (Unit_File_Name (Unit))
1159 then
1160 -- Here we definitely have a non-referenced unit. If
1161 -- it is the special call for a spec unit, then just
1162 -- set the flag to be read later.
1164 if Unit = Spec_Unit then
1165 Set_Unreferenced_In_Spec (Item);
1167 -- Otherwise simple unreferenced message
1169 else
1170 Error_Msg_N
1171 ("unit& is not referenced?", Name (Item));
1172 end if;
1173 end if;
1175 -- If main unit is a renaming of this unit, then we consider
1176 -- the with to be OK (obviously it is needed in this case!)
1178 elsif Present (Renamed_Entity (Munite))
1179 and then Renamed_Entity (Munite) = Lunit
1180 then
1181 null;
1183 -- If this unit is referenced, and it is a package, we
1184 -- do another test, to see if any of the entities in the
1185 -- package are referenced. If none of the entities are
1186 -- referenced, we still post a warning. This occurs if
1187 -- the only use of the package is in a use clause, or
1188 -- in a package renaming declaration.
1190 elsif Ekind (Lunit) = E_Package then
1192 -- If Is_Instantiated is set, it means that the package
1193 -- is implicitly instantiated (this is the case of a
1194 -- parent instance or an actual for a generic package
1195 -- formal), and this counts as a reference.
1197 if Is_Instantiated (Lunit) then
1198 null;
1200 -- If no entities in package, and there is a pragma
1201 -- Elaborate_Body present, then assume that this with
1202 -- is done for purposes of this elaboration.
1204 elsif No (First_Entity (Lunit))
1205 and then Has_Pragma_Elaborate_Body (Lunit)
1206 then
1207 null;
1209 -- Otherwise see if any entities have been referenced
1211 else
1212 Ent := First_Entity (Lunit);
1213 loop
1214 -- No more entities, and we did not find one
1215 -- that was referenced. Means we have a definite
1216 -- case of a with none of whose entities was
1217 -- referenced.
1219 if No (Ent) then
1221 -- If in spec, just set the flag
1223 if Unit = Spec_Unit then
1224 Set_No_Entities_Ref_In_Spec (Item);
1226 elsif Check_System_Aux then
1227 null;
1229 -- Else give the warning
1231 else
1232 Error_Msg_N
1233 ("no entities of & are referenced?",
1234 Name (Item));
1236 -- Look for renamings of this package, and
1237 -- flag them as well. If the original package
1238 -- has warnings off, we suppress the warning
1239 -- on the renaming as well.
1241 Pack := Find_Package_Renaming (Munite, Lunit);
1243 if Present (Pack)
1244 and then not Warnings_Off (Lunit)
1245 then
1246 Error_Msg_NE
1247 ("no entities of & are referenced?",
1248 Unit_Declaration_Node (Pack),
1249 Pack);
1250 end if;
1251 end if;
1253 exit;
1255 -- Case of next entity is referenced
1257 elsif Referenced (Ent)
1258 or else Referenced_As_LHS (Ent)
1259 then
1260 -- This means that the with is indeed fine, in
1261 -- that it is definitely needed somewhere, and
1262 -- we can quite worrying about this one.
1264 -- Except for one little detail, if either of
1265 -- the flags was set during spec processing,
1266 -- this is where we complain that the with
1267 -- could be moved from the spec. If the spec
1268 -- contains a visible renaming of the package,
1269 -- inhibit warning to move with_clause to body.
1271 if Ekind (Munite) = E_Package_Body then
1272 Pack :=
1273 Find_Package_Renaming
1274 (Spec_Entity (Munite), Lunit);
1275 end if;
1277 if Unreferenced_In_Spec (Item) then
1278 Error_Msg_N
1279 ("unit& is not referenced in spec?",
1280 Name (Item));
1282 elsif No_Entities_Ref_In_Spec (Item) then
1283 Error_Msg_N
1284 ("no entities of & are referenced in spec?",
1285 Name (Item));
1287 else
1288 if Ekind (Ent) = E_Package then
1289 Check_Inner_Package (Ent);
1290 end if;
1292 exit;
1293 end if;
1295 if not Is_Visible_Renaming then
1296 Error_Msg_N
1297 ("\with clause might be moved to body?",
1298 Name (Item));
1299 end if;
1301 exit;
1303 -- Move to next entity to continue search
1305 else
1306 Next_Entity (Ent);
1307 end if;
1308 end loop;
1309 end if;
1311 -- For a generic package, the only interesting kind of
1312 -- reference is an instantiation, since entities cannot
1313 -- be referenced directly.
1315 elsif Is_Generic_Unit (Lunit) then
1317 -- Unit was never instantiated, set flag for case of spec
1318 -- call, or give warning for normal call.
1320 if not Is_Instantiated (Lunit) then
1321 if Unit = Spec_Unit then
1322 Set_Unreferenced_In_Spec (Item);
1323 else
1324 Error_Msg_N
1325 ("unit& is never instantiated?", Name (Item));
1326 end if;
1328 -- If unit was indeed instantiated, make sure that
1329 -- flag is not set showing it was uninstantiated in
1330 -- the spec, and if so, give warning.
1332 elsif Unreferenced_In_Spec (Item) then
1333 Error_Msg_N
1334 ("unit& is not instantiated in spec?", Name (Item));
1335 Error_Msg_N
1336 ("\with clause can be moved to body?", Name (Item));
1337 end if;
1338 end if;
1339 end if;
1341 Next (Item);
1342 end loop;
1344 end Check_One_Unit;
1346 -- Start of processing for Check_Unused_Withs
1348 begin
1349 if not Opt.Check_Withs
1350 or else Operating_Mode = Check_Syntax
1351 then
1352 return;
1353 end if;
1355 -- Flag any unused with clauses, but skip this step if we are
1356 -- compiling a subunit on its own, since we do not have enough
1357 -- information to determine whether with's are used. We will get
1358 -- the relevant warnings when we compile the parent. This is the
1359 -- normal style of GNAT compilation in any case.
1361 if Nkind (Unit (Cunit (Main_Unit))) = N_Subunit then
1362 return;
1363 end if;
1365 -- Process specified units
1367 if Spec_Unit = No_Unit then
1369 -- For main call, check all units
1371 for Unit in Main_Unit .. Last_Unit loop
1372 Check_One_Unit (Unit);
1373 end loop;
1375 else
1376 -- For call for spec, check only the spec
1378 Check_One_Unit (Spec_Unit);
1379 end if;
1380 end Check_Unused_Withs;
1382 ---------------------------------
1383 -- Generic_Package_Spec_Entity --
1384 ---------------------------------
1386 function Generic_Package_Spec_Entity (E : Entity_Id) return Boolean is
1387 S : Entity_Id;
1389 begin
1390 if Is_Package_Body_Entity (E) then
1391 return False;
1393 else
1394 S := Scope (E);
1396 loop
1397 if S = Standard_Standard then
1398 return False;
1400 elsif Ekind (S) = E_Generic_Package then
1401 return True;
1403 elsif Ekind (S) = E_Package then
1404 S := Scope (S);
1406 else
1407 return False;
1408 end if;
1409 end loop;
1410 end if;
1411 end Generic_Package_Spec_Entity;
1413 -------------------------------------
1414 -- Operand_Has_Warnings_Suppressed --
1415 -------------------------------------
1417 function Operand_Has_Warnings_Suppressed (N : Node_Id) return Boolean is
1419 function Check_For_Warnings (N : Node_Id) return Traverse_Result;
1420 -- Function used to check one node to see if it is or was originally
1421 -- a reference to an entity for which Warnings are off. If so, Abandon
1422 -- is returned, otherwise OK_Orig is returned to continue the traversal
1423 -- of the original expression.
1425 function Traverse is new Traverse_Func (Check_For_Warnings);
1426 -- Function used to traverse tree looking for warnings
1428 ------------------------
1429 -- Check_For_Warnings --
1430 ------------------------
1432 function Check_For_Warnings (N : Node_Id) return Traverse_Result is
1433 R : constant Node_Id := Original_Node (N);
1435 begin
1436 if Nkind (R) in N_Has_Entity
1437 and then Present (Entity (R))
1438 and then Warnings_Off (Entity (R))
1439 then
1440 return Abandon;
1441 else
1442 return OK_Orig;
1443 end if;
1444 end Check_For_Warnings;
1446 -- Start of processing for Operand_Has_Warnings_Suppressed
1448 begin
1449 return Traverse (N) = Abandon;
1451 -- If any exception occurs, then something has gone wrong, and this is
1452 -- only a minor aesthetic issue anyway, so just say we did not find what
1453 -- we are looking for, rather than blow up.
1455 exception
1456 when others =>
1457 return False;
1458 end Operand_Has_Warnings_Suppressed;
1460 ----------------------------------
1461 -- Output_Unreferenced_Messages --
1462 ----------------------------------
1464 procedure Output_Unreferenced_Messages is
1465 E : Entity_Id;
1467 begin
1468 for J in Unreferenced_Entities.First ..
1469 Unreferenced_Entities.Last
1470 loop
1471 E := Unreferenced_Entities.Table (J);
1473 if not Referenced (E) and then not Warnings_Off (E) then
1474 case Ekind (E) is
1475 when E_Variable =>
1477 -- Case of variable that is assigned but not read. We
1478 -- suppress the message if the variable is volatile,
1479 -- has an address clause, or is imported.
1481 if Referenced_As_LHS (E)
1482 and then No (Address_Clause (E))
1483 and then not Is_Volatile (E)
1484 then
1485 if Warn_On_Modified_Unread
1486 and then not Is_Imported (E)
1488 -- Suppress the message for aliased or renamed
1489 -- variables, since there may be other entities
1490 -- read the same memory location.
1492 and then not Is_Aliased (E)
1493 and then No (Renamed_Object (E))
1495 then
1496 Error_Msg_N
1497 ("variable & is assigned but never read?", E);
1498 end if;
1500 -- Normal case of neither assigned nor read
1502 else
1503 if Present (Renamed_Object (E))
1504 and then Comes_From_Source (Renamed_Object (E))
1505 then
1506 Error_Msg_N
1507 ("renamed variable & is not referenced?", E);
1508 else
1509 Error_Msg_N
1510 ("variable & is not referenced?", E);
1511 end if;
1512 end if;
1514 when E_Constant =>
1515 if Present (Renamed_Object (E))
1516 and then Comes_From_Source (Renamed_Object (E))
1517 then
1518 Error_Msg_N ("renamed constant & is not referenced?", E);
1519 else
1520 Error_Msg_N ("constant & is not referenced?", E);
1521 end if;
1523 when E_In_Parameter |
1524 E_Out_Parameter |
1525 E_In_Out_Parameter =>
1527 -- Do not emit message for formals of a renaming, because
1528 -- they are never referenced explicitly.
1530 if Nkind (Original_Node (Unit_Declaration_Node (Scope (E))))
1531 /= N_Subprogram_Renaming_Declaration
1532 then
1533 Error_Msg_N ("formal parameter & is not referenced?", E);
1534 end if;
1536 when E_Named_Integer |
1537 E_Named_Real =>
1538 Error_Msg_N ("named number & is not referenced?", E);
1540 when E_Enumeration_Literal =>
1541 Error_Msg_N ("literal & is not referenced?", E);
1543 when E_Function =>
1544 Error_Msg_N ("function & is not referenced?", E);
1546 when E_Procedure =>
1547 Error_Msg_N ("procedure & is not referenced?", E);
1549 when E_Generic_Procedure =>
1550 Error_Msg_N
1551 ("generic procedure & is never instantiated?", E);
1553 when E_Generic_Function =>
1554 Error_Msg_N ("generic function & is never instantiated?", E);
1556 when Type_Kind =>
1557 Error_Msg_N ("type & is not referenced?", E);
1559 when others =>
1560 Error_Msg_N ("& is not referenced?", E);
1561 end case;
1563 Set_Warnings_Off (E);
1564 end if;
1565 end loop;
1566 end Output_Unreferenced_Messages;
1568 -----------------------------
1569 -- Warn_On_Known_Condition --
1570 -----------------------------
1572 procedure Warn_On_Known_Condition (C : Node_Id) is
1573 P : Node_Id;
1575 begin
1576 -- Argument replacement in an inlined body can make conditions
1577 -- static. Do not emit warnings in this case.
1579 if In_Inlined_Body then
1580 return;
1581 end if;
1583 if Constant_Condition_Warnings
1584 and then Nkind (C) = N_Identifier
1585 and then
1586 (Entity (C) = Standard_False or else Entity (C) = Standard_True)
1587 and then Comes_From_Source (Original_Node (C))
1588 and then not In_Instance
1589 then
1590 -- See if this is in a statement or a declaration
1592 P := Parent (C);
1593 loop
1594 -- If tree is not attached, do not issue warning (this is very
1595 -- peculiar, and probably arises from some other error condition)
1597 if No (P) then
1598 return;
1600 -- If we are in a declaration, then no warning, since in practice
1601 -- conditionals in declarations are used for intended tests which
1602 -- may be known at compile time, e.g. things like
1604 -- x : constant Integer := 2 + (Word'Size = 32);
1606 -- And a warning is annoying in such cases
1608 elsif Nkind (P) in N_Declaration
1609 or else
1610 Nkind (P) in N_Later_Decl_Item
1611 then
1612 return;
1614 -- Don't warn in assert pragma, since presumably tests in such
1615 -- a context are very definitely intended, and might well be
1616 -- known at compile time. Note that we have to test the original
1617 -- node, since assert pragmas get rewritten at analysis time.
1619 elsif Nkind (Original_Node (P)) = N_Pragma
1620 and then Chars (Original_Node (P)) = Name_Assert
1621 then
1622 return;
1623 end if;
1625 exit when Is_Statement (P);
1626 P := Parent (P);
1627 end loop;
1629 -- Here we issue the warning unless some sub-operand has warnings
1630 -- set off, in which case we suppress the warning for the node.
1632 if not Operand_Has_Warnings_Suppressed (C) then
1633 if Entity (C) = Standard_True then
1634 Error_Msg_N ("condition is always True?", C);
1635 else
1636 Error_Msg_N ("condition is always False?", C);
1637 end if;
1638 end if;
1639 end if;
1640 end Warn_On_Known_Condition;
1642 end Sem_Warn;