The GNAT specific pragma Source_Reference can alter the source line mapping,
leading to (logical) line numbers lower than 1 or greater than the maximum
number of lines in the file.  Dodji's patch shows that we weren't taking it 
into account in gigi.

Tested on i586-suse-linux, applied on the mainline.


2011-10-20  Eric Botcazou  <ebotca...@adacore.com>

        * back_end.adb (Call_Back_End): Pass the maximum logical line number
        instead of the maximum physical line number to gigi.
        * gcc-interface/trans.c (Sloc_to_locus): Cope with line zero.


2011-10-20  Eric Botcazou  <ebotca...@adacore.com>

        * gnat.dg/source_ref1.adb: New test.
        * gnat.dg/source_ref2.adb: Likewise.


-- 
Eric Botcazou
Index: back_end.adb
===================================================================
--- back_end.adb	(revision 180235)
+++ back_end.adb	(working copy)
@@ -114,9 +114,13 @@ package body Back_End is
          return;
       end if;
 
+      --  The back end needs to know the maximum line number that can appear
+      --  in a Sloc, in other words the maximum logical line number.
+
       for J in 1 .. Last_Source_File loop
          File_Info_Array (J).File_Name        := Full_Debug_Name (J);
-         File_Info_Array (J).Num_Source_Lines := Num_Source_Lines (J);
+         File_Info_Array (J).Num_Source_Lines :=
+           Nat (Physical_To_Logical (Last_Source_Line (J), J));
       end loop;
 
       if Generate_SCIL then
Index: gcc-interface/trans.c
===================================================================
--- gcc-interface/trans.c	(revision 180235)
+++ gcc-interface/trans.c	(working copy)
@@ -8393,6 +8393,10 @@ Sloc_to_locus (Source_Ptr Sloc, location
       Column_Number column = Get_Column_Number (Sloc);
       struct line_map *map = LINEMAPS_ORDINARY_MAP_AT (line_table, file - 1);
 
+      /* We can have zero if pragma Source_Reference is in effect.  */
+      if (line < 1)
+	line = 1;
+
       /* Translate the location.  */
       *locus = linemap_position_for_line_and_column (map, line, column);
     }
pragma Source_Reference (1, "p2.adb");

procedure Source_Ref2 is
pragma Source_Reference (2, "p2.adb");
begin
   null;
end;
pragma Source_Reference (3, "p1.adb");

procedure Source_Ref1 is
begin
   null;
end;

Reply via email to