OSDN Git Service

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