Merge -r 127928:132243 from trunk
[official-gcc.git] / gcc / ada / sem_warn.adb
blob46a6954bc21b3f5a413df0334376b839781761e0
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-2007, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Alloc;
27 with Atree; use Atree;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Errout; use Errout;
31 with Exp_Code; use Exp_Code;
32 with Fname; use Fname;
33 with Lib; use Lib;
34 with Namet; use Namet;
35 with Nlists; use Nlists;
36 with Opt; use Opt;
37 with Rtsfind; use Rtsfind;
38 with Sem; use Sem;
39 with Sem_Ch8; use Sem_Ch8;
40 with Sem_Eval; use Sem_Eval;
41 with Sem_Util; use Sem_Util;
42 with Sinfo; use Sinfo;
43 with Sinput; use Sinput;
44 with Snames; use Snames;
45 with Stand; use Stand;
46 with Stringt; use Stringt;
47 with Table;
48 with Uintp; use Uintp;
50 package body Sem_Warn is
52 -- The following table collects Id's of entities that are potentially
53 -- unreferenced. See Check_Unset_Reference for further details.
54 -- ??? Check_Unset_Reference has zero information about this table.
56 package Unreferenced_Entities is new Table.Table (
57 Table_Component_Type => Entity_Id,
58 Table_Index_Type => Nat,
59 Table_Low_Bound => 1,
60 Table_Initial => Alloc.Unreferenced_Entities_Initial,
61 Table_Increment => Alloc.Unreferenced_Entities_Increment,
62 Table_Name => "Unreferenced_Entities");
64 -- The following table collects potential warnings for IN OUT parameters
65 -- that are referenced but not modified. These warnings are processed when
66 -- the front end calls the procedure Output_Non_Modifed_In_Out_Warnings.
67 -- The reason that we defer output of these messages is that we want to
68 -- detect the case where the relevant procedure is used as a generic actual
69 -- in an instantation, since we suppress the warnings in this case. The
70 -- flag Used_As_Generic_Actual will be set in this case, but will not be
71 -- set till later. Similarly, we suppress the message if the address of
72 -- the procedure is taken, where the flag Address_Taken may be set later.
74 package In_Out_Warnings is new Table.Table (
75 Table_Component_Type => Entity_Id,
76 Table_Index_Type => Nat,
77 Table_Low_Bound => 1,
78 Table_Initial => Alloc.In_Out_Warnings_Initial,
79 Table_Increment => Alloc.In_Out_Warnings_Increment,
80 Table_Name => "In_Out_Warnings");
82 -----------------------
83 -- Local Subprograms --
84 -----------------------
86 function Generic_Package_Spec_Entity (E : Entity_Id) return Boolean;
87 -- This returns true if the entity E is declared within a generic package.
88 -- The point of this is to detect variables which are not assigned within
89 -- the generic, but might be assigned outside the package for any given
90 -- instance. These are cases where we leave the warnings to be posted for
91 -- the instance, when we will know more.
93 function Goto_Spec_Entity (E : Entity_Id) return Entity_Id;
94 -- If E is a parameter entity for a subprogram body, then this function
95 -- returns the corresponding spec entity, if not, E is returned unchanged.
97 function Has_Pragma_Unmodified_Check_Spec (E : Entity_Id) return Boolean;
98 -- Tests Has_Pragma_Unmodified flag for entity E. If E is not a formal,
99 -- this is simply the setting of the flag Has_Pragma_Unmodified. If E is
100 -- a body formal, the setting of the flag in the corresponding spec is
101 -- also checked (and True returned if either flag is True).
103 function Has_Pragma_Unreferenced_Check_Spec (E : Entity_Id) return Boolean;
104 -- Tests Has_Pragma_Unreferenced flag for entity E. If E is not a formal,
105 -- this is simply the setting of the flag Has_Pragma_Unreferenced. If E is
106 -- a body formal, the setting of the flag in the corresponding spec is
107 -- also checked (and True returned if either flag is True).
109 function Never_Set_In_Source_Check_Spec (E : Entity_Id) return Boolean;
110 -- Tests Never_Set_In_Source status for entity E. If E is not a formal,
111 -- this is simply the setting of the flag Never_Set_In_Source. If E is
112 -- a body formal, the setting of the flag in the corresponding spec is
113 -- also checked (and False returned if either flag is False).
115 function Operand_Has_Warnings_Suppressed (N : Node_Id) return Boolean;
116 -- This function traverses the expression tree represented by the node N
117 -- and determines if any sub-operand is a reference to an entity for which
118 -- the Warnings_Off flag is set. True is returned if such an entity is
119 -- encountered, and False otherwise.
121 function Referenced_Check_Spec (E : Entity_Id) return Boolean;
122 -- Tests Referenced status for entity E. If E is not a formal, this is
123 -- simply the setting of the flag Referenced. If E is a body formal, the
124 -- setting of the flag in the corresponding spec is also checked (and True
125 -- returned if either flag is True).
127 function Referenced_As_LHS_Check_Spec (E : Entity_Id) return Boolean;
128 -- Tests Referenced_As_LHS status for entity E. If E is not a formal, this
129 -- is simply the setting of the flag Referenced_As_LHS. If E is a body
130 -- formal, the setting of the flag in the corresponding spec is also
131 -- checked (and True returned if either flag is True).
133 function Referenced_As_Out_Parameter_Check_Spec
134 (E : Entity_Id) return Boolean;
135 -- Tests Referenced_As_Out_Parameter status for entity E. If E is not a
136 -- formal, this is simply the setting of Referenced_As_Out_Parameter. If E
137 -- is a body formal, the setting of the flag in the corresponding spec is
138 -- also checked (and True returned if either flag is True).
140 procedure Warn_On_Unreferenced_Entity
141 (Spec_E : Entity_Id;
142 Body_E : Entity_Id := Empty);
143 -- Output warnings for unreferenced entity E. For the case of an entry
144 -- formal, Body_E is the corresponding body entity for a particular
145 -- accept statement, and the message is posted on Body_E. In all other
146 -- cases, Body_E is ignored and must be Empty.
148 --------------------------
149 -- Check_Code_Statement --
150 --------------------------
152 procedure Check_Code_Statement (N : Node_Id) is
153 begin
154 -- If volatile, nothing to worry about
156 if Is_Asm_Volatile (N) then
157 return;
158 end if;
160 -- Warn if no input or no output
162 Setup_Asm_Inputs (N);
164 if No (Asm_Input_Value) then
165 Error_Msg_F
166 ("?code statement with no inputs should usually be Volatile!", N);
167 return;
168 end if;
170 Setup_Asm_Outputs (N);
172 if No (Asm_Output_Variable) then
173 Error_Msg_F
174 ("?code statement with no outputs should usually be Volatile!", N);
175 return;
176 end if;
178 -- Check multiple code statements in a row
180 if Is_List_Member (N)
181 and then Present (Prev (N))
182 and then Nkind (Prev (N)) = N_Code_Statement
183 then
184 Error_Msg_F
185 ("?code statements in sequence should usually be Volatile!", N);
186 Error_Msg_F
187 ("\?(suggest using template with multiple instructions)!", N);
188 end if;
189 end Check_Code_Statement;
191 ---------------------------------
192 -- Check_Infinite_Loop_Warning --
193 ---------------------------------
195 -- The case we look for is a while loop which tests a local variable, where
196 -- there is no obvious direct or possible indirect update of the variable
197 -- within the body of the loop.
199 procedure Check_Infinite_Loop_Warning (Loop_Statement : Node_Id) is
200 Iter : constant Node_Id := Iteration_Scheme (Loop_Statement);
202 Ref : Node_Id := Empty;
203 -- Reference in iteration scheme to variable that may not be modified in
204 -- loop, indicating a possible infinite loop.
206 Var : Entity_Id := Empty;
207 -- Corresponding entity (entity of Ref)
209 procedure Find_Var (N : Node_Id);
210 -- Inspect condition to see if it depends on a single entity reference.
211 -- If so, Ref is set to point to the reference node, and Var is set to
212 -- the referenced Entity.
214 function Has_Indirection (T : Entity_Id) return Boolean;
215 -- If the controlling variable is an access type, or is a record type
216 -- with access components, assume that it is changed indirectly and
217 -- suppress the warning. As a concession to low-level programming, in
218 -- particular within Declib, we also suppress warnings on a record
219 -- type that contains components of type Address or Short_Address.
221 function Is_Suspicious_Function_Name (E : Entity_Id) return Boolean;
222 -- Given an entity name, see if the name appears to have something to
223 -- do with I/O or network stuff, and if so, return True. Used to kill
224 -- some false positives on a heuristic basis that such functions will
225 -- likely have some strange side effect dependencies. A rather funny
226 -- kludge, but warning messages are in the heuristics business.
228 function Test_Ref (N : Node_Id) return Traverse_Result;
229 -- Test for reference to variable in question. Returns Abandon if
230 -- matching reference found.
232 function Find_Ref is new Traverse_Func (Test_Ref);
233 -- Function to traverse body of procedure. Returns Abandon if matching
234 -- reference found.
236 --------------
237 -- Find_Var --
238 --------------
240 procedure Find_Var (N : Node_Id) is
241 begin
242 -- Condition is a direct variable reference
244 if Is_Entity_Name (N) then
245 Ref := N;
246 Var := Entity (Ref);
248 -- Case of condition is a comparison with compile time known value
250 elsif Nkind (N) in N_Op_Compare then
251 if Compile_Time_Known_Value (Right_Opnd (N)) then
252 Find_Var (Left_Opnd (N));
254 elsif Compile_Time_Known_Value (Left_Opnd (N)) then
255 Find_Var (Right_Opnd (N));
257 -- Ignore any other comparison
259 else
260 return;
261 end if;
263 -- If condition is a negation, check its operand
265 elsif Nkind (N) = N_Op_Not then
266 Find_Var (Right_Opnd (N));
268 -- Case of condition is function call
270 elsif Nkind (N) = N_Function_Call then
272 -- Forget it if function name is not entity, who knows what
273 -- we might be calling?
275 if not Is_Entity_Name (Name (N)) then
276 return;
278 -- Forget it if warnings are suppressed on function entity
280 elsif Warnings_Off (Entity (Name (N))) then
281 return;
283 -- Forget it if function name is suspicious. A strange test
284 -- but warning generation is in the heuristics business!
286 elsif Is_Suspicious_Function_Name (Entity (Name (N))) then
287 return;
288 end if;
290 -- OK, see if we have one argument
292 declare
293 PA : constant List_Id := Parameter_Associations (N);
295 begin
296 -- One argument, so check the argument
298 if Present (PA)
299 and then List_Length (PA) = 1
300 then
301 if Nkind (First (PA)) = N_Parameter_Association then
302 Find_Var (Explicit_Actual_Parameter (First (PA)));
303 else
304 Find_Var (First (PA));
305 end if;
307 -- Not one argument
309 else
310 return;
311 end if;
312 end;
314 -- Any other kind of node is not something we warn for
316 else
317 return;
318 end if;
319 end Find_Var;
321 ---------------------
322 -- Has_Indirection --
323 ---------------------
325 function Has_Indirection (T : Entity_Id) return Boolean is
326 Comp : Entity_Id;
327 Rec : Entity_Id;
329 begin
330 if Is_Access_Type (T) then
331 return True;
333 elsif Is_Private_Type (T)
334 and then Present (Full_View (T))
335 and then Is_Access_Type (Full_View (T))
336 then
337 return True;
339 elsif Is_Record_Type (T) then
340 Rec := T;
342 elsif Is_Private_Type (T)
343 and then Present (Full_View (T))
344 and then Is_Record_Type (Full_View (T))
345 then
346 Rec := Full_View (T);
347 else
348 return False;
349 end if;
351 Comp := First_Component (Rec);
352 while Present (Comp) loop
353 if Is_Access_Type (Etype (Comp))
354 or else Is_Descendent_Of_Address (Etype (Comp))
355 then
356 return True;
357 end if;
359 Next_Component (Comp);
360 end loop;
362 return False;
363 end Has_Indirection;
365 ---------------------------------
366 -- Is_Suspicious_Function_Name --
367 ---------------------------------
369 function Is_Suspicious_Function_Name (E : Entity_Id) return Boolean is
370 S : Entity_Id;
372 function Substring_Present (S : String) return Boolean;
373 -- Returns True if name buffer has given string delimited by non-
374 -- alphabetic characters or by end of string. S is lower case.
376 -----------------------
377 -- Substring_Present --
378 -----------------------
380 function Substring_Present (S : String) return Boolean is
381 Len : constant Natural := S'Length;
383 begin
384 for J in 1 .. Name_Len - (Len - 1) loop
385 if Name_Buffer (J .. J + (Len - 1)) = S
386 and then
387 (J = 1
388 or else Name_Buffer (J - 1) not in 'a' .. 'z')
389 and then
390 (J + Len > Name_Len
391 or else Name_Buffer (J + Len) not in 'a' .. 'z')
392 then
393 return True;
394 end if;
395 end loop;
397 return False;
398 end Substring_Present;
400 -- Start of processing for Is_Suspicious_Function_Name
402 begin
403 S := E;
404 while Present (S) and then S /= Standard_Standard loop
405 Get_Name_String (Chars (S));
407 if Substring_Present ("io")
408 or else Substring_Present ("file")
409 or else Substring_Present ("network")
410 then
411 return True;
412 else
413 S := Scope (S);
414 end if;
415 end loop;
417 return False;
418 end Is_Suspicious_Function_Name;
420 --------------
421 -- Test_Ref --
422 --------------
424 function Test_Ref (N : Node_Id) return Traverse_Result is
425 begin
426 -- Waste of time to look at iteration scheme
428 if N = Iter then
429 return Skip;
431 -- Direct reference to variable in question
433 elsif Is_Entity_Name (N)
434 and then Present (Entity (N))
435 and then Entity (N) = Var
436 then
437 -- If this is an Lvalue, then definitely abandon, since
438 -- this could be a direct modification of the variable.
440 if May_Be_Lvalue (N) then
441 return Abandon;
442 end if;
444 -- If we appear in the context of a procedure call, then also
445 -- abandon, since there may be issues of non-visible side
446 -- effects going on in the call.
448 declare
449 P : Node_Id;
451 begin
452 P := N;
453 loop
454 P := Parent (P);
455 exit when P = Loop_Statement;
457 if Nkind (P) = N_Procedure_Call_Statement then
458 return Abandon;
459 end if;
460 end loop;
461 end;
463 -- Reference to variable renaming variable in question
465 elsif Is_Entity_Name (N)
466 and then Present (Entity (N))
467 and then Ekind (Entity (N)) = E_Variable
468 and then Present (Renamed_Object (Entity (N)))
469 and then Is_Entity_Name (Renamed_Object (Entity (N)))
470 and then Entity (Renamed_Object (Entity (N))) = Var
471 and then May_Be_Lvalue (N)
472 then
473 return Abandon;
475 -- Call to subprogram
477 elsif Nkind (N) = N_Procedure_Call_Statement
478 or else Nkind (N) = N_Function_Call
479 then
480 -- If subprogram is within the scope of the entity we are dealing
481 -- with as the loop variable, then it could modify this parameter,
482 -- so we abandon in this case. In the case of a subprogram that is
483 -- not an entity we also abandon. The check for no entity being
484 -- present is a defense against previous errors.
486 if not Is_Entity_Name (Name (N))
487 or else No (Entity (Name (N)))
488 or else Scope_Within (Entity (Name (N)), Scope (Var))
489 then
490 return Abandon;
491 end if;
492 end if;
494 -- All OK, continue scan
496 return OK;
497 end Test_Ref;
499 -- Start of processing for Check_Infinite_Loop_Warning
501 begin
502 -- We need a while iteration with no condition actions. Conditions
503 -- actions just make things too complicated to get the warning right.
505 if No (Iter)
506 or else No (Condition (Iter))
507 or else Present (Condition_Actions (Iter))
508 or else Debug_Flag_Dot_W
509 then
510 return;
511 end if;
513 -- Initial conditions met, see if condition is of right form
515 Find_Var (Condition (Iter));
517 -- Nothing to do if local variable from source not found
519 if No (Var)
520 or else Ekind (Var) /= E_Variable
521 or else Is_Library_Level_Entity (Var)
522 or else not Comes_From_Source (Var)
523 then
524 return;
526 -- Nothing to do if there is some indirection involved (assume that the
527 -- designated variable might be modified in some way we don't see).
529 elsif Has_Indirection (Etype (Var)) then
530 return;
532 -- Same sort of thing for volatile variable, might be modified by
533 -- some other task or by the operating system in some way.
535 elsif Is_Volatile (Var) then
536 return;
537 end if;
539 -- Filter out case of original statement sequence starting with delay.
540 -- We assume this is a multi-tasking program and that the condition
541 -- is affected by other threads (some kind of busy wait).
543 declare
544 Fstm : constant Node_Id :=
545 Original_Node (First (Statements (Loop_Statement)));
546 begin
547 if Nkind (Fstm) = N_Delay_Relative_Statement
548 or else Nkind (Fstm) = N_Delay_Until_Statement
549 then
550 return;
551 end if;
552 end;
554 -- We have a variable reference of the right form, now we scan the loop
555 -- body to see if it looks like it might not be modified
557 if Find_Ref (Loop_Statement) = OK then
558 Error_Msg_NE
559 ("?variable& is not modified in loop body!", Ref, Var);
560 Error_Msg_N
561 ("\?possible infinite loop!", Ref);
562 end if;
563 end Check_Infinite_Loop_Warning;
565 ----------------------
566 -- Check_References --
567 ----------------------
569 procedure Check_References (E : Entity_Id; Anod : Node_Id := Empty) is
570 E1 : Entity_Id;
571 E1T : Entity_Id;
572 UR : Node_Id;
574 function Body_Formal
575 (E : Entity_Id;
576 Accept_Statement : Node_Id) return Entity_Id;
577 -- For an entry formal entity from an entry declaration, find the
578 -- corrsesponding body formal from the given accept statement.
580 function Missing_Subunits return Boolean;
581 -- We suppress warnings when there are missing subunits, because this
582 -- may generate too many false positives: entities in a parent may only
583 -- be referenced in one of the subunits. We make an exception for
584 -- subunits that contain no other stubs.
586 procedure Output_Reference_Error (M : String);
587 -- Used to output an error message. Deals with posting the error on the
588 -- body formal in the accept case.
590 function Publicly_Referenceable (Ent : Entity_Id) return Boolean;
591 -- This is true if the entity in question is potentially referenceable
592 -- from another unit. This is true for entities in packages that are at
593 -- the library level.
595 ----------------------
596 -- Missing_Subunits --
597 ----------------------
599 function Missing_Subunits return Boolean is
600 D : Node_Id;
602 begin
603 if not Unloaded_Subunits then
605 -- Normal compilation, all subunits are present
607 return False;
609 elsif E /= Main_Unit_Entity then
611 -- No warnings on a stub that is not the main unit
613 return True;
615 elsif Nkind (Unit_Declaration_Node (E)) in N_Proper_Body then
616 D := First (Declarations (Unit_Declaration_Node (E)));
617 while Present (D) loop
619 -- No warnings if the proper body contains nested stubs
621 if Nkind (D) in N_Body_Stub then
622 return True;
623 end if;
625 Next (D);
626 end loop;
628 return False;
630 else
631 -- Missing stubs elsewhere
633 return True;
634 end if;
635 end Missing_Subunits;
637 -----------------
638 -- Body_Formal --
639 -----------------
641 function Body_Formal
642 (E : Entity_Id;
643 Accept_Statement : Node_Id) return Entity_Id
645 Body_Param : Node_Id;
646 Body_E : Entity_Id;
648 begin
649 -- Loop to find matching parameter in accept statement
651 Body_Param := First (Parameter_Specifications (Accept_Statement));
652 while Present (Body_Param) loop
653 Body_E := Defining_Identifier (Body_Param);
655 if Chars (Body_E) = Chars (E) then
656 return Body_E;
657 end if;
659 Next (Body_Param);
660 end loop;
662 -- Should never fall through, should always find a match
664 raise Program_Error;
665 end Body_Formal;
667 ----------------------------
668 -- Output_Reference_Error --
669 ----------------------------
671 procedure Output_Reference_Error (M : String) is
672 begin
673 -- Never issue messages for internal names
675 if Is_Internal_Name (Chars (E1)) then
676 return;
677 end if;
679 -- Don't output message for IN OUT formal unless we have the warning
680 -- flag specifically set. It is a bit odd to distinguish IN OUT
681 -- formals from other cases. This distinction is historical in
682 -- nature. Warnings for IN OUT formals were added fairly late.
684 if Ekind (E1) = E_In_Out_Parameter
685 and then not Check_Unreferenced_Formals
686 then
687 return;
688 end if;
690 -- Other than accept case, post error on defining identifier
692 if No (Anod) then
693 Error_Msg_N (M, E1);
695 -- Accept case, find body formal to post the message
697 else
698 Error_Msg_NE (M, Body_Formal (E1, Accept_Statement => Anod), E1);
700 end if;
701 end Output_Reference_Error;
703 ----------------------------
704 -- Publicly_Referenceable --
705 ----------------------------
707 function Publicly_Referenceable (Ent : Entity_Id) return Boolean is
708 P : Node_Id;
709 Prev : Node_Id;
711 begin
712 -- A formal parameter is never referenceable outside the body of its
713 -- subprogram or entry.
715 if Is_Formal (Ent) then
716 return False;
717 end if;
719 -- Examine parents to look for a library level package spec. But if
720 -- we find a body or block or other similar construct along the way,
721 -- we cannot be referenced.
723 Prev := Ent;
724 P := Parent (Ent);
725 loop
726 case Nkind (P) is
728 -- If we get to top of tree, then publicly referenceable
730 when N_Empty =>
731 return True;
733 -- If we reach a generic package declaration, then always
734 -- consider this referenceable, since any instantiation will
735 -- have access to the entities in the generic package. Note
736 -- that the package itself may not be instantiated, but then
737 -- we will get a warning for the package entity.
739 -- Note that generic formal parameters are themselves not
740 -- publicly referenceable in an instance, and warnings on them
741 -- are useful.
743 when N_Generic_Package_Declaration =>
744 return
745 not Is_List_Member (Prev)
746 or else List_Containing (Prev)
747 /= Generic_Formal_Declarations (P);
749 -- Similarly, the generic formals of a generic subprogram are
750 -- not accessible.
752 when N_Generic_Subprogram_Declaration =>
753 if Is_List_Member (Prev)
754 and then List_Containing (Prev) =
755 Generic_Formal_Declarations (P)
756 then
757 return False;
758 else
759 P := Parent (P);
760 end if;
762 -- If we reach a subprogram body, entity is not referenceable
763 -- unless it is the defining entity of the body. This will
764 -- happen, e.g. when a function is an attribute renaming that
765 -- is rewritten as a body.
767 when N_Subprogram_Body =>
768 if Ent /= Defining_Entity (P) then
769 return False;
770 else
771 P := Parent (P);
772 end if;
774 -- If we reach any other body, definitely not referenceable
776 when N_Package_Body |
777 N_Task_Body |
778 N_Entry_Body |
779 N_Protected_Body |
780 N_Block_Statement |
781 N_Subunit =>
782 return False;
784 -- For all other cases, keep looking up tree
786 when others =>
787 Prev := P;
788 P := Parent (P);
789 end case;
790 end loop;
791 end Publicly_Referenceable;
793 -- Start of processing for Check_References
795 begin
796 -- No messages if warnings are suppressed, or if we have detected any
797 -- real errors so far (this last check avoids junk messages resulting
798 -- from errors, e.g. a subunit that is not loaded).
800 if Warning_Mode = Suppress
801 or else Serious_Errors_Detected /= 0
802 then
803 return;
804 end if;
806 -- We also skip the messages if any subunits were not loaded (see
807 -- comment in Sem_Ch10 to understand how this is set, and why it is
808 -- necessary to suppress the warnings in this case).
810 if Missing_Subunits then
811 return;
812 end if;
814 -- Otherwise loop through entities, looking for suspicious stuff
816 E1 := First_Entity (E);
817 while Present (E1) loop
818 E1T := Etype (E1);
820 -- We only look at source entities with warning flag on. We also
821 -- ignore objects whose type or base type has warnings suppressed.
822 -- We also don't issue warnings within instances, since the proper
823 -- place for such warnings is on the template when it is compiled.
825 if Comes_From_Source (E1)
826 and then not Warnings_Off (E1)
827 and then not Warnings_Off (E1T)
828 and then not Warnings_Off (Base_Type (E1T))
829 and then Instantiation_Location (Sloc (E1)) = No_Location
830 then
831 -- We are interested in variables and out/in-out parameters, but
832 -- we exclude protected types, too complicated to worry about.
834 if Ekind (E1) = E_Variable
835 or else
836 ((Ekind (E1) = E_Out_Parameter
837 or else Ekind (E1) = E_In_Out_Parameter)
838 and then not Is_Protected_Type (Current_Scope))
839 then
840 -- Case of an unassigned variable
842 -- First gather any Unset_Reference indication for E1. In the
843 -- case of a parameter, it is the Spec_Entity that is relevant.
845 if Ekind (E1) = E_Out_Parameter
846 and then Present (Spec_Entity (E1))
847 then
848 UR := Unset_Reference (Spec_Entity (E1));
849 else
850 UR := Unset_Reference (E1);
851 end if;
853 -- If the entity is an out parameter of the current subprogram
854 -- body, check the warning status of the parameter in the spec.
856 if Is_Formal (E1)
857 and then Present (Spec_Entity (E1))
858 and then Warnings_Off (Spec_Entity (E1))
859 then
860 null;
862 -- Special processing for access types
864 elsif Present (UR)
865 and then Is_Access_Type (E1T)
866 then
867 -- For access types, the only time we made a UR entry was
868 -- for a dereference, and so we post the appropriate warning
869 -- here (note that the dereference may not be explicit in
870 -- the source, for example in the case of a dispatching call
871 -- with an anonymous access controlling formal, or of an
872 -- assignment of a pointer involving discriminant check
873 -- on the designated object).
875 Error_Msg_NE ("?& may be null!", UR, E1);
876 goto Continue;
878 -- Case of variable that could be a constant. Note that we
879 -- never signal such messages for generic package entities,
880 -- since a given instance could have modifications outside
881 -- the package.
883 elsif Warn_On_Constant
884 and then (Ekind (E1) = E_Variable
885 and then Has_Initial_Value (E1))
886 and then Never_Set_In_Source_Check_Spec (E1)
887 and then not Address_Taken (E1)
888 and then not Generic_Package_Spec_Entity (E1)
889 then
890 -- A special case, if this variable is volatile and not
891 -- imported, it is not helpful to tell the programmer
892 -- to mark the variable as constant, since this would be
893 -- illegal by virtue of RM C.6(13).
895 if (Is_Volatile (E1) or else Has_Volatile_Components (E1))
896 and then not Is_Imported (E1)
897 then
898 Error_Msg_N
899 ("?& is not modified, volatile has no effect!", E1);
901 -- Another special case, Exception_Occurrence, this catches
902 -- the case of exception choice (and a bit more too, but not
903 -- worth doing more investigation here).
905 elsif Is_RTE (E1T, RE_Exception_Occurrence) then
906 null;
908 -- Here we give the warning if referenced and no pragma
909 -- Unreferenced or Unmodified is present.
911 else
912 -- Variable case
914 if Ekind (E1) = E_Variable then
915 if Referenced_Check_Spec (E1)
916 and then not Has_Pragma_Unreferenced_Check_Spec (E1)
917 and then not Has_Pragma_Unmodified_Check_Spec (E1)
918 then
919 Error_Msg_N
920 ("?& is not modified, "
921 & "could be declared constant!",
922 E1);
923 end if;
924 end if;
925 end if;
927 -- Other cases of a variable or parameter never set in source
929 elsif Never_Set_In_Source_Check_Spec (E1)
931 -- No warning if warning for this case turned off
933 and then Warn_On_No_Value_Assigned
935 -- No warning if address taken somewhere
937 and then not Address_Taken (E1)
939 -- No warning if explicit initial value
941 and then not Has_Initial_Value (E1)
943 -- No warning for generic package spec entities, since we
944 -- might set them in a child unit or something like that
946 and then not Generic_Package_Spec_Entity (E1)
948 -- No warning if fully initialized type, except that for
949 -- this purpose we do not consider access types to qualify
950 -- as fully initialized types (relying on an access type
951 -- variable being null when it is never set is a bit odd!)
953 -- Also we generate warning for an out parameter that is
954 -- never referenced, since again it seems odd to rely on
955 -- default initialization to set an out parameter value.
957 and then (Is_Access_Type (E1T)
958 or else Ekind (E1) = E_Out_Parameter
959 or else not Is_Fully_Initialized_Type (E1T))
960 then
961 -- Do not output complaint about never being assigned a
962 -- value if a pragma Unreferenced applies to the variable
963 -- we are examining, or if it is a parameter, if there is
964 -- a pragma Unreferenced for the corresponding spec.
966 if Has_Pragma_Unreferenced_Check_Spec (E1)
967 or else Has_Pragma_Unreferenced_Objects (E1T)
968 then
969 null;
971 -- IN OUT parameter case where parameter is referenced. We
972 -- separate this out, since this is the case where we delay
973 -- output of the warning until more information is available
974 -- (about use in an instantiation or address being taken).
976 elsif Ekind (E1) = E_In_Out_Parameter
977 and then Referenced_Check_Spec (E1)
978 then
979 -- Suppress warning if private type, and the procedure
980 -- has a separate declaration in a different unit. This
981 -- is the case where the client of a package sees only
982 -- the private type, and it it may be quite reasonable
983 -- for the logical view to be in out, even if the
984 -- implementation ends up using access types or some
985 -- other method to achieve the local effect of a
986 -- modification. On the other hand if the spec and body
987 -- are in the same unit, we are in the package body and
988 -- there we less excuse for a junk IN OUT parameter.
990 if Has_Private_Declaration (E1T)
991 and then Present (Spec_Entity (E1))
992 and then not In_Same_Source_Unit (E1, Spec_Entity (E1))
993 then
994 null;
996 -- Suppress warning for any parameter of a dispatching
997 -- operation, since it is quite reasonable to have an
998 -- operation that is overridden, and for some subclasses
999 -- needs to be IN OUT and for others the parameter does
1000 -- not happen to be assigned.
1002 elsif Is_Dispatching_Operation
1003 (Scope (Goto_Spec_Entity (E1)))
1004 then
1005 null;
1007 -- Suppress warning if composite type containing any
1008 -- access element component, since the logical effect
1009 -- of modifying a parameter may be achieved by modifying
1010 -- a referenced entity.
1012 elsif Is_Composite_Type (E1T)
1013 and then Has_Access_Values (E1T)
1014 then
1015 null;
1017 -- OK, looks like warning for an IN OUT parameter that
1018 -- could be IN makes sense, but we delay the output of
1019 -- the warning, pending possibly finding out later on
1020 -- that the associated subprogram is used as a generic
1021 -- actual, or its address/access is taken. In these two
1022 -- cases, we suppress the warning because the context may
1023 -- force use of IN OUT, even if in this particular case
1024 -- the formal is not modifed.
1026 else
1027 In_Out_Warnings.Append (E1);
1028 end if;
1030 -- Other cases of formals
1032 elsif Is_Formal (E1) then
1033 if Referenced_Check_Spec (E1) then
1034 if not Has_Pragma_Unmodified_Check_Spec (E1) then
1035 Output_Reference_Error
1036 ("?formal parameter& is read but "
1037 & "never assigned!");
1038 end if;
1040 else
1041 Output_Reference_Error
1042 ("?formal parameter& is not referenced!");
1043 end if;
1045 -- Case of variable
1047 else
1048 if Referenced (E1) then
1049 Output_Reference_Error
1050 ("?variable& is read but never assigned!");
1051 else
1052 Output_Reference_Error
1053 ("?variable& is never read and never assigned!");
1054 end if;
1056 -- Deal with special case where this variable is hidden
1057 -- by a loop variable.
1059 if Ekind (E1) = E_Variable
1060 and then Present (Hiding_Loop_Variable (E1))
1061 then
1062 Error_Msg_N
1063 ("?for loop implicitly declares loop variable!",
1064 Hiding_Loop_Variable (E1));
1066 Error_Msg_Sloc := Sloc (E1);
1067 Error_Msg_N
1068 ("\?declaration hides & declared#!",
1069 Hiding_Loop_Variable (E1));
1070 end if;
1071 end if;
1073 goto Continue;
1074 end if;
1076 -- Check for unset reference
1078 if Warn_On_No_Value_Assigned and then Present (UR) then
1080 -- For other than access type, go back to original node to
1081 -- deal with case where original unset reference has been
1082 -- rewritten during expansion.
1084 -- In some cases, the original node may be a type conversion
1085 -- or qualification, and in this case we want the object
1086 -- entity inside.
1088 UR := Original_Node (UR);
1089 while Nkind (UR) = N_Type_Conversion
1090 or else Nkind (UR) = N_Qualified_Expression
1091 loop
1092 UR := Expression (UR);
1093 end loop;
1095 -- Here we issue the warning, all checks completed
1097 -- If we have a return statement, this was a case of an OUT
1098 -- parameter not being set at the time of the return. (Note:
1099 -- it can't be N_Extended_Return_Statement, because those
1100 -- are only for functions, and functions do not allow OUT
1101 -- parameters.)
1103 if Nkind (UR) = N_Simple_Return_Statement
1104 and then not Has_Pragma_Unmodified_Check_Spec (E1)
1105 then
1106 Error_Msg_NE
1107 ("?OUT parameter& not set before return", UR, E1);
1109 -- If the unset reference is prefix of a selected component
1110 -- that comes from source, mention the component as well. If
1111 -- the selected component comes from expansion, all we know
1112 -- is that the entity is not fully initialized at the point
1113 -- of the reference. Locate an unintialized component to get
1114 -- a better error message.
1116 elsif Nkind (Parent (UR)) = N_Selected_Component then
1117 Error_Msg_Node_2 := Selector_Name (Parent (UR));
1119 if not Comes_From_Source (Parent (UR)) then
1120 declare
1121 Comp : Entity_Id;
1123 begin
1124 Comp := First_Entity (E1T);
1125 while Present (Comp) loop
1126 if Ekind (Comp) = E_Component
1127 and then Nkind (Parent (Comp)) =
1128 N_Component_Declaration
1129 and then No (Expression (Parent (Comp)))
1130 then
1131 Error_Msg_Node_2 := Comp;
1132 exit;
1133 end if;
1135 Next_Entity (Comp);
1136 end loop;
1137 end;
1138 end if;
1140 -- Issue proper warning. This is a case of referencing
1141 -- a variable before it has been explicitly assigned.
1142 -- For access types, UR was only set for dereferences,
1143 -- so the issue is that the value may be null.
1145 if Is_Access_Type (Etype (Parent (UR))) then
1146 Error_Msg_N ("?`&.&` may be null!", UR);
1147 else
1148 Error_Msg_N
1149 ("?`&.&` may be referenced before it has a value!",
1150 UR);
1151 end if;
1153 -- All other cases of unset reference active
1155 else
1156 Error_Msg_N
1157 ("?& may be referenced before it has a value!",
1158 UR);
1159 end if;
1161 goto Continue;
1162 end if;
1163 end if;
1165 -- Then check for unreferenced entities. Note that we are only
1166 -- interested in entities which do not have the Referenced flag
1167 -- set. The Referenced_As_LHS flag is interesting only if the
1168 -- Referenced flag is not set.
1170 if not Referenced_Check_Spec (E1)
1172 -- Check that warnings on unreferenced entities are enabled
1174 and then
1175 ((Check_Unreferenced and then not Is_Formal (E1))
1177 -- Case of warning on unreferenced formal
1179 or else
1180 (Check_Unreferenced_Formals and then Is_Formal (E1))
1182 -- Case of warning on unread variables modified by an
1183 -- assignment, or an out parameter if it is the only one.
1185 or else
1186 (Warn_On_Modified_Unread
1187 and then Referenced_As_LHS_Check_Spec (E1))
1189 -- Case of warning on any unread out parameter (note
1190 -- such indications are only set if the appropriate
1191 -- warning options were set, so no need to recheck here.
1193 or else
1194 Referenced_As_Out_Parameter_Check_Spec (E1))
1196 -- Labels, and enumeration literals, and exceptions. The
1197 -- warnings are also placed on local packages that cannot be
1198 -- referenced from elsewhere, including those declared within a
1199 -- package body.
1201 and then (Is_Object (E1)
1202 or else
1203 Is_Type (E1)
1204 or else
1205 Ekind (E1) = E_Label
1206 or else
1207 Ekind (E1) = E_Exception
1208 or else
1209 Ekind (E1) = E_Named_Integer
1210 or else
1211 Ekind (E1) = E_Named_Real
1212 or else
1213 Is_Overloadable (E1)
1215 -- Package case, if the main unit is a package
1216 -- spec or generic package spec, then there may
1217 -- be a corresponding body that references this
1218 -- package in some other file. Otherwise we can
1219 -- be sure that there is no other reference.
1221 or else
1222 (Ekind (E1) = E_Package
1223 and then
1224 Ekind (Cunit_Entity (Current_Sem_Unit)) /=
1225 E_Package
1226 and then
1227 Ekind (Cunit_Entity (Current_Sem_Unit)) /=
1228 E_Generic_Package))
1230 -- Exclude instantiations, since there is no reason why every
1231 -- entity in an instantiation should be referenced.
1233 and then Instantiation_Location (Sloc (E1)) = No_Location
1235 -- Exclude formal parameters from bodies if the corresponding
1236 -- spec entity has been referenced in the case where there is
1237 -- a separate spec.
1239 and then not (Is_Formal (E1)
1240 and then
1241 Ekind (Scope (E1)) = E_Subprogram_Body
1242 and then
1243 Present (Spec_Entity (E1))
1244 and then
1245 Referenced (Spec_Entity (E1)))
1247 -- Consider private type referenced if full view is referenced
1248 -- If there is not full view, this is a generic type on which
1249 -- warnings are also useful.
1251 and then
1252 not (Is_Private_Type (E1)
1253 and then
1254 Present (Full_View (E1))
1255 and then Referenced (Full_View (E1)))
1257 -- Don't worry about full view, only about private type
1259 and then not Has_Private_Declaration (E1)
1261 -- Eliminate dispatching operations from consideration, we
1262 -- cannot tell if these are referenced or not in any easy
1263 -- manner (note this also catches Adjust/Finalize/Initialize)
1265 and then not Is_Dispatching_Operation (E1)
1267 -- Check entity that can be publicly referenced (we do not give
1268 -- messages for such entities, since there could be other
1269 -- units, not involved in this compilation, that contain
1270 -- relevant references.
1272 and then not Publicly_Referenceable (E1)
1274 -- Class wide types are marked as source entities, but they are
1275 -- not really source entities, and are always created, so we do
1276 -- not care if they are not referenced.
1278 and then Ekind (E1) /= E_Class_Wide_Type
1280 -- Objects other than parameters of task types are allowed to
1281 -- be non-referenced, since they start up tasks!
1283 and then ((Ekind (E1) /= E_Variable
1284 and then Ekind (E1) /= E_Constant
1285 and then Ekind (E1) /= E_Component)
1286 or else not Is_Task_Type (E1T))
1288 -- For subunits, only place warnings on the main unit itself,
1289 -- since parent units are not completely compiled
1291 and then (Nkind (Unit (Cunit (Main_Unit))) /= N_Subunit
1292 or else
1293 Get_Source_Unit (E1) = Main_Unit)
1295 -- No warning on a return object, because these are often
1296 -- created with a single expression and an implicit return.
1297 -- If the object is a variable there will be a warning
1298 -- indicating that it could be declared constant.
1300 and then not
1301 (Ekind (E1) = E_Constant and then Is_Return_Object (E1))
1302 then
1303 -- Suppress warnings in internal units if not in -gnatg mode
1304 -- (these would be junk warnings for an applications program,
1305 -- since they refer to problems in internal units)
1307 if GNAT_Mode
1308 or else not
1309 Is_Internal_File_Name
1310 (Unit_File_Name (Get_Source_Unit (E1)))
1311 then
1312 -- We do not immediately flag the error. This is because we
1313 -- have not expanded generic bodies yet, and they may have
1314 -- the missing reference. So instead we park the entity on a
1315 -- list, for later processing. However for the case of an
1316 -- accept statement we want to output messages now, since
1317 -- we know we already have all information at hand, and we
1318 -- also want to have separate warnings for each accept
1319 -- statement for the same entry.
1321 if Present (Anod) then
1322 pragma Assert (Is_Formal (E1));
1324 -- The unreferenced entity is E1, but post the warning
1325 -- on the body entity for this accept statement.
1327 Warn_On_Unreferenced_Entity
1328 (E1, Body_Formal (E1, Accept_Statement => Anod));
1330 else
1331 Unreferenced_Entities.Append (E1);
1332 end if;
1333 end if;
1335 -- Generic units are referenced in the generic body, but if they
1336 -- are not public and never instantiated we want to force a
1337 -- warning on them. We treat them as redundant constructs to
1338 -- minimize noise.
1340 elsif Is_Generic_Subprogram (E1)
1341 and then not Is_Instantiated (E1)
1342 and then not Publicly_Referenceable (E1)
1343 and then Instantiation_Depth (Sloc (E1)) = 0
1344 and then Warn_On_Redundant_Constructs
1345 then
1346 Unreferenced_Entities.Append (E1);
1348 -- Force warning on entity
1350 Set_Referenced (E1, False);
1351 end if;
1352 end if;
1354 -- Recurse into nested package or block. Do not recurse into a
1355 -- formal package, because the correponding body is not analyzed.
1357 <<Continue>>
1358 if ((Ekind (E1) = E_Package or else Ekind (E1) = E_Generic_Package)
1359 and then Nkind (Parent (E1)) = N_Package_Specification
1360 and then
1361 Nkind (Original_Node (Unit_Declaration_Node (E1)))
1362 /= N_Formal_Package_Declaration)
1364 or else Ekind (E1) = E_Block
1365 then
1366 Check_References (E1);
1367 end if;
1369 Next_Entity (E1);
1370 end loop;
1371 end Check_References;
1373 ---------------------------
1374 -- Check_Unset_Reference --
1375 ---------------------------
1377 procedure Check_Unset_Reference (N : Node_Id) is
1378 Typ : constant Entity_Id := Etype (N);
1380 function Is_OK_Fully_Initialized return Boolean;
1381 -- This function returns true if the given node N is fully initialized
1382 -- so that the reference is safe as far as this routine is concerned.
1383 -- Safe generally means that the type of N is a fully initialized type.
1384 -- The one special case is that for access types, which are always fully
1385 -- initialized, we don't consider a dereference OK since it will surely
1386 -- be dereferencing a null value, which won't do.
1388 function Prefix_Has_Dereference (Pref : Node_Id) return Boolean;
1389 -- Used to test indexed or selected component or slice to see if the
1390 -- evaluation of the prefix depends on a dereference, and if so, returns
1391 -- True, in which case we always check the prefix, even if we know that
1392 -- the referenced component is initialized. Pref is the prefix to test.
1394 -----------------------------
1395 -- Is_OK_Fully_Initialized --
1396 -----------------------------
1398 function Is_OK_Fully_Initialized return Boolean is
1399 begin
1400 if Is_Access_Type (Typ) and then Is_Dereferenced (N) then
1401 return False;
1402 else
1403 return Is_Fully_Initialized_Type (Typ);
1404 end if;
1405 end Is_OK_Fully_Initialized;
1407 ----------------------------
1408 -- Prefix_Has_Dereference --
1409 ----------------------------
1411 function Prefix_Has_Dereference (Pref : Node_Id) return Boolean is
1412 begin
1413 -- If prefix is of an access type, certainly need a dereference
1415 if Is_Access_Type (Etype (Pref)) then
1416 return True;
1418 -- If prefix is explicit dereference, that's a dereference for sure
1420 elsif Nkind (Pref) = N_Explicit_Dereference then
1421 return True;
1423 -- If prefix is itself a component reference or slice check prefix
1425 elsif Nkind (Pref) = N_Slice
1426 or else Nkind (Pref) = N_Indexed_Component
1427 or else Nkind (Pref) = N_Selected_Component
1428 then
1429 return Prefix_Has_Dereference (Prefix (Pref));
1431 -- All other cases do not involve a dereference
1433 else
1434 return False;
1435 end if;
1436 end Prefix_Has_Dereference;
1438 -- Start of processing for Check_Unset_Reference
1440 begin
1441 -- Nothing to do if warnings suppressed
1443 if Warning_Mode = Suppress then
1444 return;
1445 end if;
1447 -- Ignore reference unless it comes from source. Almost always if we
1448 -- have a reference from generated code, it is bogus (e.g. calls to init
1449 -- procs to set default discriminant values).
1451 if not Comes_From_Source (N) then
1452 return;
1453 end if;
1455 -- Otherwise see what kind of node we have. If the entity already
1456 -- has an unset reference, it is not necessarily the earliest in
1457 -- the text, because resolution of the prefix of selected components
1458 -- is completed before the resolution of the selected component itself.
1459 -- as a result, given (R /= null and then R.X > 0), the occurrences
1460 -- of R are examined in right-to-left order. If there is already an
1461 -- unset reference, we check whether N is earlier before proceeding.
1463 case Nkind (N) is
1465 -- For identifier or exanded name, examine the entity involved
1467 when N_Identifier | N_Expanded_Name =>
1468 declare
1469 E : constant Entity_Id := Entity (N);
1471 begin
1472 if (Ekind (E) = E_Variable
1473 or else
1474 Ekind (E) = E_Out_Parameter)
1475 and then Never_Set_In_Source_Check_Spec (E)
1476 and then not Has_Initial_Value (E)
1477 and then (No (Unset_Reference (E))
1478 or else
1479 Earlier_In_Extended_Unit
1480 (Sloc (N), Sloc (Unset_Reference (E))))
1481 and then not Warnings_Off (E)
1482 then
1483 -- We may have an unset reference. The first test is whether
1484 -- this is an access to a discriminant of a record or a
1485 -- component with default initialization. Both of these
1486 -- cases can be ignored, since the actual object that is
1487 -- referenced is definitely initialized. Note that this
1488 -- covers the case of reading discriminants of an out
1489 -- parameter, which is OK even in Ada 83.
1491 -- Note that we are only interested in a direct reference to
1492 -- a record component here. If the reference is via an
1493 -- access type, then the access object is being referenced,
1494 -- not the record, and still deserves an unset reference.
1496 if Nkind (Parent (N)) = N_Selected_Component
1497 and not Is_Access_Type (Typ)
1498 then
1499 declare
1500 ES : constant Entity_Id :=
1501 Entity (Selector_Name (Parent (N)));
1502 begin
1503 if Ekind (ES) = E_Discriminant
1504 or else
1505 (Present (Declaration_Node (ES))
1506 and then
1507 Present (Expression (Declaration_Node (ES))))
1508 then
1509 return;
1510 end if;
1511 end;
1512 end if;
1514 -- Exclude fully initialized types
1516 if Is_OK_Fully_Initialized then
1517 return;
1518 end if;
1520 -- Here we have a potential unset reference. But before we
1521 -- get worried about it, we have to make sure that the
1522 -- entity declaration is in the same procedure as the
1523 -- reference, since if they are in separate procedures, then
1524 -- we have no idea about sequential execution.
1526 -- The tests in the loop below catch all such cases, but do
1527 -- allow the reference to appear in a loop, block, or
1528 -- package spec that is nested within the declaring scope.
1529 -- As always, it is possible to construct cases where the
1530 -- warning is wrong, that is why it is a warning!
1532 declare
1533 SR : Entity_Id;
1534 SE : constant Entity_Id := Scope (E);
1536 begin
1537 SR := Current_Scope;
1538 while SR /= SE loop
1539 if SR = Standard_Standard
1540 or else Is_Subprogram (SR)
1541 or else Is_Concurrent_Body (SR)
1542 or else Is_Concurrent_Type (SR)
1543 then
1544 return;
1545 end if;
1547 SR := Scope (SR);
1548 end loop;
1550 -- Case of reference has an access type. This is special
1551 -- case since access types are always set to null so
1552 -- cannot be truly uninitialized, but we still want to
1553 -- warn about cases of obvious null dereference.
1555 if Is_Access_Type (Typ) then
1556 Access_Type_Case : declare
1557 P : Node_Id;
1559 function Process
1560 (N : Node_Id) return Traverse_Result;
1561 -- Process function for instantation of Traverse
1562 -- below. Checks if N contains reference to other
1563 -- than a dereference.
1565 function Ref_In (Nod : Node_Id) return Boolean;
1566 -- Determines whether Nod contains a reference to
1567 -- the entity E that is not a dereference.
1569 -------------
1570 -- Process --
1571 -------------
1573 function Process
1574 (N : Node_Id) return Traverse_Result
1576 begin
1577 if Is_Entity_Name (N)
1578 and then Entity (N) = E
1579 and then not Is_Dereferenced (N)
1580 then
1581 return Abandon;
1582 else
1583 return OK;
1584 end if;
1585 end Process;
1587 ------------
1588 -- Ref_In --
1589 ------------
1591 function Ref_In (Nod : Node_Id) return Boolean is
1592 function Traverse is new Traverse_Func (Process);
1593 begin
1594 return Traverse (Nod) = Abandon;
1595 end Ref_In;
1597 -- Start of processing for Access_Type_Case
1599 begin
1600 -- Don't bother if we are inside an instance, since
1601 -- the compilation of the generic template is where
1602 -- the warning should be issued.
1604 if In_Instance then
1605 return;
1606 end if;
1608 -- Don't bother if this is not the main unit. If we
1609 -- try to give this warning for with'ed units, we
1610 -- get some false positives, since we do not record
1611 -- references in other units.
1613 if not In_Extended_Main_Source_Unit (E)
1614 or else
1615 not In_Extended_Main_Source_Unit (N)
1616 then
1617 return;
1618 end if;
1620 -- We are only interested in dereferences
1622 if not Is_Dereferenced (N) then
1623 return;
1624 end if;
1626 -- One more check, don't bother with references
1627 -- that are inside conditional statements or while
1628 -- loops if the condition references the entity in
1629 -- question. This avoids most false positives.
1631 P := Parent (N);
1632 loop
1633 P := Parent (P);
1634 exit when No (P);
1636 if (Nkind (P) = N_If_Statement
1637 or else
1638 Nkind (P) = N_Elsif_Part)
1639 and then Ref_In (Condition (P))
1640 then
1641 return;
1643 elsif Nkind (P) = N_Loop_Statement
1644 and then Present (Iteration_Scheme (P))
1645 and then
1646 Ref_In (Condition (Iteration_Scheme (P)))
1647 then
1648 return;
1649 end if;
1650 end loop;
1651 end Access_Type_Case;
1652 end if;
1654 -- Here we definitely have a case for giving a warning
1655 -- for a reference to an unset value. But we don't give
1656 -- the warning now. Instead we set the Unset_Reference
1657 -- field of the identifier involved. The reason for this
1658 -- is that if we find the variable is never ever assigned
1659 -- a value then that warning is more important and there
1660 -- is no point in giving the reference warning.
1662 -- If this is an identifier, set the field directly
1664 if Nkind (N) = N_Identifier then
1665 Set_Unset_Reference (E, N);
1667 -- Otherwise it is an expanded name, so set the field of
1668 -- the actual identifier for the reference.
1670 else
1671 Set_Unset_Reference (E, Selector_Name (N));
1672 end if;
1673 end;
1674 end if;
1675 end;
1677 -- Indexed component or slice
1679 when N_Indexed_Component | N_Slice =>
1681 -- If prefix does not involve dereferencing an access type, then
1682 -- we know we are OK if the component type is fully initialized,
1683 -- since the component will have been set as part of the default
1684 -- initialization.
1686 if not Prefix_Has_Dereference (Prefix (N))
1687 and then Is_OK_Fully_Initialized
1688 then
1689 return;
1691 -- Look at prefix in access type case, or if the component is not
1692 -- fully initialized.
1694 else
1695 Check_Unset_Reference (Prefix (N));
1696 end if;
1698 -- Record component
1700 when N_Selected_Component =>
1701 declare
1702 Pref : constant Node_Id := Prefix (N);
1703 Ent : constant Entity_Id := Entity (Selector_Name (N));
1705 begin
1706 -- If prefix involves dereferencing an access type, always
1707 -- check the prefix, since the issue then is whether this
1708 -- access value is null.
1710 if Prefix_Has_Dereference (Pref) then
1711 null;
1713 -- Always go to prefix if no selector entity is set. Can this
1714 -- happen in the normal case? Not clear, but it definitely can
1715 -- happen in error cases.
1717 elsif No (Ent) then
1718 null;
1720 -- For a record component, check some cases where we have
1721 -- reasonable cause to consider that the component is known to
1722 -- be or probably is initialized. In this case, we don't care
1723 -- if the prefix itself was explicitly initialized.
1725 -- Discriminants are always considered initialized
1727 elsif Ekind (Ent) = E_Discriminant then
1728 return;
1730 -- An explicitly initialized component is certainly initialized
1732 elsif Nkind (Parent (Ent)) = N_Component_Declaration
1733 and then Present (Expression (Parent (Ent)))
1734 then
1735 return;
1737 -- A fully initialized component is initialized
1739 elsif Is_OK_Fully_Initialized then
1740 return;
1741 end if;
1743 -- If none of those cases apply, check the record type prefix
1745 Check_Unset_Reference (Pref);
1746 end;
1748 -- For type conversions or qualifications examine the expression
1750 when N_Type_Conversion | N_Qualified_Expression =>
1751 Check_Unset_Reference (Expression (N));
1753 -- For explicit dereference, always check prefix, which will generate
1754 -- an unset reference (since this is a case of dereferencing null).
1756 when N_Explicit_Dereference =>
1757 Check_Unset_Reference (Prefix (N));
1759 -- All other cases are not cases of an unset reference
1761 when others =>
1762 null;
1764 end case;
1765 end Check_Unset_Reference;
1767 ------------------------
1768 -- Check_Unused_Withs --
1769 ------------------------
1771 procedure Check_Unused_Withs (Spec_Unit : Unit_Number_Type := No_Unit) is
1772 Cnode : Node_Id;
1773 Item : Node_Id;
1774 Lunit : Node_Id;
1775 Ent : Entity_Id;
1777 Munite : constant Entity_Id := Cunit_Entity (Main_Unit);
1778 -- This is needed for checking the special renaming case
1780 procedure Check_One_Unit (Unit : Unit_Number_Type);
1781 -- Subsidiary procedure, performs checks for specified unit
1783 --------------------
1784 -- Check_One_Unit --
1785 --------------------
1787 procedure Check_One_Unit (Unit : Unit_Number_Type) is
1788 Is_Visible_Renaming : Boolean := False;
1789 Pack : Entity_Id;
1791 procedure Check_Inner_Package (Pack : Entity_Id);
1792 -- Pack is a package local to a unit in a with_clause. Both the
1793 -- unit and Pack are referenced. If none of the entities in Pack
1794 -- are referenced, then the only occurrence of Pack is in a use
1795 -- clause or a pragma, and a warning is worthwhile as well.
1797 function Check_System_Aux return Boolean;
1798 -- Before giving a warning on a with_clause for System, check
1799 -- whether a system extension is present.
1801 function Find_Package_Renaming
1802 (P : Entity_Id;
1803 L : Entity_Id) return Entity_Id;
1804 -- The only reference to a context unit may be in a renaming
1805 -- declaration. If this renaming declares a visible entity, do
1806 -- not warn that the context clause could be moved to the body,
1807 -- because the renaming may be intented to re-export the unit.
1809 -------------------------
1810 -- Check_Inner_Package --
1811 -------------------------
1813 procedure Check_Inner_Package (Pack : Entity_Id) is
1814 E : Entity_Id;
1815 Un : constant Node_Id := Sinfo.Unit (Cnode);
1817 function Check_Use_Clause (N : Node_Id) return Traverse_Result;
1818 -- If N is a use_clause for Pack, emit warning
1820 procedure Check_Use_Clauses is new
1821 Traverse_Proc (Check_Use_Clause);
1823 ----------------------
1824 -- Check_Use_Clause --
1825 ----------------------
1827 function Check_Use_Clause (N : Node_Id) return Traverse_Result is
1828 Nam : Node_Id;
1830 begin
1831 if Nkind (N) = N_Use_Package_Clause then
1832 Nam := First (Names (N));
1833 while Present (Nam) loop
1834 if Entity (Nam) = Pack then
1835 Error_Msg_Qual_Level := 1;
1836 Error_Msg_NE
1837 ("?no entities of package& are referenced!",
1838 Nam, Pack);
1839 Error_Msg_Qual_Level := 0;
1840 end if;
1842 Next (Nam);
1843 end loop;
1844 end if;
1846 return OK;
1847 end Check_Use_Clause;
1849 -- Start of processing for Check_Inner_Package
1851 begin
1852 E := First_Entity (Pack);
1853 while Present (E) loop
1854 if Referenced_Check_Spec (E) then
1855 return;
1856 end if;
1858 Next_Entity (E);
1859 end loop;
1861 -- No entities of the package are referenced. Check whether the
1862 -- reference to the package itself is a use clause, and if so
1863 -- place a warning on it.
1865 Check_Use_Clauses (Un);
1866 end Check_Inner_Package;
1868 ----------------------
1869 -- Check_System_Aux --
1870 ----------------------
1872 function Check_System_Aux return Boolean is
1873 Ent : Entity_Id;
1875 begin
1876 if Chars (Lunit) = Name_System
1877 and then Scope (Lunit) = Standard_Standard
1878 and then Present_System_Aux
1879 then
1880 Ent := First_Entity (System_Aux_Id);
1881 while Present (Ent) loop
1882 if Referenced_Check_Spec (Ent) then
1883 return True;
1884 end if;
1886 Next_Entity (Ent);
1887 end loop;
1888 end if;
1890 return False;
1891 end Check_System_Aux;
1893 ---------------------------
1894 -- Find_Package_Renaming --
1895 ---------------------------
1897 function Find_Package_Renaming
1898 (P : Entity_Id;
1899 L : Entity_Id) return Entity_Id
1901 E1 : Entity_Id;
1902 R : Entity_Id;
1904 begin
1905 Is_Visible_Renaming := False;
1907 E1 := First_Entity (P);
1908 while Present (E1) loop
1909 if Ekind (E1) = E_Package
1910 and then Renamed_Object (E1) = L
1911 then
1912 Is_Visible_Renaming := not Is_Hidden (E1);
1913 return E1;
1915 elsif Ekind (E1) = E_Package
1916 and then No (Renamed_Object (E1))
1917 and then not Is_Generic_Instance (E1)
1918 then
1919 R := Find_Package_Renaming (E1, L);
1921 if Present (R) then
1922 Is_Visible_Renaming := not Is_Hidden (R);
1923 return R;
1924 end if;
1925 end if;
1927 Next_Entity (E1);
1928 end loop;
1930 return Empty;
1931 end Find_Package_Renaming;
1933 -- Start of processing for Check_One_Unit
1935 begin
1936 Cnode := Cunit (Unit);
1938 -- Only do check in units that are part of the extended main unit.
1939 -- This is actually a necessary restriction, because in the case of
1940 -- subprogram acting as its own specification, there can be with's in
1941 -- subunits that we will not see.
1943 if not In_Extended_Main_Source_Unit (Cnode) then
1944 return;
1946 -- In configurable run time mode, we remove the bodies of non-inlined
1947 -- subprograms, which may lead to spurious warnings, which are
1948 -- clearly undesirable.
1950 elsif Configurable_Run_Time_Mode
1951 and then Is_Predefined_File_Name (Unit_File_Name (Unit))
1952 then
1953 return;
1954 end if;
1956 -- Loop through context items in this unit
1958 Item := First (Context_Items (Cnode));
1959 while Present (Item) loop
1960 if Nkind (Item) = N_With_Clause
1961 and then not Implicit_With (Item)
1962 and then In_Extended_Main_Source_Unit (Item)
1963 then
1964 Lunit := Entity (Name (Item));
1966 -- Check if this unit is referenced (skip the check if this
1967 -- is explicitly marked by a pragma Unreferenced).
1969 if not Referenced (Lunit)
1970 and then not Has_Pragma_Unreferenced (Lunit)
1971 then
1972 -- Suppress warnings in internal units if not in -gnatg mode
1973 -- (these would be junk warnings for an application program,
1974 -- since they refer to problems in internal units).
1976 if GNAT_Mode
1977 or else not Is_Internal_File_Name (Unit_File_Name (Unit))
1978 then
1979 -- Here we definitely have a non-referenced unit. If it
1980 -- is the special call for a spec unit, then just set the
1981 -- flag to be read later.
1983 if Unit = Spec_Unit then
1984 Set_Unreferenced_In_Spec (Item);
1986 -- Otherwise simple unreferenced message
1988 else
1989 Error_Msg_N
1990 ("?unit& is not referenced!", Name (Item));
1991 end if;
1992 end if;
1994 -- If main unit is a renaming of this unit, then we consider
1995 -- the with to be OK (obviously it is needed in this case!)
1996 -- This may be transitive: the unit in the with_clause may
1997 -- itself be a renaming, in which case both it and the main
1998 -- unit rename the same ultimate package.
2000 elsif Present (Renamed_Entity (Munite))
2001 and then
2002 (Renamed_Entity (Munite) = Lunit
2003 or else Renamed_Entity (Munite) = Renamed_Entity (Lunit))
2004 then
2005 null;
2007 -- If this unit is referenced, and it is a package, we do
2008 -- another test, to see if any of the entities in the package
2009 -- are referenced. If none of the entities are referenced, we
2010 -- still post a warning. This occurs if the only use of the
2011 -- package is in a use clause, or in a package renaming
2012 -- declaration. This check is skipped for packages that are
2013 -- renamed in a spec, since the entities in such a package are
2014 -- visible to clients via the renaming.
2016 elsif Ekind (Lunit) = E_Package
2017 and then not Renamed_In_Spec (Lunit)
2018 then
2019 -- If Is_Instantiated is set, it means that the package is
2020 -- implicitly instantiated (this is the case of parent
2021 -- instance or an actual for a generic package formal), and
2022 -- this counts as a reference.
2024 if Is_Instantiated (Lunit) then
2025 null;
2027 -- If no entities in package, and there is a pragma
2028 -- Elaborate_Body present, then assume that this with is
2029 -- done for purposes of this elaboration.
2031 elsif No (First_Entity (Lunit))
2032 and then Has_Pragma_Elaborate_Body (Lunit)
2033 then
2034 null;
2036 -- Otherwise see if any entities have been referenced
2038 else
2039 if Limited_Present (Item) then
2040 Ent := First_Entity (Limited_View (Lunit));
2041 else
2042 Ent := First_Entity (Lunit);
2043 end if;
2045 loop
2046 -- No more entities, and we did not find one that was
2047 -- referenced. Means we have a definite case of a with
2048 -- none of whose entities was referenced.
2050 if No (Ent) then
2052 -- If in spec, just set the flag
2054 if Unit = Spec_Unit then
2055 Set_No_Entities_Ref_In_Spec (Item);
2057 elsif Check_System_Aux then
2058 null;
2060 -- Else give the warning
2062 else
2063 if not Has_Pragma_Unreferenced
2064 (Entity (Name (Item)))
2065 then
2066 Error_Msg_N
2067 ("?no entities of & are referenced!",
2068 Name (Item));
2069 end if;
2071 -- Look for renamings of this package, and flag
2072 -- them as well. If the original package has
2073 -- warnings off, we suppress the warning on the
2074 -- renaming as well.
2076 Pack := Find_Package_Renaming (Munite, Lunit);
2078 if Present (Pack)
2079 and then not Warnings_Off (Lunit)
2080 and then not Has_Pragma_Unreferenced (Pack)
2081 then
2082 Error_Msg_NE
2083 ("?no entities of & are referenced!",
2084 Unit_Declaration_Node (Pack),
2085 Pack);
2086 end if;
2087 end if;
2089 exit;
2091 -- Case of entity being referenced. The reference may
2092 -- come from a limited_with_clause, in which case the
2093 -- limited view of the entity carries the flag.
2095 elsif Referenced_Check_Spec (Ent)
2096 or else Referenced_As_LHS_Check_Spec (Ent)
2097 or else Referenced_As_Out_Parameter_Check_Spec (Ent)
2098 or else
2099 (From_With_Type (Ent)
2100 and then Is_Incomplete_Type (Ent)
2101 and then Present (Non_Limited_View (Ent))
2102 and then Referenced (Non_Limited_View (Ent)))
2103 then
2104 -- This means that the with is indeed fine, in that
2105 -- it is definitely needed somewhere, and we can
2106 -- quit worrying about this one...
2108 -- Except for one little detail: if either of the
2109 -- flags was set during spec processing, this is
2110 -- where we complain that the with could be moved
2111 -- from the spec. If the spec contains a visible
2112 -- renaming of the package, inhibit warning to move
2113 -- with_clause to body.
2115 if Ekind (Munite) = E_Package_Body then
2116 Pack :=
2117 Find_Package_Renaming
2118 (Spec_Entity (Munite), Lunit);
2119 end if;
2121 if Unreferenced_In_Spec (Item) then
2122 Error_Msg_N
2123 ("?unit& is not referenced in spec!",
2124 Name (Item));
2126 elsif No_Entities_Ref_In_Spec (Item) then
2127 Error_Msg_N
2128 ("?no entities of & are referenced in spec!",
2129 Name (Item));
2131 else
2132 if Ekind (Ent) = E_Package then
2133 Check_Inner_Package (Ent);
2134 end if;
2136 exit;
2137 end if;
2139 if not Is_Visible_Renaming then
2140 Error_Msg_N
2141 ("\?with clause might be moved to body!",
2142 Name (Item));
2143 end if;
2145 exit;
2147 -- Move to next entity to continue search
2149 else
2150 Next_Entity (Ent);
2151 end if;
2152 end loop;
2153 end if;
2155 -- For a generic package, the only interesting kind of
2156 -- reference is an instantiation, since entities cannot be
2157 -- referenced directly.
2159 elsif Is_Generic_Unit (Lunit) then
2161 -- Unit was never instantiated, set flag for case of spec
2162 -- call, or give warning for normal call.
2164 if not Is_Instantiated (Lunit) then
2165 if Unit = Spec_Unit then
2166 Set_Unreferenced_In_Spec (Item);
2167 else
2168 Error_Msg_N
2169 ("?unit& is never instantiated!", Name (Item));
2170 end if;
2172 -- If unit was indeed instantiated, make sure that flag is
2173 -- not set showing it was uninstantiated in the spec, and if
2174 -- so, give warning.
2176 elsif Unreferenced_In_Spec (Item) then
2177 Error_Msg_N
2178 ("?unit& is not instantiated in spec!", Name (Item));
2179 Error_Msg_N
2180 ("\?with clause can be moved to body!", Name (Item));
2181 end if;
2182 end if;
2183 end if;
2185 Next (Item);
2186 end loop;
2187 end Check_One_Unit;
2189 -- Start of processing for Check_Unused_Withs
2191 begin
2192 if not Opt.Check_Withs
2193 or else Operating_Mode = Check_Syntax
2194 then
2195 return;
2196 end if;
2198 -- Flag any unused with clauses, but skip this step if we are compiling
2199 -- a subunit on its own, since we do not have enough information to
2200 -- determine whether with's are used. We will get the relevant warnings
2201 -- when we compile the parent. This is the normal style of GNAT
2202 -- compilation in any case.
2204 if Nkind (Unit (Cunit (Main_Unit))) = N_Subunit then
2205 return;
2206 end if;
2208 -- Process specified units
2210 if Spec_Unit = No_Unit then
2212 -- For main call, check all units
2214 for Unit in Main_Unit .. Last_Unit loop
2215 Check_One_Unit (Unit);
2216 end loop;
2218 else
2219 -- For call for spec, check only the spec
2221 Check_One_Unit (Spec_Unit);
2222 end if;
2223 end Check_Unused_Withs;
2225 ---------------------------------
2226 -- Generic_Package_Spec_Entity --
2227 ---------------------------------
2229 function Generic_Package_Spec_Entity (E : Entity_Id) return Boolean is
2230 S : Entity_Id;
2232 begin
2233 if Is_Package_Body_Entity (E) then
2234 return False;
2236 else
2237 S := Scope (E);
2238 loop
2239 if S = Standard_Standard then
2240 return False;
2242 elsif Ekind (S) = E_Generic_Package then
2243 return True;
2245 elsif Ekind (S) = E_Package then
2246 S := Scope (S);
2248 else
2249 return False;
2250 end if;
2251 end loop;
2252 end if;
2253 end Generic_Package_Spec_Entity;
2255 ----------------------
2256 -- Goto_Spec_Entity --
2257 ----------------------
2259 function Goto_Spec_Entity (E : Entity_Id) return Entity_Id is
2260 begin
2261 if Is_Formal (E)
2262 and then Present (Spec_Entity (E))
2263 then
2264 return Spec_Entity (E);
2265 else
2266 return E;
2267 end if;
2268 end Goto_Spec_Entity;
2270 --------------------------------------
2271 -- Has_Pragma_Unmodified_Check_Spec --
2272 --------------------------------------
2274 function Has_Pragma_Unmodified_Check_Spec
2275 (E : Entity_Id) return Boolean
2277 begin
2278 if Is_Formal (E) and then Present (Spec_Entity (E)) then
2279 return Has_Pragma_Unmodified (E)
2280 or else
2281 Has_Pragma_Unmodified (Spec_Entity (E));
2282 else
2283 return Has_Pragma_Unmodified (E);
2284 end if;
2285 end Has_Pragma_Unmodified_Check_Spec;
2287 ----------------------------------------
2288 -- Has_Pragma_Unreferenced_Check_Spec --
2289 ----------------------------------------
2291 function Has_Pragma_Unreferenced_Check_Spec
2292 (E : Entity_Id) return Boolean
2294 begin
2295 if Is_Formal (E) and then Present (Spec_Entity (E)) then
2296 return Has_Pragma_Unreferenced (E)
2297 or else
2298 Has_Pragma_Unreferenced (Spec_Entity (E));
2299 else
2300 return Has_Pragma_Unreferenced (E);
2301 end if;
2302 end Has_Pragma_Unreferenced_Check_Spec;
2304 ------------------------------------
2305 -- Never_Set_In_Source_Check_Spec --
2306 ------------------------------------
2308 function Never_Set_In_Source_Check_Spec (E : Entity_Id) return Boolean is
2309 begin
2310 if Is_Formal (E) and then Present (Spec_Entity (E)) then
2311 return Never_Set_In_Source (E)
2312 and then
2313 Never_Set_In_Source (Spec_Entity (E));
2314 else
2315 return Never_Set_In_Source (E);
2316 end if;
2317 end Never_Set_In_Source_Check_Spec;
2319 -------------------------------------
2320 -- Operand_Has_Warnings_Suppressed --
2321 -------------------------------------
2323 function Operand_Has_Warnings_Suppressed (N : Node_Id) return Boolean is
2325 function Check_For_Warnings (N : Node_Id) return Traverse_Result;
2326 -- Function used to check one node to see if it is or was originally
2327 -- a reference to an entity for which Warnings are off. If so, Abandon
2328 -- is returned, otherwise OK_Orig is returned to continue the traversal
2329 -- of the original expression.
2331 function Traverse is new Traverse_Func (Check_For_Warnings);
2332 -- Function used to traverse tree looking for warnings
2334 ------------------------
2335 -- Check_For_Warnings --
2336 ------------------------
2338 function Check_For_Warnings (N : Node_Id) return Traverse_Result is
2339 R : constant Node_Id := Original_Node (N);
2341 begin
2342 if Nkind (R) in N_Has_Entity
2343 and then Present (Entity (R))
2344 and then Warnings_Off (Entity (R))
2345 then
2346 return Abandon;
2347 else
2348 return OK_Orig;
2349 end if;
2350 end Check_For_Warnings;
2352 -- Start of processing for Operand_Has_Warnings_Suppressed
2354 begin
2355 return Traverse (N) = Abandon;
2357 -- If any exception occurs, then something has gone wrong, and this is
2358 -- only a minor aesthetic issue anyway, so just say we did not find what
2359 -- we are looking for, rather than blow up.
2361 exception
2362 when others =>
2363 return False;
2364 end Operand_Has_Warnings_Suppressed;
2366 -----------------------------------------
2367 -- Output_Non_Modified_In_Out_Warnings --
2368 -----------------------------------------
2370 procedure Output_Non_Modifed_In_Out_Warnings is
2372 function No_Warn_On_In_Out (E : Entity_Id) return Boolean;
2373 -- Given a formal parameter entity E, determines if there is a reason to
2374 -- suppress IN OUT warnings (not modified, could be IN) for formals of
2375 -- the subprogram. We suppress these warnings if Warnings Off is set, or
2376 -- if we have seen the address of the subprogram being taken, or if the
2377 -- subprogram is used as a generic actual (in the latter cases the
2378 -- context may force use of IN OUT, even if the parameter is not
2379 -- modifies for this particular case.
2381 -----------------------
2382 -- No_Warn_On_In_Out --
2383 -----------------------
2385 function No_Warn_On_In_Out (E : Entity_Id) return Boolean is
2386 S : constant Entity_Id := Scope (E);
2387 begin
2388 if Warnings_Off (S) then
2389 return True;
2390 elsif Address_Taken (S) then
2391 return True;
2392 elsif Used_As_Generic_Actual (S) then
2393 return True;
2394 elsif Present (Spec_Entity (E)) then
2395 return No_Warn_On_In_Out (Spec_Entity (E));
2396 else
2397 return False;
2398 end if;
2399 end No_Warn_On_In_Out;
2401 -- Start of processing for Output_Non_Modifed_In_Out_Warnings
2403 begin
2404 -- Loop through entities for which a warning may be needed
2406 for J in In_Out_Warnings.First .. In_Out_Warnings.Last loop
2407 declare
2408 E1 : constant Entity_Id := In_Out_Warnings.Table (J);
2410 begin
2411 -- Suppress warning in specific cases (see details in comments for
2412 -- No_Warn_On_In_Out), or if there is a pragma Unmodified.
2414 if No_Warn_On_In_Out (E1)
2415 or else Has_Pragma_Unmodified_Check_Spec (E1)
2416 then
2417 null;
2419 -- Here we generate the warning
2421 else
2422 -- If -gnatwc is set then output message that we could be IN
2424 if Warn_On_Constant then
2425 Error_Msg_N ("?formal parameter & is not modified!", E1);
2426 Error_Msg_N ("\?mode could be IN instead of `IN OUT`!", E1);
2428 -- We do not generate warnings for IN OUT parameters unless we
2429 -- have at least -gnatwu. This is deliberately inconsistent
2430 -- with the treatment of variables, but otherwise we get too
2431 -- many unexpected warnings in default mode.
2433 elsif Check_Unreferenced then
2434 Error_Msg_N ("?formal parameter& is read but "
2435 & "never assigned!", E1);
2436 end if;
2438 -- Kill any other warnings on this entity, since this is the
2439 -- one that should dominate any other unreferenced warning.
2441 Set_Warnings_Off (E1);
2442 end if;
2443 end;
2444 end loop;
2445 end Output_Non_Modifed_In_Out_Warnings;
2447 ----------------------------------------
2448 -- Output_Obsolescent_Entity_Warnings --
2449 ----------------------------------------
2451 procedure Output_Obsolescent_Entity_Warnings (N : Node_Id; E : Entity_Id) is
2452 P : constant Node_Id := Parent (N);
2453 S : Entity_Id;
2455 begin
2456 S := Current_Scope;
2458 -- Do not output message if we are the scope of standard. This means
2459 -- we have a reference from a context clause from when it is originally
2460 -- processed, and that's too early to tell whether it is an obsolescent
2461 -- unit doing the with'ing. In Sem_Ch10.Analyze_Compilation_Unit we make
2462 -- sure that we have a later call when the scope is available. This test
2463 -- also eliminates all messages for use clauses, which is fine (we do
2464 -- not want messages for use clauses, since they are always redundant
2465 -- with respect to the associated with clause).
2467 if S = Standard_Standard then
2468 return;
2469 end if;
2471 -- Do not output message if we are in scope of an obsolescent package
2472 -- or subprogram.
2474 loop
2475 if Is_Obsolescent (S) then
2476 return;
2477 end if;
2479 S := Scope (S);
2480 exit when S = Standard_Standard;
2481 end loop;
2483 -- Here we will output the message
2485 Error_Msg_Sloc := Sloc (E);
2487 -- Case of with clause
2489 if Nkind (P) = N_With_Clause then
2490 if Ekind (E) = E_Package then
2491 Error_Msg_NE
2492 ("?with of obsolescent package& declared#", N, E);
2493 elsif Ekind (E) = E_Procedure then
2494 Error_Msg_NE
2495 ("?with of obsolescent procedure& declared#", N, E);
2496 else
2497 Error_Msg_NE
2498 ("?with of obsolescent function& declared#", N, E);
2499 end if;
2501 -- If we do not have a with clause, then ignore any reference to an
2502 -- obsolescent package name. We only want to give the one warning of
2503 -- withing the package, not one each time it is used to qualify.
2505 elsif Ekind (E) = E_Package then
2506 return;
2508 -- Procedure call statement
2510 elsif Nkind (P) = N_Procedure_Call_Statement then
2511 Error_Msg_NE
2512 ("?call to obsolescent procedure& declared#", N, E);
2514 -- Function call
2516 elsif Nkind (P) = N_Function_Call then
2517 Error_Msg_NE
2518 ("?call to obsolescent function& declared#", N, E);
2520 -- Reference to obsolescent type
2522 elsif Is_Type (E) then
2523 Error_Msg_NE
2524 ("?reference to obsolescent type& declared#", N, E);
2526 -- Reference to obsolescent component
2528 elsif Ekind (E) = E_Component
2529 or else Ekind (E) = E_Discriminant
2530 then
2531 Error_Msg_NE
2532 ("?reference to obsolescent component& declared#", N, E);
2534 -- Reference to obsolescent variable
2536 elsif Ekind (E) = E_Variable then
2537 Error_Msg_NE
2538 ("?reference to obsolescent variable& declared#", N, E);
2540 -- Reference to obsolescent constant
2542 elsif Ekind (E) = E_Constant
2543 or else Ekind (E) in Named_Kind
2544 then
2545 Error_Msg_NE
2546 ("?reference to obsolescent constant& declared#", N, E);
2548 -- Reference to obsolescent enumeration literal
2550 elsif Ekind (E) = E_Enumeration_Literal then
2551 Error_Msg_NE
2552 ("?reference to obsolescent enumeration literal& declared#", N, E);
2554 -- Generic message for any other case we missed
2556 else
2557 Error_Msg_NE
2558 ("?reference to obsolescent entity& declared#", N, E);
2559 end if;
2561 -- Output additional warning if present
2563 declare
2564 W : constant Node_Id := Obsolescent_Warning (E);
2566 begin
2567 if Present (W) then
2569 -- This is a warning continuation to start on a new line
2570 Name_Buffer (1) := '\';
2571 Name_Buffer (2) := '\';
2572 Name_Buffer (3) := '?';
2573 Name_Len := 3;
2575 -- Add characters to message, and output message. Note that
2576 -- we quote every character of the message since we don't
2577 -- want to process any insertions.
2579 for J in 1 .. String_Length (Strval (W)) loop
2580 Add_Char_To_Name_Buffer (''');
2581 Add_Char_To_Name_Buffer
2582 (Get_Character (Get_String_Char (Strval (W), J)));
2583 end loop;
2585 Error_Msg_N (Name_Buffer (1 .. Name_Len), N);
2586 end if;
2587 end;
2588 end Output_Obsolescent_Entity_Warnings;
2590 ----------------------------------
2591 -- Output_Unreferenced_Messages --
2592 ----------------------------------
2594 procedure Output_Unreferenced_Messages is
2595 begin
2596 for J in Unreferenced_Entities.First ..
2597 Unreferenced_Entities.Last
2598 loop
2599 Warn_On_Unreferenced_Entity (Unreferenced_Entities.Table (J));
2600 end loop;
2601 end Output_Unreferenced_Messages;
2603 ---------------------------
2604 -- Referenced_Check_Spec --
2605 ---------------------------
2607 function Referenced_Check_Spec (E : Entity_Id) return Boolean is
2608 begin
2609 if Is_Formal (E) and then Present (Spec_Entity (E)) then
2610 return Referenced (E) or else Referenced (Spec_Entity (E));
2611 else
2612 return Referenced (E);
2613 end if;
2614 end Referenced_Check_Spec;
2616 ----------------------------------
2617 -- Referenced_As_LHS_Check_Spec --
2618 ----------------------------------
2620 function Referenced_As_LHS_Check_Spec (E : Entity_Id) return Boolean is
2621 begin
2622 if Is_Formal (E) and then Present (Spec_Entity (E)) then
2623 return Referenced_As_LHS (E)
2624 or else Referenced_As_LHS (Spec_Entity (E));
2625 else
2626 return Referenced_As_LHS (E);
2627 end if;
2628 end Referenced_As_LHS_Check_Spec;
2630 --------------------------------------------
2631 -- Referenced_As_Out_Parameter_Check_Spec --
2632 --------------------------------------------
2634 function Referenced_As_Out_Parameter_Check_Spec
2635 (E : Entity_Id) return Boolean
2637 begin
2638 if Is_Formal (E) and then Present (Spec_Entity (E)) then
2639 return Referenced_As_Out_Parameter (E)
2640 or else Referenced_As_Out_Parameter (Spec_Entity (E));
2641 else
2642 return Referenced_As_Out_Parameter (E);
2643 end if;
2644 end Referenced_As_Out_Parameter_Check_Spec;
2646 ----------------------------
2647 -- Set_Dot_Warning_Switch --
2648 ----------------------------
2650 function Set_Dot_Warning_Switch (C : Character) return Boolean is
2651 begin
2652 case C is
2653 when 'a' =>
2654 Warn_On_Assertion_Failure := True;
2656 when 'A' =>
2657 Warn_On_Assertion_Failure := False;
2659 when 'c' =>
2660 Warn_On_Unrepped_Components := True;
2662 when 'C' =>
2663 Warn_On_Unrepped_Components := False;
2665 when 'o' =>
2666 Warn_On_All_Unread_Out_Parameters := True;
2668 when 'O' =>
2669 Warn_On_All_Unread_Out_Parameters := False;
2671 when 'r' =>
2672 Warn_On_Object_Renames_Function := True;
2674 when 'R' =>
2675 Warn_On_Object_Renames_Function := False;
2677 when 'x' =>
2678 Warn_On_Non_Local_Exception := True;
2680 when 'X' =>
2681 Warn_On_Non_Local_Exception := False;
2683 when others =>
2684 return False;
2685 end case;
2687 return True;
2688 end Set_Dot_Warning_Switch;
2690 ------------------------
2691 -- Set_Warning_Switch --
2692 ------------------------
2694 function Set_Warning_Switch (C : Character) return Boolean is
2695 begin
2696 case C is
2697 when 'a' =>
2698 Check_Unreferenced := True;
2699 Check_Unreferenced_Formals := True;
2700 Check_Withs := True;
2701 Constant_Condition_Warnings := True;
2702 Implementation_Unit_Warnings := True;
2703 Ineffective_Inline_Warnings := True;
2704 Warn_On_Ada_2005_Compatibility := True;
2705 Warn_On_Assertion_Failure := True;
2706 Warn_On_Assumed_Low_Bound := True;
2707 Warn_On_Bad_Fixed_Value := True;
2708 Warn_On_Constant := True;
2709 Warn_On_Export_Import := True;
2710 Warn_On_Modified_Unread := True;
2711 Warn_On_No_Value_Assigned := True;
2712 Warn_On_Non_Local_Exception := True;
2713 Warn_On_Obsolescent_Feature := True;
2714 Warn_On_Questionable_Missing_Parens := True;
2715 Warn_On_Redundant_Constructs := True;
2716 Warn_On_Object_Renames_Function := True;
2717 Warn_On_Unchecked_Conversion := True;
2718 Warn_On_Unrecognized_Pragma := True;
2719 Warn_On_Unrepped_Components := True;
2721 when 'A' =>
2722 Check_Unreferenced := False;
2723 Check_Unreferenced_Formals := False;
2724 Check_Withs := False;
2725 Constant_Condition_Warnings := False;
2726 Elab_Warnings := False;
2727 Implementation_Unit_Warnings := False;
2728 Ineffective_Inline_Warnings := False;
2729 Warn_On_Ada_2005_Compatibility := False;
2730 Warn_On_Assertion_Failure := False;
2731 Warn_On_Assumed_Low_Bound := False;
2732 Warn_On_Bad_Fixed_Value := False;
2733 Warn_On_Constant := False;
2734 Warn_On_Deleted_Code := False;
2735 Warn_On_Dereference := False;
2736 Warn_On_Export_Import := False;
2737 Warn_On_Hiding := False;
2738 Warn_On_Modified_Unread := False;
2739 Warn_On_No_Value_Assigned := False;
2740 Warn_On_Non_Local_Exception := False;
2741 Warn_On_Obsolescent_Feature := False;
2742 Warn_On_All_Unread_Out_Parameters := False;
2743 Warn_On_Questionable_Missing_Parens := False;
2744 Warn_On_Redundant_Constructs := False;
2745 Warn_On_Object_Renames_Function := False;
2746 Warn_On_Unchecked_Conversion := False;
2747 Warn_On_Unrecognized_Pragma := False;
2748 Warn_On_Unrepped_Components := False;
2750 when 'b' =>
2751 Warn_On_Bad_Fixed_Value := True;
2753 when 'B' =>
2754 Warn_On_Bad_Fixed_Value := False;
2756 when 'c' =>
2757 Constant_Condition_Warnings := True;
2759 when 'C' =>
2760 Constant_Condition_Warnings := False;
2762 when 'd' =>
2763 Warn_On_Dereference := True;
2765 when 'D' =>
2766 Warn_On_Dereference := False;
2768 when 'e' =>
2769 Warning_Mode := Treat_As_Error;
2771 when 'f' =>
2772 Check_Unreferenced_Formals := True;
2774 when 'F' =>
2775 Check_Unreferenced_Formals := False;
2777 when 'g' =>
2778 Warn_On_Unrecognized_Pragma := True;
2780 when 'G' =>
2781 Warn_On_Unrecognized_Pragma := False;
2783 when 'h' =>
2784 Warn_On_Hiding := True;
2786 when 'H' =>
2787 Warn_On_Hiding := False;
2789 when 'i' =>
2790 Implementation_Unit_Warnings := True;
2792 when 'I' =>
2793 Implementation_Unit_Warnings := False;
2795 when 'j' =>
2796 Warn_On_Obsolescent_Feature := True;
2798 when 'J' =>
2799 Warn_On_Obsolescent_Feature := False;
2801 when 'k' =>
2802 Warn_On_Constant := True;
2804 when 'K' =>
2805 Warn_On_Constant := False;
2807 when 'l' =>
2808 Elab_Warnings := True;
2810 when 'L' =>
2811 Elab_Warnings := False;
2813 when 'm' =>
2814 Warn_On_Modified_Unread := True;
2816 when 'M' =>
2817 Warn_On_Modified_Unread := False;
2819 when 'n' =>
2820 Warning_Mode := Normal;
2822 when 'o' =>
2823 Address_Clause_Overlay_Warnings := True;
2825 when 'O' =>
2826 Address_Clause_Overlay_Warnings := False;
2828 when 'p' =>
2829 Ineffective_Inline_Warnings := True;
2831 when 'P' =>
2832 Ineffective_Inline_Warnings := False;
2834 when 'q' =>
2835 Warn_On_Questionable_Missing_Parens := True;
2837 when 'Q' =>
2838 Warn_On_Questionable_Missing_Parens := False;
2840 when 'r' =>
2841 Warn_On_Redundant_Constructs := True;
2843 when 'R' =>
2844 Warn_On_Redundant_Constructs := False;
2846 when 's' =>
2847 Warning_Mode := Suppress;
2849 when 't' =>
2850 Warn_On_Deleted_Code := True;
2852 when 'T' =>
2853 Warn_On_Deleted_Code := False;
2855 when 'u' =>
2856 Check_Unreferenced := True;
2857 Check_Withs := True;
2858 Check_Unreferenced_Formals := True;
2860 when 'U' =>
2861 Check_Unreferenced := False;
2862 Check_Withs := False;
2863 Check_Unreferenced_Formals := False;
2865 when 'v' =>
2866 Warn_On_No_Value_Assigned := True;
2868 when 'V' =>
2869 Warn_On_No_Value_Assigned := False;
2871 when 'w' =>
2872 Warn_On_Assumed_Low_Bound := True;
2874 when 'W' =>
2875 Warn_On_Assumed_Low_Bound := False;
2877 when 'x' =>
2878 Warn_On_Export_Import := True;
2880 when 'X' =>
2881 Warn_On_Export_Import := False;
2883 when 'y' =>
2884 Warn_On_Ada_2005_Compatibility := True;
2886 when 'Y' =>
2887 Warn_On_Ada_2005_Compatibility := False;
2889 when 'z' =>
2890 Warn_On_Unchecked_Conversion := True;
2892 when 'Z' =>
2893 Warn_On_Unchecked_Conversion := False;
2895 when others =>
2896 return False;
2897 end case;
2899 return True;
2900 end Set_Warning_Switch;
2902 -----------------------------
2903 -- Warn_On_Known_Condition --
2904 -----------------------------
2906 procedure Warn_On_Known_Condition (C : Node_Id) is
2907 P : Node_Id;
2909 procedure Track (N : Node_Id; Loc : Node_Id);
2910 -- Adds continuation warning(s) pointing to reason (assignment or test)
2911 -- for the operand of the conditional having a known value (or at least
2912 -- enough is known about the value to issue the warning). N is the node
2913 -- which is judged to have a known value. Loc is the warning location.
2915 -----------
2916 -- Track --
2917 -----------
2919 procedure Track (N : Node_Id; Loc : Node_Id) is
2920 Nod : constant Node_Id := Original_Node (N);
2922 begin
2923 if Nkind (Nod) in N_Op_Compare then
2924 Track (Left_Opnd (Nod), Loc);
2925 Track (Right_Opnd (Nod), Loc);
2927 elsif Is_Entity_Name (Nod)
2928 and then Is_Object (Entity (Nod))
2929 then
2930 declare
2931 CV : constant Node_Id := Current_Value (Entity (Nod));
2933 begin
2934 if Present (CV) then
2935 Error_Msg_Sloc := Sloc (CV);
2937 if Nkind (CV) not in N_Subexpr then
2938 Error_Msg_N ("\\?(see test #)", Loc);
2940 elsif Nkind (Parent (CV)) =
2941 N_Case_Statement_Alternative
2942 then
2943 Error_Msg_N ("\\?(see case alternative #)", Loc);
2945 else
2946 Error_Msg_N ("\\?(see assignment #)", Loc);
2947 end if;
2948 end if;
2949 end;
2950 end if;
2951 end Track;
2953 -- Start of processing for Warn_On_Known_Condition
2955 begin
2956 -- Argument replacement in an inlined body can make conditions static.
2957 -- Do not emit warnings in this case.
2959 if In_Inlined_Body then
2960 return;
2961 end if;
2963 if Constant_Condition_Warnings
2964 and then Nkind (C) = N_Identifier
2965 and then
2966 (Entity (C) = Standard_False or else Entity (C) = Standard_True)
2967 and then Comes_From_Source (Original_Node (C))
2968 and then not In_Instance
2969 then
2970 -- See if this is in a statement or a declaration
2972 P := Parent (C);
2973 loop
2974 -- If tree is not attached, do not issue warning (this is very
2975 -- peculiar, and probably arises from some other error condition)
2977 if No (P) then
2978 return;
2980 -- If we are in a declaration, then no warning, since in practice
2981 -- conditionals in declarations are used for intended tests which
2982 -- may be known at compile time, e.g. things like
2984 -- x : constant Integer := 2 + (Word'Size = 32);
2986 -- And a warning is annoying in such cases
2988 elsif Nkind (P) in N_Declaration
2989 or else
2990 Nkind (P) in N_Later_Decl_Item
2991 then
2992 return;
2994 -- Don't warn in assert pragma, since presumably tests in such
2995 -- a context are very definitely intended, and might well be
2996 -- known at compile time. Note that we have to test the original
2997 -- node, since assert pragmas get rewritten at analysis time.
2999 elsif Nkind (Original_Node (P)) = N_Pragma
3000 and then Chars (Original_Node (P)) = Name_Assert
3001 then
3002 return;
3003 end if;
3005 exit when Is_Statement (P);
3006 P := Parent (P);
3007 end loop;
3009 -- Here we issue the warning unless some sub-operand has warnings
3010 -- set off, in which case we suppress the warning for the node. If
3011 -- the original expression is an inequality, it has been expanded
3012 -- into a negation, and the value of the original expression is the
3013 -- negation of the equality. If the expression is an entity that
3014 -- appears within a negation, it is clearer to flag the negation
3015 -- itself, and report on its constant value.
3017 if not Operand_Has_Warnings_Suppressed (C) then
3018 declare
3019 True_Branch : Boolean := Entity (C) = Standard_True;
3020 Cond : Node_Id := C;
3022 begin
3023 if Present (Parent (C))
3024 and then Nkind (Parent (C)) = N_Op_Not
3025 then
3026 True_Branch := not True_Branch;
3027 Cond := Parent (C);
3028 end if;
3030 if True_Branch then
3031 if Is_Entity_Name (Original_Node (C))
3032 and then Nkind (Cond) /= N_Op_Not
3033 then
3034 Error_Msg_NE
3035 ("object & is always True?", Cond, Original_Node (C));
3036 Track (Original_Node (C), Cond);
3038 else
3039 Error_Msg_N ("condition is always True?", Cond);
3040 Track (Cond, Cond);
3041 end if;
3043 else
3044 Error_Msg_N ("condition is always False?", Cond);
3045 Track (Cond, Cond);
3046 end if;
3047 end;
3048 end if;
3049 end if;
3050 end Warn_On_Known_Condition;
3052 ---------------------------------------
3053 -- Warn_On_Modified_As_Out_Parameter --
3054 ---------------------------------------
3056 function Warn_On_Modified_As_Out_Parameter (E : Entity_Id) return Boolean is
3057 begin
3058 return
3059 (Warn_On_Modified_Unread and then Is_Only_Out_Parameter (E))
3060 or else Warn_On_All_Unread_Out_Parameters;
3061 end Warn_On_Modified_As_Out_Parameter;
3063 ------------------------------
3064 -- Warn_On_Suspicious_Index --
3065 ------------------------------
3067 procedure Warn_On_Suspicious_Index (Name : Entity_Id; X : Node_Id) is
3069 Low_Bound : Uint;
3070 -- Set to lower bound for a suspicious type
3072 Ent : Entity_Id;
3073 -- Entity for array reference
3075 Typ : Entity_Id;
3076 -- Array type
3078 function Is_Suspicious_Type (Typ : Entity_Id) return Boolean;
3079 -- Tests to see if Typ is a type for which we may have a suspicious
3080 -- index, namely an unconstrained array type, whose lower bound is
3081 -- either zero or one. If so, True is returned, and Low_Bound is set
3082 -- to this lower bound. If not, False is returned, and Low_Bound is
3083 -- undefined on return.
3085 -- For now, we limite this to standard string types, so any other
3086 -- unconstrained types return False. We may change our minds on this
3087 -- later on, but strings seem the most important case.
3089 procedure Test_Suspicious_Index;
3090 -- Test if index is of suspicious type and if so, generate warning
3092 ------------------------
3093 -- Is_Suspicious_Type --
3094 ------------------------
3096 function Is_Suspicious_Type (Typ : Entity_Id) return Boolean is
3097 LB : Node_Id;
3099 begin
3100 if Is_Array_Type (Typ)
3101 and then not Is_Constrained (Typ)
3102 and then Number_Dimensions (Typ) = 1
3103 and then not Warnings_Off (Typ)
3104 and then (Root_Type (Typ) = Standard_String
3105 or else
3106 Root_Type (Typ) = Standard_Wide_String
3107 or else
3108 Root_Type (Typ) = Standard_Wide_Wide_String)
3109 then
3110 LB := Type_Low_Bound (Etype (First_Index (Typ)));
3112 if Compile_Time_Known_Value (LB) then
3113 Low_Bound := Expr_Value (LB);
3114 return Low_Bound = Uint_0 or else Low_Bound = Uint_1;
3115 end if;
3116 end if;
3118 return False;
3119 end Is_Suspicious_Type;
3121 ---------------------------
3122 -- Test_Suspicious_Index --
3123 ---------------------------
3125 procedure Test_Suspicious_Index is
3127 function Length_Reference (N : Node_Id) return Boolean;
3128 -- Check if node N is of the form Name'Length
3130 procedure Warn1;
3131 -- Generate first warning line
3133 ----------------------
3134 -- Length_Reference --
3135 ----------------------
3137 function Length_Reference (N : Node_Id) return Boolean is
3138 R : constant Node_Id := Original_Node (N);
3139 begin
3140 return
3141 Nkind (R) = N_Attribute_Reference
3142 and then Attribute_Name (R) = Name_Length
3143 and then Is_Entity_Name (Prefix (R))
3144 and then Entity (Prefix (R)) = Ent;
3145 end Length_Reference;
3147 -----------
3148 -- Warn1 --
3149 -----------
3151 procedure Warn1 is
3152 begin
3153 Error_Msg_Uint_1 := Low_Bound;
3154 Error_Msg_FE ("?index for& may assume lower bound of^", X, Ent);
3155 end Warn1;
3157 -- Start of processing for Test_Suspicious_Index
3159 begin
3160 -- Nothing to do if subscript does not come from source (we don't
3161 -- want to give garbage warnings on compiler expanded code, e.g. the
3162 -- loops generated for slice assignments. Sucb junk warnings would
3163 -- be placed on source constructs with no subscript in sight!)
3165 if not Comes_From_Source (Original_Node (X)) then
3166 return;
3167 end if;
3169 -- Case where subscript is a constant integer
3171 if Nkind (X) = N_Integer_Literal then
3172 Warn1;
3174 -- Case where original form of subscript is an integer literal
3176 if Nkind (Original_Node (X)) = N_Integer_Literal then
3177 if Intval (X) = Low_Bound then
3178 Error_Msg_FE
3179 ("\suggested replacement: `&''First`", X, Ent);
3180 else
3181 Error_Msg_Uint_1 := Intval (X) - Low_Bound;
3182 Error_Msg_FE
3183 ("\suggested replacement: `&''First + ^`", X, Ent);
3185 end if;
3187 -- Case where original form of subscript is more complex
3189 else
3190 -- Build string X'First - 1 + expression where the expression
3191 -- is the original subscript. If the expression starts with "1
3192 -- + ", then the "- 1 + 1" is elided.
3194 Error_Msg_String (1 .. 13) := "'First - 1 + ";
3195 Error_Msg_Strlen := 13;
3197 declare
3198 Sref : Source_Ptr := Sloc (First_Node (Original_Node (X)));
3199 Tref : constant Source_Buffer_Ptr :=
3200 Source_Text (Get_Source_File_Index (Sref));
3201 -- Tref (Sref) is used to scan the subscript
3203 Pctr : Natural;
3204 -- Paretheses counter when scanning subscript
3206 begin
3207 -- Tref (Sref) points to start of subscript
3209 -- Elide - 1 if subscript starts with 1 +
3211 if Tref (Sref .. Sref + 2) = "1 +" then
3212 Error_Msg_Strlen := Error_Msg_Strlen - 6;
3213 Sref := Sref + 2;
3215 elsif Tref (Sref .. Sref + 1) = "1+" then
3216 Error_Msg_Strlen := Error_Msg_Strlen - 6;
3217 Sref := Sref + 1;
3218 end if;
3220 -- Now we will copy the subscript to the string buffer
3222 Pctr := 0;
3223 loop
3224 -- Count parens, exit if terminating right paren. Note
3225 -- check to ignore paren appearing as character literal.
3227 if Tref (Sref + 1) = '''
3228 and then
3229 Tref (Sref - 1) = '''
3230 then
3231 null;
3232 else
3233 if Tref (Sref) = '(' then
3234 Pctr := Pctr + 1;
3235 elsif Tref (Sref) = ')' then
3236 exit when Pctr = 0;
3237 Pctr := Pctr - 1;
3238 end if;
3239 end if;
3241 -- Done if terminating double dot (slice case)
3243 exit when Pctr = 0
3244 and then (Tref (Sref .. Sref + 1) = ".."
3245 or else
3246 Tref (Sref .. Sref + 2) = " ..");
3248 -- Quit if we have hit EOF character, something wrong
3250 if Tref (Sref) = EOF then
3251 return;
3252 end if;
3254 -- String literals are too much of a pain to handle
3256 if Tref (Sref) = '"' or else Tref (Sref) = '%' then
3257 return;
3258 end if;
3260 -- If we have a 'Range reference, then this is a case
3261 -- where we cannot easily give a replacement. Don't try!
3263 if Tref (Sref .. Sref + 4) = "range"
3264 and then Tref (Sref - 1) < 'A'
3265 and then Tref (Sref + 5) < 'A'
3266 then
3267 return;
3268 end if;
3270 -- Else store next character
3272 Error_Msg_Strlen := Error_Msg_Strlen + 1;
3273 Error_Msg_String (Error_Msg_Strlen) := Tref (Sref);
3274 Sref := Sref + 1;
3276 -- If we get more than 40 characters then the expression
3277 -- is too long to copy, or something has gone wrong. In
3278 -- either case, just skip the attempt at a suggested fix.
3280 if Error_Msg_Strlen > 40 then
3281 return;
3282 end if;
3283 end loop;
3284 end;
3286 -- Replacement subscript is now in string buffer
3288 Error_Msg_FE
3289 ("\suggested replacement: `&~`", Original_Node (X), Ent);
3290 end if;
3292 -- Case where subscript is of the form X'Length
3294 elsif Length_Reference (X) then
3295 Warn1;
3296 Error_Msg_Node_2 := Ent;
3297 Error_Msg_FE
3298 ("\suggest replacement of `&''Length` by `&''Last`",
3299 X, Ent);
3301 -- Case where subscript is of the form X'Length - expression
3303 elsif Nkind (X) = N_Op_Subtract
3304 and then Length_Reference (Left_Opnd (X))
3305 then
3306 Warn1;
3307 Error_Msg_Node_2 := Ent;
3308 Error_Msg_FE
3309 ("\suggest replacement of `&''Length` by `&''Last`",
3310 Left_Opnd (X), Ent);
3311 end if;
3312 end Test_Suspicious_Index;
3314 -- Start of processing for Warn_On_Suspicious_Index
3316 begin
3317 -- Only process if warnings activated
3319 if Warn_On_Assumed_Low_Bound then
3321 -- Test if array is simple entity name
3323 if Is_Entity_Name (Name) then
3325 -- Test if array is parameter of unconstrained string type
3327 Ent := Entity (Name);
3328 Typ := Etype (Ent);
3330 if Is_Formal (Ent)
3331 and then Is_Suspicious_Type (Typ)
3332 and then not Low_Bound_Known (Ent)
3333 then
3334 Test_Suspicious_Index;
3335 end if;
3336 end if;
3337 end if;
3338 end Warn_On_Suspicious_Index;
3340 --------------------------------------
3341 -- Warn_On_Unassigned_Out_Parameter --
3342 --------------------------------------
3344 procedure Warn_On_Unassigned_Out_Parameter
3345 (Return_Node : Node_Id;
3346 Scope_Id : Entity_Id)
3348 Form : Entity_Id;
3349 Form2 : Entity_Id;
3351 begin
3352 -- Ignore if procedure or return statement does not come from source
3354 if not Comes_From_Source (Scope_Id)
3355 or else not Comes_From_Source (Return_Node)
3356 then
3357 return;
3358 end if;
3360 -- Loop through formals
3362 Form := First_Formal (Scope_Id);
3363 while Present (Form) loop
3365 -- We are only interested in OUT parameters that come from source
3366 -- and are never set in the source, and furthermore only in scalars
3367 -- since non-scalars generate too many false positives.
3369 if Ekind (Form) = E_Out_Parameter
3370 and then Never_Set_In_Source_Check_Spec (Form)
3371 and then Is_Scalar_Type (Etype (Form))
3372 and then not Present (Unset_Reference (Form))
3373 then
3374 -- Before we issue the warning, an add ad hoc defence against the
3375 -- most common case of false positives with this warning which is
3376 -- the case where there is a Boolean OUT parameter that has been
3377 -- set, and whose meaning is "ignore the values of the other
3378 -- parameters". We can't of course reliably tell this case at
3379 -- compile time, but the following test kills a lot of false
3380 -- positives, without generating a significant number of false
3381 -- negatives (missed real warnings).
3383 Form2 := First_Formal (Scope_Id);
3384 while Present (Form2) loop
3385 if Ekind (Form2) = E_Out_Parameter
3386 and then Root_Type (Etype (Form2)) = Standard_Boolean
3387 and then not Never_Set_In_Source_Check_Spec (Form2)
3388 then
3389 return;
3390 end if;
3392 Next_Formal (Form2);
3393 end loop;
3395 -- Here all conditionas are met, record possible unset reference
3397 Set_Unset_Reference (Form, Return_Node);
3398 end if;
3400 Next_Formal (Form);
3401 end loop;
3402 end Warn_On_Unassigned_Out_Parameter;
3404 ---------------------------------
3405 -- Warn_On_Unreferenced_Entity --
3406 ---------------------------------
3408 procedure Warn_On_Unreferenced_Entity
3409 (Spec_E : Entity_Id;
3410 Body_E : Entity_Id := Empty)
3412 E : Entity_Id := Spec_E;
3414 begin
3415 if not Referenced_Check_Spec (E) and then not Warnings_Off (E) then
3416 case Ekind (E) is
3417 when E_Variable =>
3419 -- Case of variable that is assigned but not read. We suppress
3420 -- the message if the variable is volatile, has an address
3421 -- clause, is aliasied, or is a renaming, or is imported.
3423 if Referenced_As_LHS_Check_Spec (E)
3424 and then No (Address_Clause (E))
3425 and then not Is_Volatile (E)
3426 then
3427 if Warn_On_Modified_Unread
3428 and then not Is_Imported (E)
3429 and then not Is_Return_Object (E)
3430 and then not Is_Aliased (E)
3431 and then No (Renamed_Object (E))
3432 then
3433 if not Has_Pragma_Unmodified_Check_Spec (E) then
3434 Error_Msg_N
3435 ("?variable & is assigned but never read!", E);
3436 end if;
3438 Set_Last_Assignment (E, Empty);
3439 end if;
3441 -- Normal case of neither assigned nor read (exclude variables
3442 -- referenced as out parameters, since we already generated
3443 -- appropriate warnings at the call point in this case).
3445 elsif not Referenced_As_Out_Parameter (E) then
3447 -- We suppress the message for types for which a valid
3448 -- pragma Unreferenced_Objects has been given, otherwise
3449 -- we go ahead and give the message.
3451 if not Has_Pragma_Unreferenced_Objects (Etype (E)) then
3453 -- Distinguish renamed case in message
3455 if Present (Renamed_Object (E))
3456 and then Comes_From_Source (Renamed_Object (E))
3457 then
3458 Error_Msg_N
3459 ("?renamed variable & is not referenced!", E);
3460 else
3461 Error_Msg_N
3462 ("?variable & is not referenced!", E);
3463 end if;
3464 end if;
3465 end if;
3467 when E_Constant =>
3468 if Present (Renamed_Object (E))
3469 and then Comes_From_Source (Renamed_Object (E))
3470 then
3471 Error_Msg_N
3472 ("?renamed constant & is not referenced!", E);
3473 else
3474 Error_Msg_N ("?constant & is not referenced!", E);
3475 end if;
3477 when E_In_Parameter |
3478 E_In_Out_Parameter =>
3480 -- Do not emit message for formals of a renaming, because
3481 -- they are never referenced explicitly.
3483 if Nkind (Original_Node (Unit_Declaration_Node (Scope (E))))
3484 /= N_Subprogram_Renaming_Declaration
3485 then
3486 -- Suppress this message for an IN OUT parameter of a
3487 -- non-scalar type, since it is normal to have only an
3488 -- assignment in such a case.
3490 if Ekind (E) = E_In_Parameter
3491 or else not Referenced_As_LHS_Check_Spec (E)
3492 or else Is_Scalar_Type (E)
3493 then
3494 if Present (Body_E) then
3495 E := Body_E;
3496 end if;
3497 Error_Msg_NE
3498 ("?formal parameter & is not referenced!", E, Spec_E);
3499 end if;
3500 end if;
3502 when E_Out_Parameter =>
3503 null;
3505 when E_Named_Integer |
3506 E_Named_Real =>
3507 Error_Msg_N ("?named number & is not referenced!", E);
3509 when E_Enumeration_Literal =>
3510 Error_Msg_N ("?literal & is not referenced!", E);
3512 when E_Function =>
3513 Error_Msg_N ("?function & is not referenced!", E);
3515 when E_Procedure =>
3516 Error_Msg_N ("?procedure & is not referenced!", E);
3518 when E_Generic_Procedure =>
3519 Error_Msg_N
3520 ("?generic procedure & is never instantiated!", E);
3522 when E_Generic_Function =>
3523 Error_Msg_N
3524 ("?generic function & is never instantiated!", E);
3526 when Type_Kind =>
3527 Error_Msg_N ("?type & is not referenced!", E);
3529 when others =>
3530 Error_Msg_N ("?& is not referenced!", E);
3531 end case;
3533 -- Kill warnings on the entity on which the message has been posted
3535 Set_Warnings_Off (E);
3536 end if;
3537 end Warn_On_Unreferenced_Entity;
3539 --------------------------------
3540 -- Warn_On_Useless_Assignment --
3541 --------------------------------
3543 procedure Warn_On_Useless_Assignment
3544 (Ent : Entity_Id;
3545 N : Node_Id := Empty)
3547 P : Node_Id;
3548 X : Node_Id;
3550 function Check_Ref (N : Node_Id) return Traverse_Result;
3551 -- Used to instantiate Traverse_Func. Returns Abandon if
3552 -- a reference to the entity in question is found.
3554 function Test_No_Refs is new Traverse_Func (Check_Ref);
3556 ---------------
3557 -- Check_Ref --
3558 ---------------
3560 function Check_Ref (N : Node_Id) return Traverse_Result is
3561 begin
3562 -- Check reference to our identifier. We use name equality here
3563 -- because the exception handlers have not yet been analyzed. This
3564 -- is not quite right, but it really does not matter that we fail
3565 -- to output the warning in some obscure cases of name clashes.
3567 if Nkind (N) = N_Identifier
3568 and then Chars (N) = Chars (Ent)
3569 then
3570 return Abandon;
3571 else
3572 return OK;
3573 end if;
3574 end Check_Ref;
3576 -- Start of processing for Warn_On_Useless_Assignment
3578 begin
3579 -- Check if this is a case we want to warn on, a scalar or access
3580 -- variable with the last assignment field set, with warnings enabled,
3581 -- and which is not imported or exported. We also check that it is OK
3582 -- to capture the value. We are not going to capture any value, but
3583 -- the warning messages depends on the same kind of conditions.
3585 if Is_Assignable (Ent)
3586 and then not Is_Return_Object (Ent)
3587 and then Present (Last_Assignment (Ent))
3588 and then not Warnings_Off (Ent)
3589 and then not Has_Pragma_Unreferenced_Check_Spec (Ent)
3590 and then not Is_Imported (Ent)
3591 and then not Is_Exported (Ent)
3592 and then Safe_To_Capture_Value (N, Ent)
3593 then
3594 -- Before we issue the message, check covering exception handlers.
3595 -- Search up tree for enclosing statement sequences and handlers
3597 P := Parent (Last_Assignment (Ent));
3598 while Present (P) loop
3600 -- Something is really wrong if we don't find a handled
3601 -- statement sequence, so just suppress the warning.
3603 if No (P) then
3604 Set_Last_Assignment (Ent, Empty);
3605 return;
3607 -- When we hit a package/subprogram body, issue warning and exit
3609 elsif Nkind (P) = N_Subprogram_Body
3610 or else Nkind (P) = N_Package_Body
3611 then
3612 -- Case of assigned value never referenced
3614 if No (N) then
3616 -- Don't give this for OUT and IN OUT formals, since
3617 -- clearly caller may reference the assigned value. Also
3618 -- never give such warnings for internal variables.
3620 if Ekind (Ent) = E_Variable
3621 and then not Is_Internal_Name (Chars (Ent))
3622 then
3623 if Referenced_As_Out_Parameter (Ent) then
3624 Error_Msg_NE
3625 ("?& modified by call, but value never referenced",
3626 Last_Assignment (Ent), Ent);
3627 else
3628 Error_Msg_NE
3629 ("?useless assignment to&, value never referenced!",
3630 Last_Assignment (Ent), Ent);
3631 end if;
3632 end if;
3634 -- Case of assigned value overwritten
3636 else
3637 Error_Msg_Sloc := Sloc (N);
3639 if Referenced_As_Out_Parameter (Ent) then
3640 Error_Msg_NE
3641 ("?& modified by call, but value overwritten #!",
3642 Last_Assignment (Ent), Ent);
3643 else
3644 Error_Msg_NE
3645 ("?useless assignment to&, value overwritten #!",
3646 Last_Assignment (Ent), Ent);
3647 end if;
3648 end if;
3650 -- Clear last assignment indication and we are done
3652 Set_Last_Assignment (Ent, Empty);
3653 return;
3655 -- Enclosing handled sequence of statements
3657 elsif Nkind (P) = N_Handled_Sequence_Of_Statements then
3659 -- Check exception handlers present
3661 if Present (Exception_Handlers (P)) then
3663 -- If we are not at the top level, we regard an inner
3664 -- exception handler as a decisive indicator that we should
3665 -- not generate the warning, since the variable in question
3666 -- may be acceessed after an exception in the outer block.
3668 if Nkind (Parent (P)) /= N_Subprogram_Body
3669 and then Nkind (Parent (P)) /= N_Package_Body
3670 then
3671 Set_Last_Assignment (Ent, Empty);
3672 return;
3674 -- Otherwise we are at the outer level. An exception
3675 -- handler is significant only if it references the
3676 -- variable in question.
3678 else
3679 X := First (Exception_Handlers (P));
3680 while Present (X) loop
3681 if Test_No_Refs (X) = Abandon then
3682 Set_Last_Assignment (Ent, Empty);
3683 return;
3684 end if;
3686 X := Next (X);
3687 end loop;
3688 end if;
3689 end if;
3690 end if;
3692 P := Parent (P);
3693 end loop;
3694 end if;
3695 end Warn_On_Useless_Assignment;
3697 ---------------------------------
3698 -- Warn_On_Useless_Assignments --
3699 ---------------------------------
3701 procedure Warn_On_Useless_Assignments (E : Entity_Id) is
3702 Ent : Entity_Id;
3703 begin
3704 if Warn_On_Modified_Unread
3705 and then In_Extended_Main_Source_Unit (E)
3706 then
3707 Ent := First_Entity (E);
3708 while Present (Ent) loop
3709 Warn_On_Useless_Assignment (Ent);
3710 Next_Entity (Ent);
3711 end loop;
3712 end if;
3713 end Warn_On_Useless_Assignments;
3715 end Sem_Warn;