Debian Codegraph @

gcc/ada/comperr.adb

raw
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-2025, 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 error
27 -- is detected. Calls to these routines cause termination of the current
28 -- compilation with appropriate error output.
29
30 with Atree; use Atree;
31 with Debug; use Debug;
32 with Errout; use Errout;
33 with Generate_Minimal_Reproducer;
34 with Gnatvsn; use Gnatvsn;
35 with Lib; use Lib;
36 with Namet; use Namet;
37 with Opt; use Opt;
38 with Osint; use Osint;
39 with Output; use Output;
40 with Sinfo; use Sinfo;
41 with Sinfo.Nodes; use Sinfo.Nodes;
42 with Sinput; use Sinput;
43 with Sprint; use Sprint;
44 with Sdefault; use Sdefault;
45 with Treepr; use Treepr;
46 with Types; use Types;
47
48 with Ada.Exceptions; use Ada.Exceptions;
49
50 with System.OS_Lib; use System.OS_Lib;
51 with System.Soft_Links; use System.Soft_Links;
52
53 package body Comperr is
54
55 ----------------
56 -- Local Data --
57 ----------------
58
59 Abort_In_Progress : Boolean := False;
60 -- Used to prevent runaway recursion if something segfaults
61 -- while processing a previous abort.
62
63 -----------------------
64 -- Local Subprograms --
65 -----------------------
66
67 procedure Repeat_Char (Char : Character; Col : Nat; After : Character);
68 -- Output Char until current column is at or past Col, and then output
69 -- the character given by After (if column is already past Col on entry,
70 -- then the effect is simply to output the After character).
71
72 --------------------
73 -- Compiler_Abort --
74 --------------------
75
76 procedure Compiler_Abort
77 (X : String;
78 Fallback_Loc : String := "";
79 From_GCC : Boolean := False)
80 is
81 -- The procedures below output a "bug box" with information about
82 -- the cause of the compiler abort and about the preferred method
83 -- of reporting bugs. The default is a bug box appropriate for
84 -- the FSF version of GNAT, but there are specializations for
85 -- the GNATPRO and Public releases by AdaCore.
86
87 XF : constant Positive := X'First;
88 -- Start index, usually 1, but we won't assume this
89
90 procedure End_Line;
91 -- Add blanks up to column 76, and then a final vertical bar
92
93 --------------
94 -- End_Line --
95 --------------
96
97 procedure End_Line is
98 begin
99 Repeat_Char (' ', 76, '|');
100 Write_Eol;
101 end End_Line;
102
103 Is_GPL_Version : constant Boolean := Gnatvsn.Build_Type = GPL;
104 Is_FSF_Version : constant Boolean := Gnatvsn.Build_Type = FSF;
105
106 -- Start of processing for Compiler_Abort
107
108 begin
109 Cancel_Special_Output;
110
111 -- Prevent recursion through Compiler_Abort, e.g. via SIGSEGV
112
113 if Abort_In_Progress then
114 Exit_Program (E_Abort);
115 end if;
116
117 Abort_In_Progress := True;
118
119 -- Generate a "standard" error message instead of a bug box in case
120 -- of CodePeer rather than generating a bug box, friendlier.
121
122 -- Note that the call to Error_Msg_N below sets Serious_Errors_Detected
123 -- to 1, so we use the regular mechanism below in order to display a
124 -- "compilation abandoned" message and exit, so we still know we have
125 -- this case (and -gnatdk can still be used to get the bug box).
126
127 if CodePeer_Mode
128 and then Serious_Errors_Detected = 0
129 and then not Debug_Flag_K
130 and then Sloc (Current_Error_Node) > No_Location
131 then
132 Error_Msg_N ("cannot generate 'S'C'I'L", Current_Error_Node);
133 end if;
134
135 -- If we are in CodePeer mode, we must also delete SCIL files
136
137 if CodePeer_Mode then
138 Delete_SCIL_Files;
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 (Target_Name'Last) in '/' | '\' then
182 Write_Str (Target_Name (1 .. Target_Name'Last - 1));
183 else
184 Write_Str (Target_Name.all);
185 end if;
186
187 -- Output identification of error
188
189 Write_Str (") ");
190
191 if X'Length + Column > 76 then
192 if From_GCC then
193 Write_Str ("GCC error:");
194 end if;
195
196 End_Line;
197
198 Write_Str ("| ");
199 end if;
200
201 if X'Length > 70 then
202 declare
203 Last_Blank : Integer := 70;
204
205 begin
206 for P in 39 .. 68 loop
207 if X (XF + P) = ' ' then
208 Last_Blank := P;
209 end if;
210 end loop;
211
212 Write_Str (X (XF .. XF - 1 + Last_Blank));
213 End_Line;
214 Write_Str ("| ");
215 Write_Str (X (XF + Last_Blank .. X'Last));
216 end;
217 else
218 Write_Str (X);
219 end if;
220
221 if not From_GCC then
222
223 -- For exception case, get exception message from the TSD. Note
224 -- that it would be neater and cleaner to pass the exception
225 -- message (obtained from Exception_Message) as a parameter to
226 -- Compiler_Abort, but we can't do this quite yet since it would
227 -- cause bootstrap path problems for 3.10 to 3.11.
228
229 Write_Char (' ');
230 Write_Str (Exception_Message (Get_Current_Excep.all.all));
231 end if;
232
233 End_Line;
234
235 -- Output source location information
236
237 if Sloc (Current_Error_Node) <= No_Location then
238 if Fallback_Loc'Length > 0 then
239 Write_Str ("| Error detected around ");
240 Write_Str (Fallback_Loc);
241 else
242 Write_Str ("| No source file position information available");
243 end if;
244
245 End_Line;
246
247 else
248 Write_Str ("| Error detected at ");
249 Write_Location (Sloc (Current_Error_Node));
250 End_Line;
251 end if;
252
253 Write_Str ("| Compiling ");
254 Write_Str (Get_First_Main_File_Name);
255 End_Line;
256
257 -- There are two cases now. If the file gnat_bug.box exists,
258 -- we use the contents of this file at this point.
259
260 declare
261 FD : File_Descriptor;
262 Lo : Source_Ptr;
263 Hi : Source_Ptr;
264 Src : Source_Buffer_Ptr;
265
266 begin
267 Namet.Unlock_If_Locked;
268 Name_Buffer (1 .. 12) := "gnat_bug.box";
269 Name_Len := 12;
270 Read_Source_File (Name_Enter, 0, Hi, Src, FD);
271
272 -- If we get a Src file, we use it
273
274 if not Null_Source_Buffer_Ptr (Src) 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 " https://gcc.gnu.org/bugs/ .");
304 End_Line;
305
306 elsif Is_GPL_Version then
307
308 Write_Str
309 ("| Please submit a bug report by email " &
310 "to [email protected].");
311 End_Line;
312
313 Write_Str
314 ("| GAP members can alternatively use GNAT Tracker:");
315 End_Line;
316
317 Write_Str
318 ("| https://www.adacore.com/login?mode=gap " &
319 "section 'Create New Ticket'.");
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 ("| https://www.adacore.com/login " &
334 "section 'Create New Ticket'.");
335 End_Line;
336
337 Write_Str
338 ("| Or submit a bug report by email " &
339 "to [email protected]");
340 End_Line;
341
342 Write_Str
343 ("| and include 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 command that you entered.");
360 End_Line;
361
362 Write_Str
363 ("| Also include sources listed below.");
364 End_Line;
365
366 if not Is_FSF_Version then
367 Write_Str
368 ("| Use plain ASCII or MIME attachment(s).");
369 End_Line;
370 end if;
371 end if;
372 end;
373
374 -- Complete output of bug box
375
376 Write_Char ('+');
377 Repeat_Char ('=', 76, '+');
378 Write_Eol;
379
380 if Debug_Flag_3 then
381 Write_Eol;
382 Write_Eol;
383 Print_Tree_Node (Current_Error_Node);
384 Write_Eol;
385 end if;
386
387 Write_Eol;
388
389 Write_Line ("Please include these source files with error report");
390 Write_Line ("Note that list may not be accurate in some cases, ");
391 Write_Line ("so please double check that the problem can still ");
392 Write_Line ("be reproduced with the set of files listed.");
393 Write_Line ("Consider also -gnatd.n switch (see debug.adb).");
394 Write_Eol;
395
396 begin
397 Dump_Source_File_Names;
398
399 -- If we blow up trying to print the list of file names, just output
400 -- informative msg and continue.
401
402 exception
403 when others =>
404 Write_Str ("list may be incomplete");
405 end;
406
407 begin
408 if Debug_Flag_Underscore_M then
409 Generate_Minimal_Reproducer;
410 end if;
411 exception
412 when others =>
413 Write_Str ("failed to generate reproducer");
414 end;
415
416 Write_Eol;
417 Set_Standard_Output;
418
419 Tree_Dump;
420 Sinput.Unlock; -- so Source_Dump can modify it
421 Source_Dump;
422 raise Unrecoverable_Error;
423 end if;
424 end Compiler_Abort;
425
426 -----------------------
427 -- Delete_SCIL_Files --
428 -----------------------
429
430 procedure Delete_SCIL_Files is
431 Main : Node_Id;
432 Unit_Name : Node_Id;
433
434 Success : Boolean;
435 pragma Warnings (Off, "modified by call");
436
437 procedure Decode_Name_Buffer;
438 -- Replace "__" by "." in Name_Buffer, and adjust Name_Len accordingly
439
440 ------------------------
441 -- Decode_Name_Buffer --
442 ------------------------
443
444 procedure Decode_Name_Buffer is
445 J : Natural;
446 K : Natural;
447
448 begin
449 J := 1;
450 K := 0;
451 while J <= Name_Len loop
452 K := K + 1;
453
454 if J < Name_Len
455 and then Name_Buffer (J) = '_'
456 and then Name_Buffer (J + 1) = '_'
457 then
458 Name_Buffer (K) := '.';
459 J := J + 1;
460 else
461 Name_Buffer (K) := Name_Buffer (J);
462 end if;
463
464 J := J + 1;
465 end loop;
466
467 Name_Len := K;
468 end Decode_Name_Buffer;
469
470 -- Start of processing for Delete_SCIL_Files
471
472 begin
473 -- If parsing was not successful, no Main_Unit is available, so return
474 -- immediately.
475
476 if Main_Source_File <= No_Source_File then
477 return;
478 end if;
479
480 -- Retrieve unit name, and remove old versions of SCIL/<unit>.scil and
481 -- SCIL/<unit>__body.scil, ditto for .scilx files.
482
483 Main := Unit (Cunit (Main_Unit));
484
485 case Nkind (Main) is
486 when N_Package_Declaration
487 | N_Subprogram_Body
488 | N_Subprogram_Declaration
489 | N_Subprogram_Renaming_Declaration
490 =>
491 Unit_Name := Defining_Unit_Name (Specification (Main));
492
493 when N_Package_Body =>
494 Unit_Name := Corresponding_Spec (Main);
495
496 when N_Package_Instantiation
497 | N_Package_Renaming_Declaration
498 =>
499 Unit_Name := Defining_Unit_Name (Main);
500
501 -- No SCIL file generated for generic unit declarations
502
503 when N_Generic_Declaration
504 | N_Generic_Renaming_Declaration
505 =>
506 return;
507
508 -- Should never happen, but can be ignored in production
509
510 when others =>
511 pragma Assert (False);
512 return;
513 end case;
514
515 case Nkind (Unit_Name) is
516 when N_Defining_Identifier =>
517 Get_Name_String (Chars (Unit_Name));
518
519 when N_Defining_Program_Unit_Name =>
520 Get_Name_String (Chars (Defining_Identifier (Unit_Name)));
521 Decode_Name_Buffer;
522
523 -- Should never happen, but can be ignored in production
524
525 when others =>
526 pragma Assert (False);
527 return;
528 end case;
529
530 Delete_File
531 ("SCIL/" & Name_Buffer (1 .. Name_Len) & ".scil", Success);
532 Delete_File
533 ("SCIL/" & Name_Buffer (1 .. Name_Len) & ".scilx", Success);
534 Delete_File
535 ("SCIL/" & Name_Buffer (1 .. Name_Len) & "__body.scil", Success);
536 Delete_File
537 ("SCIL/" & Name_Buffer (1 .. Name_Len) & "__body.scilx", Success);
538 end Delete_SCIL_Files;
539
540 -----------------
541 -- Repeat_Char --
542 -----------------
543
544 procedure Repeat_Char (Char : Character; Col : Nat; After : Character) is
545 begin
546 while Column < Col loop
547 Write_Char (Char);
548 end loop;
549
550 Write_Char (After);
551 end Repeat_Char;
552 end Comperr;