1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
27 ------------------------------------------------------------------------------
29 -- This package contains the routines to output error messages. They
30 -- are basically system independent, however in some environments, e.g.
31 -- when the parser is embedded into an editor, it may be appropriate
32 -- to replace the implementation of this package.
35 with Types; use Types;
36 with Uintp; use Uintp;
40 Errors_Detected : Nat;
41 -- Number of errors detected so far
43 Warnings_Detected : Nat;
44 -- Number of warnings detected
46 type Compiler_State_Type is (Parsing, Analyzing);
47 Compiler_State : Compiler_State_Type;
48 -- Indicates current state of compilation. This is put in the Errout
49 -- spec because it affects the action of the error message handling.
50 -- In particular, an attempt is made by Errout to suppress cascaded
51 -- error messages in Parsing mode, but not in the other modes.
53 Current_Error_Source_File : Source_File_Index;
54 -- Id of current messages. Used to post file name when unit changes. This
55 -- is initialized to Main_Source_File at the start of a compilation, which
56 -- means that no file names will be output unless there are errors in units
57 -- other than the main unit. However, if the main unit has a pragma
58 -- Source_Reference line, then this is initialized to No_Source_File,
59 -- to force an initial reference to the real source file name.
61 Raise_Exception_On_Error : Nat := 0;
62 -- If this value is non-zero, then any attempt to generate an error
63 -- message raises the exception Error_Msg_Exception, and the error
64 -- message is not output. This is used for defending against junk
65 -- resulting from illegalities, and also for substitution of more
66 -- appropriate error messages from higher semantic levels. It is
67 -- a counter so that the increment/decrement protocol nests neatly.
69 Error_Msg_Exception : exception;
70 -- Exception raised if Raise_Exception_On_Error is true
72 -----------------------------------
73 -- Suppression of Error Messages --
74 -----------------------------------
76 -- In an effort to reduce the impact of redundant error messages, the
77 -- error output routines in this package normally suppress certain
78 -- classes of messages as follows:
80 -- 1. Identical messages placed at the same point in the text. Such
81 -- duplicate error message result for example from rescanning
82 -- sections of the text that contain lexical errors. Only one of
83 -- such a set of duplicate messages is output, and the rest are
86 -- 2. If more than one parser message is generated for a single source
87 -- line, then only the first message is output, the remaining
88 -- messages on the same line are suppressed.
90 -- 3. If a message is posted on a node for which a message has been
91 -- previously posted, then only the first message is retained. The
92 -- Error_Posted flag is used to detect such multiple postings. Note
93 -- that this only applies to semantic messages, since otherwise
94 -- for parser messages, this would be a special case of case 2.
96 -- 4. If a message is posted on a node whose Etype or Entity
97 -- fields reference entities on which an error message has
98 -- already been placed, as indicated by the Error_Posted flag
99 -- being set on these entities, then the message is suppressed.
101 -- 5. If a message attempts to insert an Error node, or a direct
102 -- reference to the Any_Type node, then the message is suppressed.
104 -- This normal suppression action may be overridden in cases 2-5 (but not
105 -- in case 1) by setting All_Errors mode, or by setting the special
106 -- unconditional message insertion character (!) at the end of the message
107 -- text as described below.
109 ---------------------------------------------------------
110 -- Error Message Text and Message Insertion Characters --
111 ---------------------------------------------------------
113 -- Error message text strings are composed of lower case letters, digits
114 -- and the special characters space, comma, period, colon and semicolon,
115 -- apostrophe and parentheses. Special insertion characters can also
116 -- appear which cause the error message circuit to modify the given
117 -- string as follows:
119 -- Insertion character % (Percent: insert name from Names table)
120 -- The character % is replaced by the text for the name specified by
121 -- the Name_Id value stored in Error_Msg_Name_1. A blank precedes
122 -- the name if it is preceded by a non-blank character other than a
123 -- left parenthesis. The name is enclosed in quotes unless manual
124 -- quotation mode is set. If the Name_Id is set to No_Name, then
125 -- no insertion occurs; if the Name_Id is set to Error_Name, then
126 -- the string <error> is inserted. A second and third % may appear
127 -- in a single message, similarly replaced by the names which are
128 -- specified by the Name_Id values stored in Error_Msg_Name_2 and
129 -- Error_Msg_Name_3. The names are decoded and cased according to
130 -- the current identifier casing mode.
132 -- Insertion character $ (Dollar: insert unit name from Names table)
133 -- The character $ is treated similarly to %, except that the name
134 -- is obtained from the Unit_Name_Type value in Error_Msg_Unit_1
135 -- and Error_Msg_Unit_2, as provided by Get_Unit_Name_String in
136 -- package Uname. Note that this name includes the postfix (spec)
137 -- or (body) strings. If this postfix is not required, use the
138 -- normal % insertion for the unit name.
140 -- Insertion character { (Left brace: insert literally from names table)
141 -- The character { is treated similarly to %, except that the
142 -- name is output literally as stored in the names table without
143 -- adjusting the casing. This can be used for file names and in
144 -- other situations where the name string is to be output unchanged.
146 -- Insertion character * (Asterisk, insert reserved word name)
147 -- The insertion character * is treated exactly like % except that
148 -- the resulting name is cased according to the default conventions
149 -- for reserved words (see package Scans).
151 -- Insertion character & (Ampersand: insert name from node)
152 -- The insertion character & is treated similarly to %, except that
153 -- the name is taken from the Chars field of the given node, and may
154 -- refer to a child unit name, or a selected component. The casing
155 -- is, if possible, taken from the original source reference, which
156 -- is obtained from the Sloc field of the given node or nodes. If no
157 -- Sloc is available (happens e.g. for nodes in package Standard),
158 -- then the default case (see Scans spec) is used. The nodes to be
159 -- used are stored in Error_Msg_Node_1, Error_Msg_Node_2. No insertion
160 -- occurs for the Empty node, and the Error node results in the
161 -- insertion of the characters <error>. In addition, if the special
162 -- global variable Error_Msg_Qual_Level is non-zero, then the
163 -- reference will include up to the given number of levels of
164 -- qualification, using the scope chain.
166 -- Insertion character # (Pound: insert line number reference)
167 -- The character # is replaced by the string indicating the source
168 -- position stored in Error_Msg_Sloc. There are three cases:
170 -- for package Standard: in package Standard
171 -- for locations in current file: at line nnn:ccc
172 -- for locations in other files: at filename:nnn:ccc
174 -- By convention, the # insertion character is only used at the end
175 -- of an error message, so the above strings only appear as the last
176 -- characters of an error message.
178 -- Insertion character } (Right brace: insert type reference)
179 -- The character } is replaced by a string describing the type
180 -- referenced by the entity whose Id is stored in Error_Msg_Node_1.
181 -- the string gives the name or description of the type, and also
182 -- where appropriate the location of its declaration. Special
183 -- cases like "some integer type" are handled appropriately. Only
184 -- one } is allowed in a message, since there is not enough room
185 -- for two (the insertion can be quite long, including a file name)
186 -- In addition, if the special global variable Error_Msg_Qual_Level
187 -- is non-zero, then the reference will include up to the given
188 -- number of levels of qualification, using the scope chain.
190 -- Insertion character @ (At: insert column number reference)
191 -- The character @ is replaced by null if the RM_Column_Check mode is
192 -- off (False). If the switch is on (True), then @ is replaced by the
193 -- text string " in column nnn" where nnn is the decimal representation
194 -- of the column number stored in Error_Msg_Col plus one (the plus one
195 -- is because the number is stored 0-origin and displayed 1-origin).
197 -- Insertion character ^ (Carret: insert integer value)
198 -- The character ^ is replaced by the decimal conversion of the Uint
199 -- value stored in Error_Msg_Uint_1, with a possible leading minus.
200 -- A second ^ may occur in the message, in which case it is replaced
201 -- by the decimal conversion of the Uint value in Error_Msg_Uint_2.
203 -- Insertion character ! (Exclamation: unconditional message)
204 -- The character ! appearing as the last character of a message makes
205 -- the message unconditional which means that it is output even if it
206 -- would normally be suppressed. See section above for a description
207 -- of the cases in which messages are normally suppressed.
209 -- Insertion character ? (Question: warning message)
210 -- The character ? appearing anywhere in a message makes the message
211 -- a warning instead of a normal error message, and the text of the
212 -- message will be preceded by "Warning:" instead of "Error:" The
213 -- handling of warnings if further controlled by the Warning_Mode
214 -- option (-w switch), see package Opt for further details, and
215 -- also by the current setting from pragma Warnings. This pragma
216 -- applies only to warnings issued from the semantic phase (not
217 -- the parser), but currently all relevant warnings are posted
218 -- by the semantic phase anyway. Messages starting with (style)
219 -- are also treated as warning messages.
221 -- Insertion character A-Z (Upper case letter: Ada reserved word)
222 -- If two or more upper case letters appear in the message, they are
223 -- taken as an Ada reserved word, and are converted to the default
224 -- case for reserved words (see Scans package spec). Surrounding
225 -- quotes are added unless manual quotation mode is currently set.
227 -- Insertion character ` (Backquote: set manual quotation mode)
228 -- The backquote character always appears in pairs. Each backquote
229 -- of the pair is replaced by a double quote character. In addition,
230 -- Any reserved keywords, or name insertions between these backquotes
231 -- are not surrounded by the usual automatic double quotes. See the
232 -- section below on manual quotation mode for further details.
234 -- Insertion character ' (Quote: literal character)
235 -- Precedes a character which is placed literally into the message.
236 -- Used to insert characters into messages that are one of the
237 -- insertion characters defined here.
239 -- Insertion character \ (Backslash: continuation message)
240 -- Indicates that the message is a continuation of a message
241 -- previously posted. This is used to ensure that such groups
242 -- of messages are treated as a unit. The \ character must be
243 -- the first character of the message text.
245 -----------------------------------------------------
246 -- Global Values Used for Error Message Insertions --
247 -----------------------------------------------------
249 -- The following global variables are essentially additional parameters
250 -- passed to the error message routine for insertion sequences described
251 -- above. The reason these are passed globally is that the insertion
252 -- mechanism is essentially an untyped one in which the appropriate
253 -- variables are set dependingon the specific insertion characters used.
255 Error_Msg_Col : Column_Number;
256 -- Column for @ insertion character in message
258 Error_Msg_Uint_1 : Uint;
259 Error_Msg_Uint_2 : Uint;
260 -- Uint values for ^ insertion characters in message
262 Error_Msg_Sloc : Source_Ptr;
263 -- Source location for # insertion character in message
265 Error_Msg_Name_1 : Name_Id;
266 Error_Msg_Name_2 : Name_Id;
267 Error_Msg_Name_3 : Name_Id;
268 -- Name_Id values for % insertion characters in message
270 Error_Msg_Unit_1 : Name_Id;
271 Error_Msg_Unit_2 : Name_Id;
272 -- Name_Id values for $ insertion characters in message
274 Error_Msg_Node_1 : Node_Id;
275 Error_Msg_Node_2 : Node_Id;
276 -- Node_Id values for & insertion characters in message
278 Error_Msg_Qual_Level : Int := 0;
279 -- Number of levels of qualification required for type name (see the
280 -- description of the } insertion character. Note that this value does
281 -- note get reset by any Error_Msg call, so the caller is responsible
284 Warn_On_Instance : Boolean := False;
285 -- Normally if a warning is generated in a generic template from the
286 -- analysis of the template, then the warning really belongs in the
287 -- template, and the default value of False for this Boolean achieves
288 -- that effect. If Warn_On_Instance is set True, then the warnings are
289 -- generated on the instantiation (referring to the template) rather
290 -- than on the template itself.
292 -----------------------------------------------------
293 -- Format of Messages and Manual Quotation Control --
294 -----------------------------------------------------
296 -- Messages are generally all in lower case, except for inserted names
297 -- and appear in one of the following three forms:
302 -- The prefixes error and warning are supplied automatically (depending
303 -- on the use of the ? insertion character), and the call to the error
304 -- message routine supplies the text. The "error: " prefix is omitted
305 -- in brief error message formats.
307 -- Reserved Ada keywords in the message are in the default keyword case
308 -- (determined from the given source program), surrounded by quotation
309 -- marks. This is achieved by spelling the reserved word in upper case
310 -- letters, which is recognized as a request for insertion of quotation
311 -- marks by the error text processor. Thus for example:
313 -- Error_Msg_AP ("IS expected");
315 -- would result in the output of one of the following:
317 -- error: "is" expected
318 -- error: "IS" expected
319 -- error: "Is" expected
321 -- the choice between these being made by looking at the casing convention
322 -- used for keywords (actually the first compilation unit keyword) in the
325 -- In the case of names, the default mode for the error text processor
326 -- is to surround the name by quotation marks automatically. The case
327 -- used for the identifier names is taken from the source program where
328 -- possible, and otherwise is the default casing convention taken from
329 -- the source file usage.
331 -- In some cases, better control over the placement of quote marks is
332 -- required. This is achieved using manual quotation mode. In this mode,
333 -- one or more insertion sequences is surrounded by backquote characters.
334 -- The backquote characters are output as double quote marks, and normal
335 -- automatic insertion of quotes is suppressed between the double quotes.
338 -- Error_Msg_AP ("`END &;` expected");
340 -- generates a message like
342 -- error: "end Open_Scope;" expected
344 -- where the node specifying the name Open_Scope has been stored in
345 -- Error_Msg_Node_1 prior to the call. The great majority of error
346 -- messages operates in normal quotation mode.
348 -- Note: the normal automatic insertion of spaces before insertion
349 -- sequences (such as those that come from & and %) is suppressed in
350 -- manual quotation mode, so blanks, if needed as in the above example,
351 -- must be explicitly present.
353 ----------------------------
354 -- Message ID Definitions --
355 ----------------------------
357 type Error_Msg_Id is new Int;
358 -- A type used to represent specific error messages. Used by the clients
359 -- of this package only in the context of the Get_Error_Id and
360 -- Change_Error_Text subprograms.
362 No_Error_Msg : constant Error_Msg_Id := 0;
363 -- A constant which is different from any value returned by Get_Error_Id.
364 -- Typically used by a client to indicate absense of a saved Id value.
366 function Get_Msg_Id return Error_Msg_Id;
367 -- Returns the Id of the message most recently posted using one of the
368 -- Error_Msg routines.
370 function Get_Location (E : Error_Msg_Id) return Source_Ptr;
371 -- Returns the flag location of the error message with the given id E.
373 ------------------------
374 -- List Pragmas Table --
375 ------------------------
377 -- When a pragma Page or pragma List is encountered by the parser, an
378 -- entry is made in the following table. This table is then used to
379 -- control the full listing if one is being generated. Note that the
380 -- reason we do the processing in the parser is so that we get proper
381 -- listing control even in syntax check only mode.
383 type List_Pragma_Type is (List_On, List_Off, Page);
385 type List_Pragma_Record is record
386 Ptyp : List_Pragma_Type;
390 -- Note: Ploc points to the terminating semicolon in the List_Off and
391 -- Page cases, and to the pragma keyword for List_On. In the case of
392 -- a pragma List_Off, a List_On entry is also made in the table,
393 -- pointing to the pragma keyword. This ensures that, as required,
394 -- a List (Off) pragma is listed even in list off mode.
396 package List_Pragmas is new Table.Table (
397 Table_Component_Type => List_Pragma_Record,
398 Table_Index_Type => Int,
399 Table_Low_Bound => 1,
401 Table_Increment => 200,
402 Table_Name => "List_Pragmas");
404 ---------------------------
405 -- Ignore_Errors Feature --
406 ---------------------------
408 -- In certain cases, notably for optional subunits, the compiler operates
409 -- in a mode where errors are to be ignored, and the whole unit is to be
410 -- considered as not present. To implement this we provide the following
411 -- flag to enable special handling, where error messages are suppressed,
412 -- but the Fatal_Error flag will still be set in the normal manner.
414 Ignore_Errors_Enable : Nat := 0;
415 -- Triggering switch. If non-zero, then ignore errors mode is activated.
416 -- This is a counter to allow convenient nesting of enable/disable.
418 ------------------------------
419 -- Error Output Subprograms --
420 ------------------------------
422 procedure Initialize;
423 -- Initializes for output of error messages. Must be called for each
424 -- source file before using any of the other routines in the package.
427 -- Finalize processing of error messages for one file and output message
428 -- indicating the number of detected errors.
430 procedure Error_Msg (Msg : String; Flag_Location : Source_Ptr);
431 -- Output a message at specified location. Can be called from the parser
432 -- or the semantic analyzer.
434 procedure Error_Msg_S (Msg : String);
435 -- Output a message at current scan pointer location. This routine can be
436 -- called only from the parser, since it references Scan_Ptr.
438 procedure Error_Msg_AP (Msg : String);
439 -- Output a message just after the previous token. This routine can be
440 -- called only from the parser, since it references Prev_Token_Ptr.
442 procedure Error_Msg_BC (Msg : String);
443 -- Output a message just before the current token. Note that the important
444 -- difference between this and the previous routine is that the BC case
445 -- posts a flag on the current line, whereas AP can post a flag at the
446 -- end of the preceding line. This routine can be called only from the
447 -- parser, since it references Token_Ptr.
449 procedure Error_Msg_SC (Msg : String);
450 -- Output a message at the start of the current token, unless we are at
451 -- the end of file, in which case we always output the message after the
452 -- last real token in the file. This routine can be called only from the
453 -- parser, since it references Token_Ptr.
455 procedure Error_Msg_SP (Msg : String);
456 -- Output a message at the start of the previous token. This routine can
457 -- be called only from the parser, since it references Prev_Token_Ptr.
459 procedure Error_Msg_N (Msg : String; N : Node_Or_Entity_Id);
460 -- Output a message at the Sloc of the given node. This routine can be
461 -- called from the parser or the semantic analyzer, although the call
462 -- from the latter is much more common (and is the most usual way of
463 -- generating error messages from the analyzer). The message text may
464 -- contain a single & insertion, which will reference the given node.
466 procedure Error_Msg_NE
468 N : Node_Or_Entity_Id;
469 E : Node_Or_Entity_Id);
470 -- Output a message at the Sloc of the given node, with an insertion of
471 -- the name from the given entity node. This is used by the semantic
472 -- routines, where this is a common error message situation. The Msg
473 -- text will contain a & or } as usual to mark the insertion point.
474 -- This routine can be called from the parser or the analyzer.
476 procedure Change_Error_Text (Error_Id : Error_Msg_Id; New_Msg : String);
477 -- The error message text of the message identified by Id is replaced by
478 -- the given text. This text may contain insertion characters in the
479 -- usual manner, and need not be the same length as the original text.
481 procedure Purge_Messages (From : Source_Ptr; To : Source_Ptr);
482 -- All error messages whose location is in the range From .. To (not
483 -- including the end points) will be deleted from the error listing.
485 procedure Remove_Warning_Messages (N : Node_Id);
486 -- Remove any warning messages corresponding to the Sloc of N or any
487 -- of its descendent nodes. No effect if no such warnings.
489 procedure Set_Warnings_Mode_Off (Loc : Source_Ptr);
490 -- Called in response to a pragma Warnings (Off) to record the source
491 -- location from which warnings are to be turned off.
493 procedure Set_Warnings_Mode_On (Loc : Source_Ptr);
494 -- Called in response to a pragma Warnings (On) to record the source
495 -- location from which warnings are to be turned back on.
497 function Compilation_Errors return Boolean;
498 -- Returns true if errors have been detected, or warnings in -gnatwe
499 -- (treat warnings as errors) mode.
501 procedure dmsg (Id : Error_Msg_Id);
502 -- Debugging routine to dump an error message