Debian Codegraph @

gcc/ada/sem_warn.adb

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