OSDN Git Service

New Language: Ada
[pf3gnuchains/gcc-fork.git] / gcc / ada / comperr.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              C O M P E R R                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --                            $Revision: 1.57 $
10 --                                                                          --
11 --          Copyright (C) 1992-2001 Free Software Foundation, Inc.          --
12 --                                                                          --
13 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
14 -- terms of the  GNU General Public License as published  by the Free Soft- --
15 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
16 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
19 -- for  more details.  You should have  received  a copy of the GNU General --
20 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
21 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
22 -- MA 02111-1307, USA.                                                      --
23 --                                                                          --
24 -- GNAT was originally developed  by the GNAT team at  New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
26 --                                                                          --
27 ------------------------------------------------------------------------------
28
29 --  This package contains routines called when a fatal internal compiler
30 --  error is detected. Calls to these routines cause termination of the
31 --  current compilation with appropriate error output.
32
33 with Atree;    use Atree;
34 with Debug;    use Debug;
35 with Errout;   use Errout;
36 with Fname;    use Fname;
37 with Gnatvsn;  use Gnatvsn;
38 with Lib;      use Lib;
39 with Namet;    use Namet;
40 with Osint;    use Osint;
41 with Output;   use Output;
42 with Sinput;   use Sinput;
43 with Sprint;   use Sprint;
44 with Sdefault; use Sdefault;
45 with Treepr;   use Treepr;
46 with Types;    use Types;
47
48 with Ada.Exceptions; use Ada.Exceptions;
49
50 with System.Soft_Links; use System.Soft_Links;
51
52 package body Comperr is
53
54    -----------------------
55    -- Local Subprograms --
56    -----------------------
57
58    procedure Repeat_Char (Char : Character; Col : Nat; After : Character);
59    --  Output Char until current column is at or past Col, and then output
60    --  the character given by After (if column is already past Col on entry,
61    --  then the effect is simply to output the After character).
62
63    --------------------
64    -- Compiler_Abort --
65    --------------------
66
67    procedure Compiler_Abort
68      (X    : String;
69       Code : Integer := 0)
70    is
71       procedure End_Line;
72       --  Add blanks up to column 76, and then a final vertical bar
73
74       procedure End_Line is
75       begin
76          Repeat_Char (' ', 76, '|');
77          Write_Eol;
78       end End_Line;
79
80       Public_Version : constant Boolean := (Gnat_Version_String (5) = 'p');
81
82    --  Start of processing for Compiler_Abort
83
84    begin
85       --  If errors have already occured, then we guess that the abort may
86       --  well be caused by previous errors, and we don't make too much fuss
87       --  about it, since we want to let the programmer fix the errors first.
88
89       --  Debug flag K disables this behavior (useful for debugging)
90
91       if Errors_Detected /= 0 and then not Debug_Flag_K then
92          Errout.Finalize;
93
94          Set_Standard_Error;
95          Write_Str ("compilation abandoned due to previous error");
96          Write_Eol;
97
98          Set_Standard_Output;
99          Source_Dump;
100          Tree_Dump;
101          Exit_Program (E_Errors);
102
103       --  Otherwise give message with details of the abort
104
105       else
106          Set_Standard_Error;
107
108          --  Generate header for bug box
109
110          Write_Char ('+');
111          Repeat_Char ('=', 29, 'G');
112          Write_Str ("NAT BUG DETECTED");
113          Repeat_Char ('=', 76, '+');
114          Write_Eol;
115
116          --  Output GNAT version identification
117
118          Write_Str ("| ");
119          Write_Str (Gnat_Version_String);
120          Write_Str (" (");
121
122          --  Output target name, deleting junk final reverse slash
123
124          if Target_Name.all (Target_Name.all'Last) = '\'
125            or else Target_Name.all (Target_Name.all'Last) = '/'
126          then
127             Write_Str (Target_Name.all (1 .. Target_Name.all'Last - 1));
128          else
129             Write_Str (Target_Name.all);
130          end if;
131
132          --  Output identification of error
133
134          Write_Str (") ");
135
136          if X'Length + Column > 76 then
137             if Code < 0 then
138                Write_Str ("GCC error:");
139             end if;
140
141             End_Line;
142
143             Write_Str ("| ");
144          end if;
145
146          if X'Length > 70 then
147             declare
148                Last_Blank : Integer := 70;
149
150             begin
151                for P in 40 .. 69 loop
152                   if X (P) = ' ' then
153                      Last_Blank := P;
154                   end if;
155                end loop;
156
157                Write_Str (X (1 .. Last_Blank));
158                End_Line;
159                Write_Str ("|    ");
160                Write_Str (X (Last_Blank + 1 .. X'Length));
161             end;
162          else
163             Write_Str (X);
164          end if;
165
166          if Code > 0 then
167             Write_Str (", Code=");
168             Write_Int (Int (Code));
169
170          elsif Code = 0 then
171
172             --  For exception case, get exception message from the TSD. Note
173             --  that it would be neater and cleaner to pass the exception
174             --  message (obtained from Exception_Message) as a parameter to
175             --  Compiler_Abort, but we can't do this quite yet since it would
176             --  cause bootstrap path problems for 3.10 to 3.11.
177
178             Write_Char (' ');
179             Write_Str (Exception_Message (Get_Current_Excep.all.all));
180          end if;
181
182          End_Line;
183
184          --  Output source location information
185
186          if Sloc (Current_Error_Node) <= Standard_Location
187            or else Sloc (Current_Error_Node) = No_Location
188          then
189             Write_Str ("| No source file position information available");
190             End_Line;
191          else
192             Write_Str ("| Error detected at ");
193             Write_Location (Sloc (Current_Error_Node));
194             End_Line;
195          end if;
196
197          --  There are two cases now. If the file gnat_bug.box exists,
198          --  we use the contents of this file at this point.
199
200          declare
201             Lo  : Source_Ptr;
202             Hi  : Source_Ptr;
203             Src : Source_Buffer_Ptr;
204
205          begin
206             Namet.Unlock;
207             Name_Buffer (1 .. 12) := "gnat_bug.box";
208             Name_Len := 12;
209             Read_Source_File (Name_Enter, 0, Hi, Src);
210
211             --  If we get a Src file, we use it
212
213             if Src /= null then
214                Lo := 0;
215
216                Outer : while Lo < Hi loop
217                   Write_Str ("| ");
218
219                   Inner : loop
220                      exit Inner when Src (Lo) = ASCII.CR
221                        or else Src (Lo) = ASCII.LF;
222                      Write_Char (Src (Lo));
223                      Lo := Lo + 1;
224                   end loop Inner;
225
226                   End_Line;
227
228                   while Lo <= Hi
229                     and then (Src (Lo) = ASCII.CR
230                                 or else Src (Lo) = ASCII.LF)
231                   loop
232                      Lo := Lo + 1;
233                   end loop;
234                end loop Outer;
235
236             --  Otherwise we use the standard fixed text
237
238             else
239                Write_Str
240                  ("| Please submit bug report by email to report@gnat.com.");
241                End_Line;
242
243                if not Public_Version then
244                   Write_Str
245                     ("| Use a subject line meaningful to you" &
246                      " and us to track the bug.");
247                   End_Line;
248
249                   Write_Str
250                     ("| (include your customer number #nnn " &
251                      "in the subject line).");
252                   End_Line;
253                end if;
254
255                Write_Str
256                  ("| Include the entire contents of this bug " &
257                   "box in the report.");
258                End_Line;
259
260                Write_Str
261                  ("| Include the exact gcc or gnatmake command " &
262                   "that you entered.");
263                End_Line;
264
265                Write_Str
266                  ("| Also include sources listed below in gnatchop format");
267                End_Line;
268
269                Write_Str
270                  ("| (concatenated together with no headers between files).");
271                End_Line;
272
273                if Public_Version then
274                   Write_Str
275                     ("| (use plain ASCII or MIME attachment).");
276                   End_Line;
277
278                   Write_Str
279                     ("| See gnatinfo.txt for full info on procedure " &
280                      "for submitting bugs.");
281                   End_Line;
282
283                else
284                   Write_Str
285                     ("| (use plain ASCII or MIME attachment, or FTP "
286                      & "to your customer directory).");
287                   End_Line;
288
289                   Write_Str
290                     ("| See README.GNATPRO for full info on procedure " &
291                      "for submitting bugs.");
292                   End_Line;
293                end if;
294             end if;
295          end;
296
297          --  Complete output of bug box
298
299          Write_Char ('+');
300          Repeat_Char ('=', 76, '+');
301          Write_Eol;
302
303          if Debug_Flag_3 then
304             Write_Eol;
305             Write_Eol;
306             Print_Tree_Node (Current_Error_Node);
307             Write_Eol;
308          end if;
309
310          Write_Eol;
311
312          Write_Line ("Please include these source files with error report");
313          Write_Eol;
314
315          for U in Main_Unit .. Last_Unit loop
316             begin
317                if not Is_Internal_File_Name
318                         (File_Name (Source_Index (U)))
319                then
320                   Write_Name (Full_File_Name (Source_Index (U)));
321                   Write_Eol;
322                end if;
323
324             --  No point in double bug box if we blow up trying to print
325             --  the list of file names! Output informative msg and quit.
326
327             exception
328                when others =>
329                   Write_Str ("list may be incomplete");
330                   exit;
331             end;
332          end loop;
333
334          Write_Eol;
335          Set_Standard_Output;
336
337          Tree_Dump;
338          Source_Dump;
339          raise Unrecoverable_Error;
340       end if;
341
342    end Compiler_Abort;
343
344    -----------------
345    -- Repeat_Char --
346    -----------------
347
348    procedure Repeat_Char (Char : Character; Col : Nat; After : Character) is
349    begin
350       while Column < Col loop
351          Write_Char (Char);
352       end loop;
353
354       Write_Char (After);
355    end Repeat_Char;
356
357 end Comperr;