1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2007, Free Software Foundation, Inc. --
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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
28 with Osint; use Osint;
29 with Output; use Output;
31 with Types; use Types;
33 with System.OS_Lib; use System.OS_Lib;
35 with Unchecked_Conversion;
41 subtype Big_String is String (Positive);
42 type Big_String_Ptr is access all Big_String;
44 function To_Big_String_Ptr is new Unchecked_Conversion
45 (Source_Buffer_Ptr, Big_String_Ptr);
47 Max_Buffer : constant := 1_500;
48 Buffer : String (1 .. Max_Buffer);
49 -- Used to bufferize output when writing to a new mapping file
51 Buffer_Last : Natural := 0;
52 -- Index of last valid character in Buffer
54 type Mapping is record
55 Uname : Unit_Name_Type;
56 Fname : File_Name_Type;
59 package File_Mapping is new Table.Table (
60 Table_Component_Type => Mapping,
61 Table_Index_Type => Int,
63 Table_Initial => 1_000,
64 Table_Increment => 1_000,
65 Table_Name => "Fmap.File_Mapping");
66 -- Mapping table to map unit names to file names
68 package Path_Mapping is new Table.Table (
69 Table_Component_Type => Mapping,
70 Table_Index_Type => Int,
72 Table_Initial => 1_000,
73 Table_Increment => 1_000,
74 Table_Name => "Fmap.Path_Mapping");
75 -- Mapping table to map file names to path names
77 type Header_Num is range 0 .. 1_000;
79 function Hash (F : Unit_Name_Type) return Header_Num;
80 -- Function used to compute hash of unit name
82 No_Entry : constant Int := -1;
83 -- Signals no entry in following table
85 package Unit_Hash_Table is new GNAT.HTable.Simple_HTable (
86 Header_Num => Header_Num,
88 No_Element => No_Entry,
89 Key => Unit_Name_Type,
92 -- Hash table to map unit names to file names. Used in conjunction with
93 -- table File_Mapping above.
95 function Hash (F : File_Name_Type) return Header_Num;
96 -- Function used to compute hash of file name
98 package File_Hash_Table is new GNAT.HTable.Simple_HTable (
99 Header_Num => Header_Num,
101 No_Element => No_Entry,
102 Key => File_Name_Type,
105 -- Hash table to map file names to path names. Used in conjunction with
106 -- table Path_Mapping above.
108 Last_In_Table : Int := 0;
110 package Forbidden_Names is new GNAT.HTable.Simple_HTable (
111 Header_Num => Header_Num,
114 Key => File_Name_Type,
118 -----------------------------
119 -- Add_Forbidden_File_Name --
120 -----------------------------
122 procedure Add_Forbidden_File_Name (Name : File_Name_Type) is
124 Forbidden_Names.Set (Name, True);
125 end Add_Forbidden_File_Name;
127 ---------------------
128 -- Add_To_File_Map --
129 ---------------------
131 procedure Add_To_File_Map
132 (Unit_Name : Unit_Name_Type;
133 File_Name : File_Name_Type;
134 Path_Name : File_Name_Type)
136 Unit_Entry : constant Int := Unit_Hash_Table.Get (Unit_Name);
137 File_Entry : constant Int := File_Hash_Table.Get (File_Name);
139 if Unit_Entry = No_Entry or else
140 File_Mapping.Table (Unit_Entry).Fname /= File_Name
142 File_Mapping.Increment_Last;
143 Unit_Hash_Table.Set (Unit_Name, File_Mapping.Last);
144 File_Mapping.Table (File_Mapping.Last) :=
145 (Uname => Unit_Name, Fname => File_Name);
148 if File_Entry = No_Entry or else
149 Path_Mapping.Table (File_Entry).Fname /= Path_Name
151 Path_Mapping.Increment_Last;
152 File_Hash_Table.Set (File_Name, Path_Mapping.Last);
153 Path_Mapping.Table (Path_Mapping.Last) :=
154 (Uname => Unit_Name, Fname => Path_Name);
162 function Hash (F : File_Name_Type) return Header_Num is
164 return Header_Num (Int (F) rem Header_Num'Range_Length);
167 function Hash (F : Unit_Name_Type) return Header_Num is
169 return Header_Num (Int (F) rem Header_Num'Range_Length);
176 procedure Initialize (File_Name : String) is
177 Src : Source_Buffer_Ptr;
182 First : Positive := 1;
185 Uname : Unit_Name_Type;
186 Fname : File_Name_Type;
187 Pname : File_Name_Type;
189 procedure Empty_Tables;
190 -- Remove all entries in case of incorrect mapping file
192 function Find_File_Name return File_Name_Type;
193 -- Return Error_File_Name for "/", otherwise call Name_Find
194 -- What is this about, explanation required ???
196 function Find_Unit_Name return Unit_Name_Type;
197 -- Return Error_Unit_Name for "/", otherwise call Name_Find
198 -- Even more mysterious??? function appeared when Find_Name was split
199 -- for the two types, but this routine is definitely called!
202 -- Get a line from the mapping file
204 procedure Report_Truncated;
205 -- Report a warning when the mapping file is truncated
206 -- (number of lines is not a multiple of 3).
212 procedure Empty_Tables is
214 Unit_Hash_Table.Reset;
215 File_Hash_Table.Reset;
216 Path_Mapping.Set_Last (0);
217 File_Mapping.Set_Last (0);
225 -- Why is only / illegal, why not \ on windows ???
227 function Find_File_Name return File_Name_Type is
229 if Name_Buffer (1 .. Name_Len) = "/" then
230 return Error_File_Name;
240 function Find_Unit_Name return Unit_Name_Type is
242 return Unit_Name_Type (Find_File_Name);
250 procedure Get_Line is
256 -- If not at the end of file, skip the end of line
258 while First < SP'Last
259 and then (SP (First) = CR
260 or else SP (First) = LF
261 or else SP (First) = EOF)
266 -- If not at the end of file, find the end of this new line
268 if First < SP'Last and then SP (First) /= EOF then
272 and then SP (Last + 1) /= CR
273 and then SP (Last + 1) /= LF
274 and then SP (Last + 1) /= EOF
282 ----------------------
283 -- Report_Truncated --
284 ----------------------
286 procedure Report_Truncated is
288 Write_Str ("warning: mapping file """);
289 Write_Str (File_Name);
290 Write_Line (""" is truncated");
291 end Report_Truncated;
293 -- Start of processing for Initialize
297 Name_Len := File_Name'Length;
298 Name_Buffer (1 .. Name_Len) := File_Name;
299 Read_Source_File (Name_Enter, 0, Hi, Src, Config);
302 Write_Str ("warning: could not read mapping file """);
303 Write_Str (File_Name);
307 BS := To_Big_String_Ptr (Src);
308 SP := BS (1 .. Natural (Hi))'Unrestricted_Access;
315 -- Exit if end of file has been reached
317 exit when First > Last;
319 if (Last < First + 2) or else (SP (Last - 1) /= '%')
320 or else (SP (Last) /= 's' and then SP (Last) /= 'b')
322 Write_Str ("warning: mapping file """);
323 Write_Str (File_Name);
324 Write_Line (""" is incorrectly formatted");
329 Name_Len := Last - First + 1;
330 Name_Buffer (1 .. Name_Len) := SP (First .. Last);
331 Uname := Find_Unit_Name;
337 -- If end of line has been reached, file is truncated
345 Name_Len := Last - First + 1;
346 Name_Buffer (1 .. Name_Len) := SP (First .. Last);
347 Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
348 Fname := Find_File_Name;
354 -- If end of line has been reached, file is truncated
362 Name_Len := Last - First + 1;
363 Name_Buffer (1 .. Name_Len) := SP (First .. Last);
364 Pname := Find_File_Name;
366 -- Add the mappings for this unit name
368 Add_To_File_Map (Uname, Fname, Pname);
372 -- Record the length of the two mapping tables
374 Last_In_Table := File_Mapping.Last;
377 ----------------------
378 -- Mapped_File_Name --
379 ----------------------
381 function Mapped_File_Name (Unit : Unit_Name_Type) return File_Name_Type is
382 The_Index : constant Int := Unit_Hash_Table.Get (Unit);
385 if The_Index = No_Entry then
388 return File_Mapping.Table (The_Index).Fname;
390 end Mapped_File_Name;
392 ----------------------
393 -- Mapped_Path_Name --
394 ----------------------
396 function Mapped_Path_Name (File : File_Name_Type) return File_Name_Type is
397 Index : Int := No_Entry;
400 if Forbidden_Names.Get (File) then
401 return Error_File_Name;
404 Index := File_Hash_Table.Get (File);
406 if Index = No_Entry then
409 return Path_Mapping.Table (Index).Fname;
411 end Mapped_Path_Name;
413 --------------------------------
414 -- Remove_Forbidden_File_Name --
415 --------------------------------
417 procedure Remove_Forbidden_File_Name (Name : File_Name_Type) is
419 Forbidden_Names.Set (Name, False);
420 end Remove_Forbidden_File_Name;
426 procedure Reset_Tables is
430 Unit_Hash_Table.Reset;
431 File_Hash_Table.Reset;
432 Forbidden_Names.Reset;
436 -------------------------
437 -- Update_Mapping_File --
438 -------------------------
440 procedure Update_Mapping_File (File_Name : String) is
441 File : File_Descriptor;
447 -- For the call to Close
449 procedure Put_Line (Name : Name_Id);
450 -- Put Name as a line in the Mapping File
456 procedure Put_Line (Name : Name_Id) is
458 Get_Name_String (Name);
460 -- If the Buffer is full, write it to the file
462 if Buffer_Last + Name_Len + 1 > Buffer'Last then
463 N_Bytes := Write (File, Buffer (1)'Address, Buffer_Last);
465 if N_Bytes < Buffer_Last then
472 -- Add the line to the Buffer
474 Buffer (Buffer_Last + 1 .. Buffer_Last + Name_Len) :=
475 Name_Buffer (1 .. Name_Len);
476 Buffer_Last := Buffer_Last + Name_Len + 1;
477 Buffer (Buffer_Last) := ASCII.LF;
480 -- Start of Update_Mapping_File
484 -- Only Update if there are new entries in the mappings
486 if Last_In_Table < File_Mapping.Last then
488 -- If the tables have been emptied, recreate the file.
489 -- Otherwise, append to it.
491 if Last_In_Table = 0 then
496 Delete_File (File_Name, Discard);
499 File := Create_File (File_Name, Binary);
502 File := Open_Read_Write (Name => File_Name, Fmode => Binary);
505 if File /= Invalid_FD then
506 if Last_In_Table > 0 then
507 Lseek (File, 0, Seek_End);
510 for Unit in Last_In_Table + 1 .. File_Mapping.Last loop
511 Put_Line (Name_Id (File_Mapping.Table (Unit).Uname));
512 Put_Line (Name_Id (File_Mapping.Table (Unit).Fname));
514 File_Hash_Table.Get (File_Mapping.Table (Unit).Fname);
515 Put_Line (Name_Id (Path_Mapping.Table (File_Entry).Fname));
518 -- Before closing the file, write the buffer to the file. It is
519 -- guaranteed that the Buffer is not empty, because Put_Line has
520 -- been called at least 3 times, and after a call to Put_Line, the
521 -- Buffer is not empty.
523 N_Bytes := Write (File, Buffer (1)'Address, Buffer_Last);
525 if N_Bytes < Buffer_Last then
529 Close (File, Status);
535 elsif not Quiet_Output then
536 Write_Str ("warning: could not open mapping file """);
537 Write_Str (File_Name);
538 Write_Line (""" for update");
542 end Update_Mapping_File;