PR c++/3637
[official-gcc.git] / gcc / ada / comperr.adb
blobb3d868fab607e67fa5e377ade5fddad7569340d6
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.1 $
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 ------------------------------------------------------------------------------
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.
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;
48 with Ada.Exceptions; use Ada.Exceptions;
50 with System.Soft_Links; use System.Soft_Links;
52 package body Comperr is
54 ----------------
55 -- Local Data --
56 ----------------
58 Abort_In_Progress : Boolean := False;
59 -- Used to prevent runaway recursion if something segfaults
60 -- while processing a previous abort.
62 -----------------------
63 -- Local Subprograms --
64 -----------------------
66 procedure Repeat_Char (Char : Character; Col : Nat; After : Character);
67 -- Output Char until current column is at or past Col, and then output
68 -- the character given by After (if column is already past Col on entry,
69 -- then the effect is simply to output the After character).
71 --------------------
72 -- Compiler_Abort --
73 --------------------
75 procedure Compiler_Abort
76 (X : String;
77 Code : Integer := 0)
79 procedure End_Line;
80 -- Add blanks up to column 76, and then a final vertical bar
82 procedure End_Line is
83 begin
84 Repeat_Char (' ', 76, '|');
85 Write_Eol;
86 end End_Line;
88 Public_Version : constant Boolean := (Gnat_Version_String (5) = 'p');
90 -- Start of processing for Compiler_Abort
92 begin
93 -- Prevent recursion through Compiler_Abort, e.g. via SIGSEGV.
95 if Abort_In_Progress then
96 Exit_Program (E_Abort);
97 end if;
99 Abort_In_Progress := True;
101 -- If errors have already occured, then we guess that the abort may
102 -- well be caused by previous errors, and we don't make too much fuss
103 -- about it, since we want to let the programmer fix the errors first.
105 -- Debug flag K disables this behavior (useful for debugging)
107 if Errors_Detected /= 0 and then not Debug_Flag_K then
108 Errout.Finalize;
110 Set_Standard_Error;
111 Write_Str ("compilation abandoned due to previous error");
112 Write_Eol;
114 Set_Standard_Output;
115 Source_Dump;
116 Tree_Dump;
117 Exit_Program (E_Errors);
119 -- Otherwise give message with details of the abort
121 else
122 Set_Standard_Error;
124 -- Generate header for bug box
126 Write_Char ('+');
127 Repeat_Char ('=', 29, 'G');
128 Write_Str ("NAT BUG DETECTED");
129 Repeat_Char ('=', 76, '+');
130 Write_Eol;
132 -- Output GNAT version identification
134 Write_Str ("| ");
135 Write_Str (Gnat_Version_String);
136 Write_Str (" (");
138 -- Output target name, deleting junk final reverse slash
140 if Target_Name.all (Target_Name.all'Last) = '\'
141 or else Target_Name.all (Target_Name.all'Last) = '/'
142 then
143 Write_Str (Target_Name.all (1 .. Target_Name.all'Last - 1));
144 else
145 Write_Str (Target_Name.all);
146 end if;
148 -- Output identification of error
150 Write_Str (") ");
152 if X'Length + Column > 76 then
153 if Code < 0 then
154 Write_Str ("GCC error:");
155 end if;
157 End_Line;
159 Write_Str ("| ");
160 end if;
162 if X'Length > 70 then
163 declare
164 Last_Blank : Integer := 70;
166 begin
167 for P in 40 .. 69 loop
168 if X (P) = ' ' then
169 Last_Blank := P;
170 end if;
171 end loop;
173 Write_Str (X (1 .. Last_Blank));
174 End_Line;
175 Write_Str ("| ");
176 Write_Str (X (Last_Blank + 1 .. X'Length));
177 end;
178 else
179 Write_Str (X);
180 end if;
182 if Code > 0 then
183 Write_Str (", Code=");
184 Write_Int (Int (Code));
186 elsif Code = 0 then
188 -- For exception case, get exception message from the TSD. Note
189 -- that it would be neater and cleaner to pass the exception
190 -- message (obtained from Exception_Message) as a parameter to
191 -- Compiler_Abort, but we can't do this quite yet since it would
192 -- cause bootstrap path problems for 3.10 to 3.11.
194 Write_Char (' ');
195 Write_Str (Exception_Message (Get_Current_Excep.all.all));
196 end if;
198 End_Line;
200 -- Output source location information
202 if Sloc (Current_Error_Node) <= Standard_Location
203 or else Sloc (Current_Error_Node) = No_Location
204 then
205 Write_Str ("| No source file position information available");
206 End_Line;
207 else
208 Write_Str ("| Error detected at ");
209 Write_Location (Sloc (Current_Error_Node));
210 End_Line;
211 end if;
213 -- There are two cases now. If the file gnat_bug.box exists,
214 -- we use the contents of this file at this point.
216 declare
217 Lo : Source_Ptr;
218 Hi : Source_Ptr;
219 Src : Source_Buffer_Ptr;
221 begin
222 Namet.Unlock;
223 Name_Buffer (1 .. 12) := "gnat_bug.box";
224 Name_Len := 12;
225 Read_Source_File (Name_Enter, 0, Hi, Src);
227 -- If we get a Src file, we use it
229 if Src /= null then
230 Lo := 0;
232 Outer : while Lo < Hi loop
233 Write_Str ("| ");
235 Inner : loop
236 exit Inner when Src (Lo) = ASCII.CR
237 or else Src (Lo) = ASCII.LF;
238 Write_Char (Src (Lo));
239 Lo := Lo + 1;
240 end loop Inner;
242 End_Line;
244 while Lo <= Hi
245 and then (Src (Lo) = ASCII.CR
246 or else Src (Lo) = ASCII.LF)
247 loop
248 Lo := Lo + 1;
249 end loop;
250 end loop Outer;
252 -- Otherwise we use the standard fixed text
254 else
255 Write_Str
256 ("| Please submit bug report by email to report@gnat.com.");
257 End_Line;
259 if not Public_Version then
260 Write_Str
261 ("| Use a subject line meaningful to you" &
262 " and us to track the bug.");
263 End_Line;
265 Write_Str
266 ("| (include your customer number #nnn " &
267 "in the subject line).");
268 End_Line;
269 end if;
271 Write_Str
272 ("| Include the entire contents of this bug " &
273 "box in the report.");
274 End_Line;
276 Write_Str
277 ("| Include the exact gcc or gnatmake command " &
278 "that you entered.");
279 End_Line;
281 Write_Str
282 ("| Also include sources listed below in gnatchop format");
283 End_Line;
285 Write_Str
286 ("| (concatenated together with no headers between files).");
287 End_Line;
289 if Public_Version then
290 Write_Str
291 ("| (use plain ASCII or MIME attachment).");
292 End_Line;
294 Write_Str
295 ("| See gnatinfo.txt for full info on procedure " &
296 "for submitting bugs.");
297 End_Line;
299 else
300 Write_Str
301 ("| (use plain ASCII or MIME attachment, or FTP "
302 & "to your customer directory).");
303 End_Line;
305 Write_Str
306 ("| See README.GNATPRO for full info on procedure " &
307 "for submitting bugs.");
308 End_Line;
309 end if;
310 end if;
311 end;
313 -- Complete output of bug box
315 Write_Char ('+');
316 Repeat_Char ('=', 76, '+');
317 Write_Eol;
319 if Debug_Flag_3 then
320 Write_Eol;
321 Write_Eol;
322 Print_Tree_Node (Current_Error_Node);
323 Write_Eol;
324 end if;
326 Write_Eol;
328 Write_Line ("Please include these source files with error report");
329 Write_Eol;
331 for U in Main_Unit .. Last_Unit loop
332 begin
333 if not Is_Internal_File_Name
334 (File_Name (Source_Index (U)))
335 then
336 Write_Name (Full_File_Name (Source_Index (U)));
337 Write_Eol;
338 end if;
340 -- No point in double bug box if we blow up trying to print
341 -- the list of file names! Output informative msg and quit.
343 exception
344 when others =>
345 Write_Str ("list may be incomplete");
346 exit;
347 end;
348 end loop;
350 Write_Eol;
351 Set_Standard_Output;
353 Tree_Dump;
354 Source_Dump;
355 raise Unrecoverable_Error;
356 end if;
358 end Compiler_Abort;
360 -----------------
361 -- Repeat_Char --
362 -----------------
364 procedure Repeat_Char (Char : Character; Col : Nat; After : Character) is
365 begin
366 while Column < Col loop
367 Write_Char (Char);
368 end loop;
370 Write_Char (After);
371 end Repeat_Char;
373 end Comperr;