comperr.adb 16.3 KB
Newer Older
Richard Kenner committed
1 2 3 4 5 6 7 8
------------------------------------------------------------------------------
--                                                                          --
--                         GNAT COMPILER COMPONENTS                         --
--                                                                          --
--                              C O M P E R R                               --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
--          Copyright (C) 1992-2015, Free Software Foundation, Inc.         --
Richard Kenner committed
10 11 12
--                                                                          --
-- GNAT is free software;  you can  redistribute it  and/or modify it under --
-- 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- --
Richard Kenner committed
14 15 16 17
-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
-- for  more details.  You should have  received  a copy of the GNU General --
18 19
-- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
-- http://www.gnu.org/licenses for a complete copy of the license.          --
Richard Kenner committed
20 21
--                                                                          --
-- GNAT was originally developed  by the GNAT team at  New York University. --
Arnaud Charlet committed
22
-- Extensive contributions were provided by AdaCore.                         --
Richard Kenner committed
23 24 25
--                                                                          --
------------------------------------------------------------------------------

Arnaud Charlet committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
--  This package contains routines called when a fatal internal compiler error
--  is detected. Calls to these routines cause termination of the current
--  compilation with appropriate error output.

with Atree;    use Atree;
with Debug;    use Debug;
with Errout;   use Errout;
with Gnatvsn;  use Gnatvsn;
with Lib;      use Lib;
with Namet;    use Namet;
with Opt;      use Opt;
with Osint;    use Osint;
with Output;   use Output;
with Sinfo;    use Sinfo;
with Sinput;   use Sinput;
with Sprint;   use Sprint;
with Sdefault; use Sdefault;
with Treepr;   use Treepr;
with Types;    use Types;
Richard Kenner committed
45 46 47

with Ada.Exceptions; use Ada.Exceptions;

Arnaud Charlet committed
48
with System.OS_Lib;     use System.OS_Lib;
Richard Kenner committed
49 50 51 52
with System.Soft_Links; use System.Soft_Links;

package body Comperr is

53 54 55 56 57 58 59 60
   ----------------
   -- Local Data --
   ----------------

   Abort_In_Progress : Boolean := False;
   --  Used to prevent runaway recursion if something segfaults
   --  while processing a previous abort.

Richard Kenner committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74
   -----------------------
   -- Local Subprograms --
   -----------------------

   procedure Repeat_Char (Char : Character; Col : Nat; After : Character);
   --  Output Char until current column is at or past Col, and then output
   --  the character given by After (if column is already past Col on entry,
   --  then the effect is simply to output the After character).

   --------------------
   -- Compiler_Abort --
   --------------------

   procedure Compiler_Abort
75
     (X            : String;
76 77
      Fallback_Loc : String  := "";
      From_GCC     : Boolean := False)
Richard Kenner committed
78
   is
79 80 81
      --  The procedures below output a "bug box" with information about
      --  the cause of the compiler abort and about the preferred method
      --  of reporting bugs. The default is a bug box appropriate for
82
      --  the FSF version of GNAT, but there are specializations for
Arnaud Charlet committed
83
      --  the GNATPRO and Public releases by AdaCore.
84

85 86 87
      XF : constant Positive := X'First;
      --  Start index, usually 1, but we won't assume this

Richard Kenner committed
88 89 90
      procedure End_Line;
      --  Add blanks up to column 76, and then a final vertical bar

91 92 93 94
      --------------
      -- End_Line --
      --------------

Richard Kenner committed
95 96 97 98 99 100
      procedure End_Line is
      begin
         Repeat_Char (' ', 76, '|');
         Write_Eol;
      end End_Line;

101 102
      Is_GPL_Version : constant Boolean := Gnatvsn.Build_Type = GPL;
      Is_FSF_Version : constant Boolean := Gnatvsn.Build_Type = FSF;
Arnaud Charlet committed
103

Richard Kenner committed
104 105 106
   --  Start of processing for Compiler_Abort

   begin
107 108
      Cancel_Special_Output;

109
      --  Prevent recursion through Compiler_Abort, e.g. via SIGSEGV
110 111 112 113 114 115 116

      if Abort_In_Progress then
         Exit_Program (E_Abort);
      end if;

      Abort_In_Progress := True;

117
      --  Generate a "standard" error message instead of a bug box in case
118
      --  of CodePeer rather than generating a bug box, friendlier.
Arnaud Charlet committed
119

120 121 122 123 124
      --  Note that the call to Error_Msg_N below sets Serious_Errors_Detected
      --  to 1, so we use the regular mechanism below in order to display a
      --  "compilation abandoned" message and exit, so we still know we have
      --  this case (and -gnatdk can still be used to get the bug box).

125
      if CodePeer_Mode
126 127 128 129
        and then Serious_Errors_Detected = 0
        and then not Debug_Flag_K
        and then Sloc (Current_Error_Node) > No_Location
      then
130
         Error_Msg_N ("cannot generate 'S'C'I'L", Current_Error_Node);
131 132
      end if;

Arnaud Charlet committed
133 134
      --  If we are in CodePeer mode, we must also delete SCIL files

Arnaud Charlet committed
135 136 137 138
      if CodePeer_Mode then
         Delete_SCIL_Files;
      end if;

139 140 141
      --  If any errors have already occurred, then we guess that the abort
      --  may well be caused by previous errors, and we don't make too much
      --  fuss about it, since we want to let programmer fix the errors first.
Richard Kenner committed
142 143 144

      --  Debug flag K disables this behavior (useful for debugging)

145
      if Serious_Errors_Detected /= 0 and then not Debug_Flag_K then
146
         Errout.Finalize (Last_Call => True);
147
         Errout.Output_Messages;
Richard Kenner committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

         Set_Standard_Error;
         Write_Str ("compilation abandoned due to previous error");
         Write_Eol;

         Set_Standard_Output;
         Source_Dump;
         Tree_Dump;
         Exit_Program (E_Errors);

      --  Otherwise give message with details of the abort

      else
         Set_Standard_Error;

         --  Generate header for bug box

         Write_Char ('+');
         Repeat_Char ('=', 29, 'G');
         Write_Str ("NAT BUG DETECTED");
         Repeat_Char ('=', 76, '+');
         Write_Eol;

         --  Output GNAT version identification

         Write_Str ("| ");
         Write_Str (Gnat_Version_String);
         Write_Str (" (");

         --  Output target name, deleting junk final reverse slash

         if Target_Name.all (Target_Name.all'Last) = '\'
           or else Target_Name.all (Target_Name.all'Last) = '/'
         then
            Write_Str (Target_Name.all (1 .. Target_Name.all'Last - 1));
         else
            Write_Str (Target_Name.all);
         end if;

         --  Output identification of error

         Write_Str (") ");

         if X'Length + Column > 76 then
192
            if From_GCC then
Richard Kenner committed
193 194 195 196 197 198 199 200 201 202 203 204 205
               Write_Str ("GCC error:");
            end if;

            End_Line;

            Write_Str ("| ");
         end if;

         if X'Length > 70 then
            declare
               Last_Blank : Integer := 70;

            begin
206 207
               for P in 39 .. 68 loop
                  if X (XF + P) = ' ' then
Richard Kenner committed
208 209 210 211
                     Last_Blank := P;
                  end if;
               end loop;

212
               Write_Str (X (XF .. XF - 1 + Last_Blank));
Richard Kenner committed
213 214
               End_Line;
               Write_Str ("|    ");
215
               Write_Str (X (XF + Last_Blank .. X'Last));
Richard Kenner committed
216 217 218 219 220
            end;
         else
            Write_Str (X);
         end if;

221
         if not From_GCC then
Richard Kenner committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236

            --  For exception case, get exception message from the TSD. Note
            --  that it would be neater and cleaner to pass the exception
            --  message (obtained from Exception_Message) as a parameter to
            --  Compiler_Abort, but we can't do this quite yet since it would
            --  cause bootstrap path problems for 3.10 to 3.11.

            Write_Char (' ');
            Write_Str (Exception_Message (Get_Current_Excep.all.all));
         end if;

         End_Line;

         --  Output source location information

237 238 239 240 241 242 243 244
         if Sloc (Current_Error_Node) <= No_Location then
            if Fallback_Loc'Length > 0 then
               Write_Str ("| Error detected around ");
               Write_Str (Fallback_Loc);
            else
               Write_Str ("| No source file position information available");
            end if;

Richard Kenner committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
            End_Line;
         else
            Write_Str ("| Error detected at ");
            Write_Location (Sloc (Current_Error_Node));
            End_Line;
         end if;

         --  There are two cases now. If the file gnat_bug.box exists,
         --  we use the contents of this file at this point.

         declare
            Lo  : Source_Ptr;
            Hi  : Source_Ptr;
            Src : Source_Buffer_Ptr;

         begin
            Namet.Unlock;
            Name_Buffer (1 .. 12) := "gnat_bug.box";
            Name_Len := 12;
            Read_Source_File (Name_Enter, 0, Hi, Src);

            --  If we get a Src file, we use it

            if Src /= null then
               Lo := 0;

               Outer : while Lo < Hi loop
                  Write_Str ("| ");

                  Inner : loop
                     exit Inner when Src (Lo) = ASCII.CR
                       or else Src (Lo) = ASCII.LF;
                     Write_Char (Src (Lo));
                     Lo := Lo + 1;
                  end loop Inner;

                  End_Line;

                  while Lo <= Hi
                    and then (Src (Lo) = ASCII.CR
                                or else Src (Lo) = ASCII.LF)
                  loop
                     Lo := Lo + 1;
                  end loop;
               end loop Outer;

            --  Otherwise we use the standard fixed text

            else
Arnaud Charlet committed
294 295 296 297 298 299
               if Is_FSF_Version then
                  Write_Str
                    ("| Please submit a bug report; see" &
                     " http://gcc.gnu.org/bugs.html.");
                  End_Line;

300 301
               elsif Is_GPL_Version then

Arnaud Charlet committed
302
                  Write_Str
303
                    ("| Please submit a bug report by email " &
Arnaud Charlet committed
304 305 306 307
                     "to report@adacore.com.");
                  End_Line;

                  Write_Str
308 309 310 311 312 313 314 315 316
                    ("| GAP members can alternatively use GNAT Tracker:");
                  End_Line;

                  Write_Str
                    ("| http://www.adacore.com/ " &
                     "section 'send a report'.");
                  End_Line;

                  Write_Str
Arnaud Charlet committed
317 318 319 320
                    ("| See gnatinfo.txt for full info on procedure " &
                     "for submitting bugs.");
                  End_Line;

Arnaud Charlet committed
321
               else
322
                  Write_Str
Arnaud Charlet committed
323
                    ("| Please submit a bug report using GNAT Tracker:");
324 325 326
                  End_Line;

                  Write_Str
Arnaud Charlet committed
327 328 329 330 331 332
                    ("| http://www.adacore.com/gnattracker/ " &
                     "section 'send a report'.");
                  End_Line;

                  Write_Str
                    ("| alternatively submit a bug report by email " &
333 334 335 336 337 338
                     "to report@adacore.com,");
                  End_Line;

                  Write_Str
                    ("| including your customer number #nnn " &
                     "in the subject line.");
339 340 341
                  End_Line;
               end if;

Arnaud Charlet committed
342 343 344 345 346
               Write_Str
                 ("| Use a subject line meaningful to you" &
                  " and us to track the bug.");
               End_Line;

Richard Kenner committed
347 348 349 350 351 352
               Write_Str
                 ("| Include the entire contents of this bug " &
                  "box in the report.");
               End_Line;

               Write_Str
Arnaud Charlet committed
353
                 ("| Include the exact command that you entered.");
Richard Kenner committed
354 355 356
               End_Line;

               Write_Str
Arnaud Charlet committed
357
                 ("| Also include sources listed below.");
Richard Kenner committed
358 359
               End_Line;

Arnaud Charlet committed
360
               if not Is_FSF_Version then
361
                  Write_Str
Arnaud Charlet committed
362
                    ("| Use plain ASCII or MIME attachment(s).");
363 364
                  End_Line;
               end if;
Richard Kenner committed
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
            end if;
         end;

         --  Complete output of bug box

         Write_Char ('+');
         Repeat_Char ('=', 76, '+');
         Write_Eol;

         if Debug_Flag_3 then
            Write_Eol;
            Write_Eol;
            Print_Tree_Node (Current_Error_Node);
            Write_Eol;
         end if;

         Write_Eol;

         Write_Line ("Please include these source files with error report");
384 385 386
         Write_Line ("Note that list may not be accurate in some cases, ");
         Write_Line ("so please double check that the problem can still ");
         Write_Line ("be reproduced with the set of files listed.");
Arnaud Charlet committed
387
         Write_Line ("Consider also -gnatd.n switch (see debug.adb).");
Richard Kenner committed
388 389
         Write_Eol;

Arnaud Charlet committed
390 391
         begin
            Dump_Source_File_Names;
Richard Kenner committed
392

Arnaud Charlet committed
393 394
         --  If we blow up trying to print the list of file names, just output
         --  informative msg and continue.
Richard Kenner committed
395

Arnaud Charlet committed
396 397 398 399
         exception
            when others =>
               Write_Str ("list may be incomplete");
         end;
Richard Kenner committed
400 401 402 403 404 405 406 407 408 409

         Write_Eol;
         Set_Standard_Output;

         Tree_Dump;
         Source_Dump;
         raise Unrecoverable_Error;
      end if;
   end Compiler_Abort;

Arnaud Charlet committed
410 411 412 413 414
   -----------------------
   -- Delete_SCIL_Files --
   -----------------------

   procedure Delete_SCIL_Files is
Arnaud Charlet committed
415 416
      Main      : Node_Id;
      Unit_Name : Node_Id;
Arnaud Charlet committed
417 418

      Success : Boolean;
Arnaud Charlet committed
419
      pragma Unreferenced (Success);
Arnaud Charlet committed
420

Arnaud Charlet committed
421 422 423 424 425 426 427 428
      procedure Decode_Name_Buffer;
      --  Replace "__" by "." in Name_Buffer, and adjust Name_Len accordingly

      ------------------------
      -- Decode_Name_Buffer --
      ------------------------

      procedure Decode_Name_Buffer is
Arnaud Charlet committed
429 430 431
         J : Natural;
         K : Natural;

Arnaud Charlet committed
432
      begin
Arnaud Charlet committed
433 434
         J := 1;
         K := 0;
Arnaud Charlet committed
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
         while J <= Name_Len loop
            K := K + 1;

            if J < Name_Len
              and then Name_Buffer (J) = '_'
              and then Name_Buffer (J + 1) = '_'
            then
               Name_Buffer (K) := '.';
               J := J + 1;
            else
               Name_Buffer (K) := Name_Buffer (J);
            end if;

            J := J + 1;
         end loop;

         Name_Len := K;
      end Decode_Name_Buffer;

Arnaud Charlet committed
454
   --  Start of processing for Delete_SCIL_Files
Arnaud Charlet committed
455

Arnaud Charlet committed
456 457 458 459 460 461 462 463 464
   begin
      --  If parsing was not successful, no Main_Unit is available, so return
      --  immediately.

      if Main_Source_File = No_Source_File then
         return;
      end if;

      --  Retrieve unit name, and remove old versions of SCIL/<unit>.scil and
Arnaud Charlet committed
465
      --  SCIL/<unit>__body.scil, ditto for .scilx files.
Arnaud Charlet committed
466 467 468

      Main := Unit (Cunit (Main_Unit));

Arnaud Charlet committed
469 470 471 472 473 474 475
      case Nkind (Main) is
         when N_Subprogram_Body | N_Package_Declaration =>
            Unit_Name := Defining_Unit_Name (Specification (Main));

         when N_Package_Body =>
            Unit_Name := Corresponding_Spec (Main);

Arnaud Charlet committed
476 477 478
         when N_Package_Renaming_Declaration =>
            Unit_Name := Defining_Unit_Name (Main);

Arnaud Charlet committed
479 480
         --  No SCIL file generated for generic package declarations

Arnaud Charlet committed
481 482 483
         when N_Generic_Package_Declaration =>
            return;

Arnaud Charlet committed
484 485
         --  Should never happen, but can be ignored in production

Arnaud Charlet committed
486 487 488 489 490 491 492 493 494 495 496 497 498
         when others =>
            pragma Assert (False);
            return;
      end case;

      case Nkind (Unit_Name) is
         when N_Defining_Identifier =>
            Get_Name_String (Chars (Unit_Name));

         when N_Defining_Program_Unit_Name =>
            Get_Name_String (Chars (Defining_Identifier (Unit_Name)));
            Decode_Name_Buffer;

Arnaud Charlet committed
499 500
         --  Should never happen, but can be ignored in production

Arnaud Charlet committed
501 502 503 504
         when others =>
            pragma Assert (False);
            return;
      end case;
Arnaud Charlet committed
505

Arnaud Charlet committed
506 507
      Delete_File
        ("SCIL/" & Name_Buffer (1 .. Name_Len) & ".scil", Success);
Arnaud Charlet committed
508
      Delete_File
Arnaud Charlet committed
509 510
        ("SCIL/" & Name_Buffer (1 .. Name_Len) & ".scilx", Success);
      Delete_File
Arnaud Charlet committed
511
        ("SCIL/" & Name_Buffer (1 .. Name_Len) & "__body.scil", Success);
Arnaud Charlet committed
512 513
      Delete_File
        ("SCIL/" & Name_Buffer (1 .. Name_Len) & "__body.scilx", Success);
Arnaud Charlet committed
514 515
   end Delete_SCIL_Files;

Richard Kenner committed
516 517 518 519 520 521 522 523 524 525 526 527 528 529
   -----------------
   -- Repeat_Char --
   -----------------

   procedure Repeat_Char (Char : Character; Col : Nat; After : Character) is
   begin
      while Column < Col loop
         Write_Char (Char);
      end loop;

      Write_Char (After);
   end Repeat_Char;

end Comperr;