OSDN Git Service

Daily bump.
[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-2008, 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 Targparm; use Targparm;
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       --  Generate a "standard" error message instead of a bug box in case of
117       --  .NET compiler, since we do not support all constructs of the
118       --  language. Of course ideally, we should detect this before bombing
119       --  on e.g. an assertion error, but in practice most of these bombs
120       --  are due to a legitimate case of a construct not being supported (in
121       --  a sense they all are, since for sure we are not supporting something
122       --  if we bomb!) By giving this message, we provide a more reasonable
123       --  practical interface, since giving scary bug boxes on unsupported
124       --  features is definitely not helpful.
125
126       --  Note that the call to Error_Msg_N below sets Serious_Errors_Detected
127       --  to 1, so we use the regular mechanism below in order to display a
128       --  "compilation abandoned" message and exit, so we still know we have
129       --  this case (and -gnatdk can still be used to get the bug box).
130
131       if VM_Target = CLI_Target
132         and then Serious_Errors_Detected = 0
133         and then not Debug_Flag_K
134         and then Sloc (Current_Error_Node) > No_Location
135       then
136          Error_Msg_N
137            ("unsupported construct in this context",
138             Current_Error_Node);
139       end if;
140
141       --  If any errors have already occurred, then we guess that the abort
142       --  may well be caused by previous errors, and we don't make too much
143       --  fuss about it, since we want to let programmer fix the errors first.
144
145       --  Debug flag K disables this behavior (useful for debugging)
146
147       if Serious_Errors_Detected /= 0 and then not Debug_Flag_K then
148          Errout.Finalize (Last_Call => True);
149          Errout.Output_Messages;
150
151          Set_Standard_Error;
152          Write_Str ("compilation abandoned due to previous error");
153          Write_Eol;
154
155          Set_Standard_Output;
156          Source_Dump;
157          Tree_Dump;
158          Exit_Program (E_Errors);
159
160       --  Otherwise give message with details of the abort
161
162       else
163          Set_Standard_Error;
164
165          --  Generate header for bug box
166
167          Write_Char ('+');
168          Repeat_Char ('=', 29, 'G');
169          Write_Str ("NAT BUG DETECTED");
170          Repeat_Char ('=', 76, '+');
171          Write_Eol;
172
173          --  Output GNAT version identification
174
175          Write_Str ("| ");
176          Write_Str (Gnat_Version_String);
177          Write_Str (" (");
178
179          --  Output target name, deleting junk final reverse slash
180
181          if Target_Name.all (Target_Name.all'Last) = '\'
182            or else Target_Name.all (Target_Name.all'Last) = '/'
183          then
184             Write_Str (Target_Name.all (1 .. Target_Name.all'Last - 1));
185          else
186             Write_Str (Target_Name.all);
187          end if;
188
189          --  Output identification of error
190
191          Write_Str (") ");
192
193          if X'Length + Column > 76 then
194             if Code < 0 then
195                Write_Str ("GCC error:");
196             end if;
197
198             End_Line;
199
200             Write_Str ("| ");
201          end if;
202
203          if X'Length > 70 then
204             declare
205                Last_Blank : Integer := 70;
206
207             begin
208                for P in 39 .. 68 loop
209                   if X (XF + P) = ' ' then
210                      Last_Blank := P;
211                   end if;
212                end loop;
213
214                Write_Str (X (XF .. XF - 1 + Last_Blank));
215                End_Line;
216                Write_Str ("|    ");
217                Write_Str (X (XF + Last_Blank .. X'Last));
218             end;
219          else
220             Write_Str (X);
221          end if;
222
223          if Code > 0 then
224             Write_Str (", Code=");
225             Write_Int (Int (Code));
226
227          elsif Code = 0 then
228
229             --  For exception case, get exception message from the TSD. Note
230             --  that it would be neater and cleaner to pass the exception
231             --  message (obtained from Exception_Message) as a parameter to
232             --  Compiler_Abort, but we can't do this quite yet since it would
233             --  cause bootstrap path problems for 3.10 to 3.11.
234
235             Write_Char (' ');
236             Write_Str (Exception_Message (Get_Current_Excep.all.all));
237          end if;
238
239          End_Line;
240
241          --  Output source location information
242
243          if Sloc (Current_Error_Node) <= No_Location then
244             if Fallback_Loc'Length > 0 then
245                Write_Str ("| Error detected around ");
246                Write_Str (Fallback_Loc);
247             else
248                Write_Str ("| No source file position information available");
249             end if;
250
251             End_Line;
252          else
253             Write_Str ("| Error detected at ");
254             Write_Location (Sloc (Current_Error_Node));
255             End_Line;
256          end if;
257
258          --  There are two cases now. If the file gnat_bug.box exists,
259          --  we use the contents of this file at this point.
260
261          declare
262             Lo  : Source_Ptr;
263             Hi  : Source_Ptr;
264             Src : Source_Buffer_Ptr;
265
266          begin
267             Namet.Unlock;
268             Name_Buffer (1 .. 12) := "gnat_bug.box";
269             Name_Len := 12;
270             Read_Source_File (Name_Enter, 0, Hi, Src);
271
272             --  If we get a Src file, we use it
273
274             if Src /= null then
275                Lo := 0;
276
277                Outer : while Lo < Hi loop
278                   Write_Str ("| ");
279
280                   Inner : loop
281                      exit Inner when Src (Lo) = ASCII.CR
282                        or else Src (Lo) = ASCII.LF;
283                      Write_Char (Src (Lo));
284                      Lo := Lo + 1;
285                   end loop Inner;
286
287                   End_Line;
288
289                   while Lo <= Hi
290                     and then (Src (Lo) = ASCII.CR
291                                 or else Src (Lo) = ASCII.LF)
292                   loop
293                      Lo := Lo + 1;
294                   end loop;
295                end loop Outer;
296
297             --  Otherwise we use the standard fixed text
298
299             else
300                if Is_FSF_Version then
301                   Write_Str
302                     ("| Please submit a bug report; see" &
303                      " http://gcc.gnu.org/bugs.html.");
304                   End_Line;
305
306                elsif Is_GPL_Version then
307
308                   Write_Str
309                     ("| Please submit a bug report by email " &
310                      "to report@adacore.com.");
311                   End_Line;
312
313                   Write_Str
314                     ("| GAP members can alternatively use GNAT Tracker:");
315                   End_Line;
316
317                   Write_Str
318                     ("| http://www.adacore.com/ " &
319                      "section 'send a report'.");
320                   End_Line;
321
322                   Write_Str
323                     ("| See gnatinfo.txt for full info on procedure " &
324                      "for submitting bugs.");
325                   End_Line;
326
327                else
328                   Write_Str
329                     ("| Please submit a bug report using GNAT Tracker:");
330                   End_Line;
331
332                   Write_Str
333                     ("| http://www.adacore.com/gnattracker/ " &
334                      "section 'send a report'.");
335                   End_Line;
336
337                   Write_Str
338                     ("| alternatively submit a bug report by email " &
339                      "to report@adacore.com,");
340                   End_Line;
341
342                   Write_Str
343                     ("| including your customer number #nnn " &
344                      "in the subject line.");
345                   End_Line;
346                end if;
347
348                Write_Str
349                  ("| Use a subject line meaningful to you" &
350                   " and us to track the bug.");
351                End_Line;
352
353                Write_Str
354                  ("| Include the entire contents of this bug " &
355                   "box in the report.");
356                End_Line;
357
358                Write_Str
359                  ("| Include the exact gcc or gnatmake command " &
360                   "that you entered.");
361                End_Line;
362
363                Write_Str
364                  ("| Also include sources listed below in gnatchop format");
365                End_Line;
366
367                Write_Str
368                  ("| (concatenated together with no headers between files).");
369                End_Line;
370
371                if not Is_FSF_Version then
372                   Write_Str
373                     ("| Use plain ASCII or MIME attachment.");
374                   End_Line;
375                end if;
376             end if;
377          end;
378
379          --  Complete output of bug box
380
381          Write_Char ('+');
382          Repeat_Char ('=', 76, '+');
383          Write_Eol;
384
385          if Debug_Flag_3 then
386             Write_Eol;
387             Write_Eol;
388             Print_Tree_Node (Current_Error_Node);
389             Write_Eol;
390          end if;
391
392          Write_Eol;
393
394          Write_Line ("Please include these source files with error report");
395          Write_Line ("Note that list may not be accurate in some cases, ");
396          Write_Line ("so please double check that the problem can still ");
397          Write_Line ("be reproduced with the set of files listed.");
398          Write_Eol;
399
400          for U in Main_Unit .. Last_Unit loop
401             begin
402                if not Is_Internal_File_Name
403                         (File_Name (Source_Index (U)))
404                then
405                   Write_Name (Full_File_Name (Source_Index (U)));
406                   Write_Eol;
407                end if;
408
409             --  No point in double bug box if we blow up trying to print
410             --  the list of file names! Output informative msg and quit.
411
412             exception
413                when others =>
414                   Write_Str ("list may be incomplete");
415                   exit;
416             end;
417          end loop;
418
419          Write_Eol;
420          Set_Standard_Output;
421
422          Tree_Dump;
423          Source_Dump;
424          raise Unrecoverable_Error;
425       end if;
426
427    end Compiler_Abort;
428
429    -----------------
430    -- Repeat_Char --
431    -----------------
432
433    procedure Repeat_Char (Char : Character; Col : Nat; After : Character) is
434    begin
435       while Column < Col loop
436          Write_Char (Char);
437       end loop;
438
439       Write_Char (After);
440    end Repeat_Char;
441
442 end Comperr;