OSDN Git Service

PR target/32335
[pf3gnuchains/gcc-fork.git] / gcc / ada / fmap.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                                 F M A P                                  --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 2001-2007, 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 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.                                              --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 with Opt;    use Opt;
28 with Osint;  use Osint;
29 with Output; use Output;
30 with Table;
31 with Types;  use Types;
32
33 with System.OS_Lib; use System.OS_Lib;
34
35 with Unchecked_Conversion;
36
37 with GNAT.HTable;
38
39 package body Fmap is
40
41    subtype Big_String is String (Positive);
42    type Big_String_Ptr is access all Big_String;
43
44    function To_Big_String_Ptr is new Unchecked_Conversion
45      (Source_Buffer_Ptr, Big_String_Ptr);
46
47    Max_Buffer : constant := 1_500;
48    Buffer : String (1 .. Max_Buffer);
49    --  Used to bufferize output when writing to a new mapping file
50
51    Buffer_Last : Natural := 0;
52    --  Index of last valid character in Buffer
53
54    type Mapping is record
55       Uname : Unit_Name_Type;
56       Fname : File_Name_Type;
57    end record;
58
59    package File_Mapping is new Table.Table (
60      Table_Component_Type => Mapping,
61      Table_Index_Type     => Int,
62      Table_Low_Bound      => 0,
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
67
68    package Path_Mapping is new Table.Table (
69      Table_Component_Type => Mapping,
70      Table_Index_Type     => Int,
71      Table_Low_Bound      => 0,
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
76
77    type Header_Num is range 0 .. 1_000;
78
79    function Hash (F : Unit_Name_Type) return Header_Num;
80    --  Function used to compute hash of unit name
81
82    No_Entry : constant Int := -1;
83    --  Signals no entry in following table
84
85    package Unit_Hash_Table is new GNAT.HTable.Simple_HTable (
86      Header_Num => Header_Num,
87      Element    => Int,
88      No_Element => No_Entry,
89      Key        => Unit_Name_Type,
90      Hash       => Hash,
91      Equal      => "=");
92    --  Hash table to map unit names to file names. Used in conjunction with
93    --  table File_Mapping above.
94
95    function Hash (F : File_Name_Type) return Header_Num;
96    --  Function used to compute hash of file name
97
98    package File_Hash_Table is new GNAT.HTable.Simple_HTable (
99      Header_Num => Header_Num,
100      Element    => Int,
101      No_Element => No_Entry,
102      Key        => File_Name_Type,
103      Hash       => Hash,
104      Equal      => "=");
105    --  Hash table to map file names to path names. Used in conjunction with
106    --  table Path_Mapping above.
107
108    Last_In_Table : Int := 0;
109
110    package Forbidden_Names is new GNAT.HTable.Simple_HTable (
111      Header_Num => Header_Num,
112      Element    => Boolean,
113      No_Element => False,
114      Key        => File_Name_Type,
115      Hash       => Hash,
116      Equal      => "=");
117
118    -----------------------------
119    -- Add_Forbidden_File_Name --
120    -----------------------------
121
122    procedure Add_Forbidden_File_Name (Name : File_Name_Type) is
123    begin
124       Forbidden_Names.Set (Name, True);
125    end Add_Forbidden_File_Name;
126
127    ---------------------
128    -- Add_To_File_Map --
129    ---------------------
130
131    procedure Add_To_File_Map
132      (Unit_Name : Unit_Name_Type;
133       File_Name : File_Name_Type;
134       Path_Name : File_Name_Type)
135    is
136    begin
137       File_Mapping.Increment_Last;
138       Unit_Hash_Table.Set (Unit_Name, File_Mapping.Last);
139       File_Mapping.Table (File_Mapping.Last) :=
140         (Uname => Unit_Name, Fname => File_Name);
141       Path_Mapping.Increment_Last;
142       File_Hash_Table.Set (File_Name, Path_Mapping.Last);
143       Path_Mapping.Table (Path_Mapping.Last) :=
144         (Uname => Unit_Name, Fname => Path_Name);
145    end Add_To_File_Map;
146
147    ----------
148    -- Hash --
149    ----------
150
151    function Hash (F : File_Name_Type) return Header_Num is
152    begin
153       return Header_Num (Int (F) rem Header_Num'Range_Length);
154    end Hash;
155
156    function Hash (F : Unit_Name_Type) return Header_Num is
157    begin
158       return Header_Num (Int (F) rem Header_Num'Range_Length);
159    end Hash;
160
161    ----------------
162    -- Initialize --
163    ----------------
164
165    procedure Initialize (File_Name : String) is
166       Src : Source_Buffer_Ptr;
167       Hi  : Source_Ptr;
168       BS  : Big_String_Ptr;
169       SP  : String_Ptr;
170
171       First : Positive := 1;
172       Last  : Natural  := 0;
173
174       Uname : Unit_Name_Type;
175       Fname : File_Name_Type;
176       Pname : File_Name_Type;
177
178       procedure Empty_Tables;
179       --  Remove all entries in case of incorrect mapping file
180
181       function Find_File_Name return File_Name_Type;
182       --  Return Error_File_Name for "/", otherwise call Name_Find
183       --  What is this about, explanation required ???
184
185       function Find_Unit_Name return Unit_Name_Type;
186       --  Return Error_Unit_Name for "/", otherwise call Name_Find
187       --  Even more mysterious??? function appeared when Find_Name was split
188       --  for the two types, but this routine is definitely called!
189
190       procedure Get_Line;
191       --  Get a line from the mapping file
192
193       procedure Report_Truncated;
194       --  Report a warning when the mapping file is truncated
195       --  (number of lines is not a multiple of 3).
196
197       ------------------
198       -- Empty_Tables --
199       ------------------
200
201       procedure Empty_Tables is
202       begin
203          Unit_Hash_Table.Reset;
204          File_Hash_Table.Reset;
205          Path_Mapping.Set_Last (0);
206          File_Mapping.Set_Last (0);
207          Last_In_Table := 0;
208       end Empty_Tables;
209
210       --------------------
211       -- Find_File_Name --
212       --------------------
213
214       --  Why is only / illegal, why not \ on windows ???
215
216       function Find_File_Name return File_Name_Type is
217       begin
218          if Name_Buffer (1 .. Name_Len) = "/" then
219             return Error_File_Name;
220          else
221             return Name_Find;
222          end if;
223       end Find_File_Name;
224
225       --------------------
226       -- Find_Unit_Name --
227       --------------------
228
229       function Find_Unit_Name return Unit_Name_Type is
230       begin
231          return Unit_Name_Type (Find_File_Name);
232          --  very odd ???
233       end Find_Unit_Name;
234
235       --------------
236       -- Get_Line --
237       --------------
238
239       procedure Get_Line is
240          use ASCII;
241
242       begin
243          First := Last + 1;
244
245          --  If not at the end of file, skip the end of line
246
247          while First < SP'Last
248            and then (SP (First) = CR
249                       or else SP (First) = LF
250                       or else SP (First) = EOF)
251          loop
252             First := First + 1;
253          end loop;
254
255          --  If not at the end of file, find the end of this new line
256
257          if First < SP'Last and then SP (First) /= EOF then
258             Last := First;
259
260             while Last < SP'Last
261               and then SP (Last + 1) /= CR
262               and then SP (Last + 1) /= LF
263               and then SP (Last + 1) /= EOF
264             loop
265                Last := Last + 1;
266             end loop;
267
268          end if;
269       end Get_Line;
270
271       ----------------------
272       -- Report_Truncated --
273       ----------------------
274
275       procedure Report_Truncated is
276       begin
277          Write_Str ("warning: mapping file """);
278          Write_Str (File_Name);
279          Write_Line (""" is truncated");
280       end Report_Truncated;
281
282    --  Start of processing for Initialize
283
284    begin
285       Empty_Tables;
286       Name_Len := File_Name'Length;
287       Name_Buffer (1 .. Name_Len) := File_Name;
288       Read_Source_File (Name_Enter, 0, Hi, Src, Config);
289
290       if Src = null then
291          Write_Str ("warning: could not read mapping file """);
292          Write_Str (File_Name);
293          Write_Line ("""");
294
295       else
296          BS := To_Big_String_Ptr (Src);
297          SP := BS (1 .. Natural (Hi))'Unrestricted_Access;
298
299          loop
300             --  Get the unit name
301
302             Get_Line;
303
304             --  Exit if end of file has been reached
305
306             exit when First > Last;
307
308             if (Last < First + 2) or else (SP (Last - 1) /= '%')
309               or else (SP (Last) /= 's' and then SP (Last) /= 'b')
310             then
311                Write_Str ("warning: mapping file """);
312                Write_Str (File_Name);
313                Write_Line (""" is incorrectly formatted");
314                Empty_Tables;
315                return;
316             end if;
317
318             Name_Len := Last - First + 1;
319             Name_Buffer (1 .. Name_Len) := SP (First .. Last);
320             Uname := Find_Unit_Name;
321
322             --  Get the file name
323
324             Get_Line;
325
326             --  If end of line has been reached, file is truncated
327
328             if First > Last then
329                Report_Truncated;
330                Empty_Tables;
331                return;
332             end if;
333
334             Name_Len := Last - First + 1;
335             Name_Buffer (1 .. Name_Len) := SP (First .. Last);
336             Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
337             Fname := Find_File_Name;
338
339             --  Get the path name
340
341             Get_Line;
342
343             --  If end of line has been reached, file is truncated
344
345             if First > Last then
346                Report_Truncated;
347                Empty_Tables;
348                return;
349             end if;
350
351             Name_Len := Last - First + 1;
352             Name_Buffer (1 .. Name_Len) := SP (First .. Last);
353             Pname := Find_File_Name;
354
355             --  Check for duplicate entries
356
357             if Unit_Hash_Table.Get (Uname) /= No_Entry then
358                Empty_Tables;
359                return;
360             end if;
361
362             if File_Hash_Table.Get (Fname) /= No_Entry then
363                Empty_Tables;
364                return;
365             end if;
366
367             --  Add the mappings for this unit name
368
369             Add_To_File_Map (Uname, Fname, Pname);
370          end loop;
371       end if;
372
373       --  Record the length of the two mapping tables
374
375       Last_In_Table := File_Mapping.Last;
376    end Initialize;
377
378    ----------------------
379    -- Mapped_File_Name --
380    ----------------------
381
382    function Mapped_File_Name (Unit : Unit_Name_Type) return File_Name_Type is
383       The_Index : constant Int := Unit_Hash_Table.Get (Unit);
384
385    begin
386       if The_Index = No_Entry then
387          return No_File;
388       else
389          return File_Mapping.Table (The_Index).Fname;
390       end if;
391    end Mapped_File_Name;
392
393    ----------------------
394    -- Mapped_Path_Name --
395    ----------------------
396
397    function Mapped_Path_Name (File : File_Name_Type) return File_Name_Type is
398       Index : Int := No_Entry;
399
400    begin
401       if Forbidden_Names.Get (File) then
402          return Error_File_Name;
403       end if;
404
405       Index := File_Hash_Table.Get (File);
406
407       if Index = No_Entry then
408          return No_File;
409       else
410          return Path_Mapping.Table (Index).Fname;
411       end if;
412    end Mapped_Path_Name;
413
414    --------------------------------
415    -- Remove_Forbidden_File_Name --
416    --------------------------------
417
418    procedure Remove_Forbidden_File_Name (Name : File_Name_Type) is
419    begin
420       Forbidden_Names.Set (Name, False);
421    end Remove_Forbidden_File_Name;
422
423    ------------------
424    -- Reset_Tables --
425    ------------------
426
427    procedure Reset_Tables is
428    begin
429       File_Mapping.Init;
430       Path_Mapping.Init;
431       Unit_Hash_Table.Reset;
432       File_Hash_Table.Reset;
433       Forbidden_Names.Reset;
434       Last_In_Table := 0;
435    end Reset_Tables;
436
437    -------------------------
438    -- Update_Mapping_File --
439    -------------------------
440
441    procedure Update_Mapping_File (File_Name : String) is
442       File    : File_Descriptor;
443       N_Bytes : Integer;
444
445       Status : Boolean;
446       --  For the call to Close
447
448       procedure Put_Line (Name : Name_Id);
449       --  Put Name as a line in the Mapping File
450
451       --------------
452       -- Put_Line --
453       --------------
454
455       procedure Put_Line (Name : Name_Id) is
456       begin
457          Get_Name_String (Name);
458
459          --  If the Buffer is full, write it to the file
460
461          if Buffer_Last + Name_Len + 1 > Buffer'Last then
462             N_Bytes := Write (File, Buffer (1)'Address, Buffer_Last);
463
464             if N_Bytes < Buffer_Last then
465                Fail ("disk full");
466             end if;
467
468             Buffer_Last := 0;
469          end if;
470
471          --  Add the line to the Buffer
472
473          Buffer (Buffer_Last + 1 .. Buffer_Last + Name_Len) :=
474            Name_Buffer (1 .. Name_Len);
475          Buffer_Last := Buffer_Last + Name_Len + 1;
476          Buffer (Buffer_Last) := ASCII.LF;
477       end Put_Line;
478
479    --  Start of Update_Mapping_File
480
481    begin
482
483       --  Only Update if there are new entries in the mappings
484
485       if Last_In_Table < File_Mapping.Last then
486
487          --  If the tables have been emptied, recreate the file.
488          --  Otherwise, append to it.
489
490          if Last_In_Table = 0 then
491             declare
492                Discard : Boolean;
493
494             begin
495                Delete_File (File_Name, Discard);
496             end;
497
498             File := Create_File (File_Name, Binary);
499
500          else
501             File := Open_Read_Write (Name => File_Name, Fmode => Binary);
502          end if;
503
504          if File /= Invalid_FD then
505             if Last_In_Table > 0 then
506                Lseek (File, 0, Seek_End);
507             end if;
508
509             for Unit in Last_In_Table + 1 .. File_Mapping.Last loop
510                Put_Line (Name_Id (File_Mapping.Table (Unit).Uname));
511                Put_Line (Name_Id (File_Mapping.Table (Unit).Fname));
512                Put_Line (Name_Id (Path_Mapping.Table (Unit).Fname));
513             end loop;
514
515             --  Before closing the file, write the buffer to the file.
516             --  It is guaranteed that the Buffer is not empty, because
517             --  Put_Line has been called at least 3 times, and after
518             --  a call to Put_Line, the Buffer is not empty.
519
520             N_Bytes := Write (File, Buffer (1)'Address, Buffer_Last);
521
522             if N_Bytes < Buffer_Last then
523                Fail ("disk full");
524             end if;
525
526             Close (File, Status);
527
528             if not Status then
529                Fail ("disk full");
530             end if;
531
532          elsif not Quiet_Output then
533             Write_Str ("warning: could not open mapping file """);
534             Write_Str (File_Name);
535             Write_Line (""" for update");
536          end if;
537
538       end if;
539    end Update_Mapping_File;
540
541 end Fmap;