1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1999-2006, Free Software Foundation, Inc. --
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. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 -- This package contains the routines used to deal with issuing warnings
28 -- about uses of uninitialized variables and unused with's. It also has
29 -- some unrelated routines related to the generation of warnings.
31 with Types; use Types;
39 function Set_Warning_Switch (C : Character) return Boolean;
40 -- This function sets the warning switch or switches corresponding to the
41 -- given character. It is used to process a -gnatw switch on the command
42 -- line, or a character in a string literal in pragma Warnings. Returns
43 -- True for valid warning character C, False for invalid character.
45 function Set_Dot_Warning_Switch (C : Character) return Boolean;
46 -- This function sets the warning switch or switches corresponding to the
47 -- given character preceded by a dot. Used to process a -gnatw. switch on
48 -- the command line or .C in a string literal in pragma Warnings. Returns
49 -- True for valid warning character C, False for invalid character.
51 ------------------------------------------
52 -- Routines to Handle Unused References --
53 ------------------------------------------
55 procedure Check_References (E : Entity_Id; Anod : Node_Id := Empty);
56 -- Called at the end of processing a declarative region. The entity E
57 -- is the entity for the scope. All entities declared in the region,
58 -- as indicated by First_Entity and the entity chain, are checked to
59 -- see if they are variables for which warnings need to be posted for
60 -- either no assignments, or a use before an assignment or no references
61 -- at all. The Anod node is present for the case of an accept statement,
62 -- and references the accept statement. This is used to place the warning
63 -- messages in the right place.
65 procedure Check_Unset_Reference (N : Node_Id);
66 -- N is the node for an expression which occurs in a reference position,
67 -- e.g. as the right side of an assignment. This procedure checks to see
68 -- if the node is a reference to a variable entity where the entity has
69 -- Not_Assigned set. If so, the Unset_Reference field is set if it is not
70 -- the first occurrence. No warning is posted, instead warnings will be
71 -- posted later by Check_References. The reason we do things that
72 -- way is that if there are no assignments anywhere, we prefer to flag
73 -- the entity, rather than a reference to it. Note that for the purposes
74 -- of this routine, a type conversion or qualified expression whose
75 -- expression is an entity is also processed. The reason that we do not
76 -- process these at the point of occurrence is that both these constructs
77 -- can occur in non-reference positions (e.g. as out parameters).
79 procedure Check_Unused_Withs (Spec_Unit : Unit_Number_Type := No_Unit);
80 -- This routine performs two kinds of checks. It checks that all with'ed
81 -- units are referenced, and that at least one entity of each with'ed
82 -- unit is referenced (the latter check catches units that are only
83 -- referenced in a use or package renaming statement). Appropriate
84 -- warning messages are generated if either of these situations is
87 -- A special case arises when a package body or a subprogram body with
88 -- a separate spec is being compiled. In this case, a with may appear
89 -- on the spec, but be needed only by the body. This still generates
90 -- a warning, but the text is different (the with is not redundant,
93 -- This special case is implemented by making an initial call to this
94 -- procedure with Spec_Unit set to the unit number of the separate spec.
95 -- This call does not generate any warning messages, but instead may
96 -- result in flags being set in the N_With_Clause node that record that
97 -- there was no use in the spec.
99 -- The main call (made after all units have been analyzed, with Spec_Unit
100 -- set to the default value of No_Unit) generates the required warnings
101 -- using the flags set by the initial call where appropriate to specialize
102 -- the text of the warning messages.
104 ---------------------
105 -- Output Routines --
106 ---------------------
108 procedure Output_Obsolescent_Entity_Warnings (N : Node_Id; E : Entity_Id);
109 -- N is a reference to obsolescent entity E, for which appropriate warning
110 -- messages are to be generated (caller has already checked that warnings
111 -- are active and appropriate for this entity).
113 procedure Output_Unreferenced_Messages;
114 -- Warnings about unreferenced entities are collected till the end of
115 -- the compilation process (see Check_Unset_Reference for further
116 -- details). This procedure outputs waiting warnings, if any.
118 ----------------------------
119 -- Other Warning Routines --
120 ----------------------------
122 procedure Check_Code_Statement (N : Node_Id);
123 -- Peform warning checks on a code statement node
125 procedure Warn_On_Known_Condition (C : Node_Id);
126 -- C is a node for a boolean expression resluting from a relational
127 -- or membership operation. If the expression has a compile time known
128 -- value, then a warning is output if all the following conditions hold:
130 -- 1. Original expression comes from source. We don't want to generate
131 -- warnings for internally generated conditionals.
133 -- 2. As noted above, the expression is a relational or membership
134 -- test, we don't want to generate warnings for boolean variables
135 -- since this is typical of conditional compilation in Ada.
137 -- 3. The expression appears in a statement, rather than a declaration.
138 -- In practice, most occurrences in declarations are legitimate
139 -- conditionalizations, but occurrences in statements are often
140 -- errors for which the warning is useful.
142 -- 4. The expression does not occur within an instantiation. A non-
143 -- static expression in a generic may become constant because of
144 -- the attributes of the actuals, and we do not want to warn on
145 -- these legitimate constant foldings.
147 -- If all these conditions are met, the warning is issued noting that
148 -- the result of the test is always false or always true as appropriate.
150 procedure Warn_On_Suspicious_Index (Name : Entity_Id; X : Node_Id);
151 -- This is called after resolving an indexed component or a slice. Name
152 -- is the entity for the name of the indexed array, and X is the subscript
153 -- for the indexed component case, or one of the bounds in the slice case.
154 -- If Name is an unconstrained parameter of a standard string type, and
155 -- the index is of the form of a literal or Name'Length [- literal], then
156 -- a warning is generated that the subscripting operation is possibly
157 -- incorrectly assuming a lower bound of 1.
159 procedure Warn_On_Useless_Assignment
161 Loc : Source_Ptr := No_Location);
162 -- Called to check if we have a case of a useless assignment to the given
163 -- entity Ent, as indicated by a non-empty Last_Assignment field. This call
164 -- should only be made if Warn_On_Modified_Unread is True, and if Ent is in
165 -- the extended main source unit. Loc is No_Location for the end of block
166 -- call (warning msg says value unreferenced), or the it is the location of
167 -- an overwriting assignment (warning msg points to this assignment).
169 procedure Warn_On_Useless_Assignments (E : Entity_Id);
170 pragma Inline (Warn_On_Useless_Assignments);
171 -- Called at the end of a block or subprogram. Scans the entities of the
172 -- block or subprogram to see if there are any variables for which useless
173 -- assignments were made (assignments whose values were never read).