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