OSDN Git Service

* Make-lang.in (gnat_ug_unx.info): Add dependency on stmp-docobjdir.
[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-2002 Free Software Foundation, Inc.          --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, USA.                                                      --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 with Alloc;
28 with Atree;    use Atree;
29 with Einfo;    use Einfo;
30 with Errout;   use Errout;
31 with Fname;    use Fname;
32 with Lib;      use Lib;
33 with Nlists;   use Nlists;
34 with Opt;      use Opt;
35 with Sem;      use Sem;
36 with Sem_Util; use Sem_Util;
37 with Sinfo;    use Sinfo;
38 with Sinput;   use Sinput;
39 with Snames;   use Snames;
40 with Stand;    use Stand;
41 with Table;
42
43 package body Sem_Warn is
44
45    --  The following table collects Id's of entities that are potentially
46    --  unreferenced. See Check_Unset_Reference for further details.
47
48    package Unreferenced_Entities is new Table.Table (
49      Table_Component_Type => Entity_Id,
50      Table_Index_Type     => Nat,
51      Table_Low_Bound      => 1,
52      Table_Initial        => Alloc.Unreferenced_Entities_Initial,
53      Table_Increment      => Alloc.Unreferenced_Entities_Increment,
54      Table_Name           => "Unreferenced_Entities");
55
56    --  One entry is made in the following table for each branch of
57    --  a conditional, e.g. an if-then-elsif-else-endif structure
58    --  creates three entries in this table.
59
60    type Branch_Entry is record
61       Sloc : Source_Ptr;
62       --  Location for warnings associated with this branch
63
64       Defs : Elist_Id;
65       --  List of entities defined for the first time in this branch. On
66       --  exit from a conditional structure, any entity that is in the
67       --  list of all branches is removed (and the entity flagged as
68       --  defined by the conditional as a whole). Thus after processing
69       --  a conditional, Defs contains a list of entities defined in this
70       --  branch for the first time, but not defined at all in some other
71       --  branch of the same conditional. A value of No_Elist is used to
72       --  represent the initial empty list.
73
74       Next : Nat;
75       --  Index of next branch for this conditional, zero = last branch
76    end record;
77
78    package Branch_Table is new Table.Table (
79      Table_Component_Type => Branch_Entry,
80      Table_Index_Type     => Nat,
81      Table_Low_Bound      => 1,
82      Table_Initial        => Alloc.Branches_Initial,
83      Table_Increment      => Alloc.Branches_Increment,
84      Table_Name           => "Branches");
85
86    --  The following table is used to represent conditionals, there is
87    --  one entry in this table for each conditional structure.
88
89    type Conditional_Entry is record
90       If_Stmt : Boolean;
91       --  True for IF statement, False for CASE statement
92
93       First_Branch : Nat;
94       --  Index in Branch table of first branch, zero = none yet
95
96       Current_Branch : Nat;
97       --  Index in Branch table of current branch, zero = none yet
98    end record;
99
100    package Conditional_Table is new Table.Table (
101      Table_Component_Type => Conditional_Entry,
102      Table_Index_Type     => Nat,
103      Table_Low_Bound      => 1,
104      Table_Initial        => Alloc.Conditionals_Initial,
105      Table_Increment      => Alloc.Conditionals_Increment,
106      Table_Name           => "Conditionals");
107
108    --  The following table is a stack that keeps track of the current
109    --  conditional. The Last entry is the top of the stack. An Empty
110    --  entry represents the start of a compilation unit. Non-zero
111    --  entries in the stack are indexes into the conditional table.
112
113    package Conditional_Stack is new Table.Table (
114      Table_Component_Type => Nat,
115      Table_Index_Type     => Nat,
116      Table_Low_Bound      => 1,
117      Table_Initial        => Alloc.Conditional_Stack_Initial,
118      Table_Increment      => Alloc.Conditional_Stack_Increment,
119      Table_Name           => "Conditional_Stack");
120
121    function Operand_Has_Warnings_Suppressed (N : Node_Id) return Boolean;
122    --  This function traverses the expression tree represented by the node
123    --  N and determines if any sub-operand is a reference to an entity for
124    --  which the Warnings_Off flag is set. True is returned if such an
125    --  entity is encountered, and False otherwise.
126
127    ----------------------
128    -- Check_References --
129    ----------------------
130
131    procedure Check_References (E : Entity_Id; Anod : Node_Id := Empty) is
132       E1 : Entity_Id;
133       UR : Node_Id;
134       PU : Node_Id;
135
136       procedure Output_Reference_Error (M : String);
137       --  Used to output an error message. Deals with posting the error on
138       --  the body formal in the accept case.
139
140       function Publicly_Referenceable (Ent : Entity_Id) return Boolean;
141       --  This is true if the entity in question is potentially referenceable
142       --  from another unit. This is true for entities in packages that are
143       --  at the library level.
144
145       ----------------------------
146       -- Output_Reference_Error --
147       ----------------------------
148
149       procedure Output_Reference_Error (M : String) is
150       begin
151          --  Other than accept case, post error on defining identifier
152
153          if No (Anod) then
154             Error_Msg_N (M, E1);
155
156          --  Accept case, find body formal to post the message
157
158          else
159             declare
160                Parm  : Node_Id;
161                Enod  : Node_Id;
162                Defid : Entity_Id;
163
164             begin
165                Enod := Anod;
166
167                if Present (Parameter_Specifications (Anod)) then
168                   Parm := First (Parameter_Specifications (Anod));
169
170                   while Present (Parm) loop
171                      Defid := Defining_Identifier (Parm);
172
173                      if Chars (E1) = Chars (Defid) then
174                         Enod := Defid;
175                         exit;
176                      end if;
177
178                      Next (Parm);
179                   end loop;
180                end if;
181
182                Error_Msg_NE (M, Enod, E1);
183             end;
184          end if;
185       end Output_Reference_Error;
186
187       ----------------------------
188       -- Publicly_Referenceable --
189       ----------------------------
190
191       function Publicly_Referenceable (Ent : Entity_Id) return Boolean is
192          P : Node_Id;
193
194       begin
195          --  Examine parents to look for a library level package spec
196          --  But if we find a body or block or other similar construct
197          --  along the way, we cannot be referenced.
198
199          P := Parent (Ent);
200          loop
201             case Nkind (P) is
202
203                --  If we get to top of tree, then publicly referencable
204
205                when N_Empty =>
206                   return True;
207
208                --  If we reach a generic package declaration, then always
209                --  consider this referenceable, since any instantiation will
210                --  have access to the entities in the generic package. Note
211                --  that the package itself may not be instantiated, but then
212                --  we will get a warning for the package entity
213
214                when N_Generic_Package_Declaration =>
215                   return True;
216
217                --  If we reach any body, then definitely not referenceable
218
219                when N_Package_Body    |
220                     N_Subprogram_Body |
221                     N_Task_Body       |
222                     N_Entry_Body      |
223                     N_Protected_Body  |
224                     N_Block_Statement |
225                     N_Subunit         =>
226                   return False;
227
228                --  For all other cases, keep looking up tree
229
230                when others =>
231                   P := Parent (P);
232             end case;
233          end loop;
234       end Publicly_Referenceable;
235
236    --  Start of processing for Check_References
237
238    begin
239       --  No messages if warnings are suppressed, or if we have detected
240       --  any real errors so far (this last check avoids junk messages
241       --  resulting from errors, e.g. a subunit that is not loaded).
242
243       --  We also skip the messages if any subunits were not loaded (see
244       --  comment in Sem_Ch10 to understand how this is set, and why it is
245       --  necessary to suppress the warnings in this case).
246
247       if Warning_Mode = Suppress
248         or else Serious_Errors_Detected /= 0
249         or else Unloaded_Subunits
250       then
251          return;
252       end if;
253
254       --  Otherwise loop through entities, looking for suspicious stuff
255
256       E1 := First_Entity (E);
257       while Present (E1) loop
258
259          --  We only look at source entities with warning flag off
260
261          if Comes_From_Source (E1) and then not Warnings_Off (E1) then
262
263             --  We are interested in variables and out parameters, but we
264             --  exclude protected types, too complicated to worry about.
265
266             if Ekind (E1) = E_Variable
267                  or else
268                (Ekind (E1) = E_Out_Parameter
269                   and then not Is_Protected_Type (Current_Scope))
270             then
271                --  Post warning if this object not assigned. Note that we
272                --  do not consider the implicit initialization of an access
273                --  type to be the assignment of a value for this purpose.
274                --  If the entity is an out parameter of the current subprogram
275                --  body, check the warning status of the parameter in the spec.
276
277                if Ekind (E1) = E_Out_Parameter
278                  and then Present (Spec_Entity (E1))
279                  and then Warnings_Off (Spec_Entity (E1))
280                then
281                   null;
282
283                elsif Not_Source_Assigned (E1) then
284                   Output_Reference_Error ("& is never assigned a value?");
285
286                   --  Deal with special case where this variable is hidden
287                   --  by a loop variable
288
289                   if Ekind (E1) = E_Variable
290                     and then Present (Hiding_Loop_Variable (E1))
291                   then
292                      Error_Msg_Sloc := Sloc (E1);
293                      Error_Msg_N
294                        ("declaration hides &#?",
295                         Hiding_Loop_Variable (E1));
296                      Error_Msg_N
297                        ("for loop implicitly declares loop variable?",
298                         Hiding_Loop_Variable (E1));
299                   end if;
300
301                   goto Continue;
302                end if;
303
304                --  Check for unset reference, note that we exclude access
305                --  types from this check, since access types do always have
306                --  a null value, and that seems legitimate in this case.
307
308                UR := Unset_Reference (E1);
309                if Present (UR) then
310
311                   --  For access types, the only time we complain is when
312                   --  we have a dereference (of a null value)
313
314                   if Is_Access_Type (Etype (E1)) then
315                      PU := Parent (UR);
316
317                      if (Nkind (PU) = N_Selected_Component
318                            or else
319                          Nkind (PU) = N_Explicit_Dereference
320                            or else
321                          Nkind (PU) = N_Indexed_Component)
322                        and then
323                          Prefix (PU) = UR
324                      then
325                         Error_Msg_N ("& may be null?", UR);
326                         goto Continue;
327                      end if;
328
329                   --  For other than access type, go back to original node
330                   --  to deal with case where original unset reference
331                   --  has been rewritten during expansion.
332
333                   else
334                      UR := Original_Node (UR);
335
336                      --  In some cases, the original node may be a type
337                      --  conversion or qualification, and in this case
338                      --  we want the object entity inside.
339
340                      while Nkind (UR) = N_Type_Conversion
341                        or else Nkind (UR) = N_Qualified_Expression
342                      loop
343                         UR := Expression (UR);
344                      end loop;
345
346                      Error_Msg_N
347                        ("& may be referenced before it has a value?", UR);
348                      goto Continue;
349                   end if;
350                end if;
351             end if;
352
353             --  Then check for unreferenced variables
354
355             if not Referenced (E1)
356
357                --  Check that warnings on unreferenced entities are enabled
358
359               and then ((Check_Unreferenced and then not Is_Formal (E1))
360                            or else
361                         (Check_Unreferenced_Formals and then Is_Formal (E1)))
362
363                --  Warnings are placed on objects, types, subprograms,
364                --  labels, and enumeration literals.
365
366                and then (Is_Object (E1)
367                            or else
368                          Is_Type (E1)
369                            or else
370                          Ekind (E1) = E_Label
371                            or else
372                          Ekind (E1) = E_Named_Integer
373                            or else
374                          Ekind (E1) = E_Named_Real
375                            or else
376                          Is_Overloadable (E1))
377
378                --  We only place warnings for the extended main unit
379
380                and then In_Extended_Main_Source_Unit (E1)
381
382                --  Exclude instantiations, since there is no reason why
383                --  every entity in an instantiation should be referenced.
384
385                and then Instantiation_Location (Sloc (E1)) = No_Location
386
387                --  Exclude formal parameters from bodies if the corresponding
388                --  spec entity has been referenced in the case where there is
389                --  a separate spec.
390
391                and then not (Is_Formal (E1)
392                                and then
393                              Ekind (Scope (E1)) = E_Subprogram_Body
394                                and then
395                              Present (Spec_Entity (E1))
396                                and then
397                              Referenced (Spec_Entity (E1)))
398
399                --  Consider private type referenced if full view is referenced
400
401                and then not (Is_Private_Type (E1)
402                                and then
403                              Referenced (Full_View (E1)))
404
405                --  Don't worry about full view, only about private type
406
407                and then not Has_Private_Declaration (E1)
408
409                --  Eliminate dispatching operations from consideration, we
410                --  cannot tell if these are referenced or not in any easy
411                --  manner (note this also catches Adjust/Finalize/Initialize)
412
413                and then not Is_Dispatching_Operation (E1)
414
415                --  Check entity that can be publicly referenced (we do not
416                --  give messages for such entities, since there could be
417                --  other units, not involved in this compilation, that
418                --  contain relevant references.
419
420                and then not Publicly_Referenceable (E1)
421
422                --  Class wide types are marked as source entities, but
423                --  they are not really source entities, and are always
424                --  created, so we do not care if they are not referenced.
425
426                and then Ekind (E1) /= E_Class_Wide_Type
427
428                --  Objects other than parameters of task types are allowed
429                --  to be non-referenced, since they start up tasks!
430
431                and then ((Ekind (E1) /= E_Variable
432                              and then Ekind (E1) /= E_Constant
433                              and then Ekind (E1) /= E_Component)
434                            or else not Is_Task_Type (Etype (E1)))
435
436                --  For subunits, only place warnings on the main unit
437                --  itself, since parent units are not completely compiled
438
439                and then (Nkind (Unit (Cunit (Main_Unit))) /= N_Subunit
440                            or else
441                          Get_Source_Unit (E1) = Main_Unit)
442             then
443                --  Suppress warnings in internal units if not in -gnatg
444                --  mode (these would be junk warnings for an applications
445                --  program, since they refer to problems in internal units)
446
447                if GNAT_Mode
448                  or else not
449                    Is_Internal_File_Name
450                      (Unit_File_Name (Get_Source_Unit (E1)))
451                then
452                   --  We do not immediately flag the error. This is because
453                   --  we have not expanded generic bodies yet, and they may
454                   --  have the missing reference. So instead we park the
455                   --  entity on a list, for later processing. However, for
456                   --  the accept case, post the error right here, since we
457                   --  have the information now in this case.
458
459                   if Present (Anod) then
460                      Output_Reference_Error ("& is not referenced?");
461
462                   else
463                      Unreferenced_Entities.Increment_Last;
464                      Unreferenced_Entities.Table
465                        (Unreferenced_Entities.Last) := E1;
466                   end if;
467                end if;
468             end if;
469          end if;
470
471          --  Recurse into nested package or block
472
473          <<Continue>>
474             if (Ekind (E1) = E_Package
475                   and then Nkind (Parent (E1)) = N_Package_Specification)
476               or else Ekind (E1) = E_Block
477             then
478                Check_References (E1);
479             end if;
480
481             Next_Entity (E1);
482       end loop;
483    end Check_References;
484
485    ---------------------------
486    -- Check_Unset_Reference --
487    ---------------------------
488
489    procedure Check_Unset_Reference (N : Node_Id) is
490    begin
491       --  Nothing to do if warnings suppressed
492
493       if Warning_Mode = Suppress then
494          return;
495       end if;
496
497       --  Otherwise see what kind of node we have. If the entity already
498       --  has an unset reference, it is not necessarily the earliest in
499       --  the text, because resolution of the prefix of selected components
500       --  is completed before the resolution of the selected component itself.
501       --  as a result, given  (R /= null and then R.X > 0), the occurrences
502       --  of R are examined in right-to-left order. If there is already an
503       --  unset reference, we check whether N is earlier before proceeding.
504
505       case Nkind (N) is
506
507          when N_Identifier | N_Expanded_Name =>
508             declare
509                E  : constant Entity_Id := Entity (N);
510
511             begin
512                if (Ekind (E) = E_Variable
513                     or else Ekind (E) = E_Out_Parameter)
514                  and then Not_Source_Assigned (E)
515                  and then (No (Unset_Reference (E))
516                              or else Earlier_In_Extended_Unit
517                                (Sloc (N),  Sloc (Unset_Reference (E))))
518                  and then not Warnings_Off (E)
519                then
520                   --  Here we have a potential unset reference. But before we
521                   --  get worried about it, we have to make sure that the
522                   --  entity declaration is in the same procedure as the
523                   --  reference, since if they are in separate procedures,
524                   --  then we have no idea about sequential execution.
525
526                   --  The tests in the loop below catch all such cases, but
527                   --  do allow the reference to appear in a loop, block, or
528                   --  package spec that is nested within the declaring scope.
529                   --  As always, it is possible to construct cases where the
530                   --  warning is wrong, that is why it is a warning!
531
532                   --  If the entity is an out_parameter, it is ok to read its
533                   --  its discriminants (that was true in Ada83) so suppress
534                   --  the message in that case as well.
535
536                   if Ekind (E) = E_Out_Parameter
537                     and then Nkind (Parent (N)) = N_Selected_Component
538                     and then Ekind (Entity (Selector_Name (Parent (N))))
539                       = E_Discriminant
540                   then
541                      return;
542                   end if;
543
544                   declare
545                      SR : Entity_Id;
546                      SE : constant Entity_Id := Scope (E);
547
548                   begin
549                      SR := Current_Scope;
550                      while SR /= SE loop
551                         if SR = Standard_Standard
552                           or else Is_Subprogram (SR)
553                           or else Is_Concurrent_Body (SR)
554                           or else Is_Concurrent_Type (SR)
555                         then
556                            return;
557                         end if;
558
559                         SR := Scope (SR);
560                      end loop;
561
562                      if Nkind (N) = N_Identifier then
563                         Set_Unset_Reference (E, N);
564                      else
565                         Set_Unset_Reference (E, Selector_Name (N));
566                      end if;
567                   end;
568                end if;
569             end;
570
571          when N_Indexed_Component | N_Selected_Component | N_Slice =>
572             Check_Unset_Reference (Prefix (N));
573             return;
574
575          when N_Type_Conversion | N_Qualified_Expression =>
576             Check_Unset_Reference (Expression (N));
577
578          when others =>
579             null;
580
581       end case;
582    end Check_Unset_Reference;
583
584    ------------------------
585    -- Check_Unused_Withs --
586    ------------------------
587
588    procedure Check_Unused_Withs (Spec_Unit : Unit_Number_Type := No_Unit) is
589       Cnode : Node_Id;
590       Item  : Node_Id;
591       Lunit : Node_Id;
592       Ent   : Entity_Id;
593
594       Munite : constant Entity_Id := Cunit_Entity (Main_Unit);
595       --  This is needed for checking the special renaming case
596
597       procedure Check_One_Unit (Unit : Unit_Number_Type);
598       --  Subsidiary procedure, performs checks for specified unit
599
600       --------------------
601       -- Check_One_Unit --
602       --------------------
603
604       procedure Check_One_Unit (Unit : Unit_Number_Type) is
605          Is_Visible_Renaming : Boolean := False;
606          Pack                : Entity_Id;
607
608          function Find_Package_Renaming
609            (P : Entity_Id;
610             L : Entity_Id) return Entity_Id;
611          --  The only reference to a context unit may be in a renaming
612          --  declaration. If this renaming declares a visible entity, do
613          --  not warn that the context clause could be moved to the body,
614          --  because the renaming may be intented to re-export the unit.
615
616          ---------------------------
617          -- Find_Package_Renaming --
618          ---------------------------
619
620          function Find_Package_Renaming
621            (P : Entity_Id;
622             L : Entity_Id) return Entity_Id
623          is
624             E1 : Entity_Id;
625             R  : Entity_Id;
626
627          begin
628             Is_Visible_Renaming := False;
629             E1 := First_Entity (P);
630
631             while Present (E1) loop
632                if Ekind (E1) = E_Package
633                   and then Renamed_Object (E1) = L
634                then
635                   Is_Visible_Renaming := not Is_Hidden (E1);
636                   return E1;
637
638                elsif Ekind (E1) = E_Package
639                  and then No (Renamed_Object (E1))
640                  and then not Is_Generic_Instance (E1)
641                then
642                   R := Find_Package_Renaming (E1, L);
643
644                   if Present (R) then
645                      Is_Visible_Renaming := not Is_Hidden (R);
646                      return R;
647                   end if;
648                end if;
649
650                Next_Entity (E1);
651             end loop;
652
653             return Empty;
654          end Find_Package_Renaming;
655
656       --  Start of processing for Check_One_Unit
657
658       begin
659          Cnode := Cunit (Unit);
660
661          --  Only do check in units that are part of the extended main
662          --  unit. This is actually a necessary restriction, because in
663          --  the case of subprogram acting as its own specification,
664          --  there can be with's in subunits that we will not see.
665
666          if not In_Extended_Main_Source_Unit (Cnode) then
667             return;
668
669          --  In No_Run_Time_Mode, we remove the bodies of non-
670          --  inlined subprograms, which may lead to spurious
671          --  warnings, clearly undesirable.
672
673          elsif No_Run_Time
674            and then Is_Predefined_File_Name (Unit_File_Name (Unit))
675          then
676             return;
677          end if;
678
679          --  Loop through context items in this unit
680
681          Item := First (Context_Items (Cnode));
682          while Present (Item) loop
683
684             if Nkind (Item) = N_With_Clause
685                and then not Implicit_With (Item)
686                and then In_Extended_Main_Source_Unit (Item)
687             then
688                Lunit := Entity (Name (Item));
689
690                --  Check if this unit is referenced
691
692                if not Referenced (Lunit) then
693
694                   --  Suppress warnings in internal units if not in -gnatg
695                   --  mode (these would be junk warnings for an applications
696                   --  program, since they refer to problems in internal units)
697
698                   if GNAT_Mode
699                     or else not Is_Internal_File_Name (Unit_File_Name (Unit))
700                   then
701                      --  Here we definitely have a non-referenced unit. If
702                      --  it is the special call for a spec unit, then just
703                      --  set the flag to be read later.
704
705                      if Unit = Spec_Unit then
706                         Set_Unreferenced_In_Spec (Item);
707
708                      --  Otherwise simple unreferenced message
709
710                      else
711                         Error_Msg_N
712                           ("unit& is not referenced?", Name (Item));
713                      end if;
714                   end if;
715
716                --  If main unit is a renaming of this unit, then we consider
717                --  the with to be OK (obviously it is needed in this case!)
718
719                elsif Present (Renamed_Entity (Munite))
720                   and then Renamed_Entity (Munite) = Lunit
721                then
722                   null;
723
724                --  If this unit is referenced, and it is a package, we
725                --  do another test, to see if any of the entities in the
726                --  package are referenced. If none of the entities are
727                --  referenced, we still post a warning. This occurs if
728                --  the only use of the package is in a use clause, or
729                --  in a package renaming declaration.
730
731                elsif Ekind (Lunit) = E_Package then
732
733                   --  If Is_Instantiated is set, it means that the package
734                   --  is implicitly instantiated (this is the case of a
735                   --  parent instance or an actual for a generic package
736                   --  formal), and this counts as a reference.
737
738                   if Is_Instantiated (Lunit) then
739                      null;
740
741                   --  If no entities in package, and there is a pragma
742                   --  Elaborate_Body present, then assume that this with
743                   --  is done for purposes of this elaboration.
744
745                   elsif No (First_Entity (Lunit))
746                     and then Has_Pragma_Elaborate_Body (Lunit)
747                   then
748                      null;
749
750                   --  Otherwise see if any entities have been referenced
751
752                   else
753                      Ent  := First_Entity (Lunit);
754
755                      loop
756                         --  No more entities, and we did not find one
757                         --  that was referenced. Means we have a definite
758                         --  case of a with none of whose entities was
759                         --  referenced.
760
761                         if No (Ent) then
762
763                            --  If in spec, just set the flag
764
765                            if Unit = Spec_Unit then
766                               Set_No_Entities_Ref_In_Spec (Item);
767
768                            --  Else give the warning
769
770                            else
771                               Error_Msg_N
772                                 ("no entities of & are referenced?",
773                                  Name (Item));
774
775                               --  Look for renamings of this package, and
776                               --  flag them as well. If the original package
777                               --  has warnings off, we suppress the warning
778                               --  on the renaming as well.
779
780                               Pack := Find_Package_Renaming (Munite, Lunit);
781
782                               if Present (Pack)
783                                 and then not Warnings_Off (Lunit)
784                               then
785                                  Error_Msg_NE
786                                    ("no entities of & are referenced?",
787                                      Unit_Declaration_Node (Pack),
788                                        Pack);
789                               end if;
790                            end if;
791
792                            exit;
793
794                         --  Case of next entity is referenced
795
796                         elsif Referenced (Ent) then
797
798                            --  This means that the with is indeed fine, in
799                            --  that it is definitely needed somewhere, and
800                            --  we can quite worrying about this one.
801
802                            --  Except for one little detail, if either of
803                            --  the flags was set during spec processing,
804                            --  this is where we complain that the with
805                            --  could be moved from the spec. If the spec
806                            --  contains a visible renaming of the package,
807                            --  inhibit warning to move with_clause to body.
808
809                            if Ekind (Munite) = E_Package_Body then
810                               Pack :=
811                                 Find_Package_Renaming
812                                   (Spec_Entity (Munite), Lunit);
813                            end if;
814
815                            if Unreferenced_In_Spec (Item) then
816                               Error_Msg_N
817                                 ("unit& is not referenced in spec?",
818                                  Name (Item));
819
820                            elsif No_Entities_Ref_In_Spec (Item) then
821                               Error_Msg_N
822                                 ("no entities of & are referenced in spec?",
823                                  Name (Item));
824
825                            else
826                               exit;
827                            end if;
828
829                            if not Is_Visible_Renaming then
830                               Error_Msg_N
831                                 ("\with clause might be moved to body?",
832                                  Name (Item));
833                            end if;
834
835                            exit;
836
837                         --  Move to next entity to continue search
838
839                         else
840                            Next_Entity (Ent);
841                         end if;
842                      end loop;
843                   end if;
844
845                --  For a generic package, the only interesting kind of
846                --  reference is an instantiation, since entities cannot
847                --  be referenced directly.
848
849                elsif Is_Generic_Unit (Lunit) then
850
851                   --  Unit was never instantiated, set flag for case of spec
852                   --  call, or give warning for normal call.
853
854                   if not Is_Instantiated (Lunit) then
855                      if Unit = Spec_Unit then
856                         Set_Unreferenced_In_Spec (Item);
857                      else
858                         Error_Msg_N
859                           ("unit& is never instantiated?", Name (Item));
860                      end if;
861
862                   --  If unit was indeed instantiated, make sure that
863                   --  flag is not set showing it was uninstantiated in
864                   --  the spec, and if so, give warning.
865
866                   elsif Unreferenced_In_Spec (Item) then
867                      Error_Msg_N
868                        ("unit& is not instantiated in spec?", Name (Item));
869                      Error_Msg_N
870                        ("\with clause can be moved to body?", Name (Item));
871                   end if;
872                end if;
873             end if;
874
875             Next (Item);
876          end loop;
877
878       end Check_One_Unit;
879
880    --  Start of processing for Check_Unused_Withs
881
882    begin
883       if not Opt.Check_Withs
884         or else Operating_Mode = Check_Syntax
885       then
886          return;
887       end if;
888
889       --  Flag any unused with clauses, but skip this step if we are
890       --  compiling a subunit on its own, since we do not have enough
891       --  information to determine whether with's are used. We will get
892       --  the relevant warnings when we compile the parent. This is the
893       --  normal style of GNAT compilation in any case.
894
895       if Nkind (Unit (Cunit (Main_Unit))) = N_Subunit then
896          return;
897       end if;
898
899       --  Process specified units
900
901       if Spec_Unit = No_Unit then
902
903          --  For main call, check all units
904
905          for Unit in Main_Unit .. Last_Unit loop
906             Check_One_Unit (Unit);
907          end loop;
908
909       else
910          --  For call for spec, check only the spec
911
912          Check_One_Unit (Spec_Unit);
913       end if;
914    end Check_Unused_Withs;
915
916    -------------------------------------
917    -- Operand_Has_Warnings_Suppressed --
918    -------------------------------------
919
920    function Operand_Has_Warnings_Suppressed (N : Node_Id) return Boolean is
921
922       function Check_For_Warnings (N : Node_Id) return Traverse_Result;
923       --  Function used to check one node to see if it is or was originally
924       --  a reference to an entity for which Warnings are off. If so, Abandon
925       --  is returned, otherwise OK_Orig is returned to continue the traversal
926       --  of the original expression.
927
928       function Traverse is new Traverse_Func (Check_For_Warnings);
929       --  Function used to traverse tree looking for warnings
930
931       ------------------------
932       -- Check_For_Warnings --
933       ------------------------
934
935       function Check_For_Warnings (N : Node_Id) return Traverse_Result is
936          R : constant Node_Id := Original_Node (N);
937
938       begin
939          if Nkind (R) in N_Has_Entity
940            and then Present (Entity (R))
941            and then Warnings_Off (Entity (R))
942          then
943             return Abandon;
944          else
945             return OK_Orig;
946          end if;
947       end Check_For_Warnings;
948
949    --  Start of processing for Operand_Has_Warnings_Suppressed
950
951    begin
952       return Traverse (N) = Abandon;
953
954    --  If any exception occurs, then something has gone wrong, and this is
955    --  only a minor aesthetic issue anyway, so just say we did not find what
956    --  we are looking for, rather than blow up.
957
958    exception
959       when others =>
960          return False;
961    end Operand_Has_Warnings_Suppressed;
962
963    ----------------------------------
964    -- Output_Unreferenced_Messages --
965    ----------------------------------
966
967    procedure Output_Unreferenced_Messages is
968       E : Entity_Id;
969
970    begin
971       for J in Unreferenced_Entities.First ..
972                Unreferenced_Entities.Last
973       loop
974          E := Unreferenced_Entities.Table (J);
975
976          if not Referenced (E) and then not Warnings_Off (E) then
977
978             case Ekind (E) is
979                when E_Variable =>
980                   if Present (Renamed_Object (E))
981                     and then Comes_From_Source (Renamed_Object (E))
982                   then
983                      Error_Msg_N ("renamed variable & is not referenced?", E);
984                   else
985                      Error_Msg_N ("variable & is not referenced?", E);
986                   end if;
987
988                when E_Constant =>
989                   if Present (Renamed_Object (E))
990                     and then Comes_From_Source (Renamed_Object (E))
991                   then
992                      Error_Msg_N ("renamed constant & is not referenced?", E);
993                   else
994                      Error_Msg_N ("constant & is not referenced?", E);
995                   end if;
996
997                when E_In_Parameter     |
998                     E_Out_Parameter    |
999                     E_In_Out_Parameter =>
1000
1001                   --  Do not emit message for formals of a renaming, because
1002                   --  they are never referenced explicitly.
1003
1004                   if Nkind (Original_Node (Unit_Declaration_Node (Scope (E))))
1005                     /= N_Subprogram_Renaming_Declaration
1006                   then
1007                      Error_Msg_N ("formal parameter & is not referenced?", E);
1008                   end if;
1009
1010                when E_Named_Integer    |
1011                     E_Named_Real       =>
1012                   Error_Msg_N ("named number & is not referenced?", E);
1013
1014                when E_Enumeration_Literal =>
1015                   Error_Msg_N ("literal & is not referenced?", E);
1016
1017                when E_Function         =>
1018                   Error_Msg_N ("function & is not referenced?", E);
1019
1020                when E_Procedure         =>
1021                   Error_Msg_N ("procedure & is not referenced?", E);
1022
1023                when Type_Kind          =>
1024                   Error_Msg_N ("type & is not referenced?", E);
1025
1026                when others =>
1027                   Error_Msg_N ("& is not referenced?", E);
1028             end case;
1029
1030             Set_Warnings_Off (E);
1031          end if;
1032       end loop;
1033    end Output_Unreferenced_Messages;
1034
1035    -----------------------------
1036    -- Warn_On_Known_Condition --
1037    -----------------------------
1038
1039    procedure Warn_On_Known_Condition (C : Node_Id) is
1040       P : Node_Id;
1041
1042    begin
1043       if Constant_Condition_Warnings
1044         and then Nkind (C) = N_Identifier
1045         and then
1046           (Entity (C) = Standard_False or else Entity (C) = Standard_True)
1047         and then Comes_From_Source (Original_Node (C))
1048         and then not In_Instance
1049       then
1050          --  See if this is in a statement or a declaration
1051
1052          P := Parent (C);
1053          loop
1054             --  If tree is not attached, do not issue warning (this is very
1055             --  peculiar, and probably arises from some other error condition)
1056
1057             if No (P) then
1058                return;
1059
1060             --  If we are in a declaration, then no warning, since in practice
1061             --  conditionals in declarations are used for intended tests which
1062             --  may be known at compile time, e.g. things like
1063
1064             --    x : constant Integer := 2 + (Word'Size = 32);
1065
1066             --  And a warning is annoying in such cases
1067
1068             elsif Nkind (P) in N_Declaration
1069                     or else
1070                   Nkind (P) in N_Later_Decl_Item
1071             then
1072                return;
1073
1074             --  Don't warn in assert pragma, since presumably tests in such
1075             --  a context are very definitely intended, and might well be
1076             --  known at compile time. Note that we have to test the original
1077             --  node, since assert pragmas get rewritten at analysis time.
1078
1079             elsif Nkind (Original_Node (P)) = N_Pragma
1080               and then Chars (Original_Node (P)) = Name_Assert
1081             then
1082                return;
1083             end if;
1084
1085             exit when Is_Statement (P);
1086             P := Parent (P);
1087          end loop;
1088
1089          --  Here we issue the warning unless some sub-operand has warnings
1090          --  set off, in which case we suppress the warning for the node.
1091
1092          if not Operand_Has_Warnings_Suppressed (C) then
1093             if Entity (C) = Standard_True then
1094                Error_Msg_N ("condition is always True?", C);
1095             else
1096                Error_Msg_N ("condition is always False?", C);
1097             end if;
1098          end if;
1099       end if;
1100    end Warn_On_Known_Condition;
1101
1102 end Sem_Warn;