1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--                              E R R O U T C                               --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 1992-2013, 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 packages contains global variables and routines common to error
27--  reporting packages, including Errout and Prj.Err.
28
29with Table;
30with Types; use Types;
31
32package Erroutc is
33
34   Class_Flag : Boolean := False;
35   --  This flag is set True when outputting a reference to a class-wide
36   --  type, and is used by Add_Class to insert 'Class at the proper point
37
38   Continuation : Boolean := False;
39   --  Indicates if current message is a continuation. Initialized from the
40   --  Msg_Cont parameter in Error_Msg_Internal and then set True if a \
41   --  insertion character is encountered.
42
43   Continuation_New_Line : Boolean := False;
44   --  Indicates if current message was a continuation line marked with \\ to
45   --  force a new line. Set True if \\ encountered.
46
47   Flag_Source : Source_File_Index;
48   --  Source file index for source file where error is being posted
49
50   Has_Double_Exclam : Boolean := False;
51   --  Set true to indicate that the current message contains the insertion
52   --  sequence !! (force warnings even in non-main unit source files).
53
54   Is_Serious_Error : Boolean := False;
55   --  Set True for a serious error (i.e. any message that is not a warning
56   --  or style message, and that does not contain a | insertion character).
57
58   Is_Unconditional_Msg : Boolean := False;
59   --  Set True to indicate that the current message contains the insertion
60   --  character ! and is thus to be treated as an unconditional message.
61
62   Is_Warning_Msg : Boolean := False;
63   --  Set True to indicate if current message is warning message (contains ?)
64
65   Warning_Msg_Char : Character;
66   --  Warning character, valid only if Is_Warning_Msg is True
67   --    ' '      -- ? appeared on its own in message
68   --    '?'      -- ?? appeared in message
69   --    'x'      -- ?x? appeared in message
70   --    'X'      -- ?x? appeared in message (X is upper case of x)
71
72   Is_Style_Msg : Boolean := False;
73   --  Set True to indicate if the current message is a style message
74   --  (i.e. a message whose text starts with the characters "(style)").
75
76   Kill_Message : Boolean := False;
77   --  A flag used to kill weird messages (e.g. those containing uninterpreted
78   --  implicit type references) if we have already seen at least one message
79   --  already. The idea is that we hope the weird message is a junk cascaded
80   --  message that should be suppressed.
81
82   Last_Killed : Boolean := False;
83   --  Set True if the most recently posted non-continuation message was
84   --  killed. This is used to determine the processing of any continuation
85   --  messages that follow.
86
87   List_Pragmas_Index : Int := 0;
88   --  Index into List_Pragmas table
89
90   List_Pragmas_Mode : Boolean := False;
91   --  Starts True, gets set False by pragma List (Off), True by List (On)
92
93   Manual_Quote_Mode : Boolean := False;
94   --  Set True in manual quotation mode
95
96   Max_Msg_Length : constant := 1024 + 2 * Int (Column_Number'Last);
97   --  Maximum length of error message. The addition of 2 * Column_Number'Last
98   --  ensures that two insertion tokens of maximum length can be accommodated.
99   --  The value of 1024 is an arbitrary value that should be more than long
100   --  enough to accommodate any reasonable message (and for that matter, some
101   --  pretty unreasonable messages).
102
103   Msg_Buffer : String (1 .. Max_Msg_Length);
104   --  Buffer used to prepare error messages
105
106   Msglen : Integer := 0;
107   --  Number of characters currently stored in the message buffer
108
109   Suppress_Message : Boolean;
110   --  A flag used to suppress certain obviously redundant messages (i.e.
111   --  those referring to a node whose type is Any_Type). This suppression
112   --  is effective only if All_Errors_Mode is off.
113
114   Suppress_Instance_Location : Boolean := False;
115   --  Normally, if a # location in a message references a location within
116   --  a generic template, then a note is added giving the location of the
117   --  instantiation. If this variable is set True, then this note is not
118   --  output. This is used for internal processing for the case of an
119   --  illegal instantiation. See Error_Msg routine for further details.
120
121   ----------------------------
122   -- Message ID Definitions --
123   ----------------------------
124
125   type Error_Msg_Id is new Int;
126   --  A type used to represent specific error messages. Used by the clients
127   --  of this package only in the context of the Get_Error_Id and
128   --  Change_Error_Text subprograms.
129
130   No_Error_Msg : constant Error_Msg_Id := 0;
131   --  A constant which is different from any value returned by Get_Error_Id.
132   --  Typically used by a client to indicate absence of a saved Id value.
133
134   Cur_Msg : Error_Msg_Id := No_Error_Msg;
135   --  Id of most recently posted error message
136
137   function Get_Msg_Id return Error_Msg_Id;
138   --  Returns the Id of the message most recently posted using one of the
139   --  Error_Msg routines.
140
141   function Get_Location (E : Error_Msg_Id) return Source_Ptr;
142   --  Returns the flag location of the error message with the given id E
143
144   -----------------------------------
145   -- Error Message Data Structures --
146   -----------------------------------
147
148   --  The error messages are stored as a linked list of error message objects
149   --  sorted into ascending order by the source location (Sloc). Each object
150   --  records the text of the message and its source location.
151
152   --  The following record type and table are used to represent error
153   --  messages, with one entry in the table being allocated for each message.
154
155   type Error_Msg_Object is record
156      Text : String_Ptr;
157      --  Text of error message, fully expanded with all insertions
158
159      Next : Error_Msg_Id;
160      --  Pointer to next message in error chain. A value of No_Error_Msg
161      --  indicates the end of the chain.
162
163      Prev : Error_Msg_Id;
164      --  Pointer to previous message in error chain. Only set during the
165      --  Finalize procedure. A value of No_Error_Msg indicates the first
166      --  message in the chain.
167
168      Sfile : Source_File_Index;
169      --  Source table index of source file. In the case of an error that
170      --  refers to a template, always references the original template
171      --  not an instantiation copy.
172
173      Sptr : Source_Ptr;
174      --  Flag pointer. In the case of an error that refers to a template,
175      --  always references the original template, not an instantiation copy.
176      --  This value is the actual place in the source that the error message
177      --  will be posted. Note that an error placed on an instantiation will
178      --  have Sptr pointing to the instantiation point.
179
180      Optr : Source_Ptr;
181      --  Flag location used in the call to post the error. This is normally
182      --  the same as Sptr, except when an error is posted on a particular
183      --  instantiation of a generic. In such a case, Sptr will point to
184      --  the original source location of the instantiation itself, but
185      --  Optr will point to the template location (more accurately to the
186      --  template copy in the instantiation copy corresponding to the
187      --  instantiation referenced by Sptr).
188
189      Line : Physical_Line_Number;
190      --  Line number for error message
191
192      Col : Column_Number;
193      --  Column number for error message
194
195      Warn : Boolean;
196      --  True if warning message (i.e. insertion character ? appeared)
197
198      Warn_Err : Boolean;
199      --  True if this is a warning message which is to be treated as an error
200      --  as a result of a match with a Warning_As_Error pragma.
201
202      Warn_Chr : Character;
203      --  Warning character (note: set even if Warning_Doc_Switch is False)
204      --    ' '      -- ? appeared on its own in message or no ? in message
205      --    '?'      -- ?? appeared in message
206      --    'x'      -- ?x? appeared in message
207      --    'X'      -- ?x? appeared in message (X is upper case of x)
208
209      Style : Boolean;
210      --  True if style message (starts with "(style)")
211
212      Serious : Boolean;
213      --  True if serious error message (not a warning and no | character)
214
215      Uncond : Boolean;
216      --  True if unconditional message (i.e. insertion character ! appeared)
217
218      Msg_Cont : Boolean;
219      --  This is used for logical messages that are composed of multiple
220      --  individual messages. For messages that are not part of such a
221      --  group, or that are the first message in such a group. Msg_Cont
222      --  is set to False. For subsequent messages in a group, Msg_Cont
223      --  is set to True. This is used to make sure that such a group of
224      --  messages is either suppressed or retained as a group (e.g. in
225      --  the circuit that deletes identical messages).
226
227      Deleted : Boolean;
228      --  If this flag is set, the message is not printed. This is used
229      --  in the circuit for deleting duplicate/redundant error messages.
230   end record;
231
232   package Errors is new Table.Table (
233     Table_Component_Type => Error_Msg_Object,
234     Table_Index_Type     => Error_Msg_Id,
235     Table_Low_Bound      => 1,
236     Table_Initial        => 200,
237     Table_Increment      => 200,
238     Table_Name           => "Error");
239
240   First_Error_Msg : Error_Msg_Id;
241   --  The list of error messages, i.e. the first entry on the list of error
242   --  messages. This is not the same as the physically first entry in the
243   --  error message table, since messages are not always inserted in sequence.
244
245   Last_Error_Msg : Error_Msg_Id;
246   --  The last entry on the list of error messages. Note: this is not the same
247   --  as the physically last entry in the error message table, since messages
248   --  are not always inserted in sequence.
249
250   --------------------------
251   -- Warning Mode Control --
252   --------------------------
253
254   --  Pragma Warnings allows warnings to be turned off for a specified region
255   --  of code, and the following tables are the data structures used to keep
256   --  track of these regions.
257
258   --  The first table is used for the basic command line control, and for the
259   --  forms of Warning with a single ON or OFF parameter.
260
261   --  It contains pairs of source locations, the first being the start
262   --  location for a warnings off region, and the second being the end
263   --  location. When a pragma Warnings (Off) is encountered, a new entry is
264   --  established extending from the location of the pragma to the end of the
265   --  current source file. A subsequent pragma Warnings (On) adjusts the end
266   --  point of this entry appropriately.
267
268   --  If all warnings are suppressed by command switch, then there is a dummy
269   --  entry (put there by Errout.Initialize) at the start of the table which
270   --  covers all possible Source_Ptr values. Note that the source pointer
271   --  values in this table always reference the original template, not an
272   --  instantiation copy, in the generic case.
273
274   --  Reason is the reason from the pragma Warnings (Off,..) or the null
275   --  string if no reason parameter is given.
276
277   type Warnings_Entry is record
278      Start  : Source_Ptr;
279      Stop   : Source_Ptr;
280      Reason : String_Id;
281   end record;
282
283   package Warnings is new Table.Table (
284     Table_Component_Type => Warnings_Entry,
285     Table_Index_Type     => Natural,
286     Table_Low_Bound      => 1,
287     Table_Initial        => 100,
288     Table_Increment      => 200,
289     Table_Name           => "Warnings");
290
291   --  The second table is used for the specific forms of the pragma, where
292   --  the first argument is ON or OFF, and the second parameter is a string
293   --  which is the pattern to match for suppressing a warning.
294
295   type Specific_Warning_Entry is record
296      Start : Source_Ptr;
297      Stop  : Source_Ptr;
298      --  Starting and ending source pointers for the range. These are always
299      --  from the same source file.
300
301      Reason : String_Id;
302      --  Reason string from pragma Warnings, or null string if none
303
304      Msg : String_Ptr;
305      --  Message from pragma Warnings (Off, string)
306
307      Open : Boolean;
308      --  Set to True if OFF has been encountered with no matching ON
309
310      Used : Boolean;
311      --  Set to True if entry has been used to suppress a warning
312
313      Config : Boolean;
314      --  True if pragma is configuration pragma (in which case no matching Off
315      --  pragma is required, and it is not required that a specific warning be
316      --  suppressed).
317   end record;
318
319   package Specific_Warnings is new Table.Table (
320     Table_Component_Type => Specific_Warning_Entry,
321     Table_Index_Type     => Natural,
322     Table_Low_Bound      => 1,
323     Table_Initial        => 100,
324     Table_Increment      => 200,
325     Table_Name           => "Specific_Warnings");
326
327   --  Note on handling configuration case versus specific case. A complication
328   --  arises from this example:
329
330   --     pragma Warnings (Off, "not referenced*");
331   --     procedure Mumble (X : Integer) is
332   --     pragma Warnings (On, "not referenced*");
333   --     begin
334   --        null;
335   --     end Mumble;
336
337   --  The trouble is that the first pragma is technically a configuration
338   --  pragma, and yet it is clearly being used in the context of thinking of
339   --  it as a specific case. To deal with this, what we do is that the On
340   --  entry can match a configuration pragma from the same file, and if we
341   --  find such an On entry, we cancel the indication of it being the
342   --  configuration case. This seems to handle all cases we run into ok.
343
344   -----------------
345   -- Subprograms --
346   -----------------
347
348   procedure Add_Class;
349   --  Add 'Class to buffer for class wide type case (Class_Flag set)
350
351   function Buffer_Ends_With (C : Character) return Boolean;
352   --  Tests if message buffer ends with given character
353
354   function Buffer_Ends_With (S : String) return Boolean;
355   --  Tests if message buffer ends with given string preceded by a space
356
357   procedure Buffer_Remove (C : Character);
358   --  Remove given character fron end of buffer if it is present
359
360   procedure Buffer_Remove (S : String);
361   --  Removes given string from end of buffer if it is present at end of
362   --  buffer, and preceded by a space.
363
364   function Compilation_Errors return Boolean;
365   --  Returns true if errors have been detected, or warnings in -gnatwe
366   --  (treat warnings as errors) mode.
367
368   procedure dmsg (Id : Error_Msg_Id);
369   --  Debugging routine to dump an error message
370
371   procedure Debug_Output (N : Node_Id);
372   --  Called from Error_Msg_N and Error_Msg_NE to generate line of debug
373   --  output giving node number (of node N) if the debug X switch is set.
374
375   procedure Check_Duplicate_Message (M1, M2 : Error_Msg_Id);
376   --  This function is passed the Id values of two error messages. If either
377   --  M1 or M2 is a continuation message, or is already deleted, the call is
378   --  ignored. Otherwise a check is made to see if M1 and M2 are duplicated or
379   --  redundant. If so, the message to be deleted and all its continuations
380   --  are marked with the Deleted flag set to True.
381
382   function Get_Warning_Tag (Id : Error_Msg_Id) return String;
383   --  Given an error message ID, return tag showing warning message class, or
384   --  the null string if this option is not enabled or this is not a warning.
385
386   procedure Output_Error_Msgs (E : in out Error_Msg_Id);
387   --  Output source line, error flag, and text of stored error message and all
388   --  subsequent messages for the same line and unit. On return E is set to be
389   --  one higher than the last message output.
390
391   procedure Output_Line_Number (L : Logical_Line_Number);
392   --  Output a line number as six digits (with leading zeroes suppressed),
393   --  followed by a period and a blank (note that this is 8 characters which
394   --  means that tabs in the source line will not get messed up). Line numbers
395   --  that match or are less than the last Source_Reference pragma are listed
396   --  as all blanks, avoiding output of junk line numbers.
397
398   procedure Output_Msg_Text (E : Error_Msg_Id);
399   --  Outputs characters of text in the text of the error message E. Note that
400   --  no end of line is output, the caller is responsible for adding the end
401   --  of line. If Error_Msg_Line_Length is non-zero, this is the routine that
402   --  splits the line generating multiple lines of output, and in this case
403   --  the last line has no terminating end of line character.
404
405   procedure Purge_Messages (From : Source_Ptr; To : Source_Ptr);
406   --  All error messages whose location is in the range From .. To (not
407   --  including the end points) will be deleted from the error listing.
408
409   function Same_Error (M1, M2 : Error_Msg_Id) return Boolean;
410   --  See if two messages have the same text. Returns true if the text of the
411   --  two messages is identical, or if one of them is the same as the other
412   --  with an appended "instance at xxx" tag.
413
414   procedure Set_Msg_Blank;
415   --  Sets a single blank in the message if the preceding character is a
416   --  non-blank character other than a left parenthesis or minus. Has no
417   --  effect if manual quote mode is turned on.
418
419   procedure Set_Msg_Blank_Conditional;
420   --  Sets a single blank in the message if the preceding character is a
421   --  non-blank character other than a left parenthesis or quote. Has no
422   --  effect if manual quote mode is turned on.
423
424   procedure Set_Msg_Char (C : Character);
425   --  Add a single character to the current message. This routine does not
426   --  check for special insertion characters (they are just treated as text
427   --  characters if they occur).
428
429   procedure Set_Msg_Insertion_File_Name;
430   --  Handle file name insertion (left brace insertion character)
431
432   procedure Set_Msg_Insertion_Line_Number (Loc, Flag : Source_Ptr);
433   --  Handle line number insertion (# insertion character). Loc is the
434   --  location to be referenced, and Flag is the location at which the
435   --  flag is posted (used to determine whether to add "in file xxx")
436
437   procedure Set_Msg_Insertion_Name_Literal;
438
439   procedure Set_Msg_Insertion_Name;
440   --  Handle name insertion (% insertion character)
441
442   procedure Set_Msg_Insertion_Reserved_Name;
443   --  Handle insertion of reserved word name (* insertion character)
444
445   procedure Set_Msg_Insertion_Reserved_Word
446     (Text : String;
447      J    : in out Integer);
448   --  Handle reserved word insertion (upper case letters). The Text argument
449   --  is the current error message input text, and J is an index which on
450   --  entry points to the first character of the reserved word, and on exit
451   --  points past the last character of the reserved word. Note that RM and
452   --  SPARK are treated specially and not considered to be keywords.
453
454   procedure Set_Msg_Insertion_Run_Time_Name;
455   --  If package System contains a definition for Run_Time_Name (see package
456   --  Targparm for details), then this procedure will insert a message of
457   --  the form (name) into the current error message, with name set in mixed
458   --  case (upper case after any spaces). If no run time name is defined,
459   --  then this routine has no effect).
460
461   procedure Set_Msg_Insertion_Uint;
462   --  Handle Uint insertion (^ insertion character)
463
464   procedure Set_Msg_Int (Line : Int);
465   --  Set the decimal representation of the argument in the error message
466   --  buffer with no leading zeroes output.
467
468   procedure Set_Msg_Name_Buffer;
469   --  Output name from Name_Buffer, with surrounding quotes unless manual
470   --  quotation mode is in effect.
471
472   procedure Set_Msg_Quote;
473   --  Set quote if in normal quote mode, nothing if in manual quote mode
474
475   procedure Set_Msg_Str (Text : String);
476   --  Add a sequence of characters to the current message. This routine does
477   --  not check for special insertion characters (they are just treated as
478   --  text characters if they occur).
479
480   procedure Set_Next_Non_Deleted_Msg (E : in out Error_Msg_Id);
481   --  Given a message id, move to next message id, but skip any deleted
482   --  messages, so that this results in E on output being the first non-
483   --  deleted message following the input value of E, or No_Error_Msg if
484   --  the input value of E was either already No_Error_Msg, or was the
485   --  last non-deleted message.
486
487   procedure Set_Specific_Warning_Off
488     (Loc    : Source_Ptr;
489      Msg    : String;
490      Reason : String_Id;
491      Config : Boolean;
492      Used   : Boolean := False);
493   --  This is called in response to the two argument form of pragma Warnings
494   --  where the first argument is OFF, and the second argument is a string
495   --  which identifies a specific warning to be suppressed. The first argument
496   --  is the start of the suppression range, and the second argument is the
497   --  string from the pragma. Loc is the location of the pragma (which is the
498   --  start of the range to suppress). Reason is the reason string from the
499   --  pragma, or the null string if no reason is given. Config is True for the
500   --  configuration pragma case (where there is no requirement for a matching
501   --  OFF pragma). Used is set True to disable the check that the warning
502   --  actually has has the effect of suppressing a warning.
503
504   procedure Set_Specific_Warning_On
505     (Loc : Source_Ptr;
506      Msg : String;
507      Err : out Boolean);
508   --  This is called in response to the two argument form of pragma Warnings
509   --  where the first argument is ON, and the second argument is a string
510   --  which identifies a specific warning to be suppressed. The first argument
511   --  is the end of the suppression range, and the second argument is the
512   --  string from the pragma. Err is set to True on return to report the error
513   --  of no matching Warnings Off pragma preceding this one.
514
515   procedure Set_Warnings_Mode_Off (Loc : Source_Ptr; Reason : String_Id);
516   --  Called in response to a pragma Warnings (Off) to record the source
517   --  location from which warnings are to be turned off. Reason is the
518   --  Reason from the pragma, or the null string if none is given.
519
520   procedure Set_Warnings_Mode_On (Loc : Source_Ptr);
521   --  Called in response to a pragma Warnings (On) to record the source
522   --  location from which warnings are to be turned back on.
523
524   procedure Test_Style_Warning_Serious_Unconditional_Msg (Msg : String);
525   --  Scans message text and sets the following variables:
526   --
527   --    Is_Warning_Msg is set True if Msg is a warning message (contains a
528   --    question mark character), and False otherwise.
529   --
530   --    Is_Style_Msg is set True if Msg is a style message (starts with
531   --    "(style)") and False otherwise.
532   --
533   --    Is_Serious_Error is set to True unless the message is a warning or
534   --    style message or contains the character | (non-serious error).
535   --
536   --    Is_Unconditional_Msg is set True if the message contains the character
537   --    ! and is otherwise set False.
538   --
539   --    Has_Double_Exclam is set True if the message contains the sequence !!
540   --    and is otherwise set False.
541   --
542   --  Note that the call has no effect for continuation messages (those whose
543   --  first character is '\'), and all variables are left unchanged.
544
545   function Warnings_Suppressed (Loc : Source_Ptr) return String_Id;
546   --  Determines if given location is covered by a warnings off suppression
547   --  range in the warnings table (or is suppressed by compilation option,
548   --  which generates a warning range for the whole source file). This routine
549   --  only deals with the general ON/OFF case, not specific warnings. The
550   --  returned result is No_String if warnings are not suppressed. If warnings
551   --  are suppressed for the given location, then then corresponding Reason
552   --  parameter from the pragma is returned (or the null string if no Reason
553   --  parameter was present).
554
555   function Warning_Specifically_Suppressed
556     (Loc : Source_Ptr;
557      Msg : String_Ptr) return String_Id;
558   --  Determines if given message to be posted at given location is suppressed
559   --  by specific ON/OFF Warnings pragmas specifying this particular message.
560   --  If the warning is not suppressed then No_String is returned, otherwise
561   --  the corresponding warning string is returned (or the null string if no
562   --  Warning argument was present in the pragma).
563
564   function Warning_Treated_As_Error (Msg : String) return Boolean;
565   --  Returns True if the warning message Msg matches any of the strings
566   --  given by Warning_As_Error pragmas, as stored in the Warnings_As_Errors
567   --  table by Set_Warning_As_Error.
568
569   type Error_Msg_Proc is
570     access procedure (Msg : String; Flag_Location : Source_Ptr);
571   procedure Validate_Specific_Warnings (Eproc : Error_Msg_Proc);
572   --  Checks that specific warnings are consistent (for non-configuration
573   --  case, properly closed, and used). The argument is a pointer to the
574   --  Error_Msg procedure to be called if any inconsistencies are detected.
575
576end Erroutc;
577