OSDN Git Service

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