1 |
281 |
jeremybenn |
------------------------------------------------------------------------------
|
2 |
|
|
-- --
|
3 |
|
|
-- GNAT COMPILER COMPONENTS --
|
4 |
|
|
-- --
|
5 |
|
|
-- S E M _ W A R N --
|
6 |
|
|
-- --
|
7 |
|
|
-- S p e c --
|
8 |
|
|
-- --
|
9 |
|
|
-- Copyright (C) 1999-2009, Free Software Foundation, Inc. --
|
10 |
|
|
-- --
|
11 |
|
|
-- GNAT is free software; you can redistribute it and/or modify it under --
|
12 |
|
|
-- terms of the GNU General Public License as published by the Free Soft- --
|
13 |
|
|
-- ware Foundation; either version 3, or (at your option) any later ver- --
|
14 |
|
|
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
|
15 |
|
|
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
|
16 |
|
|
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
|
17 |
|
|
-- for more details. You should have received a copy of the GNU General --
|
18 |
|
|
-- Public License distributed with GNAT; see file COPYING3. If not, go to --
|
19 |
|
|
-- http://www.gnu.org/licenses for a complete copy of the license. --
|
20 |
|
|
-- --
|
21 |
|
|
-- GNAT was originally developed by the GNAT team at New York University. --
|
22 |
|
|
-- Extensive contributions were provided by Ada Core Technologies Inc. --
|
23 |
|
|
-- --
|
24 |
|
|
------------------------------------------------------------------------------
|
25 |
|
|
|
26 |
|
|
-- This package contains the routines used to deal with issuing warnings
|
27 |
|
|
-- about uses of uninitialized variables and unused with's. It also has
|
28 |
|
|
-- some unrelated routines related to the generation of warnings.
|
29 |
|
|
|
30 |
|
|
with Alloc; use Alloc;
|
31 |
|
|
with Table;
|
32 |
|
|
with Types; use Types;
|
33 |
|
|
|
34 |
|
|
package Sem_Warn is
|
35 |
|
|
|
36 |
|
|
------------------------
|
37 |
|
|
-- Warnings Off Table --
|
38 |
|
|
------------------------
|
39 |
|
|
|
40 |
|
|
type Warnings_Off_Entry is record
|
41 |
|
|
N : Node_Id;
|
42 |
|
|
-- A pragma Warnings (Off, ent) node
|
43 |
|
|
|
44 |
|
|
E : Entity_Id;
|
45 |
|
|
-- The entity involved
|
46 |
|
|
end record;
|
47 |
|
|
|
48 |
|
|
-- An entry is made in the following table for any valid Pragma Warnings
|
49 |
|
|
-- (Off, entity) encountered while Opt.Warn_On_Warnings_Off is True. It
|
50 |
|
|
-- is used to generate warnings on any of these pragmas that turn out not
|
51 |
|
|
-- to be needed, or that could be replaced by Unmodified/Unreferenced.
|
52 |
|
|
|
53 |
|
|
package Warnings_Off_Pragmas is new Table.Table (
|
54 |
|
|
Table_Component_Type => Warnings_Off_Entry,
|
55 |
|
|
Table_Index_Type => Int,
|
56 |
|
|
Table_Low_Bound => 0,
|
57 |
|
|
Table_Initial => Alloc.Warnings_Off_Pragmas_Initial,
|
58 |
|
|
Table_Increment => Alloc.Warnings_Off_Pragmas_Increment,
|
59 |
|
|
Table_Name => "Name_Warnings_Off_Pragmas");
|
60 |
|
|
|
61 |
|
|
--------------------
|
62 |
|
|
-- Initialization --
|
63 |
|
|
--------------------
|
64 |
|
|
|
65 |
|
|
procedure Initialize;
|
66 |
|
|
-- Initialize this package for new compilation
|
67 |
|
|
|
68 |
|
|
function Set_Warning_Switch (C : Character) return Boolean;
|
69 |
|
|
-- This function sets the warning switch or switches corresponding to the
|
70 |
|
|
-- given character. It is used to process a -gnatw switch on the command
|
71 |
|
|
-- line, or a character in a string literal in pragma Warnings. Returns
|
72 |
|
|
-- True for valid warning character C, False for invalid character.
|
73 |
|
|
|
74 |
|
|
function Set_Dot_Warning_Switch (C : Character) return Boolean;
|
75 |
|
|
-- This function sets the warning switch or switches corresponding to the
|
76 |
|
|
-- given character preceded by a dot. Used to process a -gnatw. switch on
|
77 |
|
|
-- the command line or .C in a string literal in pragma Warnings. Returns
|
78 |
|
|
-- True for valid warning character C, False for invalid character.
|
79 |
|
|
|
80 |
|
|
procedure Set_GNAT_Mode_Warnings;
|
81 |
|
|
-- This is called in -gnatg mode to set the warnings for gnat mode. It is
|
82 |
|
|
-- also used to set the proper warning statuses for -gnatw.g.
|
83 |
|
|
|
84 |
|
|
------------------------------------------
|
85 |
|
|
-- Routines to Handle Unused References --
|
86 |
|
|
------------------------------------------
|
87 |
|
|
|
88 |
|
|
procedure Check_References (E : Entity_Id; Anod : Node_Id := Empty);
|
89 |
|
|
-- Called at the end of processing a declarative region. The entity E
|
90 |
|
|
-- is the entity for the scope. All entities declared in the region,
|
91 |
|
|
-- as indicated by First_Entity and the entity chain, are checked to
|
92 |
|
|
-- see if they are variables for which warnings need to be posted for
|
93 |
|
|
-- either no assignments, or a use before an assignment or no references
|
94 |
|
|
-- at all. The Anod node is present for the case of an accept statement,
|
95 |
|
|
-- and references the accept statement. This is used to place the warning
|
96 |
|
|
-- messages in the right place.
|
97 |
|
|
|
98 |
|
|
procedure Check_Unset_Reference (N : Node_Id);
|
99 |
|
|
-- N is the node for an expression which occurs in a reference position,
|
100 |
|
|
-- e.g. as the right side of an assignment. This procedure checks to see
|
101 |
|
|
-- if the node is a reference to a variable entity where the entity has
|
102 |
|
|
-- Not_Assigned set. If so, the Unset_Reference field is set if it is not
|
103 |
|
|
-- the first occurrence. No warning is posted, instead warnings will be
|
104 |
|
|
-- posted later by Check_References. The reason we do things that
|
105 |
|
|
-- way is that if there are no assignments anywhere, we prefer to flag
|
106 |
|
|
-- the entity, rather than a reference to it. Note that for the purposes
|
107 |
|
|
-- of this routine, a type conversion or qualified expression whose
|
108 |
|
|
-- expression is an entity is also processed. The reason that we do not
|
109 |
|
|
-- process these at the point of occurrence is that both these constructs
|
110 |
|
|
-- can occur in non-reference positions (e.g. as out parameters).
|
111 |
|
|
|
112 |
|
|
procedure Check_Unused_Withs (Spec_Unit : Unit_Number_Type := No_Unit);
|
113 |
|
|
-- This routine performs two kinds of checks. It checks that all with'ed
|
114 |
|
|
-- units are referenced, and that at least one entity of each with'ed
|
115 |
|
|
-- unit is referenced (the latter check catches units that are only
|
116 |
|
|
-- referenced in a use or package renaming statement). Appropriate
|
117 |
|
|
-- warning messages are generated if either of these situations is
|
118 |
|
|
-- detected.
|
119 |
|
|
--
|
120 |
|
|
-- A special case arises when a package body or a subprogram body with
|
121 |
|
|
-- a separate spec is being compiled. In this case, a with may appear
|
122 |
|
|
-- on the spec, but be needed only by the body. This still generates
|
123 |
|
|
-- a warning, but the text is different (the with is not redundant,
|
124 |
|
|
-- it is misplaced).
|
125 |
|
|
--
|
126 |
|
|
-- This special case is implemented by making an initial call to this
|
127 |
|
|
-- procedure with Spec_Unit set to the unit number of the separate spec.
|
128 |
|
|
-- This call does not generate any warning messages, but instead may
|
129 |
|
|
-- result in flags being set in the N_With_Clause node that record that
|
130 |
|
|
-- there was no use in the spec.
|
131 |
|
|
--
|
132 |
|
|
-- The main call (made after all units have been analyzed, with Spec_Unit
|
133 |
|
|
-- set to the default value of No_Unit) generates the required warnings
|
134 |
|
|
-- using the flags set by the initial call where appropriate to specialize
|
135 |
|
|
-- the text of the warning messages.
|
136 |
|
|
|
137 |
|
|
---------------------
|
138 |
|
|
-- Output Routines --
|
139 |
|
|
---------------------
|
140 |
|
|
|
141 |
|
|
procedure Output_Non_Modified_In_Out_Warnings;
|
142 |
|
|
-- Warnings about IN OUT parameters that could be IN are collected till
|
143 |
|
|
-- the end of the compilation process (see body of this routine for a
|
144 |
|
|
-- discussion of why this is done). This procedure outputs the warnings.
|
145 |
|
|
-- Note: this should be called before Output_Unreferenced_Messages, since
|
146 |
|
|
-- if we have an IN OUT warning, that's the one we want to see!
|
147 |
|
|
|
148 |
|
|
procedure Output_Obsolescent_Entity_Warnings (N : Node_Id; E : Entity_Id);
|
149 |
|
|
-- N is a reference to obsolescent entity E, for which appropriate warning
|
150 |
|
|
-- messages are to be generated (caller has already checked that warnings
|
151 |
|
|
-- are active and appropriate for this entity).
|
152 |
|
|
|
153 |
|
|
procedure Output_Unreferenced_Messages;
|
154 |
|
|
-- Warnings about unreferenced entities are collected till the end of
|
155 |
|
|
-- the compilation process (see Check_Unset_Reference for further
|
156 |
|
|
-- details). This procedure outputs waiting warnings, if any.
|
157 |
|
|
|
158 |
|
|
procedure Output_Unused_Warnings_Off_Warnings;
|
159 |
|
|
-- Warnings about pragma Warnings (Off, ent) statements that are unused,
|
160 |
|
|
-- or could be replaced by Unmodified/Unreferenced pragmas, are collected
|
161 |
|
|
-- till the end of the compilation process. This procedure outputs waiting
|
162 |
|
|
-- warnings if any.
|
163 |
|
|
|
164 |
|
|
----------------------------
|
165 |
|
|
-- Other Warning Routines --
|
166 |
|
|
----------------------------
|
167 |
|
|
|
168 |
|
|
procedure Check_Code_Statement (N : Node_Id);
|
169 |
|
|
-- Perform warning checks on a code statement node
|
170 |
|
|
|
171 |
|
|
procedure Check_Infinite_Loop_Warning (Loop_Statement : Node_Id);
|
172 |
|
|
-- N is the node for a loop statement. This procedure checks if a warning
|
173 |
|
|
-- should be given for a possible infinite loop, and if so issues it.
|
174 |
|
|
|
175 |
|
|
procedure Check_Low_Bound_Tested (Expr : Node_Id);
|
176 |
|
|
-- Expr is the node for a comparison operation. This procedure checks if
|
177 |
|
|
-- the comparison is a source comparison of P'First with some other value
|
178 |
|
|
-- and if so, sets the Low_Bound_Tested flag on entity P to suppress
|
179 |
|
|
-- warnings about improper low bound assumptions (we assume that if the
|
180 |
|
|
-- code has a test that explicitly checks P'First, then it is not operating
|
181 |
|
|
-- in blind assumption mode).
|
182 |
|
|
|
183 |
|
|
procedure Warn_On_Known_Condition (C : Node_Id);
|
184 |
|
|
-- C is a node for a boolean expression resulting from a relational
|
185 |
|
|
-- or membership operation. If the expression has a compile time known
|
186 |
|
|
-- value, then a warning is output if all the following conditions hold:
|
187 |
|
|
--
|
188 |
|
|
-- 1. Original expression comes from source. We don't want to generate
|
189 |
|
|
-- warnings for internally generated conditionals.
|
190 |
|
|
--
|
191 |
|
|
-- 2. As noted above, the expression is a relational or membership
|
192 |
|
|
-- test, we don't want to generate warnings for boolean variables
|
193 |
|
|
-- since this is typical of conditional compilation in Ada.
|
194 |
|
|
--
|
195 |
|
|
-- 3. The expression appears in a statement, rather than a declaration.
|
196 |
|
|
-- In practice, most occurrences in declarations are legitimate
|
197 |
|
|
-- conditionalizations, but occurrences in statements are often
|
198 |
|
|
-- errors for which the warning is useful.
|
199 |
|
|
--
|
200 |
|
|
-- 4. The expression does not occur within an instantiation. A non-
|
201 |
|
|
-- static expression in a generic may become constant because of
|
202 |
|
|
-- the attributes of the actuals, and we do not want to warn on
|
203 |
|
|
-- these legitimate constant foldings.
|
204 |
|
|
--
|
205 |
|
|
-- If all these conditions are met, the warning is issued noting that
|
206 |
|
|
-- the result of the test is always false or always true as appropriate.
|
207 |
|
|
|
208 |
|
|
function Warn_On_Modified_As_Out_Parameter (E : Entity_Id) return Boolean;
|
209 |
|
|
-- Returns True if we should activate warnings for entity E being modified
|
210 |
|
|
-- as an out parameter. True if either Warn_On_Modified_Unread is set for
|
211 |
|
|
-- an only OUT parameter, or if Warn_On_All_Unread_Out_Parameters is set.
|
212 |
|
|
|
213 |
|
|
procedure Warn_On_Overlapping_Actuals (Subp : Entity_Id; N : Node_Id);
|
214 |
|
|
-- Called on a subprogram call. Checks whether an IN OUT actual that is
|
215 |
|
|
-- not by-copy may overlap with another actual, thus leading to aliasing
|
216 |
|
|
-- in the body of the called subprogram.
|
217 |
|
|
|
218 |
|
|
procedure Warn_On_Suspicious_Index (Name : Entity_Id; X : Node_Id);
|
219 |
|
|
-- This is called after resolving an indexed component or a slice. Name
|
220 |
|
|
-- is the entity for the name of the indexed array, and X is the subscript
|
221 |
|
|
-- for the indexed component case, or one of the bounds in the slice case.
|
222 |
|
|
-- If Name is an unconstrained parameter of a standard string type, and
|
223 |
|
|
-- the index is of the form of a literal or Name'Length [- literal], then
|
224 |
|
|
-- a warning is generated that the subscripting operation is possibly
|
225 |
|
|
-- incorrectly assuming a lower bound of 1.
|
226 |
|
|
|
227 |
|
|
procedure Warn_On_Unassigned_Out_Parameter
|
228 |
|
|
(Return_Node : Node_Id;
|
229 |
|
|
Scope_Id : Entity_Id);
|
230 |
|
|
-- Called when processing a return statement given by Return_Node. Scope_Id
|
231 |
|
|
-- is the Entity_Id for the procedure in which the return statement lives.
|
232 |
|
|
-- A check is made for the case of a procedure with out parameters that
|
233 |
|
|
-- have not yet been assigned, and appropriate warnings are given.
|
234 |
|
|
|
235 |
|
|
procedure Warn_On_Useless_Assignment
|
236 |
|
|
(Ent : Entity_Id;
|
237 |
|
|
N : Node_Id := Empty);
|
238 |
|
|
-- Called to check if we have a case of a useless assignment to the given
|
239 |
|
|
-- entity Ent, as indicated by a non-empty Last_Assignment field. This call
|
240 |
|
|
-- should only be made if at least one of the flags Warn_On_Modified_Unread
|
241 |
|
|
-- or Warn_On_All_Unread_Out_Parameters is True, and if Ent is in the
|
242 |
|
|
-- extended main source unit. N is Empty for the end of block call
|
243 |
|
|
-- (warning message says value unreferenced), or the it is the node for
|
244 |
|
|
-- an overwriting assignment (warning message points to this assignment).
|
245 |
|
|
|
246 |
|
|
procedure Warn_On_Useless_Assignments (E : Entity_Id);
|
247 |
|
|
pragma Inline (Warn_On_Useless_Assignments);
|
248 |
|
|
-- Called at the end of a block or subprogram. Scans the entities of the
|
249 |
|
|
-- block or subprogram to see if there are any variables for which useless
|
250 |
|
|
-- assignments were made (assignments whose values were never read).
|
251 |
|
|
|
252 |
|
|
end Sem_Warn;
|