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