]> gcc.gnu.org Git - gcc.git/blob - gcc/ada/sem_warn.adb
sem_warn.ads, [...] (Publicly_Referenceable): Generic formals of a generic subprogram...
[gcc.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-2005, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
26
27 with Alloc;
28 with Atree; use Atree;
29 with 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_Ch8; use Sem_Ch8;
37 with Sem_Util; use Sem_Util;
38 with Sinfo; use Sinfo;
39 with Sinput; use Sinput;
40 with Snames; use Snames;
41 with Stand; use Stand;
42 with Table;
43
44 package body Sem_Warn is
45
46 -- The following table collects Id's of entities that are potentially
47 -- unreferenced. See Check_Unset_Reference for further details.
48
49 package Unreferenced_Entities is new Table.Table (
50 Table_Component_Type => Entity_Id,
51 Table_Index_Type => Nat,
52 Table_Low_Bound => 1,
53 Table_Initial => Alloc.Unreferenced_Entities_Initial,
54 Table_Increment => Alloc.Unreferenced_Entities_Increment,
55 Table_Name => "Unreferenced_Entities");
56
57 ------------------------------
58 -- Handling of Conditionals --
59 ------------------------------
60
61 -- Note: this is work in progress, the data structures and general
62 -- approach are defined, but are not in use yet. ???
63
64 -- One entry is made in the following table for each branch of
65 -- a conditional, e.g. an if-then-elsif-else-endif structure
66 -- creates three entries in this table.
67
68 type Branch_Entry is record
69 Sloc : Source_Ptr;
70 -- Location for warnings associated with this branch
71
72 Defs : Elist_Id;
73 -- List of entities defined for the first time in this branch. On
74 -- exit from a conditional structure, any entity that is in the
75 -- list of all branches is removed (and the entity flagged as
76 -- defined by the conditional as a whole). Thus after processing
77 -- a conditional, Defs contains a list of entities defined in this
78 -- branch for the first time, but not defined at all in some other
79 -- branch of the same conditional. A value of No_Elist is used to
80 -- represent the initial empty list.
81
82 Next : Nat;
83 -- Index of next branch for this conditional, zero = last branch
84 end record;
85
86 package Branch_Table is new Table.Table (
87 Table_Component_Type => Branch_Entry,
88 Table_Index_Type => Nat,
89 Table_Low_Bound => 1,
90 Table_Initial => Alloc.Branches_Initial,
91 Table_Increment => Alloc.Branches_Increment,
92 Table_Name => "Branches");
93
94 -- The following table is used to represent conditionals, there is
95 -- one entry in this table for each conditional structure.
96
97 type Conditional_Entry is record
98 If_Stmt : Boolean;
99 -- True for IF statement, False for CASE statement
100
101 First_Branch : Nat;
102 -- Index in Branch table of first branch, zero = none yet
103
104 Current_Branch : Nat;
105 -- Index in Branch table of current branch, zero = none yet
106 end record;
107
108 package Conditional_Table is new Table.Table (
109 Table_Component_Type => Conditional_Entry,
110 Table_Index_Type => Nat,
111 Table_Low_Bound => 1,
112 Table_Initial => Alloc.Conditionals_Initial,
113 Table_Increment => Alloc.Conditionals_Increment,
114 Table_Name => "Conditionals");
115
116 -- The following table is a stack that keeps track of the current
117 -- conditional. The Last entry is the top of the stack. An Empty
118 -- entry represents the start of a compilation unit. Non-zero
119 -- entries in the stack are indexes into the conditional table.
120
121 package Conditional_Stack is new Table.Table (
122 Table_Component_Type => Nat,
123 Table_Index_Type => Nat,
124 Table_Low_Bound => 1,
125 Table_Initial => Alloc.Conditional_Stack_Initial,
126 Table_Increment => Alloc.Conditional_Stack_Increment,
127 Table_Name => "Conditional_Stack");
128
129 pragma Warnings (Off, Branch_Table);
130 pragma Warnings (Off, Conditional_Table);
131 pragma Warnings (Off, Conditional_Stack);
132 -- Not yet referenced, see note above ???
133
134 -----------------------
135 -- Local Subprograms --
136 -----------------------
137
138 function Generic_Package_Spec_Entity (E : Entity_Id) return Boolean;
139 -- This returns true if the entity E is declared within a generic package.
140 -- The point of this is to detect variables which are not assigned within
141 -- the generic, but might be assigned outside the package for any given
142 -- instance. These are cases where we leave the warnings to be posted
143 -- for the instance, when we will know more.
144
145 function Operand_Has_Warnings_Suppressed (N : Node_Id) return Boolean;
146 -- This function traverses the expression tree represented by the node
147 -- N and determines if any sub-operand is a reference to an entity for
148 -- which the Warnings_Off flag is set. True is returned if such an
149 -- entity is encountered, and False otherwise.
150
151 ----------------------
152 -- Check_References --
153 ----------------------
154
155 procedure Check_References (E : Entity_Id; Anod : Node_Id := Empty) is
156 E1 : Entity_Id;
157 UR : Node_Id;
158
159 function Missing_Subunits return Boolean;
160 -- We suppress warnings when there are missing subunits, because this
161 -- may generate too many false positives: entities in a parent may
162 -- only be referenced in one of the subunits. We make an exception
163 -- for subunits that contain no other stubs.
164
165 procedure Output_Reference_Error (M : String);
166 -- Used to output an error message. Deals with posting the error on
167 -- the body formal in the accept case.
168
169 function Publicly_Referenceable (Ent : Entity_Id) return Boolean;
170 -- This is true if the entity in question is potentially referenceable
171 -- from another unit. This is true for entities in packages that are
172 -- at the library level.
173
174 ----------------------
175 -- Missing_Subunits --
176 ----------------------
177
178 function Missing_Subunits return Boolean is
179 D : Node_Id;
180
181 begin
182 if not Unloaded_Subunits then
183
184 -- Normal compilation, all subunits are present
185
186 return False;
187
188 elsif E /= Main_Unit_Entity then
189
190 -- No warnings on a stub that is not the main unit
191
192 return True;
193
194 elsif Nkind (Unit_Declaration_Node (E)) in N_Proper_Body then
195 D := First (Declarations (Unit_Declaration_Node (E)));
196
197 while Present (D) loop
198
199 -- No warnings if the proper body contains nested stubs
200
201 if Nkind (D) in N_Body_Stub then
202 return True;
203 end if;
204
205 Next (D);
206 end loop;
207
208 return False;
209
210 else
211 -- Missing stubs elsewhere
212
213 return True;
214 end if;
215 end Missing_Subunits;
216
217 ----------------------------
218 -- Output_Reference_Error --
219 ----------------------------
220
221 procedure Output_Reference_Error (M : String) is
222 begin
223 -- Other than accept case, post error on defining identifier
224
225 if No (Anod) then
226 Error_Msg_N (M, E1);
227
228 -- Accept case, find body formal to post the message
229
230 else
231 declare
232 Parm : Node_Id;
233 Enod : Node_Id;
234 Defid : Entity_Id;
235
236 begin
237 Enod := Anod;
238
239 if Present (Parameter_Specifications (Anod)) then
240 Parm := First (Parameter_Specifications (Anod));
241
242 while Present (Parm) loop
243 Defid := Defining_Identifier (Parm);
244
245 if Chars (E1) = Chars (Defid) then
246 Enod := Defid;
247 exit;
248 end if;
249
250 Next (Parm);
251 end loop;
252 end if;
253
254 Error_Msg_NE (M, Enod, E1);
255 end;
256 end if;
257 end Output_Reference_Error;
258
259 ----------------------------
260 -- Publicly_Referenceable --
261 ----------------------------
262
263 function Publicly_Referenceable (Ent : Entity_Id) return Boolean is
264 P : Node_Id;
265 Prev : Node_Id;
266
267 begin
268 -- Examine parents to look for a library level package spec
269 -- But if we find a body or block or other similar construct
270 -- along the way, we cannot be referenced.
271
272 Prev := Ent;
273 P := Parent (Ent);
274 loop
275 case Nkind (P) is
276
277 -- If we get to top of tree, then publicly referenceable
278
279 when N_Empty =>
280 return True;
281
282 -- If we reach a generic package declaration, then always
283 -- consider this referenceable, since any instantiation will
284 -- have access to the entities in the generic package. Note
285 -- that the package itself may not be instantiated, but then
286 -- we will get a warning for the package entity
287 -- Note that generic formal parameters are themselves not
288 -- publicly referenceable in an instance, and warnings on
289 -- them are useful.
290
291 when N_Generic_Package_Declaration =>
292 return
293 not Is_List_Member (Prev)
294 or else List_Containing (Prev)
295 /= Generic_Formal_Declarations (P);
296
297 -- Similarly, the generic formals of a generic subprogram
298 -- are not accessible.
299
300 when N_Generic_Subprogram_Declaration =>
301 if Is_List_Member (Prev)
302 and then List_Containing (Prev) =
303 Generic_Formal_Declarations (P)
304 then
305 return False;
306 else
307 P := Parent (P);
308 end if;
309
310 -- If we reach a subprogram body, entity is not referenceable
311 -- unless it is the defining entity of the body. This will
312 -- happen, e.g. when a function is an attribute renaming that
313 -- is rewritten as a body.
314
315 when N_Subprogram_Body =>
316 if Ent /= Defining_Entity (P) then
317 return False;
318 else
319 P := Parent (P);
320 end if;
321
322 -- If we reach any other body, definitely not referenceable
323
324 when N_Package_Body |
325 N_Task_Body |
326 N_Entry_Body |
327 N_Protected_Body |
328 N_Block_Statement |
329 N_Subunit =>
330 return False;
331
332 -- For all other cases, keep looking up tree
333
334 when others =>
335 Prev := P;
336 P := Parent (P);
337 end case;
338 end loop;
339 end Publicly_Referenceable;
340
341 -- Start of processing for Check_References
342
343 begin
344 -- No messages if warnings are suppressed, or if we have detected
345 -- any real errors so far (this last check avoids junk messages
346 -- resulting from errors, e.g. a subunit that is not loaded).
347
348 if Warning_Mode = Suppress
349 or else Serious_Errors_Detected /= 0
350 then
351 return;
352 end if;
353
354 -- We also skip the messages if any subunits were not loaded (see
355 -- comment in Sem_Ch10 to understand how this is set, and why it is
356 -- necessary to suppress the warnings in this case).
357
358 if Missing_Subunits then
359 return;
360 end if;
361
362 -- Otherwise loop through entities, looking for suspicious stuff
363
364 E1 := First_Entity (E);
365 while Present (E1) loop
366
367 -- We only look at source entities with warning flag on
368
369 if Comes_From_Source (E1) and then not Warnings_Off (E1) then
370
371 -- We are interested in variables and out parameters, but we
372 -- exclude protected types, too complicated to worry about.
373
374 if Ekind (E1) = E_Variable
375 or else
376 (Ekind (E1) = E_Out_Parameter
377 and then not Is_Protected_Type (Current_Scope))
378 then
379 -- Post warning if this object not assigned. Note that we
380 -- do not consider the implicit initialization of an access
381 -- type to be the assignment of a value for this purpose.
382
383 if Ekind (E1) = E_Out_Parameter
384 and then Present (Spec_Entity (E1))
385 then
386 UR := Unset_Reference (Spec_Entity (E1));
387 else
388 UR := Unset_Reference (E1);
389 end if;
390
391 -- If the entity is an out parameter of the current subprogram
392 -- body, check the warning status of the parameter in the spec.
393
394 if Ekind (E1) = E_Out_Parameter
395 and then Present (Spec_Entity (E1))
396 and then Warnings_Off (Spec_Entity (E1))
397 then
398 null;
399
400 elsif Present (UR)
401 and then Is_Access_Type (Etype (E1))
402 then
403
404 -- For access types, the only time we made a UR
405 -- entry was for a dereference, and so we post
406 -- the appropriate warning here (note that the
407 -- dereference may not be explicit in the source,
408 -- for example in the case of a dispatching call
409 -- with an anonymous access controlling formal, or
410 -- of an assignment of a pointer involving a
411 -- discriminant check on the designated object).
412
413 Error_Msg_NE ("& may be null?", UR, E1);
414 goto Continue;
415
416 elsif Never_Set_In_Source (E1)
417 and then not Generic_Package_Spec_Entity (E1)
418 then
419 if Warn_On_No_Value_Assigned then
420
421 -- Do not output complaint about never being assigned a
422 -- value if a pragma Unreferenced applies to the variable
423 -- or if it is a parameter, to the corresponding spec.
424
425 if Has_Pragma_Unreferenced (E1)
426 or else (Is_Formal (E1)
427 and then Present (Spec_Entity (E1))
428 and then
429 Has_Pragma_Unreferenced (Spec_Entity (E1)))
430 then
431 null;
432
433 -- Pragma Unreferenced not set, so output message
434
435 else
436 Output_Reference_Error
437 ("& is never assigned a value?");
438
439 -- Deal with special case where this variable is
440 -- hidden by a loop variable
441
442 if Ekind (E1) = E_Variable
443 and then Present (Hiding_Loop_Variable (E1))
444 then
445 Error_Msg_Sloc := Sloc (E1);
446 Error_Msg_N
447 ("declaration hides &#?",
448 Hiding_Loop_Variable (E1));
449 Error_Msg_N
450 ("for loop implicitly declares loop variable?",
451 Hiding_Loop_Variable (E1));
452 end if;
453 end if;
454 end if;
455 goto Continue;
456
457 -- Case of variable that could be a constant. Note that we
458 -- never signal such messages for generic package entities,
459 -- since a given instance could have modifications outside
460 -- the package.
461
462 elsif Warn_On_Constant
463 and then Ekind (E1) = E_Variable
464 and then Is_True_Constant (E1)
465 and then not Generic_Package_Spec_Entity (E1)
466 then
467 -- A special case, if this variable is volatile and not
468 -- imported, it is not helpful to tell the programmer
469 -- to mark the variable as constant, since this would be
470 -- illegal by virtue of RM C.6(13).
471
472 if (Is_Volatile (E1) or else Has_Volatile_Components (E1))
473 and then not Is_Imported (E1)
474 then
475 Error_Msg_N
476 ("& is not modified, volatile has no effect?", E1);
477 else
478 Error_Msg_N
479 ("& is not modified, could be declared constant?", E1);
480 end if;
481 end if;
482
483 -- Check for unset reference, note that we exclude access
484 -- types from this check, since access types do always have
485 -- a null value, and that seems legitimate in this case.
486
487 if Warn_On_No_Value_Assigned and then Present (UR) then
488
489 -- For other than access type, go back to original node
490 -- to deal with case where original unset reference
491 -- has been rewritten during expansion.
492
493 UR := Original_Node (UR);
494
495 -- In some cases, the original node may be a type
496 -- conversion or qualification, and in this case
497 -- we want the object entity inside.
498
499 while Nkind (UR) = N_Type_Conversion
500 or else Nkind (UR) = N_Qualified_Expression
501 loop
502 UR := Expression (UR);
503 end loop;
504
505 -- Here we issue the warning, all checks completed
506 -- If the unset reference is prefix of a selected
507 -- component that comes from source, mention the
508 -- component as well. If the selected component comes
509 -- from expansion, all we know is that the entity is
510 -- not fully initialized at the point of the reference.
511 -- Locate an unintialized component to get a better
512 -- error message.
513
514 if Nkind (Parent (UR)) = N_Selected_Component then
515 Error_Msg_Node_2 := Selector_Name (Parent (UR));
516
517 if not Comes_From_Source (Parent (UR)) then
518 declare
519 Comp : Entity_Id;
520
521 begin
522 Comp := First_Entity (Etype (E1));
523 while Present (Comp) loop
524 if Ekind (Comp) = E_Component
525 and then Nkind (Parent (Comp)) =
526 N_Component_Declaration
527 and then No (Expression (Parent (Comp)))
528 then
529 Error_Msg_Node_2 := Comp;
530 exit;
531 end if;
532
533 Next_Entity (Comp);
534 end loop;
535 end;
536 end if;
537
538 Error_Msg_N
539 ("`&.&` may be referenced before it has a value?",
540 UR);
541 else
542 Error_Msg_N
543 ("& may be referenced before it has a value?",
544 UR);
545 end if;
546
547 goto Continue;
548 end if;
549 end if;
550
551 -- Then check for unreferenced entities. Note that we are only
552 -- interested in entities which do not have the Referenced flag
553 -- set. The Referenced_As_LHS flag is interesting only if the
554 -- Referenced flag is not set.
555
556 if not Referenced (E1)
557
558 -- Check that warnings on unreferenced entities are enabled
559
560 and then ((Check_Unreferenced and then not Is_Formal (E1))
561 or else
562 (Check_Unreferenced_Formals and then Is_Formal (E1))
563 or else
564 (Warn_On_Modified_Unread
565 and then Referenced_As_LHS (E1)))
566
567 -- Labels, and enumeration literals, and exceptions. The
568 -- warnings are also placed on local packages that cannot
569 -- be referenced from elsewhere, including those declared
570 -- within a package body.
571
572 and then (Is_Object (E1)
573 or else
574 Is_Type (E1)
575 or else
576 Ekind (E1) = E_Label
577 or else
578 Ekind (E1) = E_Exception
579 or else
580 Ekind (E1) = E_Named_Integer
581 or else
582 Ekind (E1) = E_Named_Real
583 or else
584 Is_Overloadable (E1)
585 or else
586 (Ekind (E1) = E_Package
587 and then
588 (Ekind (E) = E_Function
589 or else Ekind (E) = E_Package_Body
590 or else Ekind (E) = E_Procedure
591 or else Ekind (E) = E_Subprogram_Body
592 or else Ekind (E) = E_Block)))
593
594 -- Exclude instantiations, since there is no reason why
595 -- every entity in an instantiation should be referenced.
596
597 and then Instantiation_Location (Sloc (E1)) = No_Location
598
599 -- Exclude formal parameters from bodies if the corresponding
600 -- spec entity has been referenced in the case where there is
601 -- a separate spec.
602
603 and then not (Is_Formal (E1)
604 and then
605 Ekind (Scope (E1)) = E_Subprogram_Body
606 and then
607 Present (Spec_Entity (E1))
608 and then
609 Referenced (Spec_Entity (E1)))
610
611 -- Consider private type referenced if full view is referenced
612 -- If there is not full view, this is a generic type on which
613 -- warnings are also useful.
614
615 and then
616 not (Is_Private_Type (E1)
617 and then
618 Present (Full_View (E1))
619 and then Referenced (Full_View (E1)))
620
621 -- Don't worry about full view, only about private type
622
623 and then not Has_Private_Declaration (E1)
624
625 -- Eliminate dispatching operations from consideration, we
626 -- cannot tell if these are referenced or not in any easy
627 -- manner (note this also catches Adjust/Finalize/Initialize)
628
629 and then not Is_Dispatching_Operation (E1)
630
631 -- Check entity that can be publicly referenced (we do not
632 -- give messages for such entities, since there could be
633 -- other units, not involved in this compilation, that
634 -- contain relevant references.
635
636 and then not Publicly_Referenceable (E1)
637
638 -- Class wide types are marked as source entities, but
639 -- they are not really source entities, and are always
640 -- created, so we do not care if they are not referenced.
641
642 and then Ekind (E1) /= E_Class_Wide_Type
643
644 -- Objects other than parameters of task types are allowed
645 -- to be non-referenced, since they start up tasks!
646
647 and then ((Ekind (E1) /= E_Variable
648 and then Ekind (E1) /= E_Constant
649 and then Ekind (E1) /= E_Component)
650 or else not Is_Task_Type (Etype (E1)))
651
652 -- For subunits, only place warnings on the main unit
653 -- itself, since parent units are not completely compiled
654
655 and then (Nkind (Unit (Cunit (Main_Unit))) /= N_Subunit
656 or else
657 Get_Source_Unit (E1) = Main_Unit)
658 then
659 -- Suppress warnings in internal units if not in -gnatg
660 -- mode (these would be junk warnings for an applications
661 -- program, since they refer to problems in internal units)
662
663 if GNAT_Mode
664 or else not
665 Is_Internal_File_Name
666 (Unit_File_Name (Get_Source_Unit (E1)))
667 then
668 -- We do not immediately flag the error. This is because
669 -- we have not expanded generic bodies yet, and they may
670 -- have the missing reference. So instead we park the
671 -- entity on a list, for later processing. However, for
672 -- the accept case, post the error right here, since we
673 -- have the information now in this case.
674
675 if Present (Anod) then
676 Output_Reference_Error ("& is not referenced?");
677
678 else
679 Unreferenced_Entities.Increment_Last;
680 Unreferenced_Entities.Table
681 (Unreferenced_Entities.Last) := E1;
682 end if;
683 end if;
684
685 -- Generic units are referenced in the generic body,
686 -- but if they are not public and never instantiated
687 -- we want to force a warning on them. We treat them
688 -- as redundant constructs to minimize noise.
689
690 elsif Is_Generic_Subprogram (E1)
691 and then not Is_Instantiated (E1)
692 and then not Publicly_Referenceable (E1)
693 and then Instantiation_Depth (Sloc (E1)) = 0
694 and then Warn_On_Redundant_Constructs
695 then
696 Unreferenced_Entities.Increment_Last;
697 Unreferenced_Entities.Table (Unreferenced_Entities.Last) := E1;
698
699 -- Force warning on entity
700
701 Set_Referenced (E1, False);
702 end if;
703 end if;
704
705 -- Recurse into nested package or block. Do not recurse into a
706 -- formal package, because the correponding body is not analyzed.
707
708 <<Continue>>
709 if ((Ekind (E1) = E_Package or else Ekind (E1) = E_Generic_Package)
710 and then Nkind (Parent (E1)) = N_Package_Specification
711 and then
712 Nkind (Original_Node (Unit_Declaration_Node (E1)))
713 /= N_Formal_Package_Declaration)
714
715 or else Ekind (E1) = E_Block
716 then
717 Check_References (E1);
718 end if;
719
720 Next_Entity (E1);
721 end loop;
722 end Check_References;
723
724 ---------------------------
725 -- Check_Unset_Reference --
726 ---------------------------
727
728 procedure Check_Unset_Reference (N : Node_Id) is
729 begin
730 -- Nothing to do if warnings suppressed
731
732 if Warning_Mode = Suppress then
733 return;
734 end if;
735
736 -- Ignore reference to non-scalar if not from source. Almost always
737 -- such references are bogus (e.g. calls to init procs to set
738 -- default discriminant values).
739
740 if not Comes_From_Source (N)
741 and then not Is_Scalar_Type (Etype (N))
742 then
743 return;
744 end if;
745
746 -- Otherwise see what kind of node we have. If the entity already
747 -- has an unset reference, it is not necessarily the earliest in
748 -- the text, because resolution of the prefix of selected components
749 -- is completed before the resolution of the selected component itself.
750 -- as a result, given (R /= null and then R.X > 0), the occurrences
751 -- of R are examined in right-to-left order. If there is already an
752 -- unset reference, we check whether N is earlier before proceeding.
753
754 case Nkind (N) is
755 when N_Identifier | N_Expanded_Name =>
756 declare
757 E : constant Entity_Id := Entity (N);
758
759 begin
760 if (Ekind (E) = E_Variable
761 or else Ekind (E) = E_Out_Parameter)
762 and then Never_Set_In_Source (E)
763 and then (No (Unset_Reference (E))
764 or else Earlier_In_Extended_Unit
765 (Sloc (N), Sloc (Unset_Reference (E))))
766 and then not Warnings_Off (E)
767 then
768 -- We may have an unset reference. The first test is
769 -- whether we are accessing a discriminant of a record
770 -- or a component with default initialization. Both of
771 -- these cases can be ignored, since the actual object
772 -- that is referenced is definitely initialized. Note
773 -- that this covers the case of reading discriminants
774 -- of an out parameter, which is OK even in Ada 83.
775
776 -- Note that we are only interested in a direct reference
777 -- to a record component here. If the reference is via an
778 -- access type, then the access object is being referenced,
779 -- not the record, and still deserves an unset reference.
780
781 if Nkind (Parent (N)) = N_Selected_Component
782 and not Is_Access_Type (Etype (N))
783 then
784 declare
785 ES : constant Entity_Id :=
786 Entity (Selector_Name (Parent (N)));
787
788 begin
789 if Ekind (ES) = E_Discriminant
790 or else Present (Expression (Declaration_Node (ES)))
791 then
792 return;
793 end if;
794 end;
795 end if;
796
797 -- Here we have a potential unset reference. But before we
798 -- get worried about it, we have to make sure that the
799 -- entity declaration is in the same procedure as the
800 -- reference, since if they are in separate procedures,
801 -- then we have no idea about sequential execution.
802
803 -- The tests in the loop below catch all such cases, but
804 -- do allow the reference to appear in a loop, block, or
805 -- package spec that is nested within the declaring scope.
806 -- As always, it is possible to construct cases where the
807 -- warning is wrong, that is why it is a warning!
808
809 declare
810 SR : Entity_Id;
811 SE : constant Entity_Id := Scope (E);
812
813 begin
814 SR := Current_Scope;
815 while SR /= SE loop
816 if SR = Standard_Standard
817 or else Is_Subprogram (SR)
818 or else Is_Concurrent_Body (SR)
819 or else Is_Concurrent_Type (SR)
820 then
821 return;
822 end if;
823
824 SR := Scope (SR);
825 end loop;
826
827 -- Case of reference has an access type. This is a
828 -- special case since access types are always set to
829 -- null so cannot be truly uninitialized, but we still
830 -- want to warn about cases of obvious null dereference.
831
832 if Is_Access_Type (Etype (N)) then
833 declare
834 P : Node_Id;
835
836 function Process
837 (N : Node_Id)
838 return Traverse_Result;
839 -- Process function for instantation of Traverse
840 -- below. Checks if N contains reference to E
841 -- other than a dereference.
842
843 function Ref_In (Nod : Node_Id) return Boolean;
844 -- Determines whether Nod contains a reference
845 -- to the entity E that is not a dereference.
846
847 function Process
848 (N : Node_Id)
849 return Traverse_Result
850 is
851 begin
852 if Is_Entity_Name (N)
853 and then Entity (N) = E
854 and then not Is_Dereferenced (N)
855 then
856 return Abandon;
857 else
858 return OK;
859 end if;
860 end Process;
861
862 function Ref_In (Nod : Node_Id) return Boolean is
863 function Traverse is new Traverse_Func (Process);
864
865 begin
866 return Traverse (Nod) = Abandon;
867 end Ref_In;
868
869 begin
870 -- Don't bother if we are inside an instance,
871 -- since the compilation of the generic template
872 -- is where the warning should be issued.
873
874 if In_Instance then
875 return;
876 end if;
877
878 -- Don't bother if this is not the main unit.
879 -- If we try to give this warning for with'ed
880 -- units, we get some false positives, since
881 -- we do not record references in other units.
882
883 if not In_Extended_Main_Source_Unit (E)
884 or else
885 not In_Extended_Main_Source_Unit (N)
886 then
887 return;
888 end if;
889
890 -- We are only interested in deferences
891
892 if not Is_Dereferenced (N) then
893 return;
894 end if;
895
896 -- One more check, don't bother with references
897 -- that are inside conditional statements or while
898 -- loops if the condition references the entity in
899 -- question. This avoids most false positives.
900
901 P := Parent (N);
902 loop
903 P := Parent (P);
904 exit when No (P);
905
906 if (Nkind (P) = N_If_Statement
907 or else
908 Nkind (P) = N_Elsif_Part)
909 and then Ref_In (Condition (P))
910 then
911 return;
912
913 elsif Nkind (P) = N_Loop_Statement
914 and then Present (Iteration_Scheme (P))
915 and then
916 Ref_In (Condition (Iteration_Scheme (P)))
917 then
918 return;
919 end if;
920 end loop;
921 end;
922 end if;
923
924 -- Here we definitely have a case for giving a warning
925 -- for a reference to an unset value. But we don't give
926 -- the warning now. Instead we set the Unset_Reference
927 -- field of the identifier involved. The reason for this
928 -- is that if we find the variable is never ever assigned
929 -- a value then that warning is more important and there
930 -- is no point in giving the reference warning.
931
932 -- If this is an identifier, set the field directly
933
934 if Nkind (N) = N_Identifier then
935 Set_Unset_Reference (E, N);
936
937 -- Otherwise it is an expanded name, so set the field
938 -- of the actual identifier for the reference.
939
940 else
941 Set_Unset_Reference (E, Selector_Name (N));
942 end if;
943 end;
944 end if;
945 end;
946
947 when N_Indexed_Component | N_Slice =>
948 Check_Unset_Reference (Prefix (N));
949
950 when N_Selected_Component =>
951
952 if Present (Entity (Selector_Name (N)))
953 and then Ekind (Entity (Selector_Name (N))) = E_Discriminant
954 then
955 -- A discriminant is always initialized
956
957 null;
958
959 else
960 Check_Unset_Reference (Prefix (N));
961 end if;
962
963 when N_Type_Conversion | N_Qualified_Expression =>
964 Check_Unset_Reference (Expression (N));
965
966 when others =>
967 null;
968
969 end case;
970 end Check_Unset_Reference;
971
972 ------------------------
973 -- Check_Unused_Withs --
974 ------------------------
975
976 procedure Check_Unused_Withs (Spec_Unit : Unit_Number_Type := No_Unit) is
977 Cnode : Node_Id;
978 Item : Node_Id;
979 Lunit : Node_Id;
980 Ent : Entity_Id;
981
982 Munite : constant Entity_Id := Cunit_Entity (Main_Unit);
983 -- This is needed for checking the special renaming case
984
985 procedure Check_One_Unit (Unit : Unit_Number_Type);
986 -- Subsidiary procedure, performs checks for specified unit
987
988 --------------------
989 -- Check_One_Unit --
990 --------------------
991
992 procedure Check_One_Unit (Unit : Unit_Number_Type) is
993 Is_Visible_Renaming : Boolean := False;
994 Pack : Entity_Id;
995
996 procedure Check_Inner_Package (Pack : Entity_Id);
997 -- Pack is a package local to a unit in a with_clause. Both the
998 -- unit and Pack are referenced. If none of the entities in Pack
999 -- are referenced, then the only occurrence of Pack is in a use
1000 -- clause or a pragma, and a warning is worthwhile as well.
1001
1002 function Check_System_Aux return Boolean;
1003 -- Before giving a warning on a with_clause for System, check
1004 -- whether a system extension is present.
1005
1006 function Find_Package_Renaming
1007 (P : Entity_Id;
1008 L : Entity_Id) return Entity_Id;
1009 -- The only reference to a context unit may be in a renaming
1010 -- declaration. If this renaming declares a visible entity, do
1011 -- not warn that the context clause could be moved to the body,
1012 -- because the renaming may be intented to re-export the unit.
1013
1014 -------------------------
1015 -- Check_Inner_Package --
1016 -------------------------
1017
1018 procedure Check_Inner_Package (Pack : Entity_Id) is
1019 E : Entity_Id;
1020 Un : constant Node_Id := Sinfo.Unit (Cnode);
1021
1022 function Check_Use_Clause (N : Node_Id) return Traverse_Result;
1023 -- If N is a use_clause for Pack, emit warning
1024
1025 procedure Check_Use_Clauses is new
1026 Traverse_Proc (Check_Use_Clause);
1027
1028 ----------------------
1029 -- Check_Use_Clause --
1030 ----------------------
1031
1032 function Check_Use_Clause (N : Node_Id) return Traverse_Result is
1033 Nam : Node_Id;
1034
1035 begin
1036 if Nkind (N) = N_Use_Package_Clause then
1037 Nam := First (Names (N));
1038
1039 while Present (Nam) loop
1040 if Entity (Nam) = Pack then
1041 Error_Msg_Qual_Level := 1;
1042 Error_Msg_NE
1043 ("no entities of package& are referenced?",
1044 Nam, Pack);
1045 Error_Msg_Qual_Level := 0;
1046 end if;
1047
1048 Next (Nam);
1049 end loop;
1050 end if;
1051
1052 return OK;
1053 end Check_Use_Clause;
1054
1055 -- Start of processing for Check_Inner_Package
1056
1057 begin
1058 E := First_Entity (Pack);
1059
1060 while Present (E) loop
1061 if Referenced (E) then
1062 return;
1063 end if;
1064
1065 Next_Entity (E);
1066 end loop;
1067
1068 -- No entities of the package are referenced. Check whether
1069 -- the reference to the package itself is a use clause, and
1070 -- if so place a warning on it.
1071
1072 Check_Use_Clauses (Un);
1073 end Check_Inner_Package;
1074
1075 ----------------------
1076 -- Check_System_Aux --
1077 ----------------------
1078
1079 function Check_System_Aux return Boolean is
1080 Ent : Entity_Id;
1081
1082 begin
1083 if Chars (Lunit) = Name_System
1084 and then Scope (Lunit) = Standard_Standard
1085 and then Present_System_Aux
1086 then
1087 Ent := First_Entity (System_Aux_Id);
1088
1089 while Present (Ent) loop
1090 if Referenced (Ent) then
1091 return True;
1092 end if;
1093
1094 Next_Entity (Ent);
1095 end loop;
1096 end if;
1097
1098 return False;
1099 end Check_System_Aux;
1100
1101 ---------------------------
1102 -- Find_Package_Renaming --
1103 ---------------------------
1104
1105 function Find_Package_Renaming
1106 (P : Entity_Id;
1107 L : Entity_Id) return Entity_Id
1108 is
1109 E1 : Entity_Id;
1110 R : Entity_Id;
1111
1112 begin
1113 Is_Visible_Renaming := False;
1114 E1 := First_Entity (P);
1115
1116 while Present (E1) loop
1117 if Ekind (E1) = E_Package
1118 and then Renamed_Object (E1) = L
1119 then
1120 Is_Visible_Renaming := not Is_Hidden (E1);
1121 return E1;
1122
1123 elsif Ekind (E1) = E_Package
1124 and then No (Renamed_Object (E1))
1125 and then not Is_Generic_Instance (E1)
1126 then
1127 R := Find_Package_Renaming (E1, L);
1128
1129 if Present (R) then
1130 Is_Visible_Renaming := not Is_Hidden (R);
1131 return R;
1132 end if;
1133 end if;
1134
1135 Next_Entity (E1);
1136 end loop;
1137
1138 return Empty;
1139 end Find_Package_Renaming;
1140
1141 -- Start of processing for Check_One_Unit
1142
1143 begin
1144 Cnode := Cunit (Unit);
1145
1146 -- Only do check in units that are part of the extended main
1147 -- unit. This is actually a necessary restriction, because in
1148 -- the case of subprogram acting as its own specification,
1149 -- there can be with's in subunits that we will not see.
1150
1151 if not In_Extended_Main_Source_Unit (Cnode) then
1152 return;
1153
1154 -- In configurable run time mode, we remove the bodies of
1155 -- non-inlined subprograms, which may lead to spurious warnings,
1156 -- which are clearly undesirable.
1157
1158 elsif Configurable_Run_Time_Mode
1159 and then Is_Predefined_File_Name (Unit_File_Name (Unit))
1160 then
1161 return;
1162 end if;
1163
1164 -- Loop through context items in this unit
1165
1166 Item := First (Context_Items (Cnode));
1167 while Present (Item) loop
1168 if Nkind (Item) = N_With_Clause
1169 and then not Implicit_With (Item)
1170 and then In_Extended_Main_Source_Unit (Item)
1171 then
1172 Lunit := Entity (Name (Item));
1173
1174 -- Check if this unit is referenced
1175
1176 if not Referenced (Lunit) then
1177
1178 -- Suppress warnings in internal units if not in -gnatg
1179 -- mode (these would be junk warnings for an applications
1180 -- program, since they refer to problems in internal units)
1181
1182 if GNAT_Mode
1183 or else not Is_Internal_File_Name (Unit_File_Name (Unit))
1184 then
1185 -- Here we definitely have a non-referenced unit. If
1186 -- it is the special call for a spec unit, then just
1187 -- set the flag to be read later.
1188
1189 if Unit = Spec_Unit then
1190 Set_Unreferenced_In_Spec (Item);
1191
1192 -- Otherwise simple unreferenced message
1193
1194 else
1195 Error_Msg_N
1196 ("unit& is not referenced?", Name (Item));
1197 end if;
1198 end if;
1199
1200 -- If main unit is a renaming of this unit, then we consider
1201 -- the with to be OK (obviously it is needed in this case!)
1202
1203 elsif Present (Renamed_Entity (Munite))
1204 and then Renamed_Entity (Munite) = Lunit
1205 then
1206 null;
1207
1208 -- If this unit is referenced, and it is a package, we
1209 -- do another test, to see if any of the entities in the
1210 -- package are referenced. If none of the entities are
1211 -- referenced, we still post a warning. This occurs if
1212 -- the only use of the package is in a use clause, or
1213 -- in a package renaming declaration.
1214
1215 elsif Ekind (Lunit) = E_Package then
1216
1217 -- If Is_Instantiated is set, it means that the package
1218 -- is implicitly instantiated (this is the case of a
1219 -- parent instance or an actual for a generic package
1220 -- formal), and this counts as a reference.
1221
1222 if Is_Instantiated (Lunit) then
1223 null;
1224
1225 -- If no entities in package, and there is a pragma
1226 -- Elaborate_Body present, then assume that this with
1227 -- is done for purposes of this elaboration.
1228
1229 elsif No (First_Entity (Lunit))
1230 and then Has_Pragma_Elaborate_Body (Lunit)
1231 then
1232 null;
1233
1234 -- Otherwise see if any entities have been referenced
1235
1236 else
1237 Ent := First_Entity (Lunit);
1238 loop
1239 -- No more entities, and we did not find one
1240 -- that was referenced. Means we have a definite
1241 -- case of a with none of whose entities was
1242 -- referenced.
1243
1244 if No (Ent) then
1245
1246 -- If in spec, just set the flag
1247
1248 if Unit = Spec_Unit then
1249 Set_No_Entities_Ref_In_Spec (Item);
1250
1251 elsif Check_System_Aux then
1252 null;
1253
1254 -- Else give the warning
1255
1256 else
1257 Error_Msg_N
1258 ("no entities of & are referenced?",
1259 Name (Item));
1260
1261 -- Look for renamings of this package, and
1262 -- flag them as well. If the original package
1263 -- has warnings off, we suppress the warning
1264 -- on the renaming as well.
1265
1266 Pack := Find_Package_Renaming (Munite, Lunit);
1267
1268 if Present (Pack)
1269 and then not Warnings_Off (Lunit)
1270 then
1271 Error_Msg_NE
1272 ("no entities of & are referenced?",
1273 Unit_Declaration_Node (Pack),
1274 Pack);
1275 end if;
1276 end if;
1277
1278 exit;
1279
1280 -- Case of next entity is referenced
1281
1282 elsif Referenced (Ent)
1283 or else Referenced_As_LHS (Ent)
1284 then
1285 -- This means that the with is indeed fine, in
1286 -- that it is definitely needed somewhere, and
1287 -- we can quite worrying about this one.
1288
1289 -- Except for one little detail, if either of
1290 -- the flags was set during spec processing,
1291 -- this is where we complain that the with
1292 -- could be moved from the spec. If the spec
1293 -- contains a visible renaming of the package,
1294 -- inhibit warning to move with_clause to body.
1295
1296 if Ekind (Munite) = E_Package_Body then
1297 Pack :=
1298 Find_Package_Renaming
1299 (Spec_Entity (Munite), Lunit);
1300 end if;
1301
1302 if Unreferenced_In_Spec (Item) then
1303 Error_Msg_N
1304 ("unit& is not referenced in spec?",
1305 Name (Item));
1306
1307 elsif No_Entities_Ref_In_Spec (Item) then
1308 Error_Msg_N
1309 ("no entities of & are referenced in spec?",
1310 Name (Item));
1311
1312 else
1313 if Ekind (Ent) = E_Package then
1314 Check_Inner_Package (Ent);
1315 end if;
1316
1317 exit;
1318 end if;
1319
1320 if not Is_Visible_Renaming then
1321 Error_Msg_N
1322 ("\with clause might be moved to body?",
1323 Name (Item));
1324 end if;
1325
1326 exit;
1327
1328 -- Move to next entity to continue search
1329
1330 else
1331 Next_Entity (Ent);
1332 end if;
1333 end loop;
1334 end if;
1335
1336 -- For a generic package, the only interesting kind of
1337 -- reference is an instantiation, since entities cannot
1338 -- be referenced directly.
1339
1340 elsif Is_Generic_Unit (Lunit) then
1341
1342 -- Unit was never instantiated, set flag for case of spec
1343 -- call, or give warning for normal call.
1344
1345 if not Is_Instantiated (Lunit) then
1346 if Unit = Spec_Unit then
1347 Set_Unreferenced_In_Spec (Item);
1348 else
1349 Error_Msg_N
1350 ("unit& is never instantiated?", Name (Item));
1351 end if;
1352
1353 -- If unit was indeed instantiated, make sure that
1354 -- flag is not set showing it was uninstantiated in
1355 -- the spec, and if so, give warning.
1356
1357 elsif Unreferenced_In_Spec (Item) then
1358 Error_Msg_N
1359 ("unit& is not instantiated in spec?", Name (Item));
1360 Error_Msg_N
1361 ("\with clause can be moved to body?", Name (Item));
1362 end if;
1363 end if;
1364 end if;
1365
1366 Next (Item);
1367 end loop;
1368
1369 end Check_One_Unit;
1370
1371 -- Start of processing for Check_Unused_Withs
1372
1373 begin
1374 if not Opt.Check_Withs
1375 or else Operating_Mode = Check_Syntax
1376 then
1377 return;
1378 end if;
1379
1380 -- Flag any unused with clauses, but skip this step if we are
1381 -- compiling a subunit on its own, since we do not have enough
1382 -- information to determine whether with's are used. We will get
1383 -- the relevant warnings when we compile the parent. This is the
1384 -- normal style of GNAT compilation in any case.
1385
1386 if Nkind (Unit (Cunit (Main_Unit))) = N_Subunit then
1387 return;
1388 end if;
1389
1390 -- Process specified units
1391
1392 if Spec_Unit = No_Unit then
1393
1394 -- For main call, check all units
1395
1396 for Unit in Main_Unit .. Last_Unit loop
1397 Check_One_Unit (Unit);
1398 end loop;
1399
1400 else
1401 -- For call for spec, check only the spec
1402
1403 Check_One_Unit (Spec_Unit);
1404 end if;
1405 end Check_Unused_Withs;
1406
1407 ---------------------------------
1408 -- Generic_Package_Spec_Entity --
1409 ---------------------------------
1410
1411 function Generic_Package_Spec_Entity (E : Entity_Id) return Boolean is
1412 S : Entity_Id;
1413
1414 begin
1415 if Is_Package_Body_Entity (E) then
1416 return False;
1417
1418 else
1419 S := Scope (E);
1420
1421 loop
1422 if S = Standard_Standard then
1423 return False;
1424
1425 elsif Ekind (S) = E_Generic_Package then
1426 return True;
1427
1428 elsif Ekind (S) = E_Package then
1429 S := Scope (S);
1430
1431 else
1432 return False;
1433 end if;
1434 end loop;
1435 end if;
1436 end Generic_Package_Spec_Entity;
1437
1438 -------------------------------------
1439 -- Operand_Has_Warnings_Suppressed --
1440 -------------------------------------
1441
1442 function Operand_Has_Warnings_Suppressed (N : Node_Id) return Boolean is
1443
1444 function Check_For_Warnings (N : Node_Id) return Traverse_Result;
1445 -- Function used to check one node to see if it is or was originally
1446 -- a reference to an entity for which Warnings are off. If so, Abandon
1447 -- is returned, otherwise OK_Orig is returned to continue the traversal
1448 -- of the original expression.
1449
1450 function Traverse is new Traverse_Func (Check_For_Warnings);
1451 -- Function used to traverse tree looking for warnings
1452
1453 ------------------------
1454 -- Check_For_Warnings --
1455 ------------------------
1456
1457 function Check_For_Warnings (N : Node_Id) return Traverse_Result is
1458 R : constant Node_Id := Original_Node (N);
1459
1460 begin
1461 if Nkind (R) in N_Has_Entity
1462 and then Present (Entity (R))
1463 and then Warnings_Off (Entity (R))
1464 then
1465 return Abandon;
1466 else
1467 return OK_Orig;
1468 end if;
1469 end Check_For_Warnings;
1470
1471 -- Start of processing for Operand_Has_Warnings_Suppressed
1472
1473 begin
1474 return Traverse (N) = Abandon;
1475
1476 -- If any exception occurs, then something has gone wrong, and this is
1477 -- only a minor aesthetic issue anyway, so just say we did not find what
1478 -- we are looking for, rather than blow up.
1479
1480 exception
1481 when others =>
1482 return False;
1483 end Operand_Has_Warnings_Suppressed;
1484
1485 ----------------------------------
1486 -- Output_Unreferenced_Messages --
1487 ----------------------------------
1488
1489 procedure Output_Unreferenced_Messages is
1490 E : Entity_Id;
1491
1492 begin
1493 for J in Unreferenced_Entities.First ..
1494 Unreferenced_Entities.Last
1495 loop
1496 E := Unreferenced_Entities.Table (J);
1497
1498 if not Referenced (E) and then not Warnings_Off (E) then
1499 case Ekind (E) is
1500 when E_Variable =>
1501
1502 -- Case of variable that is assigned but not read. We
1503 -- suppress the message if the variable is volatile,
1504 -- has an address clause, or is imported.
1505
1506 if Referenced_As_LHS (E)
1507 and then No (Address_Clause (E))
1508 and then not Is_Volatile (E)
1509 then
1510 if Warn_On_Modified_Unread
1511 and then not Is_Imported (E)
1512
1513 -- Suppress the message for aliased or renamed
1514 -- variables, since there may be other entities
1515 -- read the same memory location.
1516
1517 and then not Is_Aliased (E)
1518 and then No (Renamed_Object (E))
1519
1520 then
1521 Error_Msg_N
1522 ("variable & is assigned but never read?", E);
1523 end if;
1524
1525 -- Normal case of neither assigned nor read
1526
1527 else
1528 if Present (Renamed_Object (E))
1529 and then Comes_From_Source (Renamed_Object (E))
1530 then
1531 Error_Msg_N
1532 ("renamed variable & is not referenced?", E);
1533 else
1534 Error_Msg_N
1535 ("variable & is not referenced?", E);
1536 end if;
1537 end if;
1538
1539 when E_Constant =>
1540 if Present (Renamed_Object (E))
1541 and then Comes_From_Source (Renamed_Object (E))
1542 then
1543 Error_Msg_N ("renamed constant & is not referenced?", E);
1544 else
1545 Error_Msg_N ("constant & is not referenced?", E);
1546 end if;
1547
1548 when E_In_Parameter |
1549 E_Out_Parameter |
1550 E_In_Out_Parameter =>
1551
1552 -- Do not emit message for formals of a renaming, because
1553 -- they are never referenced explicitly.
1554
1555 if Nkind (Original_Node (Unit_Declaration_Node (Scope (E))))
1556 /= N_Subprogram_Renaming_Declaration
1557 then
1558 Error_Msg_N ("formal parameter & is not referenced?", E);
1559 end if;
1560
1561 when E_Named_Integer |
1562 E_Named_Real =>
1563 Error_Msg_N ("named number & is not referenced?", E);
1564
1565 when E_Enumeration_Literal =>
1566 Error_Msg_N ("literal & is not referenced?", E);
1567
1568 when E_Function =>
1569 Error_Msg_N ("function & is not referenced?", E);
1570
1571 when E_Procedure =>
1572 Error_Msg_N ("procedure & is not referenced?", E);
1573
1574 when E_Generic_Procedure =>
1575 Error_Msg_N
1576 ("generic procedure & is never instantiated?", E);
1577
1578 when E_Generic_Function =>
1579 Error_Msg_N ("generic function & is never instantiated?", E);
1580
1581 when Type_Kind =>
1582 Error_Msg_N ("type & is not referenced?", E);
1583
1584 when others =>
1585 Error_Msg_N ("& is not referenced?", E);
1586 end case;
1587
1588 Set_Warnings_Off (E);
1589 end if;
1590 end loop;
1591 end Output_Unreferenced_Messages;
1592
1593 ------------------------
1594 -- Set_Warning_Switch --
1595 ------------------------
1596
1597 function Set_Warning_Switch (C : Character) return Boolean is
1598 begin
1599 case C is
1600 when 'a' =>
1601 Check_Unreferenced := True;
1602 Check_Unreferenced_Formals := True;
1603 Check_Withs := True;
1604 Constant_Condition_Warnings := True;
1605 Implementation_Unit_Warnings := True;
1606 Ineffective_Inline_Warnings := True;
1607 Warn_On_Ada_2005_Compatibility := True;
1608 Warn_On_Bad_Fixed_Value := True;
1609 Warn_On_Constant := True;
1610 Warn_On_Export_Import := True;
1611 Warn_On_Modified_Unread := True;
1612 Warn_On_No_Value_Assigned := True;
1613 Warn_On_Obsolescent_Feature := True;
1614 Warn_On_Redundant_Constructs := True;
1615 Warn_On_Unchecked_Conversion := True;
1616 Warn_On_Unrecognized_Pragma := True;
1617
1618 when 'A' =>
1619 Check_Unreferenced := False;
1620 Check_Unreferenced_Formals := False;
1621 Check_Withs := False;
1622 Constant_Condition_Warnings := False;
1623 Elab_Warnings := False;
1624 Implementation_Unit_Warnings := False;
1625 Ineffective_Inline_Warnings := False;
1626 Warn_On_Ada_2005_Compatibility := False;
1627 Warn_On_Bad_Fixed_Value := False;
1628 Warn_On_Constant := False;
1629 Warn_On_Dereference := False;
1630 Warn_On_Export_Import := False;
1631 Warn_On_Hiding := False;
1632 Warn_On_Modified_Unread := False;
1633 Warn_On_No_Value_Assigned := False;
1634 Warn_On_Obsolescent_Feature := False;
1635 Warn_On_Redundant_Constructs := False;
1636 Warn_On_Unchecked_Conversion := False;
1637 Warn_On_Unrecognized_Pragma := False;
1638
1639 when 'b' =>
1640 Warn_On_Bad_Fixed_Value := True;
1641
1642 when 'B' =>
1643 Warn_On_Bad_Fixed_Value := False;
1644
1645 when 'c' =>
1646 Constant_Condition_Warnings := True;
1647
1648 when 'C' =>
1649 Constant_Condition_Warnings := False;
1650
1651 when 'd' =>
1652 Warn_On_Dereference := True;
1653
1654 when 'D' =>
1655 Warn_On_Dereference := False;
1656
1657 when 'e' =>
1658 Warning_Mode := Treat_As_Error;
1659
1660 when 'f' =>
1661 Check_Unreferenced_Formals := True;
1662
1663 when 'F' =>
1664 Check_Unreferenced_Formals := False;
1665
1666 when 'g' =>
1667 Warn_On_Unrecognized_Pragma := True;
1668
1669 when 'G' =>
1670 Warn_On_Unrecognized_Pragma := False;
1671
1672 when 'h' =>
1673 Warn_On_Hiding := True;
1674
1675 when 'H' =>
1676 Warn_On_Hiding := False;
1677
1678 when 'i' =>
1679 Implementation_Unit_Warnings := True;
1680
1681 when 'I' =>
1682 Implementation_Unit_Warnings := False;
1683
1684 when 'j' =>
1685 Warn_On_Obsolescent_Feature := True;
1686
1687 when 'J' =>
1688 Warn_On_Obsolescent_Feature := False;
1689
1690 when 'k' =>
1691 Warn_On_Constant := True;
1692
1693 when 'K' =>
1694 Warn_On_Constant := False;
1695
1696 when 'l' =>
1697 Elab_Warnings := True;
1698
1699 when 'L' =>
1700 Elab_Warnings := False;
1701
1702 when 'm' =>
1703 Warn_On_Modified_Unread := True;
1704
1705 when 'M' =>
1706 Warn_On_Modified_Unread := False;
1707
1708 when 'n' =>
1709 Warning_Mode := Normal;
1710
1711 when 'o' =>
1712 Address_Clause_Overlay_Warnings := True;
1713
1714 when 'O' =>
1715 Address_Clause_Overlay_Warnings := False;
1716
1717 when 'p' =>
1718 Ineffective_Inline_Warnings := True;
1719
1720 when 'P' =>
1721 Ineffective_Inline_Warnings := False;
1722
1723 when 'r' =>
1724 Warn_On_Redundant_Constructs := True;
1725
1726 when 'R' =>
1727 Warn_On_Redundant_Constructs := False;
1728
1729 when 's' =>
1730 Warning_Mode := Suppress;
1731
1732 when 'u' =>
1733 Check_Unreferenced := True;
1734 Check_Withs := True;
1735 Check_Unreferenced_Formals := True;
1736
1737 when 'U' =>
1738 Check_Unreferenced := False;
1739 Check_Withs := False;
1740 Check_Unreferenced_Formals := False;
1741
1742 when 'v' =>
1743 Warn_On_No_Value_Assigned := True;
1744
1745 when 'V' =>
1746 Warn_On_No_Value_Assigned := False;
1747
1748 when 'x' =>
1749 Warn_On_Export_Import := True;
1750
1751 when 'X' =>
1752 Warn_On_Export_Import := False;
1753
1754 when 'y' =>
1755 Warn_On_Ada_2005_Compatibility := True;
1756
1757 when 'Y' =>
1758 Warn_On_Ada_2005_Compatibility := False;
1759
1760 when 'z' =>
1761 Warn_On_Unchecked_Conversion := True;
1762
1763 when 'Z' =>
1764 Warn_On_Unchecked_Conversion := False;
1765
1766 -- Allow and ignore 'w' so that the old
1767 -- format (e.g. -gnatwuwl) will work.
1768
1769 when 'w' =>
1770 null;
1771
1772 when others =>
1773 return False;
1774 end case;
1775
1776 return True;
1777 end Set_Warning_Switch;
1778
1779 -----------------------------
1780 -- Warn_On_Known_Condition --
1781 -----------------------------
1782
1783 procedure Warn_On_Known_Condition (C : Node_Id) is
1784 P : Node_Id;
1785
1786 begin
1787 -- Argument replacement in an inlined body can make conditions
1788 -- static. Do not emit warnings in this case.
1789
1790 if In_Inlined_Body then
1791 return;
1792 end if;
1793
1794 if Constant_Condition_Warnings
1795 and then Nkind (C) = N_Identifier
1796 and then
1797 (Entity (C) = Standard_False or else Entity (C) = Standard_True)
1798 and then Comes_From_Source (Original_Node (C))
1799 and then not In_Instance
1800 then
1801 -- See if this is in a statement or a declaration
1802
1803 P := Parent (C);
1804 loop
1805 -- If tree is not attached, do not issue warning (this is very
1806 -- peculiar, and probably arises from some other error condition)
1807
1808 if No (P) then
1809 return;
1810
1811 -- If we are in a declaration, then no warning, since in practice
1812 -- conditionals in declarations are used for intended tests which
1813 -- may be known at compile time, e.g. things like
1814
1815 -- x : constant Integer := 2 + (Word'Size = 32);
1816
1817 -- And a warning is annoying in such cases
1818
1819 elsif Nkind (P) in N_Declaration
1820 or else
1821 Nkind (P) in N_Later_Decl_Item
1822 then
1823 return;
1824
1825 -- Don't warn in assert pragma, since presumably tests in such
1826 -- a context are very definitely intended, and might well be
1827 -- known at compile time. Note that we have to test the original
1828 -- node, since assert pragmas get rewritten at analysis time.
1829
1830 elsif Nkind (Original_Node (P)) = N_Pragma
1831 and then Chars (Original_Node (P)) = Name_Assert
1832 then
1833 return;
1834 end if;
1835
1836 exit when Is_Statement (P);
1837 P := Parent (P);
1838 end loop;
1839
1840 -- Here we issue the warning unless some sub-operand has warnings
1841 -- set off, in which case we suppress the warning for the node. If
1842 -- the original expression is an inequality, it has been expanded
1843 -- into a negation, and the value of the original expression is the
1844 -- negation of the equality. If the expression is an entity that
1845 -- appears within a negation, it is clearer to flag the negation
1846 -- itself, and report on its constant value.
1847
1848 if not Operand_Has_Warnings_Suppressed (C) then
1849 declare
1850 True_Branch : Boolean := Entity (C) = Standard_True;
1851 Cond : Node_Id := C;
1852
1853 begin
1854 if Present (Parent (C))
1855 and then Nkind (Parent (C)) = N_Op_Not
1856 then
1857 True_Branch := not True_Branch;
1858 Cond := Parent (C);
1859 end if;
1860
1861 if True_Branch then
1862 if Is_Entity_Name (Original_Node (C))
1863 and then Nkind (Cond) /= N_Op_Not
1864 then
1865 Error_Msg_NE
1866 ("object & is always True?", Cond, Original_Node (C));
1867 else
1868 Error_Msg_N ("condition is always True?", Cond);
1869 end if;
1870 else
1871 Error_Msg_N ("condition is always False?", Cond);
1872 end if;
1873 end;
1874 end if;
1875 end if;
1876 end Warn_On_Known_Condition;
1877
1878 end Sem_Warn;
This page took 0.110975 seconds and 6 git commands to generate.