OSDN Git Service

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