OSDN Git Service

2007-04-20 Robert Dewar <dewar@adacore.com>
[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 --          Copyright (C) 1992-2006, 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 2,  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 COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by AdaCore.                         --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 --  This package contains routines called when a fatal internal compiler
28 --  error is detected. Calls to these routines cause termination of the
29 --  current compilation with appropriate error output.
30
31 with Atree;    use Atree;
32 with Debug;    use Debug;
33 with Errout;   use Errout;
34 with Fname;    use Fname;
35 with Gnatvsn;  use Gnatvsn;
36 with Lib;      use Lib;
37 with Namet;    use Namet;
38 with Osint;    use Osint;
39 with Output;   use Output;
40 with Sinput;   use Sinput;
41 with Sprint;   use Sprint;
42 with Sdefault; use Sdefault;
43 with Treepr;   use Treepr;
44 with Types;    use Types;
45
46 with Ada.Exceptions; use Ada.Exceptions;
47
48 with System.Soft_Links; use System.Soft_Links;
49
50 package body Comperr is
51
52    ----------------
53    -- Local Data --
54    ----------------
55
56    Abort_In_Progress : Boolean := False;
57    --  Used to prevent runaway recursion if something segfaults
58    --  while processing a previous abort.
59
60    -----------------------
61    -- Local Subprograms --
62    -----------------------
63
64    procedure Repeat_Char (Char : Character; Col : Nat; After : Character);
65    --  Output Char until current column is at or past Col, and then output
66    --  the character given by After (if column is already past Col on entry,
67    --  then the effect is simply to output the After character).
68
69    --------------------
70    -- Compiler_Abort --
71    --------------------
72
73    procedure Compiler_Abort
74      (X            : String;
75       Code         : Integer := 0;
76       Fallback_Loc : String := "")
77    is
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, but there are specializations for
82       --  the GNATPRO and Public releases by AdaCore.
83
84       XF : constant Positive := X'First;
85       --  Start index, usually 1, but we won't assume this
86
87       procedure End_Line;
88       --  Add blanks up to column 76, and then a final vertical bar
89
90       --------------
91       -- End_Line --
92       --------------
93
94       procedure End_Line is
95       begin
96          Repeat_Char (' ', 76, '|');
97          Write_Eol;
98       end End_Line;
99
100       Is_GPL_Version : constant Boolean := Gnatvsn.Build_Type = GPL;
101       Is_FSF_Version : constant Boolean := Gnatvsn.Build_Type = FSF;
102
103    --  Start of processing for Compiler_Abort
104
105    begin
106       Cancel_Special_Output;
107
108       --  Prevent recursion through Compiler_Abort, e.g. via SIGSEGV
109
110       if Abort_In_Progress then
111          Exit_Program (E_Abort);
112       end if;
113
114       Abort_In_Progress := True;
115
116       --  If any errors have already occurred, then we guess that the abort
117       --  may well be caused by previous errors, and we don't make too much
118       --  fuss about it, since we want to let programmer fix the errors first.
119
120       --  Debug flag K disables this behavior (useful for debugging)
121
122       if Serious_Errors_Detected /= 0 and then not Debug_Flag_K then
123          Errout.Finalize;
124          Errout.Output_Messages;
125
126          Set_Standard_Error;
127          Write_Str ("compilation abandoned due to previous error");
128          Write_Eol;
129
130          Set_Standard_Output;
131          Source_Dump;
132          Tree_Dump;
133          Exit_Program (E_Errors);
134
135       --  Otherwise give message with details of the abort
136
137       else
138          Set_Standard_Error;
139
140          --  Generate header for bug box
141
142          Write_Char ('+');
143          Repeat_Char ('=', 29, 'G');
144          Write_Str ("NAT BUG DETECTED");
145          Repeat_Char ('=', 76, '+');
146          Write_Eol;
147
148          --  Output GNAT version identification
149
150          Write_Str ("| ");
151          Write_Str (Gnat_Version_String);
152          Write_Str (" (");
153
154          --  Output target name, deleting junk final reverse slash
155
156          if Target_Name.all (Target_Name.all'Last) = '\'
157            or else Target_Name.all (Target_Name.all'Last) = '/'
158          then
159             Write_Str (Target_Name.all (1 .. Target_Name.all'Last - 1));
160          else
161             Write_Str (Target_Name.all);
162          end if;
163
164          --  Output identification of error
165
166          Write_Str (") ");
167
168          if X'Length + Column > 76 then
169             if Code < 0 then
170                Write_Str ("GCC error:");
171             end if;
172
173             End_Line;
174
175             Write_Str ("| ");
176          end if;
177
178          if X'Length > 70 then
179             declare
180                Last_Blank : Integer := 70;
181
182             begin
183                for P in 39 .. 68 loop
184                   if X (XF + P) = ' ' then
185                      Last_Blank := P;
186                   end if;
187                end loop;
188
189                Write_Str (X (XF .. XF - 1 + Last_Blank));
190                End_Line;
191                Write_Str ("|    ");
192                Write_Str (X (XF + Last_Blank .. X'Last));
193             end;
194          else
195             Write_Str (X);
196          end if;
197
198          if Code > 0 then
199             Write_Str (", Code=");
200             Write_Int (Int (Code));
201
202          elsif Code = 0 then
203
204             --  For exception case, get exception message from the TSD. Note
205             --  that it would be neater and cleaner to pass the exception
206             --  message (obtained from Exception_Message) as a parameter to
207             --  Compiler_Abort, but we can't do this quite yet since it would
208             --  cause bootstrap path problems for 3.10 to 3.11.
209
210             Write_Char (' ');
211             Write_Str (Exception_Message (Get_Current_Excep.all.all));
212          end if;
213
214          End_Line;
215
216          --  Output source location information
217
218          if Sloc (Current_Error_Node) <= No_Location then
219             if Fallback_Loc'Length > 0 then
220                Write_Str ("| Error detected around ");
221                Write_Str (Fallback_Loc);
222             else
223                Write_Str ("| No source file position information available");
224             end if;
225
226             End_Line;
227          else
228             Write_Str ("| Error detected at ");
229             Write_Location (Sloc (Current_Error_Node));
230             End_Line;
231          end if;
232
233          --  There are two cases now. If the file gnat_bug.box exists,
234          --  we use the contents of this file at this point.
235
236          declare
237             Lo  : Source_Ptr;
238             Hi  : Source_Ptr;
239             Src : Source_Buffer_Ptr;
240
241          begin
242             Namet.Unlock;
243             Name_Buffer (1 .. 12) := "gnat_bug.box";
244             Name_Len := 12;
245             Read_Source_File (Name_Enter, 0, Hi, Src);
246
247             --  If we get a Src file, we use it
248
249             if Src /= null then
250                Lo := 0;
251
252                Outer : while Lo < Hi loop
253                   Write_Str ("| ");
254
255                   Inner : loop
256                      exit Inner when Src (Lo) = ASCII.CR
257                        or else Src (Lo) = ASCII.LF;
258                      Write_Char (Src (Lo));
259                      Lo := Lo + 1;
260                   end loop Inner;
261
262                   End_Line;
263
264                   while Lo <= Hi
265                     and then (Src (Lo) = ASCII.CR
266                                 or else Src (Lo) = ASCII.LF)
267                   loop
268                      Lo := Lo + 1;
269                   end loop;
270                end loop Outer;
271
272             --  Otherwise we use the standard fixed text
273
274             else
275                if Is_FSF_Version then
276                   Write_Str
277                     ("| Please submit a bug report; see" &
278                      " http://gcc.gnu.org/bugs.html.");
279                   End_Line;
280
281                elsif Is_GPL_Version then
282
283                   Write_Str
284                     ("| Please submit a bug report by email " &
285                      "to report@adacore.com.");
286                   End_Line;
287
288                   Write_Str
289                     ("| GAP members can alternatively use GNAT Tracker:");
290                   End_Line;
291
292                   Write_Str
293                     ("| http://www.adacore.com/ " &
294                      "section 'send a report'.");
295                   End_Line;
296
297                   Write_Str
298                     ("| See gnatinfo.txt for full info on procedure " &
299                      "for submitting bugs.");
300                   End_Line;
301
302                else
303                   Write_Str
304                     ("| Please submit a bug report using GNAT Tracker:");
305                   End_Line;
306
307                   Write_Str
308                     ("| http://www.adacore.com/gnattracker/ " &
309                      "section 'send a report'.");
310                   End_Line;
311
312                   Write_Str
313                     ("| alternatively submit a bug report by email " &
314                      "to report@adacore.com,");
315                   End_Line;
316
317                   Write_Str
318                     ("| including your customer number #nnn " &
319                      "in the subject line.");
320                   End_Line;
321                end if;
322
323                Write_Str
324                  ("| Use a subject line meaningful to you" &
325                   " and us to track the bug.");
326                End_Line;
327
328                Write_Str
329                  ("| Include the entire contents of this bug " &
330                   "box in the report.");
331                End_Line;
332
333                Write_Str
334                  ("| Include the exact gcc or gnatmake command " &
335                   "that you entered.");
336                End_Line;
337
338                Write_Str
339                  ("| Also include sources listed below in gnatchop format");
340                End_Line;
341
342                Write_Str
343                  ("| (concatenated together with no headers between files).");
344                End_Line;
345
346                if not Is_FSF_Version then
347                   Write_Str
348                     ("| Use plain ASCII or MIME attachment.");
349                   End_Line;
350                end if;
351             end if;
352          end;
353
354          --  Complete output of bug box
355
356          Write_Char ('+');
357          Repeat_Char ('=', 76, '+');
358          Write_Eol;
359
360          if Debug_Flag_3 then
361             Write_Eol;
362             Write_Eol;
363             Print_Tree_Node (Current_Error_Node);
364             Write_Eol;
365          end if;
366
367          Write_Eol;
368
369          Write_Line ("Please include these source files with error report");
370          Write_Line ("Note that list may not be accurate in some cases, ");
371          Write_Line ("so please double check that the problem can still ");
372          Write_Line ("be reproduced with the set of files listed.");
373          Write_Eol;
374
375          for U in Main_Unit .. Last_Unit loop
376             begin
377                if not Is_Internal_File_Name
378                         (File_Name (Source_Index (U)))
379                then
380                   Write_Name (Full_File_Name (Source_Index (U)));
381                   Write_Eol;
382                end if;
383
384             --  No point in double bug box if we blow up trying to print
385             --  the list of file names! Output informative msg and quit.
386
387             exception
388                when others =>
389                   Write_Str ("list may be incomplete");
390                   exit;
391             end;
392          end loop;
393
394          Write_Eol;
395          Set_Standard_Output;
396
397          Tree_Dump;
398          Source_Dump;
399          raise Unrecoverable_Error;
400       end if;
401
402    end Compiler_Abort;
403
404    -----------------
405    -- Repeat_Char --
406    -----------------
407
408    procedure Repeat_Char (Char : Character; Col : Nat; After : Character) is
409    begin
410       while Column < Col loop
411          Write_Char (Char);
412       end loop;
413
414       Write_Char (After);
415    end Repeat_Char;
416
417 end Comperr;