OSDN Git Service

2009-04-09 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / errout.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                               E R R O U T                                --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2008, 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 --  Warning! Error messages can be generated during Gigi processing by direct
27 --  calls to error message routines, so it is essential that the processing
28 --  in this body be consistent with the requirements for the Gigi processing
29 --  environment, and that in particular, no disallowed table expansion is
30 --  allowed to occur.
31
32 with Atree;    use Atree;
33 with Casing;   use Casing;
34 with Csets;    use Csets;
35 with Debug;    use Debug;
36 with Einfo;    use Einfo;
37 with Erroutc;  use Erroutc;
38 with Fname;    use Fname;
39 with Gnatvsn;  use Gnatvsn;
40 with Hostparm; use Hostparm;
41 with Lib;      use Lib;
42 with Opt;      use Opt;
43 with Nlists;   use Nlists;
44 with Output;   use Output;
45 with Scans;    use Scans;
46 with Sinput;   use Sinput;
47 with Sinfo;    use Sinfo;
48 with Snames;   use Snames;
49 with Stand;    use Stand;
50 with Style;
51 with Uname;    use Uname;
52
53 package body Errout is
54
55    Errors_Must_Be_Ignored : Boolean := False;
56    --  Set to True by procedure Set_Ignore_Errors (True), when calls to
57    --  error message procedures should be ignored (when parsing irrelevant
58    --  text in sources being preprocessed).
59
60    Finalize_Called : Boolean := False;
61    --  Set True if the Finalize routine has been called
62
63    Warn_On_Instance : Boolean;
64    --  Flag set true for warning message to be posted on instance
65
66    ------------------------------------
67    -- Table of Non-Instance Messages --
68    ------------------------------------
69
70    --  This table contains an entry for every error message processed by the
71    --  Error_Msg routine that is not posted on generic (or inlined) instance.
72    --  As explained in further detail in the Error_Msg procedure body, this
73    --  table is used to avoid posting redundant messages on instances.
74
75    type NIM_Record is record
76       Msg : String_Ptr;
77       Loc : Source_Ptr;
78    end record;
79    --  Type used to store text and location of one message
80
81    package Non_Instance_Msgs is new Table.Table (
82      Table_Component_Type => NIM_Record,
83      Table_Index_Type     => Int,
84      Table_Low_Bound      => 1,
85      Table_Initial        => 100,
86      Table_Increment      => 100,
87      Table_Name           => "Non_Instance_Msgs");
88
89    -----------------------
90    -- Local Subprograms --
91    -----------------------
92
93    procedure Error_Msg_Internal
94      (Msg      : String;
95       Sptr     : Source_Ptr;
96       Optr     : Source_Ptr;
97       Msg_Cont : Boolean);
98    --  This is the low level routine used to post messages after dealing with
99    --  the issue of messages placed on instantiations (which get broken up
100    --  into separate calls in Error_Msg). Sptr is the location on which the
101    --  flag will be placed in the output. In the case where the flag is on
102    --  the template, this points directly to the template, not to one of the
103    --  instantiation copies of the template. Optr is the original location
104    --  used to flag the error, and this may indeed point to an instantiation
105    --  copy. So typically we can see Optr pointing to the template location
106    --  in an instantiation copy when Sptr points to the source location of
107    --  the actual instantiation (i.e the line with the new). Msg_Cont is
108    --  set true if this is a continuation message.
109
110    function No_Warnings (N : Node_Or_Entity_Id) return Boolean;
111    --  Determines if warnings should be suppressed for the given node
112
113    function OK_Node (N : Node_Id) return Boolean;
114    --  Determines if a node is an OK node to place an error message on (return
115    --  True) or if the error message should be suppressed (return False). A
116    --  message is suppressed if the node already has an error posted on it,
117    --  or if it refers to an Etype that has an error posted on it, or if
118    --  it references an Entity that has an error posted on it.
119
120    procedure Output_Source_Line
121      (L     : Physical_Line_Number;
122       Sfile : Source_File_Index;
123       Errs  : Boolean);
124    --  Outputs text of source line L, in file S, together with preceding line
125    --  number, as described above for Output_Line_Number. The Errs parameter
126    --  indicates if there are errors attached to the line, which forces
127    --  listing on, even in the presence of pragma List (Off).
128
129    procedure Set_Msg_Insertion_Column;
130    --  Handle column number insertion (@ insertion character)
131
132    procedure Set_Msg_Insertion_Node;
133    --  Handle node (name from node) insertion (& insertion character)
134
135    procedure Set_Msg_Insertion_Type_Reference (Flag : Source_Ptr);
136    --  Handle type reference (right brace insertion character). Flag is the
137    --  location of the flag, which is provided for the internal call to
138    --  Set_Msg_Insertion_Line_Number,
139
140    procedure Set_Msg_Insertion_Unit_Name (Suffix : Boolean := True);
141    --  Handle unit name insertion ($ insertion character). Depending on Boolean
142    --  parameter Suffix, (spec) or (body) is appended after the unit name.
143
144    procedure Set_Msg_Node (Node : Node_Id);
145    --  Add the sequence of characters for the name associated with the
146    --  given node to the current message.
147
148    procedure Set_Msg_Text (Text : String; Flag : Source_Ptr);
149    --  Add a sequence of characters to the current message. The characters may
150    --  be one of the special insertion characters (see documentation in spec).
151    --  Flag is the location at which the error is to be posted, which is used
152    --  to determine whether or not the # insertion needs a file name. The
153    --  variables Msg_Buffer, Msglen, Is_Style_Msg, Is_Warning_Msg, and
154    --  Is_Unconditional_Msg are set on return.
155
156    procedure Set_Posted (N : Node_Id);
157    --  Sets the Error_Posted flag on the given node, and all its parents
158    --  that are subexpressions and then on the parent non-subexpression
159    --  construct that contains the original expression (this reduces the
160    --  number of cascaded messages). Note that this call only has an effect
161    --  for a serious error. For a non-serious error, it has no effect.
162
163    procedure Set_Qualification (N : Nat; E : Entity_Id);
164    --  Outputs up to N levels of qualification for the given entity. For
165    --  example, the entity A.B.C.D will output B.C. if N = 2.
166
167    function Special_Msg_Delete
168      (Msg : String;
169       N   : Node_Or_Entity_Id;
170       E   : Node_Or_Entity_Id) return Boolean;
171    --  This function is called from Error_Msg_NEL, passing the message Msg,
172    --  node N on which the error is to be posted, and the entity or node E
173    --  to be used for an & insertion in the message if any. The job of this
174    --  procedure is to test for certain cascaded messages that we would like
175    --  to suppress. If the message is to be suppressed then we return True.
176    --  If the message should be generated (the normal case) False is returned.
177
178    procedure Unwind_Internal_Type (Ent : in out Entity_Id);
179    --  This procedure is given an entity id for an internal type, i.e.
180    --  a type with an internal name. It unwinds the type to try to get
181    --  to something reasonably printable, generating prefixes like
182    --  "subtype of", "access to", etc along the way in the buffer. The
183    --  value in Ent on return is the final name to be printed. Hopefully
184    --  this is not an internal name, but in some internal name cases, it
185    --  is an internal name, and has to be printed anyway (although in this
186    --  case the message has been killed if possible). The global variable
187    --  Class_Flag is set to True if the resulting entity should have
188    --  'Class appended to its name (see Add_Class procedure), and is
189    --  otherwise unchanged.
190
191    procedure VMS_Convert;
192    --  This procedure has no effect if called when the host is not OpenVMS.
193    --  If the host is indeed OpenVMS, then the error message stored in
194    --  Msg_Buffer is scanned for appearances of switch names which need
195    --  converting to corresponding VMS qualifier names. See Gnames/Vnames
196    --  table in Errout spec for precise definition of the conversion that
197    --  is performed by this routine in OpenVMS mode.
198
199    -----------------------
200    -- Change_Error_Text --
201    -----------------------
202
203    procedure Change_Error_Text (Error_Id : Error_Msg_Id; New_Msg : String) is
204       Save_Next : Error_Msg_Id;
205       Err_Id    : Error_Msg_Id := Error_Id;
206
207    begin
208       Set_Msg_Text (New_Msg, Errors.Table (Error_Id).Sptr);
209       Errors.Table (Error_Id).Text := new String'(Msg_Buffer (1 .. Msglen));
210
211       --  If in immediate error message mode, output modified error message now
212       --  This is just a bit tricky, because we want to output just a single
213       --  message, and the messages we modified is already linked in. We solve
214       --  this by temporarily resetting its forward pointer to empty.
215
216       if Debug_Flag_OO then
217          Save_Next := Errors.Table (Error_Id).Next;
218          Errors.Table (Error_Id).Next := No_Error_Msg;
219          Write_Eol;
220          Output_Source_Line
221            (Errors.Table (Error_Id).Line, Errors.Table (Error_Id).Sfile, True);
222          Output_Error_Msgs (Err_Id);
223          Errors.Table (Error_Id).Next := Save_Next;
224       end if;
225    end Change_Error_Text;
226
227    ------------------------
228    -- Compilation_Errors --
229    ------------------------
230
231    function Compilation_Errors return Boolean is
232    begin
233       if not Finalize_Called then
234          raise Program_Error;
235       else
236          return Erroutc.Compilation_Errors;
237       end if;
238    end Compilation_Errors;
239
240    ---------------
241    -- Error_Msg --
242    ---------------
243
244    --  Error_Msg posts a flag at the given location, except that if the
245    --  Flag_Location points within a generic template and corresponds
246    --  to an instantiation of this generic template, then the actual
247    --  message will be posted on the generic instantiation, along with
248    --  additional messages referencing the generic declaration.
249
250    procedure Error_Msg (Msg : String; Flag_Location : Source_Ptr) is
251       Sindex : Source_File_Index;
252       --  Source index for flag location
253
254       Orig_Loc : Source_Ptr;
255       --  Original location of Flag_Location (i.e. location in original
256       --  template in instantiation case, otherwise unchanged).
257
258    begin
259       --  It is a fatal error to issue an error message when scanning from
260       --  the internal source buffer (see Sinput for further documentation)
261
262       pragma Assert (Sinput.Source /= Internal_Source_Ptr);
263
264       --  Return if all errors are to be ignored
265
266       if Errors_Must_Be_Ignored then
267          return;
268       end if;
269
270       --  If we already have messages, and we are trying to place a message
271       --  at No_Location or in package Standard, then just ignore the attempt
272       --  since we assume that what is happening is some cascaded junk. Note
273       --  that this is safe in the sense that proceeding will surely bomb.
274
275       if Flag_Location < First_Source_Ptr
276         and then Total_Errors_Detected > 0
277       then
278          return;
279       end if;
280
281       --  Start processing of new message
282
283       Sindex := Get_Source_File_Index (Flag_Location);
284       Test_Style_Warning_Serious_Msg (Msg);
285       Orig_Loc := Original_Location (Flag_Location);
286
287       --  If the current location is in an instantiation, the issue arises
288       --  of whether to post the message on the template or the instantiation.
289
290       --  The way we decide is to see if we have posted the same message
291       --  on the template when we compiled the template (the template is
292       --  always compiled before any instantiations). For this purpose,
293       --  we use a separate table of messages. The reason we do this is
294       --  twofold:
295
296       --     First, the messages can get changed by various processing
297       --     including the insertion of tokens etc, making it hard to
298       --     do the comparison.
299
300       --     Second, we will suppress a warning on a template if it is
301       --     not in the current extended source unit. That's reasonable
302       --     and means we don't want the warning on the instantiation
303       --     here either, but it does mean that the main error table
304       --     would not in any case include the message.
305
306       if Flag_Location = Orig_Loc then
307          Non_Instance_Msgs.Append ((new String'(Msg), Flag_Location));
308          Warn_On_Instance := False;
309
310       --  Here we have an instance message
311
312       else
313          --  Delete if debug flag off, and this message duplicates a
314          --  message already posted on the corresponding template
315
316          if not Debug_Flag_GG then
317             for J in Non_Instance_Msgs.First .. Non_Instance_Msgs.Last loop
318                if Msg = Non_Instance_Msgs.Table (J).Msg.all
319                  and then Non_Instance_Msgs.Table (J).Loc = Orig_Loc
320                then
321                   return;
322                end if;
323             end loop;
324          end if;
325
326          --  No duplicate, so error/warning will be posted on instance
327
328          Warn_On_Instance := Is_Warning_Msg;
329       end if;
330
331       --  Ignore warning message that is suppressed for this location. Note
332       --  that style checks are not considered warning messages for this
333       --  purpose.
334
335       if Is_Warning_Msg and then Warnings_Suppressed (Orig_Loc) then
336          return;
337
338       --  For style messages, check too many messages so far
339
340       elsif Is_Style_Msg
341         and then Maximum_Messages /= 0
342         and then Warnings_Detected >= Maximum_Messages
343       then
344          return;
345       end if;
346
347       --  The idea at this stage is that we have two kinds of messages
348
349       --  First, we have those messages that are to be placed as requested at
350       --  Flag_Location. This includes messages that have nothing to do with
351       --  generics, and also messages placed on generic templates that reflect
352       --  an error in the template itself. For such messages we simply call
353       --  Error_Msg_Internal to place the message in the requested location.
354
355       if Instantiation (Sindex) = No_Location then
356          Error_Msg_Internal (Msg, Flag_Location, Flag_Location, False);
357          return;
358       end if;
359
360       --  If we are trying to flag an error in an instantiation, we may have
361       --  a generic contract violation. What we generate in this case is:
362
363       --     instantiation error at ...
364       --     original error message
365
366       --  or
367
368       --     warning: in instantiation at
369       --     warning: original warning message
370
371       --  All these messages are posted at the location of the top level
372       --  instantiation. If there are nested instantiations, then the
373       --  instantiation error message can be repeated, pointing to each
374       --  of the relevant instantiations.
375
376       --  Note: the instantiation mechanism is also shared for inlining
377       --  of subprogram bodies when front end inlining is done. In this
378       --  case the messages have the form:
379
380       --     in inlined body at ...
381       --     original error message
382
383       --  or
384
385       --     warning: in inlined body at
386       --     warning: original warning message
387
388       --  OK, this is the case where we have an instantiation error, and
389       --  we need to generate the error on the instantiation, rather than
390       --  on the template.
391
392       declare
393          Actual_Error_Loc : Source_Ptr;
394          --  Location of outer level instantiation in instantiation case, or
395          --  just a copy of Flag_Location in the normal case. This is the
396          --  location where all error messages will actually be posted.
397
398          Save_Error_Msg_Sloc : constant Source_Ptr := Error_Msg_Sloc;
399          --  Save possible location set for caller's message. We need to
400          --  use Error_Msg_Sloc for the location of the instantiation error
401          --  but we have to preserve a possible original value.
402
403          X : Source_File_Index;
404
405          Msg_Cont_Status : Boolean;
406          --  Used to label continuation lines in instantiation case with
407          --  proper Msg_Cont status.
408
409       begin
410          --  Loop to find highest level instantiation, where all error
411          --  messages will be placed.
412
413          X := Sindex;
414          loop
415             Actual_Error_Loc := Instantiation (X);
416             X := Get_Source_File_Index (Actual_Error_Loc);
417             exit when Instantiation (X) = No_Location;
418          end loop;
419
420          --  Since we are generating the messages at the instantiation
421          --  point in any case, we do not want the references to the
422          --  bad lines in the instance to be annotated with the location
423          --  of the instantiation.
424
425          Suppress_Instance_Location := True;
426          Msg_Cont_Status := False;
427
428          --  Loop to generate instantiation messages
429
430          Error_Msg_Sloc := Flag_Location;
431          X := Get_Source_File_Index (Flag_Location);
432
433          while Instantiation (X) /= No_Location loop
434
435             --  Suppress instantiation message on continuation lines
436
437             if Msg (Msg'First) /= '\' then
438
439                --  Case of inlined body
440
441                if Inlined_Body (X) then
442                   if Is_Warning_Msg then
443                      Error_Msg_Internal
444                        ("?in inlined body #",
445                         Actual_Error_Loc, Flag_Location, Msg_Cont_Status);
446
447                   else
448                      Error_Msg_Internal
449                        ("error in inlined body #",
450                         Actual_Error_Loc, Flag_Location, Msg_Cont_Status);
451                   end if;
452
453                --  Case of generic instantiation
454
455                else
456                   if Is_Warning_Msg then
457                      Error_Msg_Internal
458                        ("?in instantiation #",
459                         Actual_Error_Loc, Flag_Location, Msg_Cont_Status);
460
461                   else
462                      Error_Msg_Internal
463                        ("instantiation error #",
464                         Actual_Error_Loc, Flag_Location, Msg_Cont_Status);
465                   end if;
466                end if;
467             end if;
468
469             Error_Msg_Sloc := Instantiation (X);
470             X := Get_Source_File_Index (Error_Msg_Sloc);
471             Msg_Cont_Status := True;
472          end loop;
473
474          Suppress_Instance_Location := False;
475          Error_Msg_Sloc := Save_Error_Msg_Sloc;
476
477          --  Here we output the original message on the outer instantiation
478
479          Error_Msg_Internal
480            (Msg, Actual_Error_Loc, Flag_Location, Msg_Cont_Status);
481       end;
482    end Error_Msg;
483
484    ------------------
485    -- Error_Msg_AP --
486    ------------------
487
488    procedure Error_Msg_AP (Msg : String) is
489       S1 : Source_Ptr;
490       C  : Character;
491
492    begin
493       --  If we had saved the Scan_Ptr value after scanning the previous
494       --  token, then we would have exactly the right place for putting
495       --  the flag immediately at hand. However, that would add at least
496       --  two instructions to a Scan call *just* to service the possibility
497       --  of an Error_Msg_AP call. So instead we reconstruct that value.
498
499       --  We have two possibilities, start with Prev_Token_Ptr and skip over
500       --  the current token, which is made harder by the possibility that this
501       --  token may be in error, or start with Token_Ptr and work backwards.
502       --  We used to take the second approach, but it's hard because of
503       --  comments, and harder still because things that look like comments
504       --  can appear inside strings. So now we take the first approach.
505
506       --  Note: in the case where there is no previous token, Prev_Token_Ptr
507       --  is set to Source_First, which is a reasonable position for the
508       --  error flag in this situation.
509
510       S1 := Prev_Token_Ptr;
511       C := Source (S1);
512
513       --  If the previous token is a string literal, we need a special approach
514       --  since there may be white space inside the literal and we don't want
515       --  to stop on that white space.
516
517       --  Note: since this is an error recovery issue anyway, it is not worth
518       --  worrying about special UTF_32 line terminator characters here.
519
520       if Prev_Token = Tok_String_Literal then
521          loop
522             S1 := S1 + 1;
523
524             if Source (S1) = C then
525                S1 := S1 + 1;
526                exit when Source (S1) /= C;
527             elsif Source (S1) in Line_Terminator then
528                exit;
529             end if;
530          end loop;
531
532       --  Character literal also needs special handling
533
534       elsif Prev_Token = Tok_Char_Literal then
535          S1 := S1 + 3;
536
537       --  Otherwise we search forward for the end of the current token, marked
538       --  by a line terminator, white space, a comment symbol or if we bump
539       --  into the following token (i.e. the current token).
540
541       --  Again, it is not worth worrying about UTF_32 special line terminator
542       --  characters in this context, since this is only for error recovery.
543
544       else
545          while Source (S1) not in Line_Terminator
546            and then Source (S1) /= ' '
547            and then Source (S1) /= ASCII.HT
548            and then (Source (S1) /= '-' or else Source (S1 + 1) /= '-')
549            and then S1 /= Token_Ptr
550          loop
551             S1 := S1 + 1;
552          end loop;
553       end if;
554
555       --  S1 is now set to the location for the flag
556
557       Error_Msg (Msg, S1);
558    end Error_Msg_AP;
559
560    ------------------
561    -- Error_Msg_BC --
562    ------------------
563
564    procedure Error_Msg_BC (Msg : String) is
565    begin
566       --  If we are at end of file, post the flag after the previous token
567
568       if Token = Tok_EOF then
569          Error_Msg_AP (Msg);
570
571       --  If we are at start of file, post the flag at the current token
572
573       elsif Token_Ptr = Source_First (Current_Source_File) then
574          Error_Msg_SC (Msg);
575
576       --  If the character before the current token is a space or a horizontal
577       --  tab, then we place the flag on this character (in the case of a tab
578       --  we would really like to place it in the "last" character of the tab
579       --  space, but that it too much trouble to worry about).
580
581       elsif Source (Token_Ptr - 1) = ' '
582          or else Source (Token_Ptr - 1) = ASCII.HT
583       then
584          Error_Msg (Msg, Token_Ptr - 1);
585
586       --  If there is no space or tab before the current token, then there is
587       --  no room to place the flag before the token, so we place it on the
588       --  token instead (this happens for example at the start of a line).
589
590       else
591          Error_Msg (Msg, Token_Ptr);
592       end if;
593    end Error_Msg_BC;
594
595    -------------------
596    -- Error_Msg_CRT --
597    -------------------
598
599    procedure Error_Msg_CRT (Feature : String; N : Node_Id) is
600       CNRT : constant String := " not allowed in no run time mode";
601       CCRT : constant String := " not supported by configuration>";
602
603       S : String (1 .. Feature'Length + 1 + CCRT'Length);
604       L : Natural;
605
606    begin
607       S (1) := '|';
608       S (2 .. Feature'Length + 1) := Feature;
609       L := Feature'Length + 2;
610
611       if No_Run_Time_Mode then
612          S (L .. L + CNRT'Length - 1) := CNRT;
613          L := L + CNRT'Length - 1;
614
615       else pragma Assert (Configurable_Run_Time_Mode);
616          S (L .. L + CCRT'Length - 1) := CCRT;
617          L := L + CCRT'Length - 1;
618       end if;
619
620       Error_Msg_N (S (1 .. L), N);
621       Configurable_Run_Time_Violations := Configurable_Run_Time_Violations + 1;
622    end Error_Msg_CRT;
623
624    -----------------
625    -- Error_Msg_F --
626    -----------------
627
628    procedure Error_Msg_F (Msg : String; N : Node_Id) is
629    begin
630       Error_Msg_NEL (Msg, N, N, Sloc (First_Node (N)));
631    end Error_Msg_F;
632
633    ------------------
634    -- Error_Msg_FE --
635    ------------------
636
637    procedure Error_Msg_FE
638      (Msg : String;
639       N   : Node_Id;
640       E   : Node_Or_Entity_Id)
641    is
642    begin
643       Error_Msg_NEL (Msg, N, E, Sloc (First_Node (N)));
644    end Error_Msg_FE;
645
646    ------------------------
647    -- Error_Msg_Internal --
648    ------------------------
649
650    procedure Error_Msg_Internal
651      (Msg      : String;
652       Sptr     : Source_Ptr;
653       Optr     : Source_Ptr;
654       Msg_Cont : Boolean)
655    is
656       Next_Msg : Error_Msg_Id;
657       --  Pointer to next message at insertion point
658
659       Prev_Msg : Error_Msg_Id;
660       --  Pointer to previous message at insertion point
661
662       Temp_Msg : Error_Msg_Id;
663
664       procedure Handle_Serious_Error;
665       --  Internal procedure to do all error message handling for a serious
666       --  error message, other than bumping the error counts and arranging
667       --  for the message to be output.
668
669       --------------------------
670       -- Handle_Serious_Error --
671       --------------------------
672
673       procedure Handle_Serious_Error is
674       begin
675          --  Turn off code generation if not done already
676
677          if Operating_Mode = Generate_Code then
678             Operating_Mode := Check_Semantics;
679             Expander_Active := False;
680          end if;
681
682          --  Set the fatal error flag in the unit table unless we are
683          --  in Try_Semantics mode. This stops the semantics from being
684          --  performed if we find a serious error. This is skipped if we
685          --  are currently dealing with the configuration pragma file.
686
687          if not Try_Semantics
688            and then Current_Source_Unit /= No_Unit
689          then
690             Set_Fatal_Error (Get_Source_Unit (Sptr));
691          end if;
692       end Handle_Serious_Error;
693
694    --  Start of processing for Error_Msg_Internal
695
696    begin
697       if Raise_Exception_On_Error /= 0 then
698          raise Error_Msg_Exception;
699       end if;
700
701       Continuation := Msg_Cont;
702       Continuation_New_Line := False;
703       Suppress_Message := False;
704       Kill_Message := False;
705       Set_Msg_Text (Msg, Sptr);
706
707       --  Kill continuation if parent message killed
708
709       if Continuation and Last_Killed then
710          return;
711       end if;
712
713       --  Return without doing anything if message is suppressed
714
715       if Suppress_Message
716         and not All_Errors_Mode
717         and not (Msg (Msg'Last) = '!')
718         and not Is_Warning_Msg
719       then
720          if not Continuation then
721             Last_Killed := True;
722          end if;
723
724          return;
725       end if;
726
727       --  Return without doing anything if message is killed and this
728       --  is not the first error message. The philosophy is that if we
729       --  get a weird error message and we already have had a message,
730       --  then we hope the weird message is a junk cascaded message
731
732       if Kill_Message
733         and then not All_Errors_Mode
734         and then Total_Errors_Detected /= 0
735       then
736          if not Continuation then
737             Last_Killed := True;
738          end if;
739
740          return;
741       end if;
742
743       --  Special check for warning message to see if it should be output
744
745       if Is_Warning_Msg then
746
747          --  Immediate return if warning message and warnings are suppressed
748
749          if Warnings_Suppressed (Optr)
750            or else Warnings_Suppressed (Sptr)
751          then
752             Cur_Msg := No_Error_Msg;
753             return;
754          end if;
755
756          --  If the flag location is in the main extended source unit
757          --  then for sure we want the warning since it definitely belongs
758
759          if In_Extended_Main_Source_Unit (Sptr) then
760             null;
761
762          --  If the flag location is not in the main extended source unit,
763          --  then we want to eliminate the warning, unless it is in the
764          --  extended main code unit and we want warnings on the instance.
765
766          elsif In_Extended_Main_Code_Unit (Sptr)
767            and then Warn_On_Instance
768          then
769             null;
770
771          --  Keep warning if debug flag G set
772
773          elsif Debug_Flag_GG then
774             null;
775
776          --  Keep warning if message text ends in !!
777
778          elsif Msg (Msg'Last) = '!' and then Msg (Msg'Last - 1) = '!' then
779             null;
780
781          --  Here is where we delete a warning from a with'ed unit
782
783          else
784             Cur_Msg := No_Error_Msg;
785
786             if not Continuation then
787                Last_Killed := True;
788             end if;
789
790             return;
791          end if;
792       end if;
793
794       --  If message is to be ignored in special ignore message mode, this is
795       --  where we do this special processing, bypassing message output.
796
797       if Ignore_Errors_Enable > 0 then
798          if Is_Serious_Error then
799             Handle_Serious_Error;
800          end if;
801
802          return;
803       end if;
804
805       --  If error message line length set, and this is a continuation message
806       --  then all we do is to append the text to the text of the last message
807       --  with a comma space separator (eliminating a possible (style) or
808       --  info prefix).
809
810       if Error_Msg_Line_Length /= 0
811         and then Continuation
812       then
813          Cur_Msg := Errors.Last;
814
815          declare
816             Oldm : String_Ptr := Errors.Table (Cur_Msg).Text;
817             Newm : String (1 .. Oldm'Last + 2 + Msglen);
818             Newl : Natural;
819             M    : Natural;
820
821          begin
822             --  First copy old message to new one and free it
823
824             Newm (Oldm'Range) := Oldm.all;
825             Newl := Oldm'Length;
826             Free (Oldm);
827
828             --  Remove (style) or info: at start of message
829
830             if Msglen > 8 and then Msg_Buffer (1 .. 8) = "(style) " then
831                M := 9;
832             elsif Msglen > 6 and then Msg_Buffer (1 .. 6) = "info: " then
833                M := 7;
834             else
835                M := 1;
836             end if;
837
838             --  Now deal with separation between messages. Normally this
839             --  is simply comma space, but there are some special cases.
840
841             --  If continuation new line, then put actual NL character in msg
842
843             if Continuation_New_Line then
844                Newl := Newl + 1;
845                Newm (Newl) := ASCII.LF;
846
847             --  If continuation message is enclosed in parentheses, then
848             --  special treatment (don't need a comma, and we want to combine
849             --  successive parenthetical remarks into a single one with
850             --  separating commas).
851
852             elsif Msg_Buffer (M) = '(' and then Msg_Buffer (Msglen) = ')' then
853
854                --  Case where existing message ends in right paren, remove
855                --  and separate parenthetical remarks with a comma.
856
857                if Newm (Newl) = ')' then
858                   Newm (Newl) := ',';
859                   Msg_Buffer (M) := ' ';
860
861                --  Case where we are adding new parenthetical comment
862
863                else
864                   Newl := Newl + 1;
865                   Newm (Newl) := ' ';
866                end if;
867
868             --  Case where continuation not in parens and no new line
869
870             else
871                Newm (Newl + 1 .. Newl + 2) := ", ";
872                Newl := Newl + 2;
873             end if;
874
875             --  Append new message
876
877             Newm (Newl + 1 .. Newl + Msglen - M + 1) :=
878               Msg_Buffer (M .. Msglen);
879             Newl := Newl + Msglen - M + 1;
880             Errors.Table (Cur_Msg).Text := new String'(Newm (1 .. Newl));
881          end;
882
883          return;
884       end if;
885
886       --  Otherwise build error message object for new message
887
888       Errors.Increment_Last;
889       Cur_Msg := Errors.Last;
890       Errors.Table (Cur_Msg).Text     := new String'(Msg_Buffer (1 .. Msglen));
891       Errors.Table (Cur_Msg).Next     := No_Error_Msg;
892       Errors.Table (Cur_Msg).Sptr     := Sptr;
893       Errors.Table (Cur_Msg).Optr     := Optr;
894       Errors.Table (Cur_Msg).Sfile    := Get_Source_File_Index (Sptr);
895       Errors.Table (Cur_Msg).Line     := Get_Physical_Line_Number (Sptr);
896       Errors.Table (Cur_Msg).Col      := Get_Column_Number (Sptr);
897       Errors.Table (Cur_Msg).Warn     := Is_Warning_Msg;
898       Errors.Table (Cur_Msg).Style    := Is_Style_Msg;
899       Errors.Table (Cur_Msg).Serious  := Is_Serious_Error;
900       Errors.Table (Cur_Msg).Uncond   := Is_Unconditional_Msg;
901       Errors.Table (Cur_Msg).Msg_Cont := Continuation;
902       Errors.Table (Cur_Msg).Deleted  := False;
903
904       --  If immediate errors mode set, output error message now. Also output
905       --  now if the -d1 debug flag is set (so node number message comes out
906       --  just before actual error message)
907
908       if Debug_Flag_OO or else Debug_Flag_1 then
909          Write_Eol;
910          Output_Source_Line
911            (Errors.Table (Cur_Msg).Line, Errors.Table (Cur_Msg).Sfile, True);
912          Temp_Msg := Cur_Msg;
913          Output_Error_Msgs (Temp_Msg);
914
915       --  If not in immediate errors mode, then we insert the message in the
916       --  error chain for later output by Finalize. The messages are sorted
917       --  first by unit (main unit comes first), and within a unit by source
918       --  location (earlier flag location first in the chain).
919
920       else
921          --  First a quick check, does this belong at the very end of the chain
922          --  of error messages. This saves a lot of time in the normal case if
923          --  there are lots of messages.
924
925          if Last_Error_Msg /= No_Error_Msg
926            and then Errors.Table (Cur_Msg).Sfile =
927                     Errors.Table (Last_Error_Msg).Sfile
928            and then (Sptr > Errors.Table (Last_Error_Msg).Sptr
929                        or else
930                           (Sptr = Errors.Table (Last_Error_Msg).Sptr
931                              and then
932                                Optr > Errors.Table (Last_Error_Msg).Optr))
933          then
934             Prev_Msg := Last_Error_Msg;
935             Next_Msg := No_Error_Msg;
936
937          --  Otherwise do a full sequential search for the insertion point
938
939          else
940             Prev_Msg := No_Error_Msg;
941             Next_Msg := First_Error_Msg;
942             while Next_Msg /= No_Error_Msg loop
943                exit when
944                  Errors.Table (Cur_Msg).Sfile < Errors.Table (Next_Msg).Sfile;
945
946                if Errors.Table (Cur_Msg).Sfile =
947                     Errors.Table (Next_Msg).Sfile
948                then
949                   exit when Sptr < Errors.Table (Next_Msg).Sptr
950                               or else
951                                 (Sptr = Errors.Table (Next_Msg).Sptr
952                                    and then
953                                  Optr < Errors.Table (Next_Msg).Optr);
954                end if;
955
956                Prev_Msg := Next_Msg;
957                Next_Msg := Errors.Table (Next_Msg).Next;
958             end loop;
959          end if;
960
961          --  Now we insert the new message in the error chain. The insertion
962          --  point for the message is after Prev_Msg and before Next_Msg.
963
964          --  The possible insertion point for the new message is after Prev_Msg
965          --  and before Next_Msg. However, this is where we do a special check
966          --  for redundant parsing messages, defined as messages posted on the
967          --  same line. The idea here is that probably such messages are junk
968          --  from the parser recovering. In full errors mode, we don't do this
969          --  deletion, but otherwise such messages are discarded at this stage.
970
971          if Prev_Msg /= No_Error_Msg
972            and then Errors.Table (Prev_Msg).Line =
973                                              Errors.Table (Cur_Msg).Line
974            and then Errors.Table (Prev_Msg).Sfile =
975                                              Errors.Table (Cur_Msg).Sfile
976            and then Compiler_State = Parsing
977            and then not All_Errors_Mode
978          then
979             --  Don't delete unconditional messages and at this stage, don't
980             --  delete continuation lines (we attempted to delete those earlier
981             --  if the parent message was deleted.
982
983             if not Errors.Table (Cur_Msg).Uncond
984               and then not Continuation
985             then
986                --  Don't delete if prev msg is warning and new msg is an error.
987                --  This is because we don't want a real error masked by a
988                --  warning. In all other cases (that is parse errors for the
989                --  same line that are not unconditional) we do delete the
990                --  message. This helps to avoid junk extra messages from
991                --  cascaded parsing errors
992
993                if not (Errors.Table (Prev_Msg).Warn
994                          or
995                        Errors.Table (Prev_Msg).Style)
996                  or else
997                       (Errors.Table (Cur_Msg).Warn
998                          or
999                        Errors.Table (Cur_Msg).Style)
1000                then
1001                   --  All tests passed, delete the message by simply returning
1002                   --  without any further processing.
1003
1004                   if not Continuation then
1005                      Last_Killed := True;
1006                   end if;
1007
1008                   return;
1009                end if;
1010             end if;
1011          end if;
1012
1013          --  Come here if message is to be inserted in the error chain
1014
1015          if not Continuation then
1016             Last_Killed := False;
1017          end if;
1018
1019          if Prev_Msg = No_Error_Msg then
1020             First_Error_Msg := Cur_Msg;
1021          else
1022             Errors.Table (Prev_Msg).Next := Cur_Msg;
1023          end if;
1024
1025          Errors.Table (Cur_Msg).Next := Next_Msg;
1026
1027          if Next_Msg = No_Error_Msg then
1028             Last_Error_Msg := Cur_Msg;
1029          end if;
1030       end if;
1031
1032       --  Bump appropriate statistics count
1033
1034       if Errors.Table (Cur_Msg).Warn or Errors.Table (Cur_Msg).Style then
1035          Warnings_Detected := Warnings_Detected + 1;
1036
1037       else
1038          Total_Errors_Detected := Total_Errors_Detected + 1;
1039
1040          if Errors.Table (Cur_Msg).Serious then
1041             Serious_Errors_Detected := Serious_Errors_Detected + 1;
1042             Handle_Serious_Error;
1043          end if;
1044       end if;
1045
1046       --  If too many warnings turn off warnings
1047
1048       if Maximum_Messages /= 0 then
1049          if Warnings_Detected = Maximum_Messages then
1050             Warning_Mode := Suppress;
1051          end if;
1052
1053          --  If too many errors abandon compilation
1054
1055          if Total_Errors_Detected = Maximum_Messages then
1056             raise Unrecoverable_Error;
1057          end if;
1058       end if;
1059    end Error_Msg_Internal;
1060
1061    -----------------
1062    -- Error_Msg_N --
1063    -----------------
1064
1065    procedure Error_Msg_N (Msg : String; N : Node_Or_Entity_Id) is
1066    begin
1067       Error_Msg_NEL (Msg, N, N, Sloc (N));
1068    end Error_Msg_N;
1069
1070    ------------------
1071    -- Error_Msg_NE --
1072    ------------------
1073
1074    procedure Error_Msg_NE
1075      (Msg : String;
1076       N   : Node_Or_Entity_Id;
1077       E   : Node_Or_Entity_Id)
1078    is
1079    begin
1080       Error_Msg_NEL (Msg, N, E, Sloc (N));
1081    end Error_Msg_NE;
1082
1083    -------------------
1084    -- Error_Msg_NEL --
1085    -------------------
1086
1087    procedure Error_Msg_NEL
1088      (Msg           : String;
1089       N             : Node_Or_Entity_Id;
1090       E             : Node_Or_Entity_Id;
1091       Flag_Location : Source_Ptr)
1092    is
1093    begin
1094       if Special_Msg_Delete (Msg, N, E) then
1095          return;
1096       end if;
1097
1098       Test_Style_Warning_Serious_Msg (Msg);
1099
1100       --  Special handling for warning messages
1101
1102       if Is_Warning_Msg then
1103
1104          --  Suppress if no warnings set for either entity or node
1105
1106          if No_Warnings (N) or else No_Warnings (E) then
1107             return;
1108          end if;
1109
1110          --  Suppress if inside loop that is known to be null or is probably
1111          --  null (case where loop executes only if invalid values present).
1112          --  In either case warnings in the loop are likely to be junk.
1113
1114          declare
1115             P : Node_Id;
1116
1117          begin
1118             P := Parent (N);
1119             while Present (P) loop
1120                if Nkind (P) = N_Loop_Statement
1121                  and then Suppress_Loop_Warnings (P)
1122                then
1123                   return;
1124                end if;
1125
1126                P := Parent (P);
1127             end loop;
1128          end;
1129       end if;
1130
1131       --  Test for message to be output
1132
1133       if All_Errors_Mode
1134         or else Msg (Msg'Last) = '!'
1135         or else Is_Warning_Msg
1136         or else OK_Node (N)
1137         or else (Msg (Msg'First) = '\' and not Last_Killed)
1138       then
1139          Debug_Output (N);
1140          Error_Msg_Node_1 := E;
1141          Error_Msg (Msg, Flag_Location);
1142
1143       else
1144          Last_Killed := True;
1145       end if;
1146
1147       if not (Is_Warning_Msg or Is_Style_Msg) then
1148          Set_Posted (N);
1149       end if;
1150    end Error_Msg_NEL;
1151
1152    ------------------
1153    -- Error_Msg_NW --
1154    ------------------
1155
1156    procedure Error_Msg_NW
1157      (Eflag : Boolean;
1158       Msg   : String;
1159       N     : Node_Or_Entity_Id)
1160    is
1161    begin
1162       if Eflag
1163         and then In_Extended_Main_Source_Unit (N)
1164         and then Comes_From_Source (N)
1165       then
1166          Error_Msg_NEL (Msg, N, N, Sloc (N));
1167       end if;
1168    end Error_Msg_NW;
1169
1170    -----------------
1171    -- Error_Msg_S --
1172    -----------------
1173
1174    procedure Error_Msg_S (Msg : String) is
1175    begin
1176       Error_Msg (Msg, Scan_Ptr);
1177    end Error_Msg_S;
1178
1179    ------------------
1180    -- Error_Msg_SC --
1181    ------------------
1182
1183    procedure Error_Msg_SC (Msg : String) is
1184    begin
1185       --  If we are at end of file, post the flag after the previous token
1186
1187       if Token = Tok_EOF then
1188          Error_Msg_AP (Msg);
1189
1190       --  For all other cases the message is posted at the current token
1191       --  pointer position
1192
1193       else
1194          Error_Msg (Msg, Token_Ptr);
1195       end if;
1196    end Error_Msg_SC;
1197
1198    ------------------
1199    -- Error_Msg_SP --
1200    ------------------
1201
1202    procedure Error_Msg_SP (Msg : String) is
1203    begin
1204       --  Note: in the case where there is no previous token, Prev_Token_Ptr
1205       --  is set to Source_First, which is a reasonable position for the
1206       --  error flag in this situation
1207
1208       Error_Msg (Msg, Prev_Token_Ptr);
1209    end Error_Msg_SP;
1210
1211    --------------
1212    -- Finalize --
1213    --------------
1214
1215    procedure Finalize (Last_Call : Boolean) is
1216       Cur : Error_Msg_Id;
1217       Nxt : Error_Msg_Id;
1218       F   : Error_Msg_Id;
1219
1220    begin
1221       --  Eliminate any duplicated error messages from the list. This is
1222       --  done after the fact to avoid problems with Change_Error_Text.
1223
1224       Cur := First_Error_Msg;
1225       while Cur /= No_Error_Msg loop
1226          Nxt := Errors.Table (Cur).Next;
1227
1228          F := Nxt;
1229          while F /= No_Error_Msg
1230            and then Errors.Table (F).Sptr = Errors.Table (Cur).Sptr
1231          loop
1232             Check_Duplicate_Message (Cur, F);
1233             F := Errors.Table (F).Next;
1234          end loop;
1235
1236          Cur := Nxt;
1237       end loop;
1238
1239       --  Mark any messages suppressed by specific warnings as Deleted
1240
1241       Cur := First_Error_Msg;
1242       while Cur /= No_Error_Msg loop
1243          if not Errors.Table (Cur).Deleted
1244            and then Warning_Specifically_Suppressed
1245                      (Errors.Table (Cur).Sptr,
1246                       Errors.Table (Cur).Text)
1247          then
1248             Errors.Table (Cur).Deleted := True;
1249             Warnings_Detected := Warnings_Detected - 1;
1250          end if;
1251
1252          Cur := Errors.Table (Cur).Next;
1253       end loop;
1254
1255       Finalize_Called := True;
1256
1257       --  Check consistency of specific warnings (may add warnings). We only
1258       --  do this on the last call, after all possible warnings are posted.
1259
1260       if Last_Call then
1261          Validate_Specific_Warnings (Error_Msg'Access);
1262       end if;
1263    end Finalize;
1264
1265    ----------------
1266    -- First_Node --
1267    ----------------
1268
1269    function First_Node (C : Node_Id) return Node_Id is
1270       L        : constant Source_Ptr        := Sloc (Original_Node (C));
1271       Sfile    : constant Source_File_Index := Get_Source_File_Index (L);
1272       Earliest : Node_Id;
1273       Eloc     : Source_Ptr;
1274
1275       function Test_Earlier (N : Node_Id) return Traverse_Result;
1276       --  Function applied to every node in the construct
1277
1278       procedure Search_Tree_First is new Traverse_Proc (Test_Earlier);
1279       --  Create traversal procedure
1280
1281       ------------------
1282       -- Test_Earlier --
1283       ------------------
1284
1285       function Test_Earlier (N : Node_Id) return Traverse_Result is
1286          Loc : constant Source_Ptr := Sloc (Original_Node (N));
1287
1288       begin
1289          --  Check for earlier. The tests for being in the same file ensures
1290          --  against strange cases of foreign code somehow being present. We
1291          --  don't want wild placement of messages if that happens, so it is
1292          --  best to just ignore this situation.
1293
1294          if Loc < Eloc
1295            and then Get_Source_File_Index (Loc) = Sfile
1296          then
1297             Earliest := Original_Node (N);
1298             Eloc     := Loc;
1299          end if;
1300
1301          return OK_Orig;
1302       end Test_Earlier;
1303
1304    --  Start of processing for First_Node
1305
1306    begin
1307       Earliest := Original_Node (C);
1308       Eloc := Sloc (Earliest);
1309       Search_Tree_First (Original_Node (C));
1310       return Earliest;
1311    end First_Node;
1312
1313    ----------------
1314    -- First_Sloc --
1315    ----------------
1316
1317    function First_Sloc (N : Node_Id) return Source_Ptr is
1318       SI : constant Source_File_Index := Source_Index (Get_Source_Unit (N));
1319       SF : constant Source_Ptr        := Source_First (SI);
1320       F  : Node_Id;
1321       S  : Source_Ptr;
1322
1323    begin
1324       F := First_Node (N);
1325       S := Sloc (F);
1326
1327       --  The following circuit is a bit subtle. When we have parenthesized
1328       --  expressions, then the Sloc will not record the location of the
1329       --  paren, but we would like to post the flag on the paren. So what
1330       --  we do is to crawl up the tree from the First_Node, adjusting the
1331       --  Sloc value for any parentheses we know are present. Yes, we know
1332       --  this circuit is not 100% reliable (e.g. because we don't record
1333       --  all possible paren level values), but this is only for an error
1334       --  message so it is good enough.
1335
1336       Node_Loop : loop
1337          Paren_Loop : for J in 1 .. Paren_Count (F) loop
1338
1339             --  We don't look more than 12 characters behind the current
1340             --  location, and in any case not past the front of the source.
1341
1342             Search_Loop : for K in 1 .. 12 loop
1343                exit Search_Loop when S = SF;
1344
1345                if Source_Text (SI) (S - 1) = '(' then
1346                   S := S - 1;
1347                   exit Search_Loop;
1348
1349                elsif Source_Text (SI) (S - 1) <= ' ' then
1350                   S := S - 1;
1351
1352                else
1353                   exit Search_Loop;
1354                end if;
1355             end loop Search_Loop;
1356          end loop Paren_Loop;
1357
1358          exit Node_Loop when F = N;
1359          F := Parent (F);
1360          exit Node_Loop when Nkind (F) not in N_Subexpr;
1361       end loop Node_Loop;
1362
1363       return S;
1364    end First_Sloc;
1365
1366    ----------------
1367    -- Initialize --
1368    ----------------
1369
1370    procedure Initialize is
1371    begin
1372       Errors.Init;
1373       First_Error_Msg := No_Error_Msg;
1374       Last_Error_Msg := No_Error_Msg;
1375       Serious_Errors_Detected := 0;
1376       Total_Errors_Detected := 0;
1377       Warnings_Detected := 0;
1378       Cur_Msg := No_Error_Msg;
1379       List_Pragmas.Init;
1380
1381       --  Initialize warnings table, if all warnings are suppressed, supply
1382       --  an initial dummy entry covering all possible source locations.
1383
1384       Warnings.Init;
1385       Specific_Warnings.Init;
1386
1387       if Warning_Mode = Suppress then
1388          Warnings.Increment_Last;
1389          Warnings.Table (Warnings.Last).Start := Source_Ptr'First;
1390          Warnings.Table (Warnings.Last).Stop  := Source_Ptr'Last;
1391       end if;
1392    end Initialize;
1393
1394    -----------------
1395    -- No_Warnings --
1396    -----------------
1397
1398    function No_Warnings (N : Node_Or_Entity_Id) return Boolean is
1399    begin
1400       if Error_Posted (N) then
1401          return True;
1402
1403       elsif Nkind (N) in N_Entity and then Has_Warnings_Off (N) then
1404          return True;
1405
1406       elsif Is_Entity_Name (N)
1407         and then Present (Entity (N))
1408         and then Has_Warnings_Off (Entity (N))
1409       then
1410          return True;
1411
1412       else
1413          return False;
1414       end if;
1415    end No_Warnings;
1416
1417    -------------
1418    -- OK_Node --
1419    -------------
1420
1421    function OK_Node (N : Node_Id) return Boolean is
1422       K : constant Node_Kind := Nkind (N);
1423
1424    begin
1425       if Error_Posted (N) then
1426          return False;
1427
1428       elsif K in N_Has_Etype
1429         and then Present (Etype (N))
1430         and then Error_Posted (Etype (N))
1431       then
1432          return False;
1433
1434       elsif (K in N_Op
1435               or else K = N_Attribute_Reference
1436               or else K = N_Character_Literal
1437               or else K = N_Expanded_Name
1438               or else K = N_Identifier
1439               or else K = N_Operator_Symbol)
1440         and then Present (Entity (N))
1441         and then Error_Posted (Entity (N))
1442       then
1443          return False;
1444       else
1445          return True;
1446       end if;
1447    end OK_Node;
1448
1449    ---------------------
1450    -- Output_Messages --
1451    ---------------------
1452
1453    procedure Output_Messages is
1454       E        : Error_Msg_Id;
1455       Err_Flag : Boolean;
1456
1457       procedure Write_Error_Summary;
1458       --  Write error summary
1459
1460       procedure Write_Header (Sfile : Source_File_Index);
1461       --  Write header line (compiling or checking given file)
1462
1463       procedure Write_Max_Errors;
1464       --  Write message if max errors reached
1465
1466       -------------------------
1467       -- Write_Error_Summary --
1468       -------------------------
1469
1470       procedure Write_Error_Summary is
1471       begin
1472          --  Extra blank line if error messages or source listing were output
1473
1474          if Total_Errors_Detected + Warnings_Detected > 0
1475            or else Full_List
1476          then
1477             Write_Eol;
1478          end if;
1479
1480          --  Message giving number of lines read and number of errors detected.
1481          --  This normally goes to Standard_Output. The exception is when brief
1482          --  mode is not set, verbose mode (or full list mode) is set, and
1483          --  there are errors. In this case we send the message to standard
1484          --  error to make sure that *something* appears on standard error in
1485          --  an error situation.
1486
1487          --  Formerly, only the "# errors" suffix was sent to stderr, whereas
1488          --  "# lines:" appeared on stdout. This caused problems on VMS when
1489          --  the stdout buffer was flushed, giving an extra line feed after
1490          --  the prefix.
1491
1492          if Total_Errors_Detected + Warnings_Detected /= 0
1493            and then not Brief_Output
1494            and then (Verbose_Mode or Full_List)
1495          then
1496             Set_Standard_Error;
1497          end if;
1498
1499          --  Message giving total number of lines
1500
1501          Write_Str (" ");
1502          Write_Int (Num_Source_Lines (Main_Source_File));
1503
1504          if Num_Source_Lines (Main_Source_File) = 1 then
1505             Write_Str (" line: ");
1506          else
1507             Write_Str (" lines: ");
1508          end if;
1509
1510          if Total_Errors_Detected = 0 then
1511             Write_Str ("No errors");
1512
1513          elsif Total_Errors_Detected = 1 then
1514             Write_Str ("1 error");
1515
1516          else
1517             Write_Int (Total_Errors_Detected);
1518             Write_Str (" errors");
1519          end if;
1520
1521          if Warnings_Detected /= 0 then
1522             Write_Str (", ");
1523             Write_Int (Warnings_Detected);
1524             Write_Str (" warning");
1525
1526             if Warnings_Detected /= 1 then
1527                Write_Char ('s');
1528             end if;
1529
1530             if Warning_Mode = Treat_As_Error then
1531                Write_Str (" (treated as error");
1532
1533                if Warnings_Detected /= 1 then
1534                   Write_Char ('s');
1535                end if;
1536
1537                Write_Char (')');
1538             end if;
1539          end if;
1540
1541          Write_Eol;
1542          Set_Standard_Output;
1543       end Write_Error_Summary;
1544
1545       ------------------
1546       -- Write_Header --
1547       ------------------
1548
1549       procedure Write_Header (Sfile : Source_File_Index) is
1550       begin
1551          if Verbose_Mode or Full_List then
1552             if Original_Operating_Mode = Generate_Code then
1553                Write_Str ("Compiling: ");
1554             else
1555                Write_Str ("Checking: ");
1556             end if;
1557
1558             Write_Name (Full_File_Name (Sfile));
1559
1560             if not Debug_Flag_7 then
1561                Write_Str (" (source file time stamp: ");
1562                Write_Time_Stamp (Sfile);
1563                Write_Char (')');
1564             end if;
1565
1566             Write_Eol;
1567          end if;
1568       end Write_Header;
1569
1570       ----------------------
1571       -- Write_Max_Errors --
1572       ----------------------
1573
1574       procedure Write_Max_Errors is
1575       begin
1576          if Maximum_Messages /= 0 then
1577             if Warnings_Detected >= Maximum_Messages then
1578                Set_Standard_Error;
1579                Write_Line ("maximum number of warnings output");
1580                Write_Line ("any further warnings suppressed");
1581                Set_Standard_Output;
1582             end if;
1583
1584             --  If too many errors print message
1585
1586             if Total_Errors_Detected >= Maximum_Messages then
1587                Set_Standard_Error;
1588                Write_Line ("fatal error: maximum number of errors detected");
1589                Set_Standard_Output;
1590             end if;
1591          end if;
1592       end Write_Max_Errors;
1593
1594    --  Start of processing for Output_Messages
1595
1596    begin
1597       --  Error if Finalize has not been called
1598
1599       if not Finalize_Called then
1600          raise Program_Error;
1601       end if;
1602
1603       --  Reset current error source file if the main unit has a pragma
1604       --  Source_Reference. This ensures outputting the proper name of
1605       --  the source file in this situation.
1606
1607       if Main_Source_File = No_Source_File
1608         or else Num_SRef_Pragmas (Main_Source_File) /= 0
1609       then
1610          Current_Error_Source_File := No_Source_File;
1611       end if;
1612
1613       --  Brief Error mode
1614
1615       if Brief_Output or (not Full_List and not Verbose_Mode) then
1616          Set_Standard_Error;
1617
1618          E := First_Error_Msg;
1619          while E /= No_Error_Msg loop
1620             if not Errors.Table (E).Deleted and then not Debug_Flag_KK then
1621                if Full_Path_Name_For_Brief_Errors then
1622                   Write_Name (Full_Ref_Name (Errors.Table (E).Sfile));
1623                else
1624                   Write_Name (Reference_Name (Errors.Table (E).Sfile));
1625                end if;
1626
1627                Write_Char (':');
1628                Write_Int (Int (Physical_To_Logical
1629                                 (Errors.Table (E).Line,
1630                                  Errors.Table (E).Sfile)));
1631                Write_Char (':');
1632
1633                if Errors.Table (E).Col < 10 then
1634                   Write_Char ('0');
1635                end if;
1636
1637                Write_Int (Int (Errors.Table (E).Col));
1638                Write_Str (": ");
1639                Output_Msg_Text (E);
1640                Write_Eol;
1641             end if;
1642
1643             E := Errors.Table (E).Next;
1644          end loop;
1645
1646          Set_Standard_Output;
1647       end if;
1648
1649       --  Full source listing case
1650
1651       if Full_List then
1652          List_Pragmas_Index := 1;
1653          List_Pragmas_Mode := True;
1654          E := First_Error_Msg;
1655
1656          --  Normal case, to stdout (copyright notice already output)
1657
1658          if Full_List_File_Name = null then
1659             if not Debug_Flag_7 then
1660                Write_Eol;
1661             end if;
1662
1663          --  Output to file
1664
1665          else
1666             Create_List_File_Access.all (Full_List_File_Name.all);
1667             Set_Special_Output (Write_List_Info_Access.all'Access);
1668
1669             --  Write copyright notice to file
1670
1671             if not Debug_Flag_7 then
1672                Write_Str ("GNAT ");
1673                Write_Str (Gnat_Version_String);
1674                Write_Eol;
1675                Write_Str ("Copyright 1992-" &
1676                           Current_Year &
1677                           ", Free Software Foundation, Inc.");
1678                Write_Eol;
1679             end if;
1680          end if;
1681
1682          --  First list extended main source file units with errors
1683
1684          --  Note: if debug flag d.m is set, only the main source is listed
1685
1686          for U in Main_Unit .. Last_Unit loop
1687             if In_Extended_Main_Source_Unit (Cunit_Entity (U))
1688               and then (U = Main_Unit or else not Debug_Flag_Dot_M)
1689             then
1690                declare
1691                   Sfile : constant Source_File_Index := Source_Index (U);
1692
1693                begin
1694                   Write_Eol;
1695                   Write_Header (Sfile);
1696                   Write_Eol;
1697
1698                   --  Normally, we don't want an "error messages from file"
1699                   --  message when listing the entire file, so we set the
1700                   --  current source file as the current error source file.
1701                   --  However, the old style of doing things was to list this
1702                   --  message if pragma Source_Reference is present, even for
1703                   --  the main unit. Since the purpose of the -gnatd.m switch
1704                   --  is to duplicate the old behavior, we skip the reset if
1705                   --  this debug flag is set.
1706
1707                   if not Debug_Flag_Dot_M then
1708                      Current_Error_Source_File := Sfile;
1709                   end if;
1710
1711                   for N in 1 .. Last_Source_Line (Sfile) loop
1712                      while E /= No_Error_Msg
1713                        and then Errors.Table (E).Deleted
1714                      loop
1715                         E := Errors.Table (E).Next;
1716                      end loop;
1717
1718                      Err_Flag :=
1719                        E /= No_Error_Msg
1720                          and then Errors.Table (E).Line = N
1721                          and then Errors.Table (E).Sfile = Sfile;
1722
1723                      Output_Source_Line (N, Sfile, Err_Flag);
1724
1725                      if Err_Flag then
1726                         Output_Error_Msgs (E);
1727
1728                         if not Debug_Flag_2 then
1729                            Write_Eol;
1730                         end if;
1731                      end if;
1732                   end loop;
1733                end;
1734             end if;
1735          end loop;
1736
1737          --  Then output errors, if any, for subsidiary units not in the
1738          --  main extended unit.
1739
1740          --  Note: if debug flag d.m set, include errors for any units other
1741          --  than the main unit in the extended source unit (e.g. spec and
1742          --  subunits for a body).
1743
1744          while E /= No_Error_Msg
1745            and then (not In_Extended_Main_Source_Unit (Errors.Table (E).Sptr)
1746                        or else
1747                         (Debug_Flag_Dot_M
1748                           and then Get_Source_Unit
1749                                      (Errors.Table (E).Sptr) /= Main_Unit))
1750          loop
1751             if Errors.Table (E).Deleted then
1752                E := Errors.Table (E).Next;
1753
1754             else
1755                Write_Eol;
1756                Output_Source_Line
1757                  (Errors.Table (E).Line, Errors.Table (E).Sfile, True);
1758                Output_Error_Msgs (E);
1759             end if;
1760          end loop;
1761
1762          --  If output to file, write extra copy of error summary to the
1763          --  output file, and then close it.
1764
1765          if Full_List_File_Name /= null then
1766             Write_Error_Summary;
1767             Write_Max_Errors;
1768             Close_List_File_Access.all;
1769             Cancel_Special_Output;
1770          end if;
1771       end if;
1772
1773       --  Verbose mode (error lines only with error flags). Normally this is
1774       --  ignored in full list mode, unless we are listing to a file, in which
1775       --  case we still generate -gnatv output to standard output.
1776
1777       if Verbose_Mode
1778         and then (not Full_List or else Full_List_File_Name /= null)
1779       then
1780          Write_Eol;
1781          Write_Header (Main_Source_File);
1782          E := First_Error_Msg;
1783
1784          --  Loop through error lines
1785
1786          while E /= No_Error_Msg loop
1787             if Errors.Table (E).Deleted then
1788                E := Errors.Table (E).Next;
1789             else
1790                Write_Eol;
1791                Output_Source_Line
1792                  (Errors.Table (E).Line, Errors.Table (E).Sfile, True);
1793                Output_Error_Msgs (E);
1794             end if;
1795          end loop;
1796       end if;
1797
1798       --  Output error summary if verbose or full list mode
1799
1800       if Verbose_Mode or else Full_List then
1801          Write_Error_Summary;
1802       end if;
1803
1804       Write_Max_Errors;
1805
1806       if Warning_Mode = Treat_As_Error then
1807          Total_Errors_Detected := Total_Errors_Detected + Warnings_Detected;
1808          Warnings_Detected := 0;
1809       end if;
1810    end Output_Messages;
1811
1812    ------------------------
1813    -- Output_Source_Line --
1814    ------------------------
1815
1816    procedure Output_Source_Line
1817      (L     : Physical_Line_Number;
1818       Sfile : Source_File_Index;
1819       Errs  : Boolean)
1820    is
1821       S : Source_Ptr;
1822       C : Character;
1823
1824       Line_Number_Output : Boolean := False;
1825       --  Set True once line number is output
1826
1827       Empty_Line : Boolean := True;
1828       --  Set False if line includes at least one character
1829
1830    begin
1831       if Sfile /= Current_Error_Source_File then
1832          Write_Str ("==============Error messages for ");
1833
1834          case Sinput.File_Type (Sfile) is
1835             when Sinput.Src =>
1836                Write_Str ("source");
1837
1838             when Sinput.Config =>
1839                Write_Str ("configuration pragmas");
1840
1841             when Sinput.Def =>
1842                Write_Str ("symbol definition");
1843
1844             when Sinput.Preproc =>
1845                Write_Str ("preprocessing data");
1846          end case;
1847
1848          Write_Str (" file: ");
1849          Write_Name (Full_File_Name (Sfile));
1850          Write_Eol;
1851
1852          if Num_SRef_Pragmas (Sfile) > 0 then
1853             Write_Str ("--------------Line numbers from file: ");
1854             Write_Name (Full_Ref_Name (Sfile));
1855             Write_Str (" (starting at line ");
1856             Write_Int (Int (First_Mapped_Line (Sfile)));
1857             Write_Char (')');
1858             Write_Eol;
1859          end if;
1860
1861          Current_Error_Source_File := Sfile;
1862       end if;
1863
1864       if Errs or List_Pragmas_Mode then
1865          Output_Line_Number (Physical_To_Logical (L, Sfile));
1866          Line_Number_Output := True;
1867       end if;
1868
1869       S := Line_Start (L, Sfile);
1870
1871       loop
1872          C := Source_Text (Sfile) (S);
1873          exit when C = ASCII.LF or else C = ASCII.CR or else C = EOF;
1874
1875          --  Deal with matching entry in List_Pragmas table
1876
1877          if Full_List
1878            and then List_Pragmas_Index <= List_Pragmas.Last
1879            and then S = List_Pragmas.Table (List_Pragmas_Index).Ploc
1880          then
1881             case List_Pragmas.Table (List_Pragmas_Index).Ptyp is
1882                when Page =>
1883                   Write_Char (C);
1884
1885                   --  Ignore if on line with errors so that error flags
1886                   --  get properly listed with the error line .
1887
1888                   if not Errs then
1889                      Write_Char (ASCII.FF);
1890                   end if;
1891
1892                when List_On =>
1893                   List_Pragmas_Mode := True;
1894
1895                   if not Line_Number_Output then
1896                      Output_Line_Number (Physical_To_Logical (L, Sfile));
1897                      Line_Number_Output := True;
1898                   end if;
1899
1900                   Write_Char (C);
1901
1902                when List_Off =>
1903                   Write_Char (C);
1904                   List_Pragmas_Mode := False;
1905             end case;
1906
1907             List_Pragmas_Index := List_Pragmas_Index + 1;
1908
1909          --  Normal case (no matching entry in List_Pragmas table)
1910
1911          else
1912             if Errs or List_Pragmas_Mode then
1913                Write_Char (C);
1914             end if;
1915          end if;
1916
1917          Empty_Line := False;
1918          S := S + 1;
1919       end loop;
1920
1921       --  If we have output a source line, then add the line terminator, with
1922       --  training spaces preserved (so we output the line exactly as input).
1923
1924       if Line_Number_Output then
1925          if Empty_Line then
1926             Write_Eol;
1927          else
1928             Write_Eol_Keep_Blanks;
1929          end if;
1930       end if;
1931    end Output_Source_Line;
1932
1933    -----------------------------
1934    -- Remove_Warning_Messages --
1935    -----------------------------
1936
1937    procedure Remove_Warning_Messages (N : Node_Id) is
1938
1939       function Check_For_Warning (N : Node_Id) return Traverse_Result;
1940       --  This function checks one node for a possible warning message
1941
1942       function Check_All_Warnings is new Traverse_Func (Check_For_Warning);
1943       --  This defines the traversal operation
1944
1945       -----------------------
1946       -- Check_For_Warning --
1947       -----------------------
1948
1949       function Check_For_Warning (N : Node_Id) return Traverse_Result is
1950          Loc : constant Source_Ptr := Sloc (N);
1951          E   : Error_Msg_Id;
1952
1953          function To_Be_Removed (E : Error_Msg_Id) return Boolean;
1954          --  Returns True for a message that is to be removed. Also adjusts
1955          --  warning count appropriately.
1956
1957          -------------------
1958          -- To_Be_Removed --
1959          -------------------
1960
1961          function To_Be_Removed (E : Error_Msg_Id) return Boolean is
1962          begin
1963             if E /= No_Error_Msg
1964
1965                --  Don't remove if location does not match
1966
1967                and then Errors.Table (E).Optr = Loc
1968
1969                --  Don't remove if not warning/info message. Note that we do
1970                --  not remove style messages here. They are warning messages
1971                --  but not ones we want removed in this context.
1972
1973                and then Errors.Table (E).Warn
1974
1975                --  Don't remove unconditional messages
1976
1977                and then not Errors.Table (E).Uncond
1978             then
1979                Warnings_Detected := Warnings_Detected - 1;
1980                return True;
1981
1982             --  No removal required
1983
1984             else
1985                return False;
1986             end if;
1987          end To_Be_Removed;
1988
1989       --  Start of processing for Check_For_Warnings
1990
1991       begin
1992          while To_Be_Removed (First_Error_Msg) loop
1993             First_Error_Msg := Errors.Table (First_Error_Msg).Next;
1994          end loop;
1995
1996          if First_Error_Msg = No_Error_Msg then
1997             Last_Error_Msg := No_Error_Msg;
1998          end if;
1999
2000          E := First_Error_Msg;
2001          while E /= No_Error_Msg loop
2002             while To_Be_Removed (Errors.Table (E).Next) loop
2003                Errors.Table (E).Next :=
2004                  Errors.Table (Errors.Table (E).Next).Next;
2005
2006                if Errors.Table (E).Next = No_Error_Msg then
2007                   Last_Error_Msg := E;
2008                end if;
2009             end loop;
2010
2011             E := Errors.Table (E).Next;
2012          end loop;
2013
2014          if Nkind (N) = N_Raise_Constraint_Error
2015            and then Original_Node (N) /= N
2016            and then No (Condition (N))
2017          then
2018             --  Warnings may have been posted on subexpressions of the original
2019             --  tree. We place the original node back on the tree to remove
2020             --  those warnings, whose sloc do not match those of any node in
2021             --  the current tree. Given that we are in unreachable code, this
2022             --  modification to the tree is harmless.
2023
2024             declare
2025                Status : Traverse_Final_Result;
2026
2027             begin
2028                if Is_List_Member (N) then
2029                   Set_Condition (N, Original_Node (N));
2030                   Status := Check_All_Warnings (Condition (N));
2031                else
2032                   Rewrite (N, Original_Node (N));
2033                   Status := Check_All_Warnings (N);
2034                end if;
2035
2036                return Status;
2037             end;
2038
2039          else
2040             return OK;
2041          end if;
2042       end Check_For_Warning;
2043
2044    --  Start of processing for Remove_Warning_Messages
2045
2046    begin
2047       if Warnings_Detected /= 0 then
2048          declare
2049             Discard : Traverse_Final_Result;
2050             pragma Warnings (Off, Discard);
2051
2052          begin
2053             Discard := Check_All_Warnings (N);
2054          end;
2055       end if;
2056    end Remove_Warning_Messages;
2057
2058    procedure Remove_Warning_Messages (L : List_Id) is
2059       Stat : Node_Id;
2060    begin
2061       if Is_Non_Empty_List (L) then
2062          Stat := First (L);
2063          while Present (Stat) loop
2064             Remove_Warning_Messages (Stat);
2065             Next (Stat);
2066          end loop;
2067       end if;
2068    end Remove_Warning_Messages;
2069
2070    ---------------------------
2071    -- Set_Identifier_Casing --
2072    ---------------------------
2073
2074    procedure Set_Identifier_Casing
2075      (Identifier_Name : System.Address;
2076       File_Name       : System.Address)
2077    is
2078       Ident : constant Big_String_Ptr := To_Big_String_Ptr (Identifier_Name);
2079       File  : constant Big_String_Ptr := To_Big_String_Ptr (File_Name);
2080       Flen  : Natural;
2081
2082       Desired_Case : Casing_Type := Mixed_Case;
2083       --  Casing required for result. Default value of Mixed_Case is used if
2084       --  for some reason we cannot find the right file name in the table.
2085
2086    begin
2087       --  Get length of file name
2088
2089       Flen := 0;
2090       while File (Flen + 1) /= ASCII.NUL loop
2091          Flen := Flen + 1;
2092       end loop;
2093
2094       --  Loop through file names to find matching one. This is a bit slow,
2095       --  but we only do it in error situations so it is not so terrible.
2096       --  Note that if the loop does not exit, then the desired case will
2097       --  be left set to Mixed_Case, this can happen if the name was not
2098       --  in canonical form, and gets canonicalized on VMS. Possibly we
2099       --  could fix this by unconditinally canonicalizing these names ???
2100
2101       for J in 1 .. Last_Source_File loop
2102          Get_Name_String (Full_Debug_Name (J));
2103
2104          if Name_Len = Flen
2105            and then Name_Buffer (1 .. Name_Len) = String (File (1 .. Flen))
2106          then
2107             Desired_Case := Identifier_Casing (J);
2108             exit;
2109          end if;
2110       end loop;
2111
2112       --  Copy identifier as given to Name_Buffer
2113
2114       for J in Name_Buffer'Range loop
2115          Name_Buffer (J) := Ident (J);
2116
2117          if Name_Buffer (J) = ASCII.NUL then
2118             Name_Len := J - 1;
2119             exit;
2120          end if;
2121       end loop;
2122
2123       Set_Casing (Desired_Case);
2124    end Set_Identifier_Casing;
2125
2126    -----------------------
2127    -- Set_Ignore_Errors --
2128    -----------------------
2129
2130    procedure Set_Ignore_Errors (To : Boolean) is
2131    begin
2132       Errors_Must_Be_Ignored := To;
2133    end Set_Ignore_Errors;
2134
2135    ------------------------------
2136    -- Set_Msg_Insertion_Column --
2137    ------------------------------
2138
2139    procedure Set_Msg_Insertion_Column is
2140    begin
2141       if Style.RM_Column_Check then
2142          Set_Msg_Str (" in column ");
2143          Set_Msg_Int (Int (Error_Msg_Col) + 1);
2144       end if;
2145    end Set_Msg_Insertion_Column;
2146
2147    ----------------------------
2148    -- Set_Msg_Insertion_Node --
2149    ----------------------------
2150
2151    procedure Set_Msg_Insertion_Node is
2152       K : Node_Kind;
2153
2154    begin
2155       Suppress_Message :=
2156         Error_Msg_Node_1 = Error
2157           or else Error_Msg_Node_1 = Any_Type;
2158
2159       if Error_Msg_Node_1 = Empty then
2160          Set_Msg_Blank_Conditional;
2161          Set_Msg_Str ("<empty>");
2162
2163       elsif Error_Msg_Node_1 = Error then
2164          Set_Msg_Blank;
2165          Set_Msg_Str ("<error>");
2166
2167       elsif Error_Msg_Node_1 = Standard_Void_Type then
2168          Set_Msg_Blank;
2169          Set_Msg_Str ("procedure name");
2170
2171       else
2172          Set_Msg_Blank_Conditional;
2173
2174          --  Output name
2175
2176          K := Nkind (Error_Msg_Node_1);
2177
2178          --  If we have operator case, skip quotes since name of operator
2179          --  itself will supply the required quotations. An operator can be
2180          --  an applied use in an expression or an explicit operator symbol,
2181          --  or an identifier whose name indicates it is an operator.
2182
2183          if K in N_Op
2184            or else K = N_Operator_Symbol
2185            or else K = N_Defining_Operator_Symbol
2186            or else ((K = N_Identifier or else K = N_Defining_Identifier)
2187                        and then Is_Operator_Name (Chars (Error_Msg_Node_1)))
2188          then
2189             Set_Msg_Node (Error_Msg_Node_1);
2190
2191          --  Normal case, not an operator, surround with quotes
2192
2193          else
2194             Set_Msg_Quote;
2195             Set_Qualification (Error_Msg_Qual_Level, Error_Msg_Node_1);
2196             Set_Msg_Node (Error_Msg_Node_1);
2197             Set_Msg_Quote;
2198          end if;
2199       end if;
2200
2201       --  The following assignment ensures that a second ampersand insertion
2202       --  character will correspond to the Error_Msg_Node_2 parameter. We
2203       --  suppress possible validity checks in case operating in -gnatVa mode,
2204       --  and Error_Msg_Node_2 is not needed and has not been set.
2205
2206       declare
2207          pragma Suppress (Range_Check);
2208       begin
2209          Error_Msg_Node_1 := Error_Msg_Node_2;
2210       end;
2211    end Set_Msg_Insertion_Node;
2212
2213    --------------------------------------
2214    -- Set_Msg_Insertion_Type_Reference --
2215    --------------------------------------
2216
2217    procedure Set_Msg_Insertion_Type_Reference (Flag : Source_Ptr) is
2218       Ent : Entity_Id;
2219
2220    begin
2221       Set_Msg_Blank;
2222
2223       if Error_Msg_Node_1 = Standard_Void_Type then
2224          Set_Msg_Str ("package or procedure name");
2225          return;
2226
2227       elsif Error_Msg_Node_1 = Standard_Exception_Type then
2228          Set_Msg_Str ("exception name");
2229          return;
2230
2231       elsif     Error_Msg_Node_1 = Any_Access
2232         or else Error_Msg_Node_1 = Any_Array
2233         or else Error_Msg_Node_1 = Any_Boolean
2234         or else Error_Msg_Node_1 = Any_Character
2235         or else Error_Msg_Node_1 = Any_Composite
2236         or else Error_Msg_Node_1 = Any_Discrete
2237         or else Error_Msg_Node_1 = Any_Fixed
2238         or else Error_Msg_Node_1 = Any_Integer
2239         or else Error_Msg_Node_1 = Any_Modular
2240         or else Error_Msg_Node_1 = Any_Numeric
2241         or else Error_Msg_Node_1 = Any_Real
2242         or else Error_Msg_Node_1 = Any_Scalar
2243         or else Error_Msg_Node_1 = Any_String
2244       then
2245          Get_Unqualified_Decoded_Name_String (Chars (Error_Msg_Node_1));
2246          Set_Msg_Name_Buffer;
2247          return;
2248
2249       elsif Error_Msg_Node_1 = Universal_Real then
2250          Set_Msg_Str ("type universal real");
2251          return;
2252
2253       elsif Error_Msg_Node_1 = Universal_Integer then
2254          Set_Msg_Str ("type universal integer");
2255          return;
2256
2257       elsif Error_Msg_Node_1 = Universal_Fixed then
2258          Set_Msg_Str ("type universal fixed");
2259          return;
2260       end if;
2261
2262       --  Special case of anonymous array
2263
2264       if Nkind (Error_Msg_Node_1) in N_Entity
2265         and then Is_Array_Type (Error_Msg_Node_1)
2266         and then Present (Related_Array_Object (Error_Msg_Node_1))
2267       then
2268          Set_Msg_Str ("type of ");
2269          Set_Msg_Node (Related_Array_Object (Error_Msg_Node_1));
2270          Set_Msg_Str (" declared");
2271          Set_Msg_Insertion_Line_Number
2272            (Sloc (Related_Array_Object (Error_Msg_Node_1)), Flag);
2273          return;
2274       end if;
2275
2276       --  If we fall through, it is not a special case, so first output
2277       --  the name of the type, preceded by private for a private type
2278
2279       if Is_Private_Type (Error_Msg_Node_1) then
2280          Set_Msg_Str ("private type ");
2281       else
2282          Set_Msg_Str ("type ");
2283       end if;
2284
2285       Ent := Error_Msg_Node_1;
2286
2287       if Is_Internal_Name (Chars (Ent)) then
2288          Unwind_Internal_Type (Ent);
2289       end if;
2290
2291       --  Types in Standard are displayed as "Standard.name"
2292
2293       if Sloc (Ent) <= Standard_Location then
2294          Set_Msg_Quote;
2295          Set_Msg_Str ("Standard.");
2296          Set_Msg_Node (Ent);
2297          Add_Class;
2298          Set_Msg_Quote;
2299
2300       --  Types in other language defined units are displayed as
2301       --  "package-name.type-name"
2302
2303       elsif
2304         Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Ent)))
2305       then
2306          Get_Unqualified_Decoded_Name_String
2307            (Unit_Name (Get_Source_Unit (Ent)));
2308          Name_Len := Name_Len - 2;
2309          Set_Msg_Quote;
2310          Set_Casing (Mixed_Case);
2311          Set_Msg_Name_Buffer;
2312          Set_Msg_Char ('.');
2313          Set_Casing (Mixed_Case);
2314          Set_Msg_Node (Ent);
2315          Add_Class;
2316          Set_Msg_Quote;
2317
2318       --  All other types display as "type name" defined at line xxx
2319       --  possibly qualified if qualification is requested.
2320
2321       else
2322          Set_Msg_Quote;
2323          Set_Qualification (Error_Msg_Qual_Level, Ent);
2324          Set_Msg_Node (Ent);
2325          Add_Class;
2326
2327          --  If Ent is an anonymous subprogram type, there is no name
2328          --  to print, so remove enclosing quotes.
2329
2330          if Buffer_Ends_With ("""") then
2331             Buffer_Remove ("""");
2332          else
2333             Set_Msg_Quote;
2334          end if;
2335       end if;
2336
2337       --  If the original type did not come from a predefined
2338       --  file, add the location where the type was defined.
2339
2340       if Sloc (Error_Msg_Node_1) > Standard_Location
2341         and then
2342           not Is_Predefined_File_Name
2343                 (Unit_File_Name (Get_Source_Unit (Error_Msg_Node_1)))
2344       then
2345          Set_Msg_Str (" defined");
2346          Set_Msg_Insertion_Line_Number (Sloc (Error_Msg_Node_1), Flag);
2347
2348       --  If it did come from a predefined file, deal with the case where
2349       --  this was a file with a generic instantiation from elsewhere.
2350
2351       else
2352          if Sloc (Error_Msg_Node_1) > Standard_Location then
2353             declare
2354                Iloc : constant Source_Ptr :=
2355                         Instantiation_Location (Sloc (Error_Msg_Node_1));
2356
2357             begin
2358                if Iloc /= No_Location
2359                  and then not Suppress_Instance_Location
2360                then
2361                   Set_Msg_Str (" from instance");
2362                   Set_Msg_Insertion_Line_Number (Iloc, Flag);
2363                end if;
2364             end;
2365          end if;
2366       end if;
2367    end Set_Msg_Insertion_Type_Reference;
2368
2369    ---------------------------------
2370    -- Set_Msg_Insertion_Unit_Name --
2371    ---------------------------------
2372
2373    procedure Set_Msg_Insertion_Unit_Name (Suffix : Boolean := True) is
2374    begin
2375       if Error_Msg_Unit_1 = No_Unit_Name then
2376          null;
2377
2378       elsif Error_Msg_Unit_1 = Error_Unit_Name then
2379          Set_Msg_Blank;
2380          Set_Msg_Str ("<error>");
2381
2382       else
2383          Get_Unit_Name_String (Error_Msg_Unit_1, Suffix);
2384          Set_Msg_Blank;
2385          Set_Msg_Quote;
2386          Set_Msg_Name_Buffer;
2387          Set_Msg_Quote;
2388       end if;
2389
2390       --  The following assignment ensures that a second percent insertion
2391       --  character will correspond to the Error_Msg_Unit_2 parameter. We
2392       --  suppress possible validity checks in case operating in -gnatVa mode,
2393       --  and Error_Msg_Unit_2 is not needed and has not been set.
2394
2395       declare
2396          pragma Suppress (Range_Check);
2397       begin
2398          Error_Msg_Unit_1 := Error_Msg_Unit_2;
2399       end;
2400    end Set_Msg_Insertion_Unit_Name;
2401
2402    ------------------
2403    -- Set_Msg_Node --
2404    ------------------
2405
2406    procedure Set_Msg_Node (Node : Node_Id) is
2407       Ent : Entity_Id;
2408       Nam : Name_Id;
2409
2410    begin
2411       if Nkind (Node) = N_Designator then
2412          Set_Msg_Node (Name (Node));
2413          Set_Msg_Char ('.');
2414          Set_Msg_Node (Identifier (Node));
2415          return;
2416
2417       elsif Nkind (Node) = N_Defining_Program_Unit_Name then
2418          Set_Msg_Node (Name (Node));
2419          Set_Msg_Char ('.');
2420          Set_Msg_Node (Defining_Identifier (Node));
2421          return;
2422
2423       elsif Nkind (Node) = N_Selected_Component then
2424          Set_Msg_Node (Prefix (Node));
2425          Set_Msg_Char ('.');
2426          Set_Msg_Node (Selector_Name (Node));
2427          return;
2428       end if;
2429
2430       --  The only remaining possibilities are identifiers, defining
2431       --  identifiers, pragmas, and pragma argument associations.
2432
2433       if Nkind (Node) = N_Pragma then
2434          Nam := Pragma_Name (Node);
2435
2436       --  The other cases have Chars fields, and we want to test for possible
2437       --  internal names, which generally represent something gone wrong. An
2438       --  exception is the case of internal type names, where we try to find a
2439       --  reasonable external representation for the external name
2440
2441       elsif Is_Internal_Name (Chars (Node))
2442         and then
2443           ((Is_Entity_Name (Node)
2444                           and then Present (Entity (Node))
2445                           and then Is_Type (Entity (Node)))
2446               or else
2447            (Nkind (Node) = N_Defining_Identifier and then Is_Type (Node)))
2448       then
2449          if Nkind (Node) = N_Identifier then
2450             Ent := Entity (Node);
2451          else
2452             Ent := Node;
2453          end if;
2454
2455          --  If the type is the designated type of an access_to_subprogram,
2456          --  there is no name to provide in the call.
2457
2458          if Ekind (Ent) = E_Subprogram_Type then
2459             return;
2460          else
2461             Unwind_Internal_Type (Ent);
2462             Nam := Chars (Ent);
2463          end if;
2464
2465       --  If not internal name, just use name in Chars field
2466
2467       else
2468          Nam := Chars (Node);
2469       end if;
2470
2471       --  At this stage, the name to output is in Nam
2472
2473       Get_Unqualified_Decoded_Name_String (Nam);
2474
2475       --  Remove trailing upper case letters from the name (useful for
2476       --  dealing with some cases of internal names.
2477
2478       while Name_Len > 1 and then Name_Buffer (Name_Len) in 'A' .. 'Z' loop
2479          Name_Len := Name_Len  - 1;
2480       end loop;
2481
2482       --  If we have any of the names from standard that start with the
2483       --  characters "any " (e.g. Any_Type), then kill the message since
2484       --  almost certainly it is a junk cascaded message.
2485
2486       if Name_Len > 4
2487         and then Name_Buffer (1 .. 4) = "any "
2488       then
2489          Kill_Message := True;
2490       end if;
2491
2492       --  Now we have to set the proper case. If we have a source location
2493       --  then do a check to see if the name in the source is the same name
2494       --  as the name in the Names table, except for possible differences
2495       --  in case, which is the case when we can copy from the source.
2496
2497       declare
2498          Src_Loc : constant Source_Ptr := Sloc (Error_Msg_Node_1);
2499          Sbuffer : Source_Buffer_Ptr;
2500          Ref_Ptr : Integer;
2501          Src_Ptr : Source_Ptr;
2502
2503       begin
2504          Ref_Ptr := 1;
2505          Src_Ptr := Src_Loc;
2506
2507          --  For standard locations, always use mixed case
2508
2509          if Src_Loc <= No_Location
2510            or else Sloc (Node) <= No_Location
2511          then
2512             Set_Casing (Mixed_Case);
2513
2514          else
2515             --  Determine if the reference we are dealing with corresponds
2516             --  to text at the point of the error reference. This will often
2517             --  be the case for simple identifier references, and is the case
2518             --  where we can copy the spelling from the source.
2519
2520             Sbuffer := Source_Text (Get_Source_File_Index (Src_Loc));
2521
2522             while Ref_Ptr <= Name_Len loop
2523                exit when
2524                  Fold_Lower (Sbuffer (Src_Ptr)) /=
2525                  Fold_Lower (Name_Buffer (Ref_Ptr));
2526                Ref_Ptr := Ref_Ptr + 1;
2527                Src_Ptr := Src_Ptr + 1;
2528             end loop;
2529
2530             --  If we get through the loop without a mismatch, then output
2531             --  the name the way it is spelled in the source program
2532
2533             if Ref_Ptr > Name_Len then
2534                Src_Ptr := Src_Loc;
2535
2536                for J in 1 .. Name_Len loop
2537                   Name_Buffer (J) := Sbuffer (Src_Ptr);
2538                   Src_Ptr := Src_Ptr + 1;
2539                end loop;
2540
2541             --  Otherwise set the casing using the default identifier casing
2542
2543             else
2544                Set_Casing (Identifier_Casing (Flag_Source), Mixed_Case);
2545             end if;
2546          end if;
2547       end;
2548
2549       Set_Msg_Name_Buffer;
2550       Add_Class;
2551    end Set_Msg_Node;
2552
2553    ------------------
2554    -- Set_Msg_Text --
2555    ------------------
2556
2557    procedure Set_Msg_Text (Text : String; Flag : Source_Ptr) is
2558       C : Character;   -- Current character
2559       P : Natural;     -- Current index;
2560
2561    begin
2562       Manual_Quote_Mode := False;
2563       Is_Unconditional_Msg := False;
2564       Msglen := 0;
2565       Flag_Source := Get_Source_File_Index (Flag);
2566       P := Text'First;
2567
2568       while P <= Text'Last loop
2569          C := Text (P);
2570          P := P + 1;
2571
2572          --  Check for insertion character or sequence
2573
2574          case C is
2575             when '%' =>
2576                if P <= Text'Last and then Text (P) = '%' then
2577                   P := P + 1;
2578                   Set_Msg_Insertion_Name_Literal;
2579                else
2580                   Set_Msg_Insertion_Name;
2581                end if;
2582
2583             when '$' =>
2584                if P <= Text'Last and then Text (P) = '$' then
2585                   P := P + 1;
2586                   Set_Msg_Insertion_Unit_Name (Suffix => False);
2587
2588                else
2589                   Set_Msg_Insertion_Unit_Name;
2590                end if;
2591
2592             when '{' =>
2593                Set_Msg_Insertion_File_Name;
2594
2595             when '}' =>
2596                Set_Msg_Insertion_Type_Reference (Flag);
2597
2598             when '*' =>
2599                Set_Msg_Insertion_Reserved_Name;
2600
2601             when '&' =>
2602                Set_Msg_Insertion_Node;
2603
2604             when '#' =>
2605                Set_Msg_Insertion_Line_Number (Error_Msg_Sloc, Flag);
2606
2607             when '\' =>
2608                Continuation := True;
2609
2610                if Text (P) = '\' then
2611                   Continuation_New_Line := True;
2612                   P := P + 1;
2613                end if;
2614
2615             when '@' =>
2616                Set_Msg_Insertion_Column;
2617
2618             when '>' =>
2619                Set_Msg_Insertion_Run_Time_Name;
2620
2621             when '^' =>
2622                Set_Msg_Insertion_Uint;
2623
2624             when '`' =>
2625                Manual_Quote_Mode := not Manual_Quote_Mode;
2626                Set_Msg_Char ('"');
2627
2628             when '!' =>
2629                Is_Unconditional_Msg := True;
2630
2631             when '?' =>
2632                null; -- already dealt with
2633
2634             when '<' =>
2635                null; -- already dealt with
2636
2637             when '|' =>
2638                null; -- already dealt with
2639
2640             when ''' =>
2641                Set_Msg_Char (Text (P));
2642                P := P + 1;
2643
2644             when '~' =>
2645                Set_Msg_Str (Error_Msg_String (1 .. Error_Msg_Strlen));
2646
2647             --  Upper case letter
2648
2649             when 'A' .. 'Z' =>
2650
2651                --  Start of reserved word if two or more
2652
2653                if P <= Text'Last and then Text (P) in 'A' .. 'Z' then
2654                   P := P - 1;
2655                   Set_Msg_Insertion_Reserved_Word (Text, P);
2656
2657                --  Single upper case letter is just inserted
2658
2659                else
2660                   Set_Msg_Char (C);
2661                end if;
2662
2663             --  Normal character with no special treatment
2664
2665             when others =>
2666                Set_Msg_Char (C);
2667          end case;
2668       end loop;
2669
2670       VMS_Convert;
2671    end Set_Msg_Text;
2672
2673    ----------------
2674    -- Set_Posted --
2675    ----------------
2676
2677    procedure Set_Posted (N : Node_Id) is
2678       P : Node_Id;
2679
2680    begin
2681       if Is_Serious_Error then
2682
2683          --  We always set Error_Posted on the node itself
2684
2685          Set_Error_Posted (N);
2686
2687          --  If it is a subexpression, then set Error_Posted on parents
2688          --  up to and including the first non-subexpression construct. This
2689          --  helps avoid cascaded error messages within a single expression.
2690
2691          P := N;
2692          loop
2693             P := Parent (P);
2694             exit when No (P);
2695             Set_Error_Posted (P);
2696             exit when Nkind (P) not in N_Subexpr;
2697          end loop;
2698
2699          --  A special check, if we just posted an error on an attribute
2700          --  definition clause, then also set the entity involved as posted.
2701          --  For example, this stops complaining about the alignment after
2702          --  complaining about the size, which is likely to be useless.
2703
2704          if Nkind (P) = N_Attribute_Definition_Clause then
2705             if Is_Entity_Name (Name (P)) then
2706                Set_Error_Posted (Entity (Name (P)));
2707             end if;
2708          end if;
2709       end if;
2710    end Set_Posted;
2711
2712    -----------------------
2713    -- Set_Qualification --
2714    -----------------------
2715
2716    procedure Set_Qualification (N : Nat; E : Entity_Id) is
2717    begin
2718       if N /= 0 and then Scope (E) /= Standard_Standard then
2719          Set_Qualification (N - 1, Scope (E));
2720          Set_Msg_Node (Scope (E));
2721          Set_Msg_Char ('.');
2722       end if;
2723    end Set_Qualification;
2724
2725    ------------------------
2726    -- Special_Msg_Delete --
2727    ------------------------
2728
2729    function Special_Msg_Delete
2730      (Msg : String;
2731       N   : Node_Or_Entity_Id;
2732       E   : Node_Or_Entity_Id) return Boolean
2733    is
2734    begin
2735       --  Never delete messages in -gnatdO mode
2736
2737       if Debug_Flag_OO then
2738          return False;
2739
2740       --  When an atomic object refers to a non-atomic type in the same
2741       --  scope, we implicitly make the type atomic. In the non-error
2742       --  case this is surely safe (and in fact prevents an error from
2743       --  occurring if the type is not atomic by default). But if the
2744       --  object cannot be made atomic, then we introduce an extra junk
2745       --  message by this manipulation, which we get rid of here.
2746
2747       --  We identify this case by the fact that it references a type for
2748       --  which Is_Atomic is set, but there is no Atomic pragma setting it.
2749
2750       elsif Msg = "atomic access to & cannot be guaranteed"
2751         and then Is_Type (E)
2752         and then Is_Atomic (E)
2753         and then No (Get_Rep_Pragma (E, Name_Atomic))
2754       then
2755          return True;
2756
2757       --  When a size is wrong for a frozen type there is no explicit
2758       --  size clause, and other errors have occurred, suppress the
2759       --  message, since it is likely that this size error is a cascaded
2760       --  result of other errors. The reason we eliminate unfrozen types
2761       --  is that messages issued before the freeze type are for sure OK.
2762
2763       elsif Msg = "size for& too small, minimum allowed is ^"
2764         and then Is_Frozen (E)
2765         and then Serious_Errors_Detected > 0
2766         and then Nkind (N) /= N_Component_Clause
2767         and then Nkind (Parent (N)) /= N_Component_Clause
2768         and then
2769           No (Get_Attribute_Definition_Clause (E, Attribute_Size))
2770         and then
2771           No (Get_Attribute_Definition_Clause (E, Attribute_Object_Size))
2772         and then
2773           No (Get_Attribute_Definition_Clause (E, Attribute_Value_Size))
2774       then
2775          return True;
2776
2777       --  All special tests complete, so go ahead with message
2778
2779       else
2780          return False;
2781       end if;
2782    end Special_Msg_Delete;
2783
2784    --------------------------
2785    -- Unwind_Internal_Type --
2786    --------------------------
2787
2788    procedure Unwind_Internal_Type (Ent : in out Entity_Id) is
2789       Derived : Boolean := False;
2790       Mchar   : Character;
2791       Old_Ent : Entity_Id;
2792
2793    begin
2794       --  Undo placement of a quote, since we will put it back later
2795
2796       Mchar := Msg_Buffer (Msglen);
2797
2798       if Mchar = '"' then
2799          Msglen := Msglen - 1;
2800       end if;
2801
2802       --  The loop here deals with recursive types, we are trying to
2803       --  find a related entity that is not an implicit type. Note
2804       --  that the check with Old_Ent stops us from getting "stuck".
2805       --  Also, we don't output the "type derived from" message more
2806       --  than once in the case where we climb up multiple levels.
2807
2808       loop
2809          Old_Ent := Ent;
2810
2811          --  Implicit access type, use directly designated type
2812          --  In Ada 2005, the designated type may be an anonymous access to
2813          --  subprogram, in which case we can only point to its definition.
2814
2815          if Is_Access_Type (Ent) then
2816             if Ekind (Ent) = E_Access_Subprogram_Type
2817               or else Ekind (Ent) = E_Anonymous_Access_Subprogram_Type
2818               or else Is_Access_Protected_Subprogram_Type (Ent)
2819             then
2820                Ent := Directly_Designated_Type (Ent);
2821
2822                if not Comes_From_Source (Ent) then
2823                   if Buffer_Ends_With ("type ") then
2824                      Buffer_Remove ("type ");
2825                   end if;
2826
2827                   Set_Msg_Str ("access to subprogram with profile ");
2828
2829                elsif Ekind (Ent) = E_Function then
2830                   Set_Msg_Str ("access to function ");
2831                else
2832                   Set_Msg_Str ("access to procedure ");
2833                end if;
2834                exit;
2835
2836             --  Type is access to object, named or anonymous
2837
2838             else
2839                Set_Msg_Str ("access to ");
2840                Ent := Directly_Designated_Type (Ent);
2841             end if;
2842
2843          --  Classwide type
2844
2845          elsif Is_Class_Wide_Type (Ent) then
2846             Class_Flag := True;
2847             Ent := Root_Type (Ent);
2848
2849          --  Use base type if this is a subtype
2850
2851          elsif Ent /= Base_Type (Ent) then
2852             Buffer_Remove ("type ");
2853
2854             --  Avoid duplication "subtype of subtype of", and also replace
2855             --  "derived from subtype of" simply by "derived from"
2856
2857             if not Buffer_Ends_With ("subtype of ")
2858               and then not Buffer_Ends_With ("derived from ")
2859             then
2860                Set_Msg_Str ("subtype of ");
2861             end if;
2862
2863             Ent := Base_Type (Ent);
2864
2865          --  If this is a base type with a first named subtype, use the
2866          --  first named subtype instead. This is not quite accurate in
2867          --  all cases, but it makes too much noise to be accurate and
2868          --  add 'Base in all cases. Note that we only do this is the
2869          --  first named subtype is not itself an internal name. This
2870          --  avoids the obvious loop (subtype->basetype->subtype) which
2871          --  would otherwise occur!)
2872
2873          elsif Present (Freeze_Node (Ent))
2874            and then Present (First_Subtype_Link (Freeze_Node (Ent)))
2875            and then
2876              not Is_Internal_Name
2877                    (Chars (First_Subtype_Link (Freeze_Node (Ent))))
2878          then
2879             Ent := First_Subtype_Link (Freeze_Node (Ent));
2880
2881          --  Otherwise use root type
2882
2883          else
2884             if not Derived then
2885                Buffer_Remove ("type ");
2886
2887                --  Test for "subtype of type derived from" which seems
2888                --  excessive and is replaced by simply "type derived from"
2889
2890                Buffer_Remove ("subtype of");
2891
2892                --  Avoid duplication "type derived from type derived from"
2893
2894                if not Buffer_Ends_With ("type derived from ") then
2895                   Set_Msg_Str ("type derived from ");
2896                end if;
2897
2898                Derived := True;
2899             end if;
2900
2901             Ent := Etype (Ent);
2902          end if;
2903
2904          --  If we are stuck in a loop, get out and settle for the internal
2905          --  name after all. In this case we set to kill the message if it
2906          --  is not the first error message (we really try hard not to show
2907          --  the dirty laundry of the implementation to the poor user!)
2908
2909          if Ent = Old_Ent then
2910             Kill_Message := True;
2911             exit;
2912          end if;
2913
2914          --  Get out if we finally found a non-internal name to use
2915
2916          exit when not Is_Internal_Name (Chars (Ent));
2917       end loop;
2918
2919       if Mchar = '"' then
2920          Set_Msg_Char ('"');
2921       end if;
2922    end Unwind_Internal_Type;
2923
2924    -----------------
2925    -- VMS_Convert --
2926    -----------------
2927
2928    procedure VMS_Convert is
2929       P : Natural;
2930       L : Natural;
2931       N : Natural;
2932
2933    begin
2934       if not OpenVMS then
2935          return;
2936       end if;
2937
2938       P := Msg_Buffer'First;
2939       loop
2940          if P >= Msglen then
2941             return;
2942          end if;
2943
2944          if Msg_Buffer (P) = '-' then
2945             for G in Gnames'Range loop
2946                L := Gnames (G)'Length;
2947
2948                --  See if we have "-ggg switch", where ggg is Gnames entry
2949
2950                if P + L + 7 <= Msglen
2951                  and then Msg_Buffer (P + 1 .. P + L) = Gnames (G).all
2952                  and then Msg_Buffer (P + L + 1 .. P + L + 7) = " switch"
2953                then
2954                   --  Replace by "/vvv qualifier", where vvv is Vnames entry
2955
2956                   N := Vnames (G)'Length;
2957                   Msg_Buffer (P + N + 11 .. Msglen + N - L + 3) :=
2958                     Msg_Buffer (P + L + 8 .. Msglen);
2959                   Msg_Buffer (P) := '/';
2960                   Msg_Buffer (P + 1 .. P + N) := Vnames (G).all;
2961                   Msg_Buffer (P + N + 1 .. P + N + 10) := " qualifier";
2962                   P := P + N + 10;
2963                   Msglen := Msglen + N - L + 3;
2964                   exit;
2965                end if;
2966             end loop;
2967          end if;
2968
2969          P := P + 1;
2970       end loop;
2971    end VMS_Convert;
2972
2973 end Errout;