OSDN Git Service

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