[RS6000] Don't be too clever with dg-do run and dg-do compile
[official-gcc.git] / gcc / ada / sem_warn.adb
blob7289ea78e4594bc63eeb615b349a1280bd035d1a
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-2020, 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 3, 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Debug; use Debug;
28 with Einfo; use Einfo;
29 with Errout; use Errout;
30 with Exp_Code; use Exp_Code;
31 with Lib; use Lib;
32 with Lib.Xref; use Lib.Xref;
33 with Namet; use Namet;
34 with Nlists; use Nlists;
35 with Opt; use Opt;
36 with Par_SCO; use Par_SCO;
37 with Rtsfind; use Rtsfind;
38 with Sem; use Sem;
39 with Sem_Ch8; use Sem_Ch8;
40 with Sem_Aux; use Sem_Aux;
41 with Sem_Eval; use Sem_Eval;
42 with Sem_Prag; use Sem_Prag;
43 with Sem_Util; use Sem_Util;
44 with Sinfo; use Sinfo;
45 with Sinput; use Sinput;
46 with Snames; use Snames;
47 with Stand; use Stand;
48 with Stringt; use Stringt;
49 with Tbuild; use Tbuild;
50 with Uintp; use Uintp;
52 package body Sem_Warn is
54 -- The following table collects Id's of entities that are potentially
55 -- unreferenced. See Check_Unset_Reference for further details.
56 -- ??? Check_Unset_Reference has zero information about this table.
58 package Unreferenced_Entities is new Table.Table (
59 Table_Component_Type => Entity_Id,
60 Table_Index_Type => Nat,
61 Table_Low_Bound => 1,
62 Table_Initial => Alloc.Unreferenced_Entities_Initial,
63 Table_Increment => Alloc.Unreferenced_Entities_Increment,
64 Table_Name => "Unreferenced_Entities");
66 -- The following table collects potential warnings for IN OUT parameters
67 -- that are referenced but not modified. These warnings are processed when
68 -- the front end calls the procedure Output_Non_Modified_In_Out_Warnings.
69 -- The reason that we defer output of these messages is that we want to
70 -- detect the case where the relevant procedure is used as a generic actual
71 -- in an instantiation, since we suppress the warnings in this case. The
72 -- flag Used_As_Generic_Actual will be set in this case, but only at the
73 -- point of usage. Similarly, we suppress the message if the address of the
74 -- procedure is taken, where the flag Address_Taken may be set later.
76 package In_Out_Warnings is new Table.Table (
77 Table_Component_Type => Entity_Id,
78 Table_Index_Type => Nat,
79 Table_Low_Bound => 1,
80 Table_Initial => Alloc.In_Out_Warnings_Initial,
81 Table_Increment => Alloc.In_Out_Warnings_Increment,
82 Table_Name => "In_Out_Warnings");
84 --------------------------------------------------------
85 -- Handling of Warnings Off, Unmodified, Unreferenced --
86 --------------------------------------------------------
88 -- The functions Has_Warnings_Off, Has_Unmodified, Has_Unreferenced must
89 -- generally be used instead of Warnings_Off, Has_Pragma_Unmodified and
90 -- Has_Pragma_Unreferenced, as noted in the specs in Einfo.
92 -- In order to avoid losing warnings in -gnatw.w (warn on unnecessary
93 -- warnings off pragma) mode, i.e. to avoid false negatives, the code
94 -- must follow some important rules.
96 -- Call these functions as late as possible, after completing all other
97 -- tests, just before the warnings is given. For example, don't write:
99 -- if not Has_Warnings_Off (E)
100 -- and then some-other-predicate-on-E then ..
102 -- Instead the following is preferred
104 -- if some-other-predicate-on-E
105 -- and then Has_Warnings_Off (E)
107 -- This way if some-other-predicate is false, we avoid a false indication
108 -- that a Warnings (Off, E) pragma was useful in preventing a warning.
110 -- The second rule is that if both Has_Unmodified and Has_Warnings_Off, or
111 -- Has_Unreferenced and Has_Warnings_Off are called, make sure that the
112 -- call to Has_Unmodified/Has_Unreferenced comes first, this way we record
113 -- that the Warnings (Off) could have been Unreferenced or Unmodified. In
114 -- fact Has_Unmodified/Has_Unreferenced includes a test for Warnings Off,
115 -- and so a subsequent test is not needed anyway (though it is harmless).
117 -----------------------
118 -- Local Subprograms --
119 -----------------------
121 function Generic_Package_Spec_Entity (E : Entity_Id) return Boolean;
122 -- This returns true if the entity E is declared within a generic package.
123 -- The point of this is to detect variables which are not assigned within
124 -- the generic, but might be assigned outside the package for any given
125 -- instance. These are cases where we leave the warnings to be posted for
126 -- the instance, when we will know more.
128 function Goto_Spec_Entity (E : Entity_Id) return Entity_Id;
129 -- If E is a parameter entity for a subprogram body, then this function
130 -- returns the corresponding spec entity, if not, E is returned unchanged.
132 function Has_Pragma_Unmodified_Check_Spec (E : Entity_Id) return Boolean;
133 -- Tests Has_Pragma_Unmodified flag for entity E. If E is not a formal,
134 -- this is simply the setting of the flag Has_Pragma_Unmodified. If E is
135 -- a body formal, the setting of the flag in the corresponding spec is
136 -- also checked (and True returned if either flag is True).
138 function Has_Pragma_Unreferenced_Check_Spec (E : Entity_Id) return Boolean;
139 -- Tests Has_Pragma_Unreferenced flag for entity E. If E is not a formal,
140 -- this is simply the setting of the flag Has_Pragma_Unreferenced. If E is
141 -- a body formal, the setting of the flag in the corresponding spec is
142 -- also checked (and True returned if either flag is True).
144 function Is_Attribute_And_Known_Value_Comparison
145 (Op : Node_Id) return Boolean;
146 -- Determine whether operator Op denotes a comparison where the left
147 -- operand is an attribute reference and the value of the right operand is
148 -- known at compile time.
150 function Never_Set_In_Source_Check_Spec (E : Entity_Id) return Boolean;
151 -- Tests Never_Set_In_Source status for entity E. If E is not a formal,
152 -- this is simply the setting of the flag Never_Set_In_Source. If E is
153 -- a body formal, the setting of the flag in the corresponding spec is
154 -- also checked (and False returned if either flag is False).
156 function Operand_Has_Warnings_Suppressed (N : Node_Id) return Boolean;
157 -- This function traverses the expression tree represented by the node N
158 -- and determines if any sub-operand is a reference to an entity for which
159 -- the Warnings_Off flag is set. True is returned if such an entity is
160 -- encountered, and False otherwise.
162 function Referenced_Check_Spec (E : Entity_Id) return Boolean;
163 -- Tests Referenced status for entity E. If E is not a formal, this is
164 -- simply the setting of the flag Referenced. If E is a body formal, the
165 -- setting of the flag in the corresponding spec is also checked (and True
166 -- returned if either flag is True).
168 function Referenced_As_LHS_Check_Spec (E : Entity_Id) return Boolean;
169 -- Tests Referenced_As_LHS status for entity E. If E is not a formal, this
170 -- is simply the setting of the flag Referenced_As_LHS. If E is a body
171 -- formal, the setting of the flag in the corresponding spec is also
172 -- checked (and True returned if either flag is True).
174 function Referenced_As_Out_Parameter_Check_Spec
175 (E : Entity_Id) return Boolean;
176 -- Tests Referenced_As_Out_Parameter status for entity E. If E is not a
177 -- formal, this is simply the setting of Referenced_As_Out_Parameter. If E
178 -- is a body formal, the setting of the flag in the corresponding spec is
179 -- also checked (and True returned if either flag is True).
181 procedure Warn_On_Unreferenced_Entity
182 (Spec_E : Entity_Id;
183 Body_E : Entity_Id := Empty);
184 -- Output warnings for unreferenced entity E. For the case of an entry
185 -- formal, Body_E is the corresponding body entity for a particular
186 -- accept statement, and the message is posted on Body_E. In all other
187 -- cases, Body_E is ignored and must be Empty.
189 function Warnings_Off_Check_Spec (E : Entity_Id) return Boolean;
190 -- Returns True if Warnings_Off is set for the entity E or (in the case
191 -- where there is a Spec_Entity), Warnings_Off is set for the Spec_Entity.
193 --------------------------
194 -- Check_Code_Statement --
195 --------------------------
197 procedure Check_Code_Statement (N : Node_Id) is
198 begin
199 -- If volatile, nothing to worry about
201 if Is_Asm_Volatile (N) then
202 return;
203 end if;
205 -- Warn if no input or no output
207 Setup_Asm_Inputs (N);
209 if No (Asm_Input_Value) then
210 Error_Msg_F
211 ("??code statement with no inputs should usually be Volatile!", N);
212 return;
213 end if;
215 Setup_Asm_Outputs (N);
217 if No (Asm_Output_Variable) then
218 Error_Msg_F
219 ("??code statement with no outputs should usually be Volatile!", N);
220 return;
221 end if;
222 end Check_Code_Statement;
224 ---------------------------------
225 -- Check_Infinite_Loop_Warning --
226 ---------------------------------
228 -- The case we look for is a while loop which tests a local variable, where
229 -- there is no obvious direct or possible indirect update of the variable
230 -- within the body of the loop.
232 procedure Check_Infinite_Loop_Warning (Loop_Statement : Node_Id) is
233 Expression : Node_Id := Empty;
234 -- Set to WHILE or EXIT WHEN condition to be tested
236 Ref : Node_Id := Empty;
237 -- Reference in Expression to variable that might not be modified
238 -- in loop, indicating a possible infinite loop.
240 Var : Entity_Id := Empty;
241 -- Corresponding entity (entity of Ref)
243 Function_Call_Found : Boolean := False;
244 -- True if Find_Var found a function call in the condition
246 procedure Find_Var (N : Node_Id);
247 -- Inspect condition to see if it depends on a single entity reference.
248 -- If so, Ref is set to point to the reference node, and Var is set to
249 -- the referenced Entity.
251 function Has_Condition_Actions (Iter : Node_Id) return Boolean;
252 -- Determine whether iteration scheme Iter has meaningful condition
253 -- actions.
255 function Has_Indirection (T : Entity_Id) return Boolean;
256 -- If the controlling variable is an access type, or is a record type
257 -- with access components, assume that it is changed indirectly and
258 -- suppress the warning. As a concession to low-level programming, in
259 -- particular within Declib, we also suppress warnings on a record
260 -- type that contains components of type Address or Short_Address.
262 function Is_Suspicious_Function_Name (E : Entity_Id) return Boolean;
263 -- Given an entity name, see if the name appears to have something to
264 -- do with I/O or network stuff, and if so, return True. Used to kill
265 -- some false positives on a heuristic basis that such functions will
266 -- likely have some strange side effect dependencies. A rather strange
267 -- test, but warning messages are in the heuristics business.
269 function Test_Ref (N : Node_Id) return Traverse_Result;
270 -- Test for reference to variable in question. Returns Abandon if
271 -- matching reference found. Used in instantiation of No_Ref_Found.
273 function No_Ref_Found is new Traverse_Func (Test_Ref);
274 -- Function to traverse body of procedure. Returns Abandon if matching
275 -- reference found.
277 --------------
278 -- Find_Var --
279 --------------
281 procedure Find_Var (N : Node_Id) is
282 begin
283 -- Condition is a direct variable reference
285 if Is_Entity_Name (N) then
286 Ref := N;
287 Var := Entity (Ref);
289 -- Case of condition is a comparison with compile time known value
291 elsif Nkind (N) in N_Op_Compare then
292 if Compile_Time_Known_Value (Right_Opnd (N)) then
293 Find_Var (Left_Opnd (N));
295 elsif Compile_Time_Known_Value (Left_Opnd (N)) then
296 Find_Var (Right_Opnd (N));
298 -- Ignore any other comparison
300 else
301 return;
302 end if;
304 -- If condition is a negation, check its operand
306 elsif Nkind (N) = N_Op_Not then
307 Find_Var (Right_Opnd (N));
309 -- Case of condition is function call
311 elsif Nkind (N) = N_Function_Call then
313 Function_Call_Found := True;
315 -- Forget it if function name is not entity, who knows what
316 -- we might be calling?
318 if not Is_Entity_Name (Name (N)) then
319 return;
321 -- Forget it if function name is suspicious. A strange test
322 -- but warning generation is in the heuristics business.
324 elsif Is_Suspicious_Function_Name (Entity (Name (N))) then
325 return;
327 -- Forget it if function is marked Volatile_Function
329 elsif Is_Volatile_Function (Entity (Name (N))) then
330 return;
332 -- Forget it if warnings are suppressed on function entity
334 elsif Has_Warnings_Off (Entity (Name (N))) then
335 return;
337 -- Forget it if the parameter is not In
339 elsif Has_Out_Or_In_Out_Parameter (Entity (Name (N))) then
340 return;
341 end if;
343 -- OK, see if we have one argument
345 declare
346 PA : constant List_Id := Parameter_Associations (N);
348 begin
349 -- One argument, so check the argument
351 if Present (PA) and then List_Length (PA) = 1 then
352 if Nkind (First (PA)) = N_Parameter_Association then
353 Find_Var (Explicit_Actual_Parameter (First (PA)));
354 else
355 Find_Var (First (PA));
356 end if;
358 -- Not one argument
360 else
361 return;
362 end if;
363 end;
365 -- Any other kind of node is not something we warn for
367 else
368 return;
369 end if;
370 end Find_Var;
372 ---------------------------
373 -- Has_Condition_Actions --
374 ---------------------------
376 function Has_Condition_Actions (Iter : Node_Id) return Boolean is
377 Action : Node_Id;
379 begin
380 -- A call marker is not considered a meaningful action because it
381 -- acts as an annotation and has no runtime semantics.
383 Action := First (Condition_Actions (Iter));
384 while Present (Action) loop
385 if Nkind (Action) /= N_Call_Marker then
386 return True;
387 end if;
389 Next (Action);
390 end loop;
392 return False;
393 end Has_Condition_Actions;
395 ---------------------
396 -- Has_Indirection --
397 ---------------------
399 function Has_Indirection (T : Entity_Id) return Boolean is
400 Comp : Entity_Id;
401 Rec : Entity_Id;
403 begin
404 if Is_Access_Type (T) then
405 return True;
407 elsif Is_Private_Type (T)
408 and then Present (Full_View (T))
409 and then Is_Access_Type (Full_View (T))
410 then
411 return True;
413 elsif Is_Record_Type (T) then
414 Rec := T;
416 elsif Is_Private_Type (T)
417 and then Present (Full_View (T))
418 and then Is_Record_Type (Full_View (T))
419 then
420 Rec := Full_View (T);
421 else
422 return False;
423 end if;
425 Comp := First_Component (Rec);
426 while Present (Comp) loop
427 if Is_Access_Type (Etype (Comp))
428 or else Is_Descendant_Of_Address (Etype (Comp))
429 then
430 return True;
431 end if;
433 Next_Component (Comp);
434 end loop;
436 return False;
437 end Has_Indirection;
439 ---------------------------------
440 -- Is_Suspicious_Function_Name --
441 ---------------------------------
443 function Is_Suspicious_Function_Name (E : Entity_Id) return Boolean is
444 S : Entity_Id;
446 function Substring_Present (S : String) return Boolean;
447 -- Returns True if name buffer has given string delimited by non-
448 -- alphabetic characters or by end of string. S is lower case.
450 -----------------------
451 -- Substring_Present --
452 -----------------------
454 function Substring_Present (S : String) return Boolean is
455 Len : constant Natural := S'Length;
457 begin
458 for J in 1 .. Name_Len - (Len - 1) loop
459 if Name_Buffer (J .. J + (Len - 1)) = S
460 and then (J = 1 or else Name_Buffer (J - 1) not in 'a' .. 'z')
461 and then
462 (J + Len > Name_Len
463 or else Name_Buffer (J + Len) not in 'a' .. 'z')
464 then
465 return True;
466 end if;
467 end loop;
469 return False;
470 end Substring_Present;
472 -- Start of processing for Is_Suspicious_Function_Name
474 begin
475 S := E;
476 while Present (S) and then S /= Standard_Standard loop
477 Get_Name_String (Chars (S));
479 if Substring_Present ("io")
480 or else Substring_Present ("file")
481 or else Substring_Present ("network")
482 then
483 return True;
484 else
485 S := Scope (S);
486 end if;
487 end loop;
489 return False;
490 end Is_Suspicious_Function_Name;
492 --------------
493 -- Test_Ref --
494 --------------
496 function Test_Ref (N : Node_Id) return Traverse_Result is
497 begin
498 -- Waste of time to look at the expression we are testing
500 if N = Expression then
501 return Skip;
503 -- Direct reference to variable in question
505 elsif Is_Entity_Name (N)
506 and then Present (Entity (N))
507 and then Entity (N) = Var
508 then
509 -- If this is an lvalue, then definitely abandon, since
510 -- this could be a direct modification of the variable.
512 if May_Be_Lvalue (N) then
513 return Abandon;
514 end if;
516 -- If the condition contains a function call, we consider it may
517 -- be modified by side effects from a procedure call. Otherwise,
518 -- we consider the condition may not be modified, although that
519 -- might happen if Variable is itself a by-reference parameter,
520 -- and the procedure called modifies the global object referred to
521 -- by Variable, but we actually prefer to issue a warning in this
522 -- odd case. Note that the case where the procedure called has
523 -- visibility over Variable is treated in another case below.
525 if Function_Call_Found then
526 declare
527 P : Node_Id;
529 begin
530 P := N;
531 loop
532 P := Parent (P);
533 exit when P = Loop_Statement;
535 -- Abandon if at procedure call, or something strange is
536 -- going on (perhaps a node with no parent that should
537 -- have one but does not?) As always, for a warning we
538 -- prefer to just abandon the warning than get into the
539 -- business of complaining about the tree structure here.
541 if No (P)
542 or else Nkind (P) = N_Procedure_Call_Statement
543 then
544 return Abandon;
545 end if;
546 end loop;
547 end;
548 end if;
550 -- Reference to variable renaming variable in question
552 elsif Is_Entity_Name (N)
553 and then Present (Entity (N))
554 and then Ekind (Entity (N)) = E_Variable
555 and then Present (Renamed_Object (Entity (N)))
556 and then Is_Entity_Name (Renamed_Object (Entity (N)))
557 and then Entity (Renamed_Object (Entity (N))) = Var
558 and then May_Be_Lvalue (N)
559 then
560 return Abandon;
562 -- Call to subprogram
564 elsif Nkind (N) in N_Subprogram_Call then
566 -- If subprogram is within the scope of the entity we are dealing
567 -- with as the loop variable, then it could modify this parameter,
568 -- so we abandon in this case. In the case of a subprogram that is
569 -- not an entity we also abandon. The check for no entity being
570 -- present is a defense against previous errors.
572 if not Is_Entity_Name (Name (N))
573 or else No (Entity (Name (N)))
574 or else Scope_Within (Entity (Name (N)), Scope (Var))
575 then
576 return Abandon;
577 end if;
579 -- If any of the arguments are of type access to subprogram, then
580 -- we may have funny side effects, so no warning in this case.
582 declare
583 Actual : Node_Id;
584 begin
585 Actual := First_Actual (N);
586 while Present (Actual) loop
587 if Is_Access_Subprogram_Type (Etype (Actual)) then
588 return Abandon;
589 else
590 Next_Actual (Actual);
591 end if;
592 end loop;
593 end;
595 -- Declaration of the variable in question
597 elsif Nkind (N) = N_Object_Declaration
598 and then Defining_Identifier (N) = Var
599 then
600 return Abandon;
601 end if;
603 -- All OK, continue scan
605 return OK;
606 end Test_Ref;
608 -- Start of processing for Check_Infinite_Loop_Warning
610 begin
611 -- Skip processing if debug flag gnatd.w is set
613 if Debug_Flag_Dot_W then
614 return;
615 end if;
617 -- Deal with Iteration scheme present
619 declare
620 Iter : constant Node_Id := Iteration_Scheme (Loop_Statement);
622 begin
623 if Present (Iter) then
625 -- While iteration
627 if Present (Condition (Iter)) then
629 -- Skip processing for while iteration with conditions actions,
630 -- since they make it too complicated to get the warning right.
632 if Has_Condition_Actions (Iter) then
633 return;
634 end if;
636 -- Capture WHILE condition
638 Expression := Condition (Iter);
640 -- For Loop_Parameter_Specification, do not process, since loop
641 -- will always terminate. For Iterator_Specification, also do not
642 -- process. Either it will always terminate (e.g. "for X of
643 -- Some_Array ..."), or we can't tell if it's going to terminate
644 -- without looking at the iterator, so any warning here would be
645 -- noise.
647 elsif Present (Loop_Parameter_Specification (Iter))
648 or else Present (Iterator_Specification (Iter))
649 then
650 return;
651 end if;
652 end if;
653 end;
655 -- Check chain of EXIT statements, we only process loops that have a
656 -- single exit condition (either a single EXIT WHEN statement, or a
657 -- WHILE loop not containing any EXIT WHEN statements).
659 declare
660 Ident : constant Node_Id := Identifier (Loop_Statement);
661 Exit_Stmt : Node_Id;
663 begin
664 -- If we don't have a proper chain set, ignore call entirely. This
665 -- happens because of previous errors.
667 if No (Entity (Ident))
668 or else Ekind (Entity (Ident)) /= E_Loop
669 then
670 Check_Error_Detected;
671 return;
672 end if;
674 -- Otherwise prepare to scan list of EXIT statements
676 Exit_Stmt := First_Exit_Statement (Entity (Ident));
677 while Present (Exit_Stmt) loop
679 -- Check for EXIT WHEN
681 if Present (Condition (Exit_Stmt)) then
683 -- Quit processing if EXIT WHEN in WHILE loop, or more than
684 -- one EXIT WHEN statement present in the loop.
686 if Present (Expression) then
687 return;
689 -- Otherwise capture condition from EXIT WHEN statement
691 else
692 Expression := Condition (Exit_Stmt);
693 end if;
695 -- If an unconditional exit statement is the last statement in the
696 -- loop, assume that no warning is needed, without any attempt at
697 -- checking whether the exit is reachable.
699 elsif Exit_Stmt = Last (Statements (Loop_Statement)) then
700 return;
701 end if;
703 Exit_Stmt := Next_Exit_Statement (Exit_Stmt);
704 end loop;
705 end;
707 -- Return if no condition to test
709 if No (Expression) then
710 return;
711 end if;
713 -- Initial conditions met, see if condition is of right form
715 Find_Var (Expression);
717 -- Nothing to do if local variable from source not found. If it's a
718 -- renaming, it is probably renaming something too complicated to deal
719 -- with here.
721 if No (Var)
722 or else Ekind (Var) /= E_Variable
723 or else Is_Library_Level_Entity (Var)
724 or else not Comes_From_Source (Var)
725 or else Nkind (Parent (Var)) = N_Object_Renaming_Declaration
726 then
727 return;
729 -- Nothing to do if there is some indirection involved (assume that the
730 -- designated variable might be modified in some way we don't see).
731 -- However, if no function call was found, then we don't care about
732 -- indirections, because the condition must be something like "while X
733 -- /= null loop", so we don't care if X.all is modified in the loop.
735 elsif Function_Call_Found and then Has_Indirection (Etype (Var)) then
736 return;
738 -- Same sort of thing for volatile variable, might be modified by
739 -- some other task or by the operating system in some way.
741 elsif Is_Volatile (Var) then
742 return;
743 end if;
745 -- Filter out case of original statement sequence starting with delay.
746 -- We assume this is a multi-tasking program and that the condition
747 -- is affected by other threads (some kind of busy wait).
749 declare
750 Fstm : constant Node_Id :=
751 Original_Node (First (Statements (Loop_Statement)));
752 begin
753 if Nkind (Fstm) = N_Delay_Relative_Statement
754 or else Nkind (Fstm) = N_Delay_Until_Statement
755 then
756 return;
757 end if;
758 end;
760 -- We have a variable reference of the right form, now we scan the loop
761 -- body to see if it looks like it might not be modified
763 if No_Ref_Found (Loop_Statement) = OK then
764 Error_Msg_NE
765 ("??variable& is not modified in loop body!", Ref, Var);
766 Error_Msg_N
767 ("\??possible infinite loop!", Ref);
768 end if;
769 end Check_Infinite_Loop_Warning;
771 ----------------------------
772 -- Check_Low_Bound_Tested --
773 ----------------------------
775 procedure Check_Low_Bound_Tested (Expr : Node_Id) is
776 procedure Check_Low_Bound_Tested_For (Opnd : Node_Id);
777 -- Determine whether operand Opnd denotes attribute 'First whose prefix
778 -- is a formal parameter. If this is the case, mark the entity of the
779 -- prefix as having its low bound tested.
781 --------------------------------
782 -- Check_Low_Bound_Tested_For --
783 --------------------------------
785 procedure Check_Low_Bound_Tested_For (Opnd : Node_Id) is
786 begin
787 if Nkind (Opnd) = N_Attribute_Reference
788 and then Attribute_Name (Opnd) = Name_First
789 and then Is_Entity_Name (Prefix (Opnd))
790 and then Present (Entity (Prefix (Opnd)))
791 and then Is_Formal (Entity (Prefix (Opnd)))
792 then
793 Set_Low_Bound_Tested (Entity (Prefix (Opnd)));
794 end if;
795 end Check_Low_Bound_Tested_For;
797 -- Start of processing for Check_Low_Bound_Tested
799 begin
800 if Comes_From_Source (Expr) then
801 Check_Low_Bound_Tested_For (Left_Opnd (Expr));
802 Check_Low_Bound_Tested_For (Right_Opnd (Expr));
803 end if;
804 end Check_Low_Bound_Tested;
806 ----------------------
807 -- Check_References --
808 ----------------------
810 procedure Check_References (E : Entity_Id; Anod : Node_Id := Empty) is
811 E1 : Entity_Id;
812 E1T : Entity_Id;
813 UR : Node_Id;
815 function Body_Formal
816 (E : Entity_Id;
817 Accept_Statement : Node_Id) return Entity_Id;
818 -- For an entry formal entity from an entry declaration, find the
819 -- corresponding body formal from the given accept statement.
821 function Generic_Body_Formal (E : Entity_Id) return Entity_Id;
822 -- Warnings on unused formals of subprograms are placed on the entity
823 -- in the subprogram body, which seems preferable because it suggests
824 -- a better codefix for GNAT Studio. The analysis of generic subprogram
825 -- bodies uses a different circuitry, so the choice for the proper
826 -- placement of the warning in the generic case takes place here, by
827 -- finding the body entity that corresponds to a formal in a spec.
829 procedure May_Need_Initialized_Actual (Ent : Entity_Id);
830 -- If an entity of a generic type has default initialization, then the
831 -- corresponding actual type should be fully initialized, or else there
832 -- will be uninitialized components in the instantiation, that might go
833 -- unreported. This routine marks the type of the uninitialized variable
834 -- appropriately to allow the compiler to emit an appropriate warning
835 -- in the instance. In a sense, the use of a type that requires full
836 -- initialization is a weak part of the generic contract.
838 function Missing_Subunits return Boolean;
839 -- We suppress warnings when there are missing subunits, because this
840 -- may generate too many false positives: entities in a parent may only
841 -- be referenced in one of the subunits. We make an exception for
842 -- subunits that contain no other stubs.
844 procedure Output_Reference_Error (M : String);
845 -- Used to output an error message. Deals with posting the error on the
846 -- body formal in the accept case.
848 function Publicly_Referenceable (Ent : Entity_Id) return Boolean;
849 -- This is true if the entity in question is potentially referenceable
850 -- from another unit. This is true for entities in packages that are at
851 -- the library level.
853 function Warnings_Off_E1 return Boolean;
854 -- Return True if Warnings_Off is set for E1, or for its Etype (E1T),
855 -- or for the base type of E1T.
857 -----------------
858 -- Body_Formal --
859 -----------------
861 function Body_Formal
862 (E : Entity_Id;
863 Accept_Statement : Node_Id) return Entity_Id
865 Body_Param : Node_Id;
866 Body_E : Entity_Id;
868 begin
869 -- Loop to find matching parameter in accept statement
871 Body_Param := First (Parameter_Specifications (Accept_Statement));
872 while Present (Body_Param) loop
873 Body_E := Defining_Identifier (Body_Param);
875 if Chars (Body_E) = Chars (E) then
876 return Body_E;
877 end if;
879 Next (Body_Param);
880 end loop;
882 -- Should never fall through, should always find a match
884 raise Program_Error;
885 end Body_Formal;
887 -------------------------
888 -- Generic_Body_Formal --
889 -------------------------
891 function Generic_Body_Formal (E : Entity_Id) return Entity_Id is
892 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Scope (E));
893 Gen_Body : constant Entity_Id := Corresponding_Body (Gen_Decl);
894 Form : Entity_Id;
896 begin
897 if No (Gen_Body) then
898 return E;
900 else
901 Form := First_Entity (Gen_Body);
902 while Present (Form) loop
903 if Chars (Form) = Chars (E) then
904 return Form;
905 end if;
907 Next_Entity (Form);
908 end loop;
909 end if;
911 -- Should never fall through, should always find a match
913 raise Program_Error;
914 end Generic_Body_Formal;
916 ---------------------------------
917 -- May_Need_Initialized_Actual --
918 ---------------------------------
920 procedure May_Need_Initialized_Actual (Ent : Entity_Id) is
921 T : constant Entity_Id := Etype (Ent);
922 Par : constant Node_Id := Parent (T);
924 begin
925 if not Is_Generic_Type (T) then
926 null;
928 elsif (Nkind (Par)) = N_Private_Extension_Declaration then
930 -- We only indicate the first such variable in the generic.
932 if No (Uninitialized_Variable (Par)) then
933 Set_Uninitialized_Variable (Par, Ent);
934 end if;
936 elsif (Nkind (Par)) = N_Formal_Type_Declaration
937 and then Nkind (Formal_Type_Definition (Par)) =
938 N_Formal_Private_Type_Definition
939 then
940 if No (Uninitialized_Variable (Formal_Type_Definition (Par))) then
941 Set_Uninitialized_Variable (Formal_Type_Definition (Par), Ent);
942 end if;
943 end if;
944 end May_Need_Initialized_Actual;
946 ----------------------
947 -- Missing_Subunits --
948 ----------------------
950 function Missing_Subunits return Boolean is
951 D : Node_Id;
953 begin
954 if not Unloaded_Subunits then
956 -- Normal compilation, all subunits are present
958 return False;
960 elsif E /= Main_Unit_Entity then
962 -- No warnings on a stub that is not the main unit
964 return True;
966 elsif Nkind (Unit_Declaration_Node (E)) in N_Proper_Body then
967 D := First (Declarations (Unit_Declaration_Node (E)));
968 while Present (D) loop
970 -- No warnings if the proper body contains nested stubs
972 if Nkind (D) in N_Body_Stub then
973 return True;
974 end if;
976 Next (D);
977 end loop;
979 return False;
981 else
982 -- Missing stubs elsewhere
984 return True;
985 end if;
986 end Missing_Subunits;
988 ----------------------------
989 -- Output_Reference_Error --
990 ----------------------------
992 procedure Output_Reference_Error (M : String) is
993 begin
994 -- Never issue messages for internal names or renamings
996 if Is_Internal_Name (Chars (E1))
997 or else Nkind (Parent (E1)) = N_Object_Renaming_Declaration
998 then
999 return;
1000 end if;
1002 -- Don't output message for IN OUT formal unless we have the warning
1003 -- flag specifically set. It is a bit odd to distinguish IN OUT
1004 -- formals from other cases. This distinction is historical in
1005 -- nature. Warnings for IN OUT formals were added fairly late.
1007 if Ekind (E1) = E_In_Out_Parameter
1008 and then not Check_Unreferenced_Formals
1009 then
1010 return;
1011 end if;
1013 -- Other than accept case, post error on defining identifier
1015 if No (Anod) then
1016 Error_Msg_N (M, E1);
1018 -- Accept case, find body formal to post the message
1020 else
1021 Error_Msg_NE (M, Body_Formal (E1, Accept_Statement => Anod), E1);
1023 end if;
1024 end Output_Reference_Error;
1026 ----------------------------
1027 -- Publicly_Referenceable --
1028 ----------------------------
1030 function Publicly_Referenceable (Ent : Entity_Id) return Boolean is
1031 P : Node_Id;
1032 Prev : Node_Id;
1034 begin
1035 -- A formal parameter is never referenceable outside the body of its
1036 -- subprogram or entry.
1038 if Is_Formal (Ent) then
1039 return False;
1040 end if;
1042 -- Examine parents to look for a library level package spec. But if
1043 -- we find a body or block or other similar construct along the way,
1044 -- we cannot be referenced.
1046 Prev := Ent;
1047 P := Parent (Ent);
1048 loop
1049 case Nkind (P) is
1051 -- If we get to top of tree, then publicly referenceable
1053 when N_Empty =>
1054 return True;
1056 -- If we reach a generic package declaration, then always
1057 -- consider this referenceable, since any instantiation will
1058 -- have access to the entities in the generic package. Note
1059 -- that the package itself may not be instantiated, but then
1060 -- we will get a warning for the package entity.
1062 -- Note that generic formal parameters are themselves not
1063 -- publicly referenceable in an instance, and warnings on them
1064 -- are useful.
1066 when N_Generic_Package_Declaration =>
1067 return
1068 not Is_List_Member (Prev)
1069 or else List_Containing (Prev) /=
1070 Generic_Formal_Declarations (P);
1072 -- Similarly, the generic formals of a generic subprogram are
1073 -- not accessible.
1075 when N_Generic_Subprogram_Declaration =>
1076 if Is_List_Member (Prev)
1077 and then List_Containing (Prev) =
1078 Generic_Formal_Declarations (P)
1079 then
1080 return False;
1081 else
1082 P := Parent (P);
1083 end if;
1085 -- If we reach a subprogram body, entity is not referenceable
1086 -- unless it is the defining entity of the body. This will
1087 -- happen, e.g. when a function is an attribute renaming that
1088 -- is rewritten as a body.
1090 when N_Subprogram_Body =>
1091 if Ent /= Defining_Entity (P) then
1092 return False;
1093 else
1094 P := Parent (P);
1095 end if;
1097 -- If we reach any other body, definitely not referenceable
1099 when N_Block_Statement
1100 | N_Entry_Body
1101 | N_Package_Body
1102 | N_Protected_Body
1103 | N_Subunit
1104 | N_Task_Body
1106 return False;
1108 -- For all other cases, keep looking up tree
1110 when others =>
1111 Prev := P;
1112 P := Parent (P);
1113 end case;
1114 end loop;
1115 end Publicly_Referenceable;
1117 ---------------------
1118 -- Warnings_Off_E1 --
1119 ---------------------
1121 function Warnings_Off_E1 return Boolean is
1122 begin
1123 return Has_Warnings_Off (E1T)
1124 or else Has_Warnings_Off (Base_Type (E1T))
1125 or else Warnings_Off_Check_Spec (E1);
1126 end Warnings_Off_E1;
1128 -- Start of processing for Check_References
1130 begin
1131 Process_Deferred_References;
1133 -- No messages if warnings are suppressed, or if we have detected any
1134 -- real errors so far (this last check avoids junk messages resulting
1135 -- from errors, e.g. a subunit that is not loaded).
1137 if Warning_Mode = Suppress or else Serious_Errors_Detected /= 0 then
1138 return;
1139 end if;
1141 -- We also skip the messages if any subunits were not loaded (see
1142 -- comment in Sem_Ch10 to understand how this is set, and why it is
1143 -- necessary to suppress the warnings in this case).
1145 if Missing_Subunits then
1146 return;
1147 end if;
1149 -- Otherwise loop through entities, looking for suspicious stuff
1151 E1 := First_Entity (E);
1152 while Present (E1) loop
1153 E1T := Etype (E1);
1155 -- We are only interested in source entities. We also don't issue
1156 -- warnings within instances, since the proper place for such
1157 -- warnings is on the template when it is compiled, and we don't
1158 -- issue warnings for variables with names like Junk, Discard etc.
1160 if Comes_From_Source (E1)
1161 and then Instantiation_Location (Sloc (E1)) = No_Location
1162 then
1163 -- We are interested in variables and out/in-out parameters, but
1164 -- we exclude protected types, too complicated to worry about.
1166 if Ekind (E1) = E_Variable
1167 or else
1168 (Ekind (E1) in E_Out_Parameter | E_In_Out_Parameter
1169 and then not Is_Protected_Type (Current_Scope))
1170 then
1171 -- If the formal has a class-wide type, retrieve its type
1172 -- because checks below depend on its private nature.
1174 if Is_Class_Wide_Type (E1T) then
1175 E1T := Etype (E1T);
1176 end if;
1178 -- Case of an unassigned variable
1180 -- First gather any Unset_Reference indication for E1. In the
1181 -- case of a parameter, it is the Spec_Entity that is relevant.
1183 if Ekind (E1) = E_Out_Parameter
1184 and then Present (Spec_Entity (E1))
1185 then
1186 UR := Unset_Reference (Spec_Entity (E1));
1187 else
1188 UR := Unset_Reference (E1);
1189 end if;
1191 -- Special processing for access types
1193 if Present (UR) and then Is_Access_Type (E1T) then
1195 -- For access types, the only time we made a UR entry was
1196 -- for a dereference, and so we post the appropriate warning
1197 -- here (note that the dereference may not be explicit in
1198 -- the source, for example in the case of a dispatching call
1199 -- with an anonymous access controlling formal, or of an
1200 -- assignment of a pointer involving discriminant check on
1201 -- the designated object).
1203 if not Warnings_Off_E1 then
1204 Error_Msg_NE ("??& may be null!", UR, E1);
1205 end if;
1207 goto Continue;
1209 -- Case of variable that could be a constant. Note that we
1210 -- never signal such messages for generic package entities,
1211 -- since a given instance could have modifications outside
1212 -- the package.
1214 -- Note that we used to check Address_Taken here, but we don't
1215 -- want to do that since it can be set for non-source cases,
1216 -- e.g. the Unrestricted_Access from a valid attribute, and
1217 -- the wanted effect is included in Never_Set_In_Source.
1219 elsif Warn_On_Constant
1220 and then (Ekind (E1) = E_Variable
1221 and then Has_Initial_Value (E1))
1222 and then Never_Set_In_Source_Check_Spec (E1)
1223 and then not Generic_Package_Spec_Entity (E1)
1224 then
1225 -- A special case, if this variable is volatile and not
1226 -- imported, it is not helpful to tell the programmer
1227 -- to mark the variable as constant, since this would be
1228 -- illegal by virtue of RM C.6(13). Instead we suggest
1229 -- using pragma Export (can't be Import because of the
1230 -- initial value).
1232 if (Is_Volatile (E1) or else Has_Volatile_Components (E1))
1233 and then not Is_Imported (E1)
1234 then
1235 Error_Msg_N
1236 ("?k?& is not modified, consider pragma Export for "
1237 & "volatile variable!", E1);
1239 -- Another special case, Exception_Occurrence, this catches
1240 -- the case of exception choice (and a bit more too, but not
1241 -- worth doing more investigation here).
1243 elsif Is_RTE (E1T, RE_Exception_Occurrence) then
1244 null;
1246 -- Here we give the warning if referenced and no pragma
1247 -- Unreferenced or Unmodified is present.
1249 else
1250 -- Variable case
1252 if Ekind (E1) = E_Variable then
1253 if Referenced_Check_Spec (E1)
1254 and then not Has_Pragma_Unreferenced_Check_Spec (E1)
1255 and then not Has_Pragma_Unmodified_Check_Spec (E1)
1256 then
1257 if not Warnings_Off_E1
1258 and then not Has_Junk_Name (E1)
1259 then
1260 Error_Msg_N -- CODEFIX
1261 ("?k?& is not modified, "
1262 & "could be declared constant!",
1263 E1);
1264 end if;
1265 end if;
1266 end if;
1267 end if;
1269 -- Other cases of a variable or parameter never set in source
1271 elsif Never_Set_In_Source_Check_Spec (E1)
1273 -- No warning if warning for this case turned off
1275 and then Warn_On_No_Value_Assigned
1277 -- No warning if address taken somewhere
1279 and then not Address_Taken (E1)
1281 -- No warning if explicit initial value
1283 and then not Has_Initial_Value (E1)
1285 -- No warning for generic package spec entities, since we
1286 -- might set them in a child unit or something like that
1288 and then not Generic_Package_Spec_Entity (E1)
1290 -- No warning if fully initialized type, except that for
1291 -- this purpose we do not consider access types to qualify
1292 -- as fully initialized types (relying on an access type
1293 -- variable being null when it is never set is a bit odd).
1295 -- Also we generate warning for an out parameter that is
1296 -- never referenced, since again it seems odd to rely on
1297 -- default initialization to set an out parameter value.
1299 and then (Is_Access_Type (E1T)
1300 or else Ekind (E1) = E_Out_Parameter
1301 or else not Is_Fully_Initialized_Type (E1T))
1302 then
1303 -- Do not output complaint about never being assigned a
1304 -- value if a pragma Unmodified applies to the variable
1305 -- we are examining, or if it is a parameter, if there is
1306 -- a pragma Unreferenced for the corresponding spec, or
1307 -- if the type is marked as having unreferenced objects.
1308 -- The last is a little peculiar, but better too few than
1309 -- too many warnings in this situation.
1311 if Has_Pragma_Unreferenced_Objects (E1T)
1312 or else Has_Pragma_Unmodified_Check_Spec (E1)
1313 then
1314 null;
1316 -- IN OUT parameter case where parameter is referenced. We
1317 -- separate this out, since this is the case where we delay
1318 -- output of the warning until more information is available
1319 -- (about use in an instantiation or address being taken).
1321 elsif Ekind (E1) = E_In_Out_Parameter
1322 and then Referenced_Check_Spec (E1)
1323 then
1324 -- Suppress warning if private type, and the procedure
1325 -- has a separate declaration in a different unit. This
1326 -- is the case where the client of a package sees only
1327 -- the private type, and it may be quite reasonable
1328 -- for the logical view to be IN OUT, even if the
1329 -- implementation ends up using access types or some
1330 -- other method to achieve the local effect of a
1331 -- modification. On the other hand if the spec and body
1332 -- are in the same unit, we are in the package body and
1333 -- there we have less excuse for a junk IN OUT parameter.
1335 if Has_Private_Declaration (E1T)
1336 and then Present (Spec_Entity (E1))
1337 and then not In_Same_Source_Unit (E1, Spec_Entity (E1))
1338 then
1339 null;
1341 -- Suppress warning for any parameter of a dispatching
1342 -- operation, since it is quite reasonable to have an
1343 -- operation that is overridden, and for some subclasses
1344 -- needs the formal to be IN OUT and for others happens
1345 -- not to assign it.
1347 elsif Is_Dispatching_Operation
1348 (Scope (Goto_Spec_Entity (E1)))
1349 then
1350 null;
1352 -- Suppress warning if composite type contains any access
1353 -- component, since the logical effect of modifying a
1354 -- parameter may be achieved by modifying a referenced
1355 -- object.
1357 elsif Is_Composite_Type (E1T)
1358 and then Has_Access_Values (E1T)
1359 then
1360 null;
1362 -- Suppress warning on formals of an entry body. All
1363 -- references are attached to the formal in the entry
1364 -- declaration, which are marked Is_Entry_Formal.
1366 elsif Ekind (Scope (E1)) = E_Entry
1367 and then not Is_Entry_Formal (E1)
1368 then
1369 null;
1371 -- OK, looks like warning for an IN OUT parameter that
1372 -- could be IN makes sense, but we delay the output of
1373 -- the warning, pending possibly finding out later on
1374 -- that the associated subprogram is used as a generic
1375 -- actual, or its address/access is taken. In these two
1376 -- cases, we suppress the warning because the context may
1377 -- force use of IN OUT, even if in this particular case
1378 -- the formal is not modified.
1380 else
1381 -- Suppress the warnings for a junk name
1383 if not Has_Junk_Name (E1) then
1384 In_Out_Warnings.Append (E1);
1385 end if;
1386 end if;
1388 -- Other cases of formals
1390 elsif Is_Formal (E1) then
1391 if not Is_Trivial_Subprogram (Scope (E1)) then
1392 if Referenced_Check_Spec (E1) then
1393 if not Has_Pragma_Unmodified_Check_Spec (E1)
1394 and then not Warnings_Off_E1
1395 and then not Has_Junk_Name (E1)
1396 then
1397 Output_Reference_Error
1398 ("?f?formal parameter& is read but "
1399 & "never assigned!");
1400 end if;
1402 elsif not Has_Pragma_Unreferenced_Check_Spec (E1)
1403 and then not Warnings_Off_E1
1404 and then not Has_Junk_Name (E1)
1405 then
1406 Output_Reference_Error
1407 ("?f?formal parameter& is not referenced!");
1408 end if;
1409 end if;
1411 -- Case of variable
1413 else
1414 if Referenced (E1) then
1415 if not Has_Unmodified (E1)
1416 and then not Warnings_Off_E1
1417 and then not Has_Junk_Name (E1)
1418 then
1419 if Is_Access_Type (E1T)
1420 or else
1421 not Is_Partially_Initialized_Type (E1T, False)
1422 then
1423 Output_Reference_Error
1424 ("?v?variable& is read but never assigned!");
1425 end if;
1427 May_Need_Initialized_Actual (E1);
1428 end if;
1430 elsif not Has_Unreferenced (E1)
1431 and then not Warnings_Off_E1
1432 and then not Has_Junk_Name (E1)
1433 then
1434 Output_Reference_Error -- CODEFIX
1435 ("?v?variable& is never read and never assigned!");
1436 end if;
1438 -- Deal with special case where this variable is hidden
1439 -- by a loop variable.
1441 if Ekind (E1) = E_Variable
1442 and then Present (Hiding_Loop_Variable (E1))
1443 and then not Warnings_Off_E1
1444 then
1445 Error_Msg_N
1446 ("?v?for loop implicitly declares loop variable!",
1447 Hiding_Loop_Variable (E1));
1449 Error_Msg_Sloc := Sloc (E1);
1450 Error_Msg_N
1451 ("\?v?declaration hides & declared#!",
1452 Hiding_Loop_Variable (E1));
1453 end if;
1454 end if;
1456 goto Continue;
1457 end if;
1459 -- Check for unset reference. If type of object has
1460 -- preelaborable initialization, warning is misleading.
1462 if Warn_On_No_Value_Assigned
1463 and then Present (UR)
1464 and then not Known_To_Have_Preelab_Init (Etype (E1))
1465 then
1467 -- For other than access type, go back to original node to
1468 -- deal with case where original unset reference has been
1469 -- rewritten during expansion.
1471 -- In some cases, the original node may be a type
1472 -- conversion, a qualification or an attribute reference and
1473 -- in this case we want the object entity inside. Same for
1474 -- an expression with actions.
1476 UR := Original_Node (UR);
1477 loop
1478 if Nkind (UR) in N_Expression_With_Actions
1479 | N_Qualified_Expression
1480 | N_Type_Conversion
1481 then
1482 UR := Expression (UR);
1484 elsif Nkind (UR) = N_Attribute_Reference then
1485 UR := Prefix (UR);
1487 else
1488 exit;
1489 end if;
1490 end loop;
1492 -- Don't issue warning if appearing inside Initial_Condition
1493 -- pragma or aspect, since that expression is not evaluated
1494 -- at the point where it occurs in the source.
1496 if In_Pragma_Expression (UR, Name_Initial_Condition) then
1497 goto Continue;
1498 end if;
1500 -- Here we issue the warning, all checks completed
1502 -- If we have a return statement, this was a case of an OUT
1503 -- parameter not being set at the time of the return. (Note:
1504 -- it can't be N_Extended_Return_Statement, because those
1505 -- are only for functions, and functions do not allow OUT
1506 -- parameters.)
1508 if not Is_Trivial_Subprogram (Scope (E1)) then
1509 if Nkind (UR) = N_Simple_Return_Statement
1510 and then not Has_Pragma_Unmodified_Check_Spec (E1)
1511 then
1512 if not Warnings_Off_E1
1513 and then not Has_Junk_Name (E1)
1514 then
1515 Error_Msg_NE
1516 ("?v?OUT parameter& not set before return",
1517 UR, E1);
1518 end if;
1520 -- If the unset reference is a selected component
1521 -- prefix from source, mention the component as well.
1522 -- If the selected component comes from expansion, all
1523 -- we know is that the entity is not fully initialized
1524 -- at the point of the reference. Locate a random
1525 -- uninitialized component to get a better message.
1527 elsif Nkind (Parent (UR)) = N_Selected_Component then
1528 Error_Msg_Node_2 := Selector_Name (Parent (UR));
1530 if not Comes_From_Source (Parent (UR)) then
1531 declare
1532 Comp : Entity_Id;
1534 begin
1535 Comp := First_Entity (E1T);
1536 while Present (Comp) loop
1537 if Ekind (Comp) = E_Component
1538 and then Nkind (Parent (Comp)) =
1539 N_Component_Declaration
1540 and then No (Expression (Parent (Comp)))
1541 then
1542 Error_Msg_Node_2 := Comp;
1543 exit;
1544 end if;
1546 Next_Entity (Comp);
1547 end loop;
1548 end;
1549 end if;
1551 -- Issue proper warning. This is a case of referencing
1552 -- a variable before it has been explicitly assigned.
1553 -- For access types, UR was only set for dereferences,
1554 -- so the issue is that the value may be null.
1556 if not Is_Trivial_Subprogram (Scope (E1)) then
1557 if not Warnings_Off_E1 then
1558 if Is_Access_Type (Etype (Parent (UR))) then
1559 Error_Msg_N ("??`&.&` may be null!", UR);
1560 else
1561 Error_Msg_N
1562 ("??`&.&` may be referenced before "
1563 & "it has a value!", UR);
1564 end if;
1565 end if;
1566 end if;
1568 -- All other cases of unset reference active
1570 elsif not Warnings_Off_E1 then
1571 Error_Msg_N
1572 ("??& may be referenced before it has a value!", UR);
1573 end if;
1574 end if;
1576 goto Continue;
1578 end if;
1579 end if;
1581 -- Then check for unreferenced entities. Note that we are only
1582 -- interested in entities whose Referenced flag is not set.
1584 if not Referenced_Check_Spec (E1)
1586 -- If Referenced_As_LHS is set, then that's still interesting
1587 -- (potential "assigned but never read" case), but not if we
1588 -- have pragma Unreferenced, which cancels this warning.
1590 and then (not Referenced_As_LHS_Check_Spec (E1)
1591 or else not Has_Unreferenced (E1))
1593 -- Check that warnings on unreferenced entities are enabled
1595 and then
1596 ((Check_Unreferenced and then not Is_Formal (E1))
1598 -- Case of warning on unreferenced formal
1600 or else (Check_Unreferenced_Formals and then Is_Formal (E1))
1602 -- Case of warning on unread variables modified by an
1603 -- assignment, or an OUT parameter if it is the only one.
1605 or else (Warn_On_Modified_Unread
1606 and then Referenced_As_LHS_Check_Spec (E1))
1608 -- Case of warning on any unread OUT parameter (note such
1609 -- indications are only set if the appropriate warning
1610 -- options were set, so no need to recheck here.)
1612 or else Referenced_As_Out_Parameter_Check_Spec (E1))
1614 -- All other entities, including local packages that cannot be
1615 -- referenced from elsewhere, including those declared within a
1616 -- package body.
1618 and then (Is_Object (E1)
1619 or else Is_Type (E1)
1620 or else Ekind (E1) = E_Label
1621 or else Ekind (E1) in E_Exception
1622 | E_Named_Integer
1623 | E_Named_Real
1624 or else Is_Overloadable (E1)
1626 -- Package case, if the main unit is a package spec
1627 -- or generic package spec, then there may be a
1628 -- corresponding body that references this package
1629 -- in some other file. Otherwise we can be sure
1630 -- that there is no other reference.
1632 or else
1633 (Ekind (E1) = E_Package
1634 and then
1635 not Is_Package_Or_Generic_Package
1636 (Cunit_Entity (Current_Sem_Unit))))
1638 -- Exclude instantiations, since there is no reason why every
1639 -- entity in an instantiation should be referenced.
1641 and then Instantiation_Location (Sloc (E1)) = No_Location
1643 -- Exclude formal parameters from bodies if the corresponding
1644 -- spec entity has been referenced in the case where there is
1645 -- a separate spec.
1647 and then not (Is_Formal (E1)
1648 and then Ekind (Scope (E1)) = E_Subprogram_Body
1649 and then Present (Spec_Entity (E1))
1650 and then Referenced (Spec_Entity (E1)))
1652 -- Consider private type referenced if full view is referenced.
1653 -- If there is not full view, this is a generic type on which
1654 -- warnings are also useful.
1656 and then
1657 not (Is_Private_Type (E1)
1658 and then Present (Full_View (E1))
1659 and then Referenced (Full_View (E1)))
1661 -- Don't worry about full view, only about private type
1663 and then not Has_Private_Declaration (E1)
1665 -- Eliminate dispatching operations from consideration, we
1666 -- cannot tell if these are referenced or not in any easy
1667 -- manner (note this also catches Adjust/Finalize/Initialize).
1669 and then not Is_Dispatching_Operation (E1)
1671 -- Check entity that can be publicly referenced (we do not give
1672 -- messages for such entities, since there could be other
1673 -- units, not involved in this compilation, that contain
1674 -- relevant references.
1676 and then not Publicly_Referenceable (E1)
1678 -- Class wide types are marked as source entities, but they are
1679 -- not really source entities, and are always created, so we do
1680 -- not care if they are not referenced.
1682 and then Ekind (E1) /= E_Class_Wide_Type
1684 -- Objects other than parameters of task types are allowed to
1685 -- be non-referenced, since they start up tasks.
1687 and then ((Ekind (E1) /= E_Variable
1688 and then Ekind (E1) /= E_Constant
1689 and then Ekind (E1) /= E_Component)
1690 or else not Is_Task_Type (E1T))
1692 -- For subunits, only place warnings on the main unit itself,
1693 -- since parent units are not completely compiled.
1695 and then (Nkind (Unit (Cunit (Main_Unit))) /= N_Subunit
1696 or else Get_Source_Unit (E1) = Main_Unit)
1698 -- No warning on a return object, because these are often
1699 -- created with a single expression and an implicit return.
1700 -- If the object is a variable there will be a warning
1701 -- indicating that it could be declared constant.
1703 and then not
1704 (Ekind (E1) = E_Constant and then Is_Return_Object (E1))
1705 then
1706 -- Suppress warnings in internal units if not in -gnatg mode
1707 -- (these would be junk warnings for an applications program,
1708 -- since they refer to problems in internal units).
1710 if GNAT_Mode or else not In_Internal_Unit (E1) then
1711 -- We do not immediately flag the error. This is because we
1712 -- have not expanded generic bodies yet, and they may have
1713 -- the missing reference. So instead we park the entity on a
1714 -- list, for later processing. However for the case of an
1715 -- accept statement we want to output messages now, since
1716 -- we know we already have all information at hand, and we
1717 -- also want to have separate warnings for each accept
1718 -- statement for the same entry.
1720 if Present (Anod) then
1721 pragma Assert (Is_Formal (E1));
1723 -- The unreferenced entity is E1, but post the warning
1724 -- on the body entity for this accept statement.
1726 if not Warnings_Off_E1 then
1727 Warn_On_Unreferenced_Entity
1728 (E1, Body_Formal (E1, Accept_Statement => Anod));
1729 end if;
1731 elsif not Warnings_Off_E1
1732 and then not Has_Junk_Name (E1)
1733 then
1734 if Is_Formal (E1)
1735 and then Nkind (Unit_Declaration_Node (Scope (E1)))
1736 = N_Generic_Subprogram_Declaration
1737 then
1738 Unreferenced_Entities.Append
1739 (Generic_Body_Formal (E1));
1740 else
1741 Unreferenced_Entities.Append (E1);
1742 end if;
1743 end if;
1744 end if;
1746 -- Generic units are referenced in the generic body, but if they
1747 -- are not public and never instantiated we want to force a
1748 -- warning on them. We treat them as redundant constructs to
1749 -- minimize noise.
1751 elsif Is_Generic_Subprogram (E1)
1752 and then not Is_Instantiated (E1)
1753 and then not Publicly_Referenceable (E1)
1754 and then Instantiation_Depth (Sloc (E1)) = 0
1755 and then Warn_On_Redundant_Constructs
1756 then
1757 if not Warnings_Off_E1 and then not Has_Junk_Name (E1) then
1758 Unreferenced_Entities.Append (E1);
1760 -- Force warning on entity
1762 Set_Referenced (E1, False);
1763 end if;
1764 end if;
1765 end if;
1767 -- Recurse into nested package or block. Do not recurse into a formal
1768 -- package, because the corresponding body is not analyzed.
1770 <<Continue>>
1771 if (Is_Package_Or_Generic_Package (E1)
1772 and then Nkind (Parent (E1)) = N_Package_Specification
1773 and then
1774 Nkind (Original_Node (Unit_Declaration_Node (E1))) /=
1775 N_Formal_Package_Declaration)
1777 or else Ekind (E1) = E_Block
1778 then
1779 Check_References (E1);
1780 end if;
1782 Next_Entity (E1);
1783 end loop;
1784 end Check_References;
1786 ---------------------------
1787 -- Check_Unset_Reference --
1788 ---------------------------
1790 procedure Check_Unset_Reference (N : Node_Id) is
1791 Typ : constant Entity_Id := Etype (N);
1793 function Is_OK_Fully_Initialized return Boolean;
1794 -- This function returns true if the given node N is fully initialized
1795 -- so that the reference is safe as far as this routine is concerned.
1796 -- Safe generally means that the type of N is a fully initialized type.
1797 -- The one special case is that for access types, which are always fully
1798 -- initialized, we don't consider a dereference OK since it will surely
1799 -- be dereferencing a null value, which won't do.
1801 function Prefix_Has_Dereference (Pref : Node_Id) return Boolean;
1802 -- Used to test indexed or selected component or slice to see if the
1803 -- evaluation of the prefix depends on a dereference, and if so, returns
1804 -- True, in which case we always check the prefix, even if we know that
1805 -- the referenced component is initialized. Pref is the prefix to test.
1807 -----------------------------
1808 -- Is_OK_Fully_Initialized --
1809 -----------------------------
1811 function Is_OK_Fully_Initialized return Boolean is
1812 begin
1813 if Is_Access_Type (Typ) and then Is_Dereferenced (N) then
1814 return False;
1816 -- A type subject to pragma Default_Initial_Condition may be fully
1817 -- default initialized depending on inheritance and the argument of
1818 -- the pragma (SPARK RM 3.1 and SPARK RM 7.3.3).
1820 elsif Has_Fully_Default_Initializing_DIC_Pragma (Typ) then
1821 return True;
1823 else
1824 return Is_Fully_Initialized_Type (Typ);
1825 end if;
1826 end Is_OK_Fully_Initialized;
1828 ----------------------------
1829 -- Prefix_Has_Dereference --
1830 ----------------------------
1832 function Prefix_Has_Dereference (Pref : Node_Id) return Boolean is
1833 begin
1834 -- If prefix is of an access type, it certainly needs a dereference
1836 if Is_Access_Type (Etype (Pref)) then
1837 return True;
1839 -- If prefix is explicit dereference, that's a dereference for sure
1841 elsif Nkind (Pref) = N_Explicit_Dereference then
1842 return True;
1844 -- If prefix is itself a component reference or slice check prefix
1846 elsif Nkind (Pref) = N_Slice
1847 or else Nkind (Pref) = N_Indexed_Component
1848 or else Nkind (Pref) = N_Selected_Component
1849 then
1850 return Prefix_Has_Dereference (Prefix (Pref));
1852 -- All other cases do not involve a dereference
1854 else
1855 return False;
1856 end if;
1857 end Prefix_Has_Dereference;
1859 -- Start of processing for Check_Unset_Reference
1861 begin
1862 -- Nothing to do if warnings suppressed
1864 if Warning_Mode = Suppress then
1865 return;
1866 end if;
1868 -- Nothing to do for numeric or string literal. Do this test early to
1869 -- save time in a common case (it does not matter that we do not include
1870 -- character literal here, since that will be caught later on in the
1871 -- when others branch of the case statement).
1873 if Nkind (N) in N_Numeric_Or_String_Literal then
1874 return;
1875 end if;
1877 -- Ignore reference unless it comes from source. Almost always if we
1878 -- have a reference from generated code, it is bogus (e.g. calls to init
1879 -- procs to set default discriminant values).
1881 if not Comes_From_Source (Original_Node (N)) then
1882 return;
1883 end if;
1885 -- Otherwise see what kind of node we have. If the entity already has an
1886 -- unset reference, it is not necessarily the earliest in the text,
1887 -- because resolution of the prefix of selected components is completed
1888 -- before the resolution of the selected component itself. As a result,
1889 -- given (R /= null and then R.X > 0), the occurrences of R are examined
1890 -- in right-to-left order. If there is already an unset reference, we
1891 -- check whether N is earlier before proceeding.
1893 case Nkind (N) is
1895 -- For identifier or expanded name, examine the entity involved
1897 when N_Expanded_Name
1898 | N_Identifier
1900 declare
1901 E : constant Entity_Id := Entity (N);
1903 begin
1904 if Ekind (E) in E_Variable | E_Out_Parameter
1905 and then Never_Set_In_Source_Check_Spec (E)
1906 and then not Has_Initial_Value (E)
1907 and then (No (Unset_Reference (E))
1908 or else
1909 Earlier_In_Extended_Unit
1910 (Sloc (N), Sloc (Unset_Reference (E))))
1911 and then not Has_Pragma_Unmodified_Check_Spec (E)
1912 and then not Warnings_Off_Check_Spec (E)
1913 and then not Has_Junk_Name (E)
1914 then
1915 -- We may have an unset reference. The first test is whether
1916 -- this is an access to a discriminant of a record or a
1917 -- component with default initialization. Both of these
1918 -- cases can be ignored, since the actual object that is
1919 -- referenced is definitely initialized. Note that this
1920 -- covers the case of reading discriminants of an OUT
1921 -- parameter, which is OK even in Ada 83.
1923 -- Note that we are only interested in a direct reference to
1924 -- a record component here. If the reference is through an
1925 -- access type, then the access object is being referenced,
1926 -- not the record, and still deserves an unset reference.
1928 if Nkind (Parent (N)) = N_Selected_Component
1929 and not Is_Access_Type (Typ)
1930 then
1931 declare
1932 ES : constant Entity_Id :=
1933 Entity (Selector_Name (Parent (N)));
1934 begin
1935 if Ekind (ES) = E_Discriminant
1936 or else
1937 (Present (Declaration_Node (ES))
1938 and then
1939 Present (Expression (Declaration_Node (ES))))
1940 then
1941 return;
1942 end if;
1943 end;
1944 end if;
1946 -- Exclude fully initialized types
1948 if Is_OK_Fully_Initialized then
1949 return;
1950 end if;
1952 -- Here we have a potential unset reference. But before we
1953 -- get worried about it, we have to make sure that the
1954 -- entity declaration is in the same procedure as the
1955 -- reference, since if they are in separate procedures, then
1956 -- we have no idea about sequential execution.
1958 -- The tests in the loop below catch all such cases, but do
1959 -- allow the reference to appear in a loop, block, or
1960 -- package spec that is nested within the declaring scope.
1961 -- As always, it is possible to construct cases where the
1962 -- warning is wrong, that is why it is a warning.
1964 Potential_Unset_Reference : declare
1965 SR : Entity_Id;
1966 SE : constant Entity_Id := Scope (E);
1968 function Within_Postcondition return Boolean;
1969 -- Returns True if N is within a Postcondition, a
1970 -- Refined_Post, an Ensures component in a Test_Case,
1971 -- or a Contract_Cases.
1973 --------------------------
1974 -- Within_Postcondition --
1975 --------------------------
1977 function Within_Postcondition return Boolean is
1978 Nod, P : Node_Id;
1980 begin
1981 Nod := Parent (N);
1982 while Present (Nod) loop
1983 if Nkind (Nod) = N_Pragma
1984 and then
1985 Pragma_Name_Unmapped (Nod)
1986 in Name_Postcondition
1987 | Name_Refined_Post
1988 | Name_Contract_Cases
1989 then
1990 return True;
1992 elsif Present (Parent (Nod)) then
1993 P := Parent (Nod);
1995 if Nkind (P) = N_Pragma
1996 and then Pragma_Name (P) =
1997 Name_Test_Case
1998 and then Nod = Test_Case_Arg (P, Name_Ensures)
1999 then
2000 return True;
2001 end if;
2002 end if;
2004 Nod := Parent (Nod);
2005 end loop;
2007 return False;
2008 end Within_Postcondition;
2010 -- Start of processing for Potential_Unset_Reference
2012 begin
2013 SR := Current_Scope;
2014 while SR /= SE loop
2015 if SR = Standard_Standard
2016 or else Is_Subprogram (SR)
2017 or else Is_Concurrent_Body (SR)
2018 or else Is_Concurrent_Type (SR)
2019 then
2020 return;
2021 end if;
2023 SR := Scope (SR);
2024 end loop;
2026 -- Case of reference has an access type. This is a
2027 -- special case since access types are always set to null
2028 -- so cannot be truly uninitialized, but we still want to
2029 -- warn about cases of obvious null dereference.
2031 if Is_Access_Type (Typ) then
2032 Access_Type_Case : declare
2033 P : Node_Id;
2035 function Process
2036 (N : Node_Id) return Traverse_Result;
2037 -- Process function for instantiation of Traverse
2038 -- below. Checks if N contains reference to E other
2039 -- than a dereference.
2041 function Ref_In (Nod : Node_Id) return Boolean;
2042 -- Determines whether Nod contains a reference to
2043 -- the entity E that is not a dereference.
2045 -------------
2046 -- Process --
2047 -------------
2049 function Process
2050 (N : Node_Id) return Traverse_Result
2052 begin
2053 if Is_Entity_Name (N)
2054 and then Entity (N) = E
2055 and then not Is_Dereferenced (N)
2056 then
2057 return Abandon;
2058 else
2059 return OK;
2060 end if;
2061 end Process;
2063 ------------
2064 -- Ref_In --
2065 ------------
2067 function Ref_In (Nod : Node_Id) return Boolean is
2068 function Traverse is new Traverse_Func (Process);
2069 begin
2070 return Traverse (Nod) = Abandon;
2071 end Ref_In;
2073 -- Start of processing for Access_Type_Case
2075 begin
2076 -- Don't bother if we are inside an instance, since
2077 -- the compilation of the generic template is where
2078 -- the warning should be issued.
2080 if In_Instance then
2081 return;
2082 end if;
2084 -- Don't bother if this is not the main unit. If we
2085 -- try to give this warning for with'ed units, we
2086 -- get some false positives, since we do not record
2087 -- references in other units.
2089 if not In_Extended_Main_Source_Unit (E)
2090 or else
2091 not In_Extended_Main_Source_Unit (N)
2092 then
2093 return;
2094 end if;
2096 -- We are only interested in dereferences
2098 if not Is_Dereferenced (N) then
2099 return;
2100 end if;
2102 -- One more check, don't bother with references
2103 -- that are inside conditional statements or WHILE
2104 -- loops if the condition references the entity in
2105 -- question. This avoids most false positives.
2107 P := Parent (N);
2108 loop
2109 P := Parent (P);
2110 exit when No (P);
2112 if Nkind (P) in N_If_Statement | N_Elsif_Part
2113 and then Ref_In (Condition (P))
2114 then
2115 return;
2117 elsif Nkind (P) = N_Loop_Statement
2118 and then Present (Iteration_Scheme (P))
2119 and then
2120 Ref_In (Condition (Iteration_Scheme (P)))
2121 then
2122 return;
2123 end if;
2124 end loop;
2125 end Access_Type_Case;
2126 end if;
2128 -- One more check, don't bother if we are within a
2129 -- postcondition, since the expression occurs in a
2130 -- place unrelated to the actual test.
2132 if not Within_Postcondition then
2134 -- Here we definitely have a case for giving a warning
2135 -- for a reference to an unset value. But we don't
2136 -- give the warning now. Instead set Unset_Reference
2137 -- in the identifier involved. The reason for this is
2138 -- that if we find the variable is never ever assigned
2139 -- a value then that warning is more important and
2140 -- there is no point in giving the reference warning.
2142 -- If this is an identifier, set the field directly
2144 if Nkind (N) = N_Identifier then
2145 Set_Unset_Reference (E, N);
2147 -- Otherwise it is an expanded name, so set the field
2148 -- of the actual identifier for the reference.
2150 else
2151 Set_Unset_Reference (E, Selector_Name (N));
2152 end if;
2153 end if;
2154 end Potential_Unset_Reference;
2155 end if;
2156 end;
2158 -- Indexed component or slice
2160 when N_Indexed_Component
2161 | N_Slice
2163 -- If prefix does not involve dereferencing an access type, then
2164 -- we know we are OK if the component type is fully initialized,
2165 -- since the component will have been set as part of the default
2166 -- initialization.
2168 if not Prefix_Has_Dereference (Prefix (N))
2169 and then Is_OK_Fully_Initialized
2170 then
2171 return;
2173 -- Look at prefix in access type case, or if the component is not
2174 -- fully initialized.
2176 else
2177 Check_Unset_Reference (Prefix (N));
2178 end if;
2180 -- Record component
2182 when N_Selected_Component =>
2183 declare
2184 Pref : constant Node_Id := Prefix (N);
2185 Ent : constant Entity_Id := Entity (Selector_Name (N));
2187 begin
2188 -- If prefix involves dereferencing an access type, always
2189 -- check the prefix, since the issue then is whether this
2190 -- access value is null.
2192 if Prefix_Has_Dereference (Pref) then
2193 null;
2195 -- Always go to prefix if no selector entity is set. Can this
2196 -- happen in the normal case? Not clear, but it definitely can
2197 -- happen in error cases.
2199 elsif No (Ent) then
2200 null;
2202 -- For a record component, check some cases where we have
2203 -- reasonable cause to consider that the component is known to
2204 -- be or probably is initialized. In this case, we don't care
2205 -- if the prefix itself was explicitly initialized.
2207 -- Discriminants are always considered initialized
2209 elsif Ekind (Ent) = E_Discriminant then
2210 return;
2212 -- An explicitly initialized component is certainly initialized
2214 elsif Nkind (Parent (Ent)) = N_Component_Declaration
2215 and then Present (Expression (Parent (Ent)))
2216 then
2217 return;
2219 -- A fully initialized component is initialized
2221 elsif Is_OK_Fully_Initialized then
2222 return;
2223 end if;
2225 -- If none of those cases apply, check the record type prefix
2227 Check_Unset_Reference (Pref);
2228 end;
2230 -- For type conversions, qualifications, or expressions with actions,
2231 -- examine the expression.
2233 when N_Expression_With_Actions
2234 | N_Qualified_Expression
2235 | N_Type_Conversion
2237 Check_Unset_Reference (Expression (N));
2239 -- For explicit dereference, always check prefix, which will generate
2240 -- an unset reference (since this is a case of dereferencing null).
2242 when N_Explicit_Dereference =>
2243 Check_Unset_Reference (Prefix (N));
2245 -- All other cases are not cases of an unset reference
2247 when others =>
2248 null;
2249 end case;
2250 end Check_Unset_Reference;
2252 ------------------------
2253 -- Check_Unused_Withs --
2254 ------------------------
2256 procedure Check_Unused_Withs (Spec_Unit : Unit_Number_Type := No_Unit) is
2258 Munite : constant Entity_Id := Cunit_Entity (Main_Unit);
2259 -- This is needed for checking the special renaming case
2261 procedure Check_One_Unit (Unit : Unit_Number_Type);
2262 -- Subsidiary procedure, performs checks for specified unit
2264 --------------------
2265 -- Check_One_Unit --
2266 --------------------
2268 procedure Check_One_Unit (Unit : Unit_Number_Type) is
2269 Cnode : constant Node_Id := Cunit (Unit);
2271 Is_Visible_Renaming : Boolean := False;
2273 procedure Check_Inner_Package (Pack : Entity_Id);
2274 -- Pack is a package local to a unit in a with_clause. Both the unit
2275 -- and Pack are referenced. If none of the entities in Pack are
2276 -- referenced, then the only occurrence of Pack is in a USE clause
2277 -- or a pragma, and a warning is worthwhile as well.
2279 function Check_System_Aux (Lunit : Entity_Id) return Boolean;
2280 -- Before giving a warning on a with_clause for System, check whether
2281 -- a system extension is present.
2283 function Find_Package_Renaming
2284 (P : Entity_Id;
2285 L : Entity_Id) return Entity_Id;
2286 -- The only reference to a context unit may be in a renaming
2287 -- declaration. If this renaming declares a visible entity, do not
2288 -- warn that the context clause could be moved to the body, because
2289 -- the renaming may be intended to re-export the unit.
2291 function Has_Visible_Entities (P : Entity_Id) return Boolean;
2292 -- This function determines if a package has any visible entities.
2293 -- True is returned if there is at least one declared visible entity,
2294 -- otherwise False is returned (e.g. case of only pragmas present).
2296 -------------------------
2297 -- Check_Inner_Package --
2298 -------------------------
2300 procedure Check_Inner_Package (Pack : Entity_Id) is
2301 E : Entity_Id;
2302 Un : constant Node_Id := Sinfo.Unit (Cnode);
2304 function Check_Use_Clause (N : Node_Id) return Traverse_Result;
2305 -- If N is a use_clause for Pack, emit warning
2307 procedure Check_Use_Clauses is new
2308 Traverse_Proc (Check_Use_Clause);
2310 ----------------------
2311 -- Check_Use_Clause --
2312 ----------------------
2314 function Check_Use_Clause (N : Node_Id) return Traverse_Result is
2315 begin
2316 if Nkind (N) = N_Use_Package_Clause
2317 and then Entity (Name (N)) = Pack
2318 then
2319 -- Suppress message if any serious errors detected that turn
2320 -- off expansion, and thus result in false positives for
2321 -- this warning.
2323 if Serious_Errors_Detected = 0 then
2324 Error_Msg_Qual_Level := 1;
2325 Error_Msg_NE -- CODEFIX
2326 ("?u?no entities of package& are referenced!",
2327 Name (N), Pack);
2328 Error_Msg_Qual_Level := 0;
2329 end if;
2330 end if;
2332 return OK;
2333 end Check_Use_Clause;
2335 -- Start of processing for Check_Inner_Package
2337 begin
2338 E := First_Entity (Pack);
2339 while Present (E) loop
2340 if Referenced_Check_Spec (E) then
2341 return;
2342 end if;
2344 Next_Entity (E);
2345 end loop;
2347 -- No entities of the package are referenced. Check whether the
2348 -- reference to the package itself is a use clause, and if so
2349 -- place a warning on it.
2351 Check_Use_Clauses (Un);
2352 end Check_Inner_Package;
2354 ----------------------
2355 -- Check_System_Aux --
2356 ----------------------
2358 function Check_System_Aux (Lunit : Entity_Id) return Boolean is
2359 Ent : Entity_Id;
2361 begin
2362 if Chars (Lunit) = Name_System
2363 and then Scope (Lunit) = Standard_Standard
2364 and then Present_System_Aux
2365 then
2366 Ent := First_Entity (System_Aux_Id);
2367 while Present (Ent) loop
2368 if Referenced_Check_Spec (Ent) then
2369 return True;
2370 end if;
2372 Next_Entity (Ent);
2373 end loop;
2374 end if;
2376 return False;
2377 end Check_System_Aux;
2379 ---------------------------
2380 -- Find_Package_Renaming --
2381 ---------------------------
2383 function Find_Package_Renaming
2384 (P : Entity_Id;
2385 L : Entity_Id) return Entity_Id
2387 E1 : Entity_Id;
2388 R : Entity_Id;
2390 begin
2391 Is_Visible_Renaming := False;
2393 E1 := First_Entity (P);
2394 while Present (E1) loop
2395 if Ekind (E1) = E_Package and then Renamed_Object (E1) = L then
2396 Is_Visible_Renaming := not Is_Hidden (E1);
2397 return E1;
2399 elsif Ekind (E1) = E_Package
2400 and then No (Renamed_Object (E1))
2401 and then not Is_Generic_Instance (E1)
2402 then
2403 R := Find_Package_Renaming (E1, L);
2405 if Present (R) then
2406 Is_Visible_Renaming := not Is_Hidden (R);
2407 return R;
2408 end if;
2409 end if;
2411 Next_Entity (E1);
2412 end loop;
2414 return Empty;
2415 end Find_Package_Renaming;
2417 --------------------------
2418 -- Has_Visible_Entities --
2419 --------------------------
2421 function Has_Visible_Entities (P : Entity_Id) return Boolean is
2422 E : Entity_Id;
2424 begin
2425 -- If unit in context is not a package, it is a subprogram that
2426 -- is not called or a generic unit that is not instantiated
2427 -- in the current unit, and warning is appropriate.
2429 if Ekind (P) /= E_Package then
2430 return True;
2431 end if;
2433 -- If unit comes from a limited_with clause, look for declaration
2434 -- of shadow entities.
2436 if Present (Limited_View (P)) then
2437 E := First_Entity (Limited_View (P));
2438 else
2439 E := First_Entity (P);
2440 end if;
2442 while Present (E) and then E /= First_Private_Entity (P) loop
2443 if Comes_From_Source (E) or else Present (Limited_View (P)) then
2444 return True;
2445 end if;
2447 Next_Entity (E);
2448 end loop;
2450 return False;
2451 end Has_Visible_Entities;
2453 -- Local variables
2455 Ent : Entity_Id;
2456 Item : Node_Id;
2457 Lunit : Entity_Id;
2458 Pack : Entity_Id;
2460 -- Start of processing for Check_One_Unit
2462 begin
2463 -- Only do check in units that are part of the extended main unit.
2464 -- This is actually a necessary restriction, because in the case of
2465 -- subprogram acting as its own specification, there can be with's in
2466 -- subunits that we will not see.
2468 if not In_Extended_Main_Source_Unit (Cnode) then
2469 return;
2470 end if;
2472 -- Loop through context items in this unit
2474 Item := First (Context_Items (Cnode));
2475 while Present (Item) loop
2476 if Nkind (Item) = N_With_Clause
2477 and then not Implicit_With (Item)
2478 and then In_Extended_Main_Source_Unit (Item)
2480 -- Guard for no entity present. Not clear under what conditions
2481 -- this happens, but it does occur, and since this is only a
2482 -- warning, we just suppress the warning in this case.
2484 and then Nkind (Name (Item)) in N_Has_Entity
2485 and then Present (Entity (Name (Item)))
2486 then
2487 Lunit := Entity (Name (Item));
2489 -- Check if this unit is referenced (skip the check if this
2490 -- is explicitly marked by a pragma Unreferenced).
2492 if not Referenced (Lunit) and then not Has_Unreferenced (Lunit)
2493 then
2494 -- Suppress warnings in internal units if not in -gnatg mode
2495 -- (these would be junk warnings for an application program,
2496 -- since they refer to problems in internal units).
2498 if GNAT_Mode or else not Is_Internal_Unit (Unit) then
2499 -- Here we definitely have a non-referenced unit. If it
2500 -- is the special call for a spec unit, then just set the
2501 -- flag to be read later.
2503 if Unit = Spec_Unit then
2504 Set_Unreferenced_In_Spec (Item);
2506 -- Otherwise simple unreferenced message, but skip this
2507 -- if no visible entities, because that is most likely a
2508 -- case where warning would be false positive (e.g. a
2509 -- package with only a linker options pragma and nothing
2510 -- else or a pragma elaborate with a body library task).
2512 elsif Has_Visible_Entities (Lunit) then
2513 Error_Msg_N -- CODEFIX
2514 ("?u?unit& is not referenced!", Name (Item));
2515 end if;
2516 end if;
2518 -- If main unit is a renaming of this unit, then we consider
2519 -- the with to be OK (obviously it is needed in this case).
2520 -- This may be transitive: the unit in the with_clause may
2521 -- itself be a renaming, in which case both it and the main
2522 -- unit rename the same ultimate package.
2524 elsif Present (Renamed_Entity (Munite))
2525 and then
2526 (Renamed_Entity (Munite) = Lunit
2527 or else Renamed_Entity (Munite) = Renamed_Entity (Lunit))
2528 then
2529 null;
2531 -- If this unit is referenced, and it is a package, we do
2532 -- another test, to see if any of the entities in the package
2533 -- are referenced. If none of the entities are referenced, we
2534 -- still post a warning. This occurs if the only use of the
2535 -- package is in a use clause, or in a package renaming
2536 -- declaration. This check is skipped for packages that are
2537 -- renamed in a spec, since the entities in such a package are
2538 -- visible to clients via the renaming.
2540 elsif Ekind (Lunit) = E_Package
2541 and then not Renamed_In_Spec (Lunit)
2542 then
2543 -- If Is_Instantiated is set, it means that the package is
2544 -- implicitly instantiated (this is the case of parent
2545 -- instance or an actual for a generic package formal), and
2546 -- this counts as a reference.
2548 if Is_Instantiated (Lunit) then
2549 null;
2551 -- If no entities in package, and there is a pragma
2552 -- Elaborate_Body present, then assume that this with is
2553 -- done for purposes of this elaboration.
2555 elsif No (First_Entity (Lunit))
2556 and then Has_Pragma_Elaborate_Body (Lunit)
2557 then
2558 null;
2560 -- Otherwise see if any entities have been referenced
2562 else
2563 if Limited_Present (Item) then
2564 Ent := First_Entity (Limited_View (Lunit));
2565 else
2566 Ent := First_Entity (Lunit);
2567 end if;
2569 loop
2570 -- No more entities, and we did not find one that was
2571 -- referenced. Means we have a definite case of a with
2572 -- none of whose entities was referenced.
2574 if No (Ent) then
2576 -- If in spec, just set the flag
2578 if Unit = Spec_Unit then
2579 Set_No_Entities_Ref_In_Spec (Item);
2581 elsif Check_System_Aux (Lunit) then
2582 null;
2584 -- Else the warning may be needed
2586 else
2587 -- Warn if we unreferenced flag set and we have
2588 -- not had serious errors. The reason we inhibit
2589 -- the message if there are errors is to prevent
2590 -- false positives from disabling expansion.
2592 if not Has_Unreferenced (Lunit)
2593 and then Serious_Errors_Detected = 0
2594 then
2595 -- Get possible package renaming
2597 Pack := Find_Package_Renaming (Munite, Lunit);
2599 -- No warning if either the package or its
2600 -- renaming is used as a generic actual.
2602 if Used_As_Generic_Actual (Lunit)
2603 or else
2604 (Present (Pack)
2605 and then
2606 Used_As_Generic_Actual (Pack))
2607 then
2608 exit;
2609 end if;
2611 -- Here we give the warning
2613 Error_Msg_N -- CODEFIX
2614 ("?u?no entities of & are referenced!",
2615 Name (Item));
2617 -- Flag renaming of package as well. If
2618 -- the original package has warnings off,
2619 -- we suppress the warning on the renaming
2620 -- as well.
2622 if Present (Pack)
2623 and then not Has_Warnings_Off (Lunit)
2624 and then not Has_Unreferenced (Pack)
2625 then
2626 Error_Msg_NE -- CODEFIX
2627 ("?u?no entities of& are referenced!",
2628 Unit_Declaration_Node (Pack), Pack);
2629 end if;
2630 end if;
2631 end if;
2633 exit;
2635 -- Case of entity being referenced. The reference may
2636 -- come from a limited_with_clause, in which case the
2637 -- limited view of the entity carries the flag.
2639 elsif Referenced_Check_Spec (Ent)
2640 or else Referenced_As_LHS_Check_Spec (Ent)
2641 or else Referenced_As_Out_Parameter_Check_Spec (Ent)
2642 or else
2643 (From_Limited_With (Ent)
2644 and then Is_Incomplete_Type (Ent)
2645 and then Present (Non_Limited_View (Ent))
2646 and then Referenced (Non_Limited_View (Ent)))
2647 then
2648 -- This means that the with is indeed fine, in that
2649 -- it is definitely needed somewhere, and we can
2650 -- quit worrying about this one...
2652 -- Except for one little detail: if either of the
2653 -- flags was set during spec processing, this is
2654 -- where we complain that the with could be moved
2655 -- from the spec. If the spec contains a visible
2656 -- renaming of the package, inhibit warning to move
2657 -- with_clause to body.
2659 if Ekind (Munite) = E_Package_Body then
2660 Pack :=
2661 Find_Package_Renaming
2662 (Spec_Entity (Munite), Lunit);
2663 else
2664 Pack := Empty;
2665 end if;
2667 -- If a renaming is present in the spec do not warn
2668 -- because the body or child unit may depend on it.
2670 if Present (Pack)
2671 and then Renamed_Entity (Pack) = Lunit
2672 then
2673 exit;
2675 elsif Unreferenced_In_Spec (Item) then
2676 Error_Msg_N -- CODEFIX
2677 ("?u?unit& is not referenced in spec!",
2678 Name (Item));
2680 elsif No_Entities_Ref_In_Spec (Item) then
2681 Error_Msg_N -- CODEFIX
2682 ("?u?no entities of & are referenced in spec!",
2683 Name (Item));
2685 else
2686 if Ekind (Ent) = E_Package then
2687 Check_Inner_Package (Ent);
2688 end if;
2690 exit;
2691 end if;
2693 if not Is_Visible_Renaming then
2694 Error_Msg_N -- CODEFIX
2695 ("\?u?with clause might be moved to body!",
2696 Name (Item));
2697 end if;
2699 exit;
2701 -- Move to next entity to continue search
2703 else
2704 Next_Entity (Ent);
2705 end if;
2706 end loop;
2707 end if;
2709 -- For a generic package, the only interesting kind of
2710 -- reference is an instantiation, since entities cannot be
2711 -- referenced directly.
2713 elsif Is_Generic_Unit (Lunit) then
2715 -- Unit was never instantiated, set flag for case of spec
2716 -- call, or give warning for normal call.
2718 if not Is_Instantiated (Lunit) then
2719 if Unit = Spec_Unit then
2720 Set_Unreferenced_In_Spec (Item);
2721 else
2722 Error_Msg_N -- CODEFIX
2723 ("?u?unit& is never instantiated!", Name (Item));
2724 end if;
2726 -- If unit was indeed instantiated, make sure that flag is
2727 -- not set showing it was uninstantiated in the spec, and if
2728 -- so, give warning.
2730 elsif Unreferenced_In_Spec (Item) then
2731 Error_Msg_N
2732 ("?u?unit& is not instantiated in spec!", Name (Item));
2733 Error_Msg_N -- CODEFIX
2734 ("\?u?with clause can be moved to body!", Name (Item));
2735 end if;
2736 end if;
2737 end if;
2739 Next (Item);
2740 end loop;
2741 end Check_One_Unit;
2743 -- Start of processing for Check_Unused_Withs
2745 begin
2746 -- Immediate return if no semantics or warning flag not set
2748 if not Opt.Check_Withs or else Operating_Mode = Check_Syntax then
2749 return;
2750 end if;
2752 Process_Deferred_References;
2754 -- Flag any unused with clauses. For a subunit, check only the units
2755 -- in its context, not those of the parent, which may be needed by other
2756 -- subunits. We will get the full warnings when we compile the parent,
2757 -- but the following is helpful when compiling a subunit by itself.
2759 if Nkind (Unit (Cunit (Main_Unit))) = N_Subunit then
2760 if Current_Sem_Unit = Main_Unit then
2761 Check_One_Unit (Main_Unit);
2762 end if;
2764 return;
2765 end if;
2767 -- Process specified units
2769 if Spec_Unit = No_Unit then
2771 -- For main call, check all units
2773 for Unit in Main_Unit .. Last_Unit loop
2774 Check_One_Unit (Unit);
2775 end loop;
2777 else
2778 -- For call for spec, check only the spec
2780 Check_One_Unit (Spec_Unit);
2781 end if;
2782 end Check_Unused_Withs;
2784 ---------------------------------
2785 -- Generic_Package_Spec_Entity --
2786 ---------------------------------
2788 function Generic_Package_Spec_Entity (E : Entity_Id) return Boolean is
2789 S : Entity_Id;
2791 begin
2792 if Is_Package_Body_Entity (E) then
2793 return False;
2795 else
2796 S := Scope (E);
2797 loop
2798 if S = Standard_Standard then
2799 return False;
2801 elsif Ekind (S) = E_Generic_Package then
2802 return True;
2804 elsif Ekind (S) = E_Package then
2805 S := Scope (S);
2807 else
2808 return False;
2809 end if;
2810 end loop;
2811 end if;
2812 end Generic_Package_Spec_Entity;
2814 ----------------------
2815 -- Goto_Spec_Entity --
2816 ----------------------
2818 function Goto_Spec_Entity (E : Entity_Id) return Entity_Id is
2819 begin
2820 if Is_Formal (E) and then Present (Spec_Entity (E)) then
2821 return Spec_Entity (E);
2822 else
2823 return E;
2824 end if;
2825 end Goto_Spec_Entity;
2827 -------------------
2828 -- Has_Junk_Name --
2829 -------------------
2831 function Has_Junk_Name (E : Entity_Id) return Boolean is
2832 function Match (S : String) return Boolean;
2833 -- Return true if substring S is found in Name_Buffer (1 .. Name_Len)
2835 -----------
2836 -- Match --
2837 -----------
2839 function Match (S : String) return Boolean is
2840 Slen1 : constant Integer := S'Length - 1;
2842 begin
2843 for J in 1 .. Name_Len - S'Length + 1 loop
2844 if Name_Buffer (J .. J + Slen1) = S then
2845 return True;
2846 end if;
2847 end loop;
2849 return False;
2850 end Match;
2852 -- Start of processing for Has_Junk_Name
2854 begin
2855 Get_Unqualified_Decoded_Name_String (Chars (E));
2857 return
2858 Match ("discard") or else
2859 Match ("dummy") or else
2860 Match ("ignore") or else
2861 Match ("junk") or else
2862 Match ("unused");
2863 end Has_Junk_Name;
2865 --------------------------------------
2866 -- Has_Pragma_Unmodified_Check_Spec --
2867 --------------------------------------
2869 function Has_Pragma_Unmodified_Check_Spec
2870 (E : Entity_Id) return Boolean
2872 begin
2873 if Is_Formal (E) and then Present (Spec_Entity (E)) then
2875 -- Note: use of OR instead of OR ELSE here is deliberate, we want
2876 -- to mess with Unmodified flags on both body and spec entities.
2877 -- Has_Unmodified has side effects!
2879 return Has_Unmodified (E)
2881 Has_Unmodified (Spec_Entity (E));
2883 else
2884 return Has_Unmodified (E);
2885 end if;
2886 end Has_Pragma_Unmodified_Check_Spec;
2888 ----------------------------------------
2889 -- Has_Pragma_Unreferenced_Check_Spec --
2890 ----------------------------------------
2892 function Has_Pragma_Unreferenced_Check_Spec
2893 (E : Entity_Id) return Boolean
2895 begin
2896 if Is_Formal (E) and then Present (Spec_Entity (E)) then
2898 -- Note: use of OR here instead of OR ELSE is deliberate, we want
2899 -- to mess with flags on both entities.
2901 return Has_Unreferenced (E)
2903 Has_Unreferenced (Spec_Entity (E));
2905 else
2906 return Has_Unreferenced (E);
2907 end if;
2908 end Has_Pragma_Unreferenced_Check_Spec;
2910 ----------------
2911 -- Initialize --
2912 ----------------
2914 procedure Initialize is
2915 begin
2916 Warnings_Off_Pragmas.Init;
2917 Unreferenced_Entities.Init;
2918 In_Out_Warnings.Init;
2919 end Initialize;
2921 ---------------------------------------------
2922 -- Is_Attribute_And_Known_Value_Comparison --
2923 ---------------------------------------------
2925 function Is_Attribute_And_Known_Value_Comparison
2926 (Op : Node_Id) return Boolean
2928 Orig_Op : constant Node_Id := Original_Node (Op);
2930 begin
2931 return
2932 Nkind (Orig_Op) in N_Op_Compare
2933 and then Nkind (Original_Node (Left_Opnd (Orig_Op))) =
2934 N_Attribute_Reference
2935 and then Compile_Time_Known_Value (Right_Opnd (Orig_Op));
2936 end Is_Attribute_And_Known_Value_Comparison;
2938 ------------------------------------
2939 -- Never_Set_In_Source_Check_Spec --
2940 ------------------------------------
2942 function Never_Set_In_Source_Check_Spec (E : Entity_Id) return Boolean is
2943 begin
2944 if Is_Formal (E) and then Present (Spec_Entity (E)) then
2945 return Never_Set_In_Source (E)
2946 and then
2947 Never_Set_In_Source (Spec_Entity (E));
2948 else
2949 return Never_Set_In_Source (E);
2950 end if;
2951 end Never_Set_In_Source_Check_Spec;
2953 -------------------------------------
2954 -- Operand_Has_Warnings_Suppressed --
2955 -------------------------------------
2957 function Operand_Has_Warnings_Suppressed (N : Node_Id) return Boolean is
2959 function Check_For_Warnings (N : Node_Id) return Traverse_Result;
2960 -- Function used to check one node to see if it is or was originally
2961 -- a reference to an entity for which Warnings are off. If so, Abandon
2962 -- is returned, otherwise OK_Orig is returned to continue the traversal
2963 -- of the original expression.
2965 function Traverse is new Traverse_Func (Check_For_Warnings);
2966 -- Function used to traverse tree looking for warnings
2968 ------------------------
2969 -- Check_For_Warnings --
2970 ------------------------
2972 function Check_For_Warnings (N : Node_Id) return Traverse_Result is
2973 R : constant Node_Id := Original_Node (N);
2975 begin
2976 if Nkind (R) in N_Has_Entity
2977 and then Present (Entity (R))
2978 and then Has_Warnings_Off (Entity (R))
2979 then
2980 return Abandon;
2981 else
2982 return OK_Orig;
2983 end if;
2984 end Check_For_Warnings;
2986 -- Start of processing for Operand_Has_Warnings_Suppressed
2988 begin
2989 return Traverse (N) = Abandon;
2991 -- If any exception occurs, then something has gone wrong, and this is
2992 -- only a minor aesthetic issue anyway, so just say we did not find what
2993 -- we are looking for, rather than blow up.
2995 exception
2996 when others =>
2997 -- With debug flag K we will get an exception unless an error has
2998 -- already occurred (useful for debugging).
3000 if Debug_Flag_K then
3001 Check_Error_Detected;
3002 end if;
3004 return False;
3005 end Operand_Has_Warnings_Suppressed;
3007 -----------------------------------------
3008 -- Output_Non_Modified_In_Out_Warnings --
3009 -----------------------------------------
3011 procedure Output_Non_Modified_In_Out_Warnings is
3013 function No_Warn_On_In_Out (E : Entity_Id) return Boolean;
3014 -- Given a formal parameter entity E, determines if there is a reason to
3015 -- suppress IN OUT warnings (not modified, could be IN) for formals of
3016 -- the subprogram. We suppress these warnings if Warnings Off is set, or
3017 -- if we have seen the address of the subprogram being taken, or if the
3018 -- subprogram is used as a generic actual (in the latter cases the
3019 -- context may force use of IN OUT, even if the parameter is not
3020 -- modifies for this particular case.
3022 -----------------------
3023 -- No_Warn_On_In_Out --
3024 -----------------------
3026 function No_Warn_On_In_Out (E : Entity_Id) return Boolean is
3027 S : constant Entity_Id := Scope (E);
3028 SE : constant Entity_Id := Spec_Entity (E);
3030 begin
3031 -- Do not warn if address is taken, since funny business may be going
3032 -- on in treating the parameter indirectly as IN OUT.
3034 if Address_Taken (S)
3035 or else (Present (SE) and then Address_Taken (Scope (SE)))
3036 then
3037 return True;
3039 -- Do not warn if used as a generic actual, since the generic may be
3040 -- what is forcing the use of an "unnecessary" IN OUT.
3042 elsif Used_As_Generic_Actual (S)
3043 or else (Present (SE) and then Used_As_Generic_Actual (Scope (SE)))
3044 then
3045 return True;
3047 -- Else test warnings off
3049 elsif Warnings_Off_Check_Spec (S) then
3050 return True;
3052 -- All tests for suppressing warning failed
3054 else
3055 return False;
3056 end if;
3057 end No_Warn_On_In_Out;
3059 -- Start of processing for Output_Non_Modified_In_Out_Warnings
3061 begin
3062 -- Loop through entities for which a warning may be needed
3064 for J in In_Out_Warnings.First .. In_Out_Warnings.Last loop
3065 declare
3066 E1 : constant Entity_Id := In_Out_Warnings.Table (J);
3068 begin
3069 -- Suppress warning in specific cases (see details in comments for
3070 -- No_Warn_On_In_Out), or if there is a pragma Unmodified.
3072 if Has_Pragma_Unmodified_Check_Spec (E1)
3073 or else No_Warn_On_In_Out (E1)
3074 then
3075 null;
3077 -- Here we generate the warning
3079 else
3080 -- If -gnatwc is set then output message that we could be IN
3082 if not Is_Trivial_Subprogram (Scope (E1)) then
3083 if Warn_On_Constant then
3084 Error_Msg_N
3085 ("?u?formal parameter & is not modified!", E1);
3086 Error_Msg_N
3087 ("\?u?mode could be IN instead of `IN OUT`!", E1);
3089 -- We do not generate warnings for IN OUT parameters
3090 -- unless we have at least -gnatwu. This is deliberately
3091 -- inconsistent with the treatment of variables, but
3092 -- otherwise we get too many unexpected warnings in
3093 -- default mode.
3095 elsif Check_Unreferenced then
3096 Error_Msg_N
3097 ("?u?formal parameter& is read but "
3098 & "never assigned!", E1);
3099 end if;
3100 end if;
3102 -- Kill any other warnings on this entity, since this is the
3103 -- one that should dominate any other unreferenced warning.
3105 Set_Warnings_Off (E1);
3106 end if;
3107 end;
3108 end loop;
3109 end Output_Non_Modified_In_Out_Warnings;
3111 ----------------------------------------
3112 -- Output_Obsolescent_Entity_Warnings --
3113 ----------------------------------------
3115 procedure Output_Obsolescent_Entity_Warnings (N : Node_Id; E : Entity_Id) is
3116 P : constant Node_Id := Parent (N);
3117 S : Entity_Id;
3119 begin
3120 S := Current_Scope;
3122 -- Do not output message if we are the scope of standard. This means
3123 -- we have a reference from a context clause from when it is originally
3124 -- processed, and that's too early to tell whether it is an obsolescent
3125 -- unit doing the with'ing. In Sem_Ch10.Analyze_Compilation_Unit we make
3126 -- sure that we have a later call when the scope is available. This test
3127 -- also eliminates all messages for use clauses, which is fine (we do
3128 -- not want messages for use clauses, since they are always redundant
3129 -- with respect to the associated with clause).
3131 if S = Standard_Standard then
3132 return;
3133 end if;
3135 -- Do not output message if we are in scope of an obsolescent package
3136 -- or subprogram.
3138 loop
3139 if Is_Obsolescent (S) then
3140 return;
3141 end if;
3143 S := Scope (S);
3144 exit when S = Standard_Standard;
3145 end loop;
3147 -- Here we will output the message
3149 Error_Msg_Sloc := Sloc (E);
3151 -- Case of with clause
3153 if Nkind (P) = N_With_Clause then
3154 if Ekind (E) = E_Package then
3155 Error_Msg_NE
3156 ("?j?with of obsolescent package& declared#", N, E);
3157 elsif Ekind (E) = E_Procedure then
3158 Error_Msg_NE
3159 ("?j?with of obsolescent procedure& declared#", N, E);
3160 else
3161 Error_Msg_NE
3162 ("??with of obsolescent function& declared#", N, E);
3163 end if;
3165 -- If we do not have a with clause, then ignore any reference to an
3166 -- obsolescent package name. We only want to give the one warning of
3167 -- withing the package, not one each time it is used to qualify.
3169 elsif Ekind (E) = E_Package then
3170 return;
3172 -- Procedure call statement
3174 elsif Nkind (P) = N_Procedure_Call_Statement then
3175 Error_Msg_NE
3176 ("??call to obsolescent procedure& declared#", N, E);
3178 -- Function call
3180 elsif Nkind (P) = N_Function_Call then
3181 Error_Msg_NE
3182 ("??call to obsolescent function& declared#", N, E);
3184 -- Reference to obsolescent type
3186 elsif Is_Type (E) then
3187 Error_Msg_NE
3188 ("??reference to obsolescent type& declared#", N, E);
3190 -- Reference to obsolescent component
3192 elsif Ekind (E) in E_Component | E_Discriminant then
3193 Error_Msg_NE
3194 ("??reference to obsolescent component& declared#", N, E);
3196 -- Reference to obsolescent variable
3198 elsif Ekind (E) = E_Variable then
3199 Error_Msg_NE
3200 ("??reference to obsolescent variable& declared#", N, E);
3202 -- Reference to obsolescent constant
3204 elsif Ekind (E) = E_Constant or else Ekind (E) in Named_Kind then
3205 Error_Msg_NE
3206 ("??reference to obsolescent constant& declared#", N, E);
3208 -- Reference to obsolescent enumeration literal
3210 elsif Ekind (E) = E_Enumeration_Literal then
3211 Error_Msg_NE
3212 ("??reference to obsolescent enumeration literal& declared#", N, E);
3214 -- Generic message for any other case we missed
3216 else
3217 Error_Msg_NE
3218 ("??reference to obsolescent entity& declared#", N, E);
3219 end if;
3221 -- Output additional warning if present
3223 for J in Obsolescent_Warnings.First .. Obsolescent_Warnings.Last loop
3224 if Obsolescent_Warnings.Table (J).Ent = E then
3225 String_To_Name_Buffer (Obsolescent_Warnings.Table (J).Msg);
3226 Error_Msg_Strlen := Name_Len;
3227 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
3228 Error_Msg_N ("\\??~", N);
3229 exit;
3230 end if;
3231 end loop;
3232 end Output_Obsolescent_Entity_Warnings;
3234 ----------------------------------
3235 -- Output_Unreferenced_Messages --
3236 ----------------------------------
3238 procedure Output_Unreferenced_Messages is
3239 begin
3240 for J in Unreferenced_Entities.First .. Unreferenced_Entities.Last loop
3241 Warn_On_Unreferenced_Entity (Unreferenced_Entities.Table (J));
3242 end loop;
3243 end Output_Unreferenced_Messages;
3245 -----------------------------------------
3246 -- Output_Unused_Warnings_Off_Warnings --
3247 -----------------------------------------
3249 procedure Output_Unused_Warnings_Off_Warnings is
3250 begin
3251 for J in Warnings_Off_Pragmas.First .. Warnings_Off_Pragmas.Last loop
3252 declare
3253 Wentry : Warnings_Off_Entry renames Warnings_Off_Pragmas.Table (J);
3254 N : Node_Id renames Wentry.N;
3255 E : Node_Id renames Wentry.E;
3257 begin
3258 -- Turn off Warnings_Off, or we won't get the warning
3260 Set_Warnings_Off (E, False);
3262 -- Nothing to do if pragma was used to suppress a general warning
3264 if Warnings_Off_Used (E) then
3265 null;
3267 -- If pragma was used both in unmodified and unreferenced contexts
3268 -- then that's as good as the general case, no warning.
3270 elsif Warnings_Off_Used_Unmodified (E)
3272 Warnings_Off_Used_Unreferenced (E)
3273 then
3274 null;
3276 -- Used only in context where Unmodified would have worked
3278 elsif Warnings_Off_Used_Unmodified (E) then
3279 Error_Msg_NE
3280 ("?W?could use Unmodified instead of "
3281 & "Warnings Off for &", Pragma_Identifier (N), E);
3283 -- Used only in context where Unreferenced would have worked
3285 elsif Warnings_Off_Used_Unreferenced (E) then
3286 Error_Msg_NE
3287 ("?W?could use Unreferenced instead of "
3288 & "Warnings Off for &", Pragma_Identifier (N), E);
3290 -- Not used at all
3292 else
3293 Error_Msg_NE
3294 ("?W?pragma Warnings Off for & unused, "
3295 & "could be omitted", N, E);
3296 end if;
3297 end;
3298 end loop;
3299 end Output_Unused_Warnings_Off_Warnings;
3301 ---------------------------
3302 -- Referenced_Check_Spec --
3303 ---------------------------
3305 function Referenced_Check_Spec (E : Entity_Id) return Boolean is
3306 begin
3307 if Is_Formal (E) and then Present (Spec_Entity (E)) then
3308 return Referenced (E) or else Referenced (Spec_Entity (E));
3309 else
3310 return Referenced (E);
3311 end if;
3312 end Referenced_Check_Spec;
3314 ----------------------------------
3315 -- Referenced_As_LHS_Check_Spec --
3316 ----------------------------------
3318 function Referenced_As_LHS_Check_Spec (E : Entity_Id) return Boolean is
3319 begin
3320 if Is_Formal (E) and then Present (Spec_Entity (E)) then
3321 return Referenced_As_LHS (E)
3322 or else Referenced_As_LHS (Spec_Entity (E));
3323 else
3324 return Referenced_As_LHS (E);
3325 end if;
3326 end Referenced_As_LHS_Check_Spec;
3328 --------------------------------------------
3329 -- Referenced_As_Out_Parameter_Check_Spec --
3330 --------------------------------------------
3332 function Referenced_As_Out_Parameter_Check_Spec
3333 (E : Entity_Id) return Boolean
3335 begin
3336 if Is_Formal (E) and then Present (Spec_Entity (E)) then
3337 return Referenced_As_Out_Parameter (E)
3338 or else Referenced_As_Out_Parameter (Spec_Entity (E));
3339 else
3340 return Referenced_As_Out_Parameter (E);
3341 end if;
3342 end Referenced_As_Out_Parameter_Check_Spec;
3344 --------------------------------------
3345 -- Warn_On_Constant_Valid_Condition --
3346 --------------------------------------
3348 procedure Warn_On_Constant_Valid_Condition (Op : Node_Id) is
3349 Left : constant Node_Id := Left_Opnd (Op);
3350 Right : constant Node_Id := Right_Opnd (Op);
3352 True_Result : Boolean;
3353 False_Result : Boolean;
3355 begin
3356 -- Determine the potential outcome of the comparison assuming that the
3357 -- scalar operands are valid.
3359 if Constant_Condition_Warnings
3360 and then Comes_From_Source (Original_Node (Op))
3361 and then Is_Scalar_Type (Etype (Left))
3362 and then Is_Scalar_Type (Etype (Right))
3364 -- Do not consider instances because the check was already performed
3365 -- in the generic.
3367 and then not In_Instance
3369 -- Do not consider comparisons between two static expressions such as
3370 -- constants or literals because those values cannot be invalidated.
3372 and then not (Is_Static_Expression (Left)
3373 and then Is_Static_Expression (Right))
3375 -- Do not consider comparison between an attribute reference and a
3376 -- compile-time known value since this is most likely a conditional
3377 -- compilation.
3379 and then not Is_Attribute_And_Known_Value_Comparison (Op)
3381 -- Do not consider internal files to allow for various assertions and
3382 -- safeguards within our runtime.
3384 and then not In_Internal_Unit (Op)
3385 then
3386 Test_Comparison
3387 (Op => Op,
3388 Assume_Valid => True,
3389 True_Result => True_Result,
3390 False_Result => False_Result);
3392 -- Warn on a possible evaluation to False / True in the presence of
3393 -- invalid values.
3395 if True_Result then
3396 Error_Msg_N
3397 ("condition can only be False if invalid values present?c?", Op);
3399 elsif False_Result then
3400 Error_Msg_N
3401 ("condition can only be True if invalid values present?c?", Op);
3402 end if;
3403 end if;
3404 end Warn_On_Constant_Valid_Condition;
3406 -----------------------------
3407 -- Warn_On_Known_Condition --
3408 -----------------------------
3410 procedure Warn_On_Known_Condition (C : Node_Id) is
3411 Test_Result : Boolean := False;
3412 -- Force initialization to facilitate static analysis
3414 function Is_Known_Branch return Boolean;
3415 -- If the type of the condition is Boolean, the constant value of the
3416 -- condition is a boolean literal. If the type is a derived boolean
3417 -- type, the constant is wrapped in a type conversion of the derived
3418 -- literal. If the value of the condition is not a literal, no warnings
3419 -- can be produced. This function returns True if the result can be
3420 -- determined, and Test_Result is set True/False accordingly. Otherwise
3421 -- False is returned, and Test_Result is unchanged.
3423 procedure Track (N : Node_Id; Loc : Node_Id);
3424 -- Adds continuation warning(s) pointing to reason (assignment or test)
3425 -- for the operand of the conditional having a known value (or at least
3426 -- enough is known about the value to issue the warning). N is the node
3427 -- which is judged to have a known value. Loc is the warning location.
3429 ---------------------
3430 -- Is_Known_Branch --
3431 ---------------------
3433 function Is_Known_Branch return Boolean is
3434 begin
3435 if Etype (C) = Standard_Boolean
3436 and then Is_Entity_Name (C)
3437 and then
3438 (Entity (C) = Standard_False or else Entity (C) = Standard_True)
3439 then
3440 Test_Result := Entity (C) = Standard_True;
3441 return True;
3443 elsif Is_Boolean_Type (Etype (C))
3444 and then Nkind (C) = N_Unchecked_Type_Conversion
3445 and then Is_Entity_Name (Expression (C))
3446 and then Ekind (Entity (Expression (C))) = E_Enumeration_Literal
3447 then
3448 Test_Result :=
3449 Chars (Entity (Expression (C))) = Chars (Standard_True);
3450 return True;
3452 else
3453 return False;
3454 end if;
3455 end Is_Known_Branch;
3457 -----------
3458 -- Track --
3459 -----------
3461 procedure Track (N : Node_Id; Loc : Node_Id) is
3462 Nod : constant Node_Id := Original_Node (N);
3464 begin
3465 if Nkind (Nod) in N_Op_Compare then
3466 Track (Left_Opnd (Nod), Loc);
3467 Track (Right_Opnd (Nod), Loc);
3469 elsif Is_Entity_Name (Nod) and then Is_Object (Entity (Nod)) then
3470 declare
3471 CV : constant Node_Id := Current_Value (Entity (Nod));
3473 begin
3474 if Present (CV) then
3475 Error_Msg_Sloc := Sloc (CV);
3477 if Nkind (CV) not in N_Subexpr then
3478 Error_Msg_N ("\\??(see test #)", Loc);
3480 elsif Nkind (Parent (CV)) =
3481 N_Case_Statement_Alternative
3482 then
3483 Error_Msg_N ("\\??(see case alternative #)", Loc);
3485 else
3486 Error_Msg_N ("\\??(see assignment #)", Loc);
3487 end if;
3488 end if;
3489 end;
3490 end if;
3491 end Track;
3493 -- Local variables
3495 Orig : constant Node_Id := Original_Node (C);
3496 P : Node_Id;
3498 -- Start of processing for Warn_On_Known_Condition
3500 begin
3501 -- Adjust SCO condition if from source
3503 if Generate_SCO
3504 and then Comes_From_Source (Orig)
3505 and then Is_Known_Branch
3506 then
3507 declare
3508 Atrue : Boolean;
3510 begin
3511 Atrue := Test_Result;
3513 if Present (Parent (C)) and then Nkind (Parent (C)) = N_Op_Not then
3514 Atrue := not Atrue;
3515 end if;
3517 Set_SCO_Condition (Orig, Atrue);
3518 end;
3519 end if;
3521 -- Argument replacement in an inlined body can make conditions static.
3522 -- Do not emit warnings in this case.
3524 if In_Inlined_Body then
3525 return;
3526 end if;
3528 if Constant_Condition_Warnings
3529 and then Is_Known_Branch
3530 and then Comes_From_Source (Orig)
3531 and then Nkind (Orig) in N_Has_Entity
3532 and then not In_Instance
3533 then
3534 -- Don't warn if comparison of result of attribute against a constant
3535 -- value, since this is likely legitimate conditional compilation.
3537 if Is_Attribute_And_Known_Value_Comparison (C) then
3538 return;
3539 end if;
3541 -- See if this is in a statement or a declaration
3543 P := Parent (C);
3544 loop
3545 -- If tree is not attached, do not issue warning (this is very
3546 -- peculiar, and probably arises from some other error condition).
3548 if No (P) then
3549 return;
3551 -- If we are in a declaration, then no warning, since in practice
3552 -- conditionals in declarations are used for intended tests which
3553 -- may be known at compile time, e.g. things like
3555 -- x : constant Integer := 2 + (Word'Size = 32);
3557 -- And a warning is annoying in such cases
3559 elsif Nkind (P) in N_Declaration
3560 or else
3561 Nkind (P) in N_Later_Decl_Item
3562 then
3563 return;
3565 -- Don't warn in assert or check pragma, since presumably tests in
3566 -- such a context are very definitely intended, and might well be
3567 -- known at compile time. Note that we have to test the original
3568 -- node, since assert pragmas get rewritten at analysis time.
3570 elsif Nkind (Original_Node (P)) = N_Pragma
3571 and then
3572 Pragma_Name_Unmapped (Original_Node (P))
3573 in Name_Assert | Name_Check
3574 then
3575 return;
3576 end if;
3578 exit when Is_Statement (P);
3579 P := Parent (P);
3580 end loop;
3582 -- Here we issue the warning unless some sub-operand has warnings
3583 -- set off, in which case we suppress the warning for the node. If
3584 -- the original expression is an inequality, it has been expanded
3585 -- into a negation, and the value of the original expression is the
3586 -- negation of the equality. If the expression is an entity that
3587 -- appears within a negation, it is clearer to flag the negation
3588 -- itself, and report on its constant value.
3590 if not Operand_Has_Warnings_Suppressed (C) then
3591 declare
3592 True_Branch : Boolean := Test_Result;
3593 Cond : Node_Id := C;
3595 begin
3596 if Present (Parent (C))
3597 and then Nkind (Parent (C)) = N_Op_Not
3598 then
3599 True_Branch := not True_Branch;
3600 Cond := Parent (C);
3601 end if;
3603 -- Condition always True
3605 if True_Branch then
3606 if Is_Entity_Name (Original_Node (C))
3607 and then Nkind (Cond) /= N_Op_Not
3608 then
3609 Error_Msg_NE
3610 ("object & is always True at this point?c?",
3611 Cond, Original_Node (C));
3612 Track (Original_Node (C), Cond);
3614 else
3615 Error_Msg_N ("condition is always True?c?", Cond);
3616 Track (Cond, Cond);
3617 end if;
3619 -- Condition always False
3621 else
3622 if Is_Entity_Name (Original_Node (C))
3623 and then Nkind (Cond) /= N_Op_Not
3624 then
3625 Error_Msg_NE
3626 ("object & is always False at this point?c?",
3627 Cond, Original_Node (C));
3628 Track (Original_Node (C), Cond);
3630 else
3631 Error_Msg_N ("condition is always False?c?", Cond);
3632 Track (Cond, Cond);
3633 end if;
3634 end if;
3635 end;
3636 end if;
3637 end if;
3638 end Warn_On_Known_Condition;
3640 ---------------------------------------
3641 -- Warn_On_Modified_As_Out_Parameter --
3642 ---------------------------------------
3644 function Warn_On_Modified_As_Out_Parameter (E : Entity_Id) return Boolean is
3645 begin
3646 return
3647 (Warn_On_Modified_Unread and then Is_Only_Out_Parameter (E))
3648 or else Warn_On_All_Unread_Out_Parameters;
3649 end Warn_On_Modified_As_Out_Parameter;
3651 ---------------------------------
3652 -- Warn_On_Overlapping_Actuals --
3653 ---------------------------------
3655 procedure Warn_On_Overlapping_Actuals (Subp : Entity_Id; N : Node_Id) is
3656 function Refer_Same_Object
3657 (Act1 : Node_Id;
3658 Act2 : Node_Id) return Boolean;
3659 -- Two names are known to refer to the same object if the two names
3660 -- are known to denote the same object; or one of the names is a
3661 -- selected_component, indexed_component, or slice and its prefix is
3662 -- known to refer to the same object as the other name; or one of the
3663 -- two names statically denotes a renaming declaration whose renamed
3664 -- object_name is known to refer to the same object as the other name
3665 -- (RM 6.4.1(6.11/3))
3667 -----------------------
3668 -- Refer_Same_Object --
3669 -----------------------
3671 function Refer_Same_Object
3672 (Act1 : Node_Id;
3673 Act2 : Node_Id) return Boolean
3675 begin
3676 return
3677 Denotes_Same_Object (Act1, Act2)
3678 or else Denotes_Same_Prefix (Act1, Act2);
3679 end Refer_Same_Object;
3681 -- Local variables
3683 Act1 : Node_Id;
3684 Act2 : Node_Id;
3685 Form1 : Entity_Id;
3686 Form2 : Entity_Id;
3688 -- Start of processing for Warn_On_Overlapping_Actuals
3690 begin
3692 if Ada_Version < Ada_2012 and then not Warn_On_Overlap then
3693 return;
3694 end if;
3696 -- Exclude calls rewritten as enumeration literals
3698 if Nkind (N) not in N_Subprogram_Call
3699 and then Nkind (N) /= N_Entry_Call_Statement
3700 then
3701 return;
3702 end if;
3704 -- If a call C has two or more parameters of mode in out or out that are
3705 -- of an elementary type, then the call is legal only if for each name
3706 -- N that is passed as a parameter of mode in out or out to the call C,
3707 -- there is no other name among the other parameters of mode in out or
3708 -- out to C that is known to denote the same object (RM 6.4.1(6.15/3))
3709 -- This has been clarified in AI12-0216 to indicate that the illegality
3710 -- only occurs if both formals are of an elementary type, because of the
3711 -- nondeterminism on the write-back of the corresponding actuals.
3712 -- Earlier versions of the language made it illegal if only one of the
3713 -- actuals was an elementary parameter that overlapped a composite
3714 -- actual, and both were writable.
3716 -- If appropriate warning switch is set, we also report warnings on
3717 -- overlapping parameters that are composite types. Users find these
3718 -- warnings useful, and they are used in style guides.
3720 -- It is also worthwhile to warn on overlaps of composite objects when
3721 -- only one of the formals is (in)-out. Note that the RM rule above is
3722 -- a legality rule. We choose to implement this check as a warning to
3723 -- avoid major incompatibilities with legacy code.
3725 -- Note also that the rule in 6.4.1 (6.17/3), introduced by AI12-0324,
3726 -- is potentially more expensive to verify, and is not yet implemented.
3728 Form1 := First_Formal (Subp);
3729 Act1 := First_Actual (N);
3730 while Present (Form1) and then Present (Act1) loop
3731 if Is_Generic_Type (Etype (Act1)) then
3732 return;
3733 end if;
3735 -- One of the formals must be either (in)-out or composite.
3736 -- The other must be (in)-out.
3738 if Is_Elementary_Type (Etype (Act1))
3739 and then Ekind (Form1) = E_In_Parameter
3740 then
3741 null;
3743 else
3744 Form2 := Next_Formal (Form1);
3745 Act2 := Next_Actual (Act1);
3746 while Present (Form2) and then Present (Act2) loop
3747 if Refer_Same_Object (Act1, Act2) then
3748 if Is_Generic_Type (Etype (Act2)) then
3749 return;
3750 end if;
3752 -- First case : two writable elementary parameters
3753 -- that overlap.
3755 if (Is_Elementary_Type (Etype (Form1))
3756 and then Is_Elementary_Type (Etype (Form2))
3757 and then Ekind (Form1) /= E_In_Parameter
3758 and then Ekind (Form2) /= E_In_Parameter)
3760 -- Second case : two composite parameters that overlap,
3761 -- one of which is writable.
3763 or else (Is_Composite_Type (Etype (Form1))
3764 and then Is_Composite_Type (Etype (Form2))
3765 and then (Ekind (Form1) /= E_In_Parameter
3766 or else Ekind (Form2) /= E_In_Parameter))
3768 -- Third case : an elementary writable parameter that
3769 -- overlaps a composite one.
3771 or else (Is_Elementary_Type (Etype (Form1))
3772 and then Ekind (Form1) /= E_In_Parameter
3773 and then Is_Composite_Type (Etype (Form2)))
3775 or else (Is_Elementary_Type (Etype (Form2))
3776 and then Ekind (Form2) /= E_In_Parameter
3777 and then Is_Composite_Type (Etype (Form1)))
3778 then
3780 -- Guard against previous errors
3782 if Error_Posted (N)
3783 or else No (Etype (Act1))
3784 or else No (Etype (Act2))
3785 then
3786 null;
3788 -- If the actual is a function call in prefix notation,
3789 -- there is no real overlap.
3791 elsif Nkind (Act2) = N_Function_Call then
3792 null;
3794 -- If type is explicitly not by-copy, assume that
3795 -- aliasing is intended.
3797 elsif
3798 Present (Underlying_Type (Etype (Form1)))
3799 and then
3800 (Is_By_Reference_Type
3801 (Underlying_Type (Etype (Form1)))
3802 or else
3803 Convention (Underlying_Type (Etype (Form1))) =
3804 Convention_Ada_Pass_By_Reference)
3805 then
3806 null;
3808 -- Under Ada 2012 we only report warnings on overlapping
3809 -- arrays and record types if switch is set.
3811 elsif Ada_Version >= Ada_2012
3812 and then not Is_Elementary_Type (Etype (Form1))
3813 and then not Warn_On_Overlap
3814 then
3815 null;
3817 -- Here we may need to issue overlap message
3819 else
3820 Error_Msg_Warn :=
3822 -- Overlap checking is an error only in Ada 2012.
3823 -- For earlier versions of Ada, this is a warning.
3825 Ada_Version < Ada_2012
3827 -- Overlap is only illegal in Ada 2012 in the case
3828 -- of elementary types (passed by copy). For other
3829 -- types we always have a warning in all versions.
3830 -- This is clarified by AI12-0216.
3832 or else not
3833 (Is_Elementary_Type (Etype (Form1))
3834 and then Is_Elementary_Type (Etype (Form2)))
3836 -- debug flag -gnatd.E changes the error to a
3837 -- warning even in Ada 2012 mode.
3839 or else Error_To_Warning;
3841 if Is_Elementary_Type (Etype (Act1))
3842 and then Ekind (Form2) = E_In_Parameter
3843 then
3844 null; -- No real aliasing
3846 elsif Is_Elementary_Type (Etype (Act2))
3847 and then Ekind (Form2) = E_In_Parameter
3848 then
3849 null; -- Ditto
3851 -- If the call was written in prefix notation, and
3852 -- thus its prefix before rewriting was a selected
3853 -- component, count only visible actuals in call.
3855 elsif Is_Entity_Name (First_Actual (N))
3856 and then Nkind (Original_Node (N)) = Nkind (N)
3857 and then Nkind (Name (Original_Node (N))) =
3858 N_Selected_Component
3859 and then
3860 Is_Entity_Name (Prefix (Name (Original_Node (N))))
3861 and then
3862 Entity (Prefix (Name (Original_Node (N)))) =
3863 Entity (First_Actual (N))
3864 then
3865 if Act1 = First_Actual (N) then
3866 Error_Msg_FE
3867 ("<I<`IN OUT` prefix overlaps with "
3868 & "actual for&", Act1, Form2);
3870 else
3871 -- For greater clarity, give name of formal
3873 Error_Msg_Node_2 := Form2;
3874 Error_Msg_FE
3875 ("<I<writable actual for & overlaps with "
3876 & "actual for&", Act1, Form2);
3877 end if;
3879 else
3880 -- For greater clarity, give name of formal
3882 Error_Msg_Node_2 := Form2;
3884 -- This is one of the messages
3886 Error_Msg_FE
3887 ("<I<writable actual for & overlaps with "
3888 & "actual for&", Act1, Form1);
3889 end if;
3890 end if;
3891 end if;
3893 return;
3894 end if;
3896 Next_Formal (Form2);
3897 Next_Actual (Act2);
3898 end loop;
3899 end if;
3901 Next_Formal (Form1);
3902 Next_Actual (Act1);
3903 end loop;
3904 end Warn_On_Overlapping_Actuals;
3906 ------------------------------
3907 -- Warn_On_Suspicious_Index --
3908 ------------------------------
3910 procedure Warn_On_Suspicious_Index (Name : Entity_Id; X : Node_Id) is
3912 Low_Bound : Uint;
3913 -- Set to lower bound for a suspicious type
3915 Ent : Entity_Id;
3916 -- Entity for array reference
3918 Typ : Entity_Id;
3919 -- Array type
3921 function Is_Suspicious_Type (Typ : Entity_Id) return Boolean;
3922 -- Tests to see if Typ is a type for which we may have a suspicious
3923 -- index, namely an unconstrained array type, whose lower bound is
3924 -- either zero or one. If so, True is returned, and Low_Bound is set
3925 -- to this lower bound. If not, False is returned, and Low_Bound is
3926 -- undefined on return.
3928 -- For now, we limit this to standard string types, so any other
3929 -- unconstrained types return False. We may change our minds on this
3930 -- later on, but strings seem the most important case.
3932 procedure Test_Suspicious_Index;
3933 -- Test if index is of suspicious type and if so, generate warning
3935 ------------------------
3936 -- Is_Suspicious_Type --
3937 ------------------------
3939 function Is_Suspicious_Type (Typ : Entity_Id) return Boolean is
3940 LB : Node_Id;
3942 begin
3943 if Is_Array_Type (Typ)
3944 and then not Is_Constrained (Typ)
3945 and then Number_Dimensions (Typ) = 1
3946 and then Is_Standard_String_Type (Typ)
3947 and then not Has_Warnings_Off (Typ)
3948 then
3949 LB := Type_Low_Bound (Etype (First_Index (Typ)));
3951 if Compile_Time_Known_Value (LB) then
3952 Low_Bound := Expr_Value (LB);
3953 return Low_Bound = Uint_0 or else Low_Bound = Uint_1;
3954 end if;
3955 end if;
3957 return False;
3958 end Is_Suspicious_Type;
3960 ---------------------------
3961 -- Test_Suspicious_Index --
3962 ---------------------------
3964 procedure Test_Suspicious_Index is
3966 function Length_Reference (N : Node_Id) return Boolean;
3967 -- Check if node N is of the form Name'Length
3969 procedure Warn1;
3970 -- Generate first warning line
3972 procedure Warn_On_Index_Below_Lower_Bound;
3973 -- Generate a warning on indexing the array with a literal value
3974 -- below the lower bound of the index type.
3976 procedure Warn_On_Literal_Index;
3977 -- Generate a warning on indexing the array with a literal value
3979 ----------------------
3980 -- Length_Reference --
3981 ----------------------
3983 function Length_Reference (N : Node_Id) return Boolean is
3984 R : constant Node_Id := Original_Node (N);
3985 begin
3986 return
3987 Nkind (R) = N_Attribute_Reference
3988 and then Attribute_Name (R) = Name_Length
3989 and then Is_Entity_Name (Prefix (R))
3990 and then Entity (Prefix (R)) = Ent;
3991 end Length_Reference;
3993 -----------
3994 -- Warn1 --
3995 -----------
3997 procedure Warn1 is
3998 begin
3999 Error_Msg_Uint_1 := Low_Bound;
4000 Error_Msg_FE -- CODEFIX
4001 ("?w?index for& may assume lower bound of^", X, Ent);
4002 end Warn1;
4004 -------------------------------------
4005 -- Warn_On_Index_Below_Lower_Bound --
4006 -------------------------------------
4008 procedure Warn_On_Index_Below_Lower_Bound is
4009 begin
4010 if Is_Standard_String_Type (Typ) then
4011 Discard_Node
4012 (Compile_Time_Constraint_Error
4013 (N => X,
4014 Msg => "?w?string index should be positive"));
4015 else
4016 Discard_Node
4017 (Compile_Time_Constraint_Error
4018 (N => X,
4019 Msg => "?w?index out of the allowed range"));
4020 end if;
4021 end Warn_On_Index_Below_Lower_Bound;
4023 ---------------------------
4024 -- Warn_On_Literal_Index --
4025 ---------------------------
4027 procedure Warn_On_Literal_Index is
4028 begin
4029 Warn1;
4031 -- Case where original form of subscript is an integer literal
4033 if Nkind (Original_Node (X)) = N_Integer_Literal then
4034 if Intval (X) = Low_Bound then
4035 Error_Msg_FE -- CODEFIX
4036 ("\?w?suggested replacement: `&''First`", X, Ent);
4037 else
4038 Error_Msg_Uint_1 := Intval (X) - Low_Bound;
4039 Error_Msg_FE -- CODEFIX
4040 ("\?w?suggested replacement: `&''First + ^`", X, Ent);
4042 end if;
4044 -- Case where original form of subscript is more complex
4046 else
4047 -- Build string X'First - 1 + expression where the expression
4048 -- is the original subscript. If the expression starts with "1
4049 -- + ", then the "- 1 + 1" is elided.
4051 Error_Msg_String (1 .. 13) := "'First - 1 + ";
4052 Error_Msg_Strlen := 13;
4054 declare
4055 Sref : Source_Ptr := Sloc (First_Node (Original_Node (X)));
4056 Tref : constant Source_Buffer_Ptr :=
4057 Source_Text (Get_Source_File_Index (Sref));
4058 -- Tref (Sref) is used to scan the subscript
4060 Pctr : Natural;
4061 -- Parentheses counter when scanning subscript
4063 begin
4064 -- Tref (Sref) points to start of subscript
4066 -- Elide - 1 if subscript starts with 1 +
4068 if Tref (Sref .. Sref + 2) = "1 +" then
4069 Error_Msg_Strlen := Error_Msg_Strlen - 6;
4070 Sref := Sref + 2;
4072 elsif Tref (Sref .. Sref + 1) = "1+" then
4073 Error_Msg_Strlen := Error_Msg_Strlen - 6;
4074 Sref := Sref + 1;
4075 end if;
4077 -- Now we will copy the subscript to the string buffer
4079 Pctr := 0;
4080 loop
4081 -- Count parens, exit if terminating right paren. Note
4082 -- check to ignore paren appearing as character literal.
4084 if Tref (Sref + 1) = '''
4085 and then
4086 Tref (Sref - 1) = '''
4087 then
4088 null;
4089 else
4090 if Tref (Sref) = '(' then
4091 Pctr := Pctr + 1;
4092 elsif Tref (Sref) = ')' then
4093 exit when Pctr = 0;
4094 Pctr := Pctr - 1;
4095 end if;
4096 end if;
4098 -- Done if terminating double dot (slice case)
4100 exit when Pctr = 0
4101 and then (Tref (Sref .. Sref + 1) = ".."
4102 or else
4103 Tref (Sref .. Sref + 2) = " ..");
4105 -- Quit if we have hit EOF character, something wrong
4107 if Tref (Sref) = EOF then
4108 return;
4109 end if;
4111 -- String literals are too much of a pain to handle
4113 if Tref (Sref) = '"' or else Tref (Sref) = '%' then
4114 return;
4115 end if;
4117 -- If we have a 'Range reference, then this is a case
4118 -- where we cannot easily give a replacement. Don't try.
4120 if Tref (Sref .. Sref + 4) = "range"
4121 and then Tref (Sref - 1) < 'A'
4122 and then Tref (Sref + 5) < 'A'
4123 then
4124 return;
4125 end if;
4127 -- Else store next character
4129 Error_Msg_Strlen := Error_Msg_Strlen + 1;
4130 Error_Msg_String (Error_Msg_Strlen) := Tref (Sref);
4131 Sref := Sref + 1;
4133 -- If we get more than 40 characters then the expression
4134 -- is too long to copy, or something has gone wrong. In
4135 -- either case, just skip the attempt at a suggested fix.
4137 if Error_Msg_Strlen > 40 then
4138 return;
4139 end if;
4140 end loop;
4141 end;
4143 -- Replacement subscript is now in string buffer
4145 Error_Msg_FE -- CODEFIX
4146 ("\?w?suggested replacement: `&~`", Original_Node (X), Ent);
4147 end if;
4148 end Warn_On_Literal_Index;
4150 -- Start of processing for Test_Suspicious_Index
4152 begin
4153 -- Nothing to do if subscript does not come from source (we don't
4154 -- want to give garbage warnings on compiler expanded code, e.g. the
4155 -- loops generated for slice assignments. Such junk warnings would
4156 -- be placed on source constructs with no subscript in sight).
4158 if not Comes_From_Source (Original_Node (X)) then
4159 return;
4160 end if;
4162 -- Case where subscript is a constant integer
4164 if Nkind (X) = N_Integer_Literal then
4166 -- Case where subscript is lower than the lowest possible bound.
4167 -- This might be the case for example when programmers try to
4168 -- access a string at index 0, as they are used to in other
4169 -- programming languages like C.
4171 if Intval (X) < Low_Bound then
4172 Warn_On_Index_Below_Lower_Bound;
4173 else
4174 Warn_On_Literal_Index;
4175 end if;
4177 -- Case where subscript is of the form X'Length
4179 elsif Length_Reference (X) then
4180 Warn1;
4181 Error_Msg_Node_2 := Ent;
4182 Error_Msg_FE
4183 ("\?w?suggest replacement of `&''Length` by `&''Last`",
4184 X, Ent);
4186 -- Case where subscript is of the form X'Length - expression
4188 elsif Nkind (X) = N_Op_Subtract
4189 and then Length_Reference (Left_Opnd (X))
4190 then
4191 Warn1;
4192 Error_Msg_Node_2 := Ent;
4193 Error_Msg_FE
4194 ("\?w?suggest replacement of `&''Length` by `&''Last`",
4195 Left_Opnd (X), Ent);
4196 end if;
4197 end Test_Suspicious_Index;
4199 -- Start of processing for Warn_On_Suspicious_Index
4201 begin
4202 -- Only process if warnings activated
4204 if Warn_On_Assumed_Low_Bound then
4206 -- Test if array is simple entity name
4208 if Is_Entity_Name (Name) then
4210 -- Test if array is parameter of unconstrained string type
4212 Ent := Entity (Name);
4213 Typ := Etype (Ent);
4215 if Is_Formal (Ent)
4216 and then Is_Suspicious_Type (Typ)
4217 and then not Low_Bound_Tested (Ent)
4218 then
4219 Test_Suspicious_Index;
4220 end if;
4221 end if;
4222 end if;
4223 end Warn_On_Suspicious_Index;
4225 -------------------------------
4226 -- Warn_On_Suspicious_Update --
4227 -------------------------------
4229 procedure Warn_On_Suspicious_Update (N : Node_Id) is
4230 Par : constant Node_Id := Parent (N);
4231 Arg : Node_Id;
4233 begin
4234 -- Only process if warnings activated
4236 if Warn_On_Suspicious_Contract then
4237 if Nkind (Par) in N_Op_Eq | N_Op_Ne then
4238 if N = Left_Opnd (Par) then
4239 Arg := Right_Opnd (Par);
4240 else
4241 Arg := Left_Opnd (Par);
4242 end if;
4244 if Same_Object (Prefix (N), Arg) then
4245 if Nkind (Par) = N_Op_Eq then
4246 Error_Msg_N
4247 ("suspicious equality test with modified version of "
4248 & "same object?T?", Par);
4249 else
4250 Error_Msg_N
4251 ("suspicious inequality test with modified version of "
4252 & "same object?T?", Par);
4253 end if;
4254 end if;
4255 end if;
4256 end if;
4257 end Warn_On_Suspicious_Update;
4259 --------------------------------------
4260 -- Warn_On_Unassigned_Out_Parameter --
4261 --------------------------------------
4263 procedure Warn_On_Unassigned_Out_Parameter
4264 (Return_Node : Node_Id;
4265 Scope_Id : Entity_Id)
4267 Form : Entity_Id;
4268 Form2 : Entity_Id;
4270 begin
4271 -- Ignore if procedure or return statement does not come from source
4273 if not Comes_From_Source (Scope_Id)
4274 or else not Comes_From_Source (Return_Node)
4275 then
4276 return;
4277 end if;
4279 -- Loop through formals
4281 Form := First_Formal (Scope_Id);
4282 while Present (Form) loop
4284 -- We are only interested in OUT parameters that come from source
4285 -- and are never set in the source, and furthermore only in scalars
4286 -- since non-scalars generate too many false positives.
4288 if Ekind (Form) = E_Out_Parameter
4289 and then Never_Set_In_Source_Check_Spec (Form)
4290 and then Is_Scalar_Type (Etype (Form))
4291 and then not Present (Unset_Reference (Form))
4292 then
4293 -- Before we issue the warning, an add ad hoc defence against the
4294 -- most common case of false positives with this warning which is
4295 -- the case where there is a Boolean OUT parameter that has been
4296 -- set, and whose meaning is "ignore the values of the other
4297 -- parameters". We can't of course reliably tell this case at
4298 -- compile time, but the following test kills a lot of false
4299 -- positives, without generating a significant number of false
4300 -- negatives (missed real warnings).
4302 Form2 := First_Formal (Scope_Id);
4303 while Present (Form2) loop
4304 if Ekind (Form2) = E_Out_Parameter
4305 and then Root_Type (Etype (Form2)) = Standard_Boolean
4306 and then not Never_Set_In_Source_Check_Spec (Form2)
4307 then
4308 return;
4309 end if;
4311 Next_Formal (Form2);
4312 end loop;
4314 -- Here all conditions are met, record possible unset reference
4316 Set_Unset_Reference (Form, Return_Node);
4317 end if;
4319 Next_Formal (Form);
4320 end loop;
4321 end Warn_On_Unassigned_Out_Parameter;
4323 ---------------------------------
4324 -- Warn_On_Unreferenced_Entity --
4325 ---------------------------------
4327 procedure Warn_On_Unreferenced_Entity
4328 (Spec_E : Entity_Id;
4329 Body_E : Entity_Id := Empty)
4331 E : Entity_Id := Spec_E;
4333 begin
4334 if not Referenced_Check_Spec (E)
4335 and then not Has_Pragma_Unreferenced_Check_Spec (E)
4336 and then not Warnings_Off_Check_Spec (E)
4337 and then not Has_Junk_Name (Spec_E)
4338 and then not Is_Exported (Spec_E)
4339 then
4340 case Ekind (E) is
4341 when E_Variable =>
4343 -- Case of variable that is assigned but not read. We suppress
4344 -- the message if the variable is volatile, has an address
4345 -- clause, is aliased, or is a renaming, or is imported.
4347 if Referenced_As_LHS_Check_Spec (E) then
4348 if Warn_On_Modified_Unread
4349 and then No (Address_Clause (E))
4350 and then not Is_Volatile (E)
4351 and then not Is_Imported (E)
4352 and then not Is_Aliased (E)
4353 and then No (Renamed_Object (E))
4354 then
4355 if not Has_Pragma_Unmodified_Check_Spec (E) then
4356 Error_Msg_N -- CODEFIX
4357 ("?m?variable & is assigned but never read!", E);
4358 end if;
4360 Set_Last_Assignment (E, Empty);
4361 end if;
4363 -- Normal case of neither assigned nor read (exclude variables
4364 -- referenced as out parameters, since we already generated
4365 -- appropriate warnings at the call point in this case).
4367 elsif not Referenced_As_Out_Parameter (E) then
4369 -- We suppress the message for types for which a valid
4370 -- pragma Unreferenced_Objects has been given, otherwise
4371 -- we go ahead and give the message.
4373 if not Has_Pragma_Unreferenced_Objects (Etype (E)) then
4375 -- Distinguish renamed case in message
4377 if Present (Renamed_Object (E))
4378 and then Comes_From_Source (Renamed_Object (E))
4379 then
4380 Error_Msg_N -- CODEFIX
4381 ("?u?renamed variable & is not referenced!", E);
4382 else
4383 Error_Msg_N -- CODEFIX
4384 ("?u?variable & is not referenced!", E);
4385 end if;
4386 end if;
4387 end if;
4389 when E_Constant =>
4390 if not Has_Pragma_Unreferenced_Objects (Etype (E)) then
4391 if Present (Renamed_Object (E))
4392 and then Comes_From_Source (Renamed_Object (E))
4393 then
4394 Error_Msg_N -- CODEFIX
4395 ("?u?renamed constant & is not referenced!", E);
4396 else
4397 Error_Msg_N -- CODEFIX
4398 ("?u?constant & is not referenced!", E);
4399 end if;
4400 end if;
4402 when E_In_Out_Parameter
4403 | E_In_Parameter
4405 -- Do not emit message for formals of a renaming, because they
4406 -- are never referenced explicitly.
4408 if Nkind (Original_Node (Unit_Declaration_Node (Scope (E)))) /=
4409 N_Subprogram_Renaming_Declaration
4410 then
4411 -- Suppress this message for an IN OUT parameter of a
4412 -- non-scalar type, since it is normal to have only an
4413 -- assignment in such a case.
4415 if Ekind (E) = E_In_Parameter
4416 or else not Referenced_As_LHS_Check_Spec (E)
4417 or else Is_Scalar_Type (Etype (E))
4418 then
4419 if Present (Body_E) then
4420 E := Body_E;
4421 end if;
4423 declare
4424 S : Node_Id := Scope (E);
4425 begin
4426 if Ekind (S) = E_Subprogram_Body then
4427 S := Parent (S);
4429 while Nkind (S) not in
4430 N_Expression_Function |
4431 N_Subprogram_Body |
4432 N_Subprogram_Renaming_Declaration |
4433 N_Empty
4434 loop
4435 S := Parent (S);
4436 end loop;
4438 if Present (S) then
4439 S := Corresponding_Spec (S);
4440 end if;
4441 end if;
4443 -- Do not warn for dispatching operations, because
4444 -- that causes too much noise. Also do not warn for
4445 -- trivial subprograms (e.g. stubs).
4447 if (No (S) or else not Is_Dispatching_Operation (S))
4448 and then not Is_Trivial_Subprogram (Scope (E))
4449 then
4450 Error_Msg_NE -- CODEFIX
4451 ("?u?formal parameter & is not referenced!",
4452 E, Spec_E);
4453 end if;
4454 end;
4455 end if;
4456 end if;
4458 when E_Out_Parameter =>
4459 null;
4461 when E_Discriminant =>
4462 Error_Msg_N ("?u?discriminant & is not referenced!", E);
4464 when E_Named_Integer
4465 | E_Named_Real
4467 Error_Msg_N -- CODEFIX
4468 ("?u?named number & is not referenced!", E);
4470 when Formal_Object_Kind =>
4471 Error_Msg_N -- CODEFIX
4472 ("?u?formal object & is not referenced!", E);
4474 when E_Enumeration_Literal =>
4475 Error_Msg_N -- CODEFIX
4476 ("?u?literal & is not referenced!", E);
4478 when E_Function =>
4479 Error_Msg_N -- CODEFIX
4480 ("?u?function & is not referenced!", E);
4482 when E_Procedure =>
4483 Error_Msg_N -- CODEFIX
4484 ("?u?procedure & is not referenced!", E);
4486 when E_Package =>
4487 Error_Msg_N -- CODEFIX
4488 ("?u?package & is not referenced!", E);
4490 when E_Exception =>
4491 Error_Msg_N -- CODEFIX
4492 ("?u?exception & is not referenced!", E);
4494 when E_Label =>
4495 Error_Msg_N -- CODEFIX
4496 ("?u?label & is not referenced!", E);
4498 when E_Generic_Procedure =>
4499 Error_Msg_N -- CODEFIX
4500 ("?u?generic procedure & is never instantiated!", E);
4502 when E_Generic_Function =>
4503 Error_Msg_N -- CODEFIX
4504 ("?u?generic function & is never instantiated!", E);
4506 when Type_Kind =>
4507 Error_Msg_N -- CODEFIX
4508 ("?u?type & is not referenced!", E);
4510 when others =>
4511 Error_Msg_N -- CODEFIX
4512 ("?u?& is not referenced!", E);
4513 end case;
4515 -- Kill warnings on the entity on which the message has been posted
4516 -- (nothing is posted on out parameters because back end might be
4517 -- able to uncover an uninitialized path, and warn accordingly).
4519 if Ekind (E) /= E_Out_Parameter then
4520 Set_Warnings_Off (E);
4521 end if;
4522 end if;
4523 end Warn_On_Unreferenced_Entity;
4525 --------------------------------
4526 -- Warn_On_Useless_Assignment --
4527 --------------------------------
4529 procedure Warn_On_Useless_Assignment
4530 (Ent : Entity_Id;
4531 N : Node_Id := Empty)
4533 P : Node_Id;
4534 X : Node_Id;
4536 function Check_Ref (N : Node_Id) return Traverse_Result;
4537 -- Used to instantiate Traverse_Func. Returns Abandon if a reference to
4538 -- the entity in question is found.
4540 function Test_No_Refs is new Traverse_Func (Check_Ref);
4542 ---------------
4543 -- Check_Ref --
4544 ---------------
4546 function Check_Ref (N : Node_Id) return Traverse_Result is
4547 begin
4548 -- Check reference to our identifier. We use name equality here
4549 -- because the exception handlers have not yet been analyzed. This
4550 -- is not quite right, but it really does not matter that we fail
4551 -- to output the warning in some obscure cases of name clashes.
4553 if Nkind (N) = N_Identifier and then Chars (N) = Chars (Ent) then
4554 return Abandon;
4555 else
4556 return OK;
4557 end if;
4558 end Check_Ref;
4560 -- Start of processing for Warn_On_Useless_Assignment
4562 begin
4563 -- Check if this is a case we want to warn on, a scalar or access
4564 -- variable with the last assignment field set, with warnings enabled,
4565 -- and which is not imported or exported. We also check that it is OK
4566 -- to capture the value. We are not going to capture any value, but
4567 -- the warning message depends on the same kind of conditions.
4569 -- If the assignment appears as an out-parameter in a call within an
4570 -- expression function it may be detected twice: once when expression
4571 -- itself is analyzed, and once when the constructed body is analyzed.
4572 -- We don't want to emit a spurious warning in this case.
4574 if Is_Assignable (Ent)
4575 and then not Is_Return_Object (Ent)
4576 and then Present (Last_Assignment (Ent))
4577 and then Last_Assignment (Ent) /= N
4578 and then not Is_Imported (Ent)
4579 and then not Is_Exported (Ent)
4580 and then Safe_To_Capture_Value (N, Ent)
4581 and then not Has_Pragma_Unreferenced_Check_Spec (Ent)
4582 and then not Has_Junk_Name (Ent)
4583 then
4584 -- Before we issue the message, check covering exception handlers.
4585 -- Search up tree for enclosing statement sequences and handlers.
4587 P := Parent (Last_Assignment (Ent));
4588 while Present (P) loop
4590 -- Something is really wrong if we don't find a handled statement
4591 -- sequence, so just suppress the warning.
4593 if No (P) then
4594 Set_Last_Assignment (Ent, Empty);
4595 return;
4597 -- When we hit a package/subprogram body, issue warning and exit
4599 elsif Nkind (P) in N_Entry_Body
4600 | N_Package_Body
4601 | N_Subprogram_Body
4602 | N_Task_Body
4603 then
4604 -- Case of assigned value never referenced
4606 if No (N) then
4607 declare
4608 LA : constant Node_Id := Last_Assignment (Ent);
4610 begin
4611 -- Don't give this for OUT and IN OUT formals, since
4612 -- clearly caller may reference the assigned value. Also
4613 -- never give such warnings for internal variables. In
4614 -- either case, word the warning in a conditional way,
4615 -- because in the case of a component of a controlled
4616 -- type, the assigned value might be referenced in the
4617 -- Finalize operation, so we can't make a definitive
4618 -- statement that it's never referenced.
4620 if Ekind (Ent) = E_Variable
4621 and then not Is_Internal_Name (Chars (Ent))
4622 then
4623 -- Give appropriate message, distinguishing between
4624 -- assignment statements and out parameters.
4626 if Nkind (Parent (LA)) in N_Parameter_Association
4627 | N_Procedure_Call_Statement
4628 then
4629 Error_Msg_NE
4630 ("?m?& modified by call, but value might not be "
4631 & "referenced", LA, Ent);
4633 else
4634 Error_Msg_NE -- CODEFIX
4635 ("?m?possibly useless assignment to&, value "
4636 & "might not be referenced!", LA, Ent);
4637 end if;
4638 end if;
4639 end;
4641 -- Case of assigned value overwritten
4643 else
4644 declare
4645 LA : constant Node_Id := Last_Assignment (Ent);
4647 begin
4648 Error_Msg_Sloc := Sloc (N);
4650 -- Give appropriate message, distinguishing between
4651 -- assignment statements and out parameters.
4653 if Nkind (Parent (LA)) in N_Procedure_Call_Statement
4654 | N_Parameter_Association
4655 then
4656 Error_Msg_NE
4657 ("?m?& modified by call, but value overwritten #!",
4658 LA, Ent);
4659 else
4660 Error_Msg_NE -- CODEFIX
4661 ("?m?useless assignment to&, value overwritten #!",
4662 LA, Ent);
4663 end if;
4664 end;
4665 end if;
4667 -- Clear last assignment indication and we are done
4669 Set_Last_Assignment (Ent, Empty);
4670 return;
4672 -- Enclosing handled sequence of statements
4674 elsif Nkind (P) = N_Handled_Sequence_Of_Statements then
4676 -- Check exception handlers present
4678 if Present (Exception_Handlers (P)) then
4680 -- If we are not at the top level, we regard an inner
4681 -- exception handler as a decisive indicator that we should
4682 -- not generate the warning, since the variable in question
4683 -- may be accessed after an exception in the outer block.
4685 if Nkind (Parent (P)) not in N_Entry_Body
4686 | N_Package_Body
4687 | N_Subprogram_Body
4688 | N_Task_Body
4689 then
4690 Set_Last_Assignment (Ent, Empty);
4691 return;
4693 -- Otherwise we are at the outer level. An exception
4694 -- handler is significant only if it references the
4695 -- variable in question, or if the entity in question
4696 -- is an OUT or IN OUT parameter, in which case
4697 -- the caller can reference it after the exception
4698 -- handler completes.
4700 else
4701 if Is_Formal (Ent) then
4702 Set_Last_Assignment (Ent, Empty);
4703 return;
4705 else
4706 X := First (Exception_Handlers (P));
4707 while Present (X) loop
4708 if Test_No_Refs (X) = Abandon then
4709 Set_Last_Assignment (Ent, Empty);
4710 return;
4711 end if;
4713 Next (X);
4714 end loop;
4715 end if;
4716 end if;
4717 end if;
4718 end if;
4720 P := Parent (P);
4721 end loop;
4722 end if;
4723 end Warn_On_Useless_Assignment;
4725 ---------------------------------
4726 -- Warn_On_Useless_Assignments --
4727 ---------------------------------
4729 procedure Warn_On_Useless_Assignments (E : Entity_Id) is
4730 Ent : Entity_Id;
4732 begin
4733 Process_Deferred_References;
4735 if Warn_On_Modified_Unread
4736 and then In_Extended_Main_Source_Unit (E)
4737 then
4738 Ent := First_Entity (E);
4739 while Present (Ent) loop
4740 Warn_On_Useless_Assignment (Ent);
4741 Next_Entity (Ent);
4742 end loop;
4743 end if;
4744 end Warn_On_Useless_Assignments;
4746 -----------------------------
4747 -- Warnings_Off_Check_Spec --
4748 -----------------------------
4750 function Warnings_Off_Check_Spec (E : Entity_Id) return Boolean is
4751 begin
4752 if Is_Formal (E) and then Present (Spec_Entity (E)) then
4754 -- Note: use of OR here instead of OR ELSE is deliberate, we want
4755 -- to mess with flags on both entities.
4757 return Has_Warnings_Off (E)
4759 Has_Warnings_Off (Spec_Entity (E));
4761 else
4762 return Has_Warnings_Off (E);
4763 end if;
4764 end Warnings_Off_Check_Spec;
4766 end Sem_Warn;