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 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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
27 with Osint
; use Osint
;
28 with Output
; use Output
;
30 with Types
; use Types
;
32 with System
.OS_Lib
; use System
.OS_Lib
;
34 with Unchecked_Conversion
;
40 subtype Big_String
is String (Positive);
41 type Big_String_Ptr
is access all Big_String
;
43 function To_Big_String_Ptr
is new Unchecked_Conversion
44 (Source_Buffer_Ptr
, Big_String_Ptr
);
46 Max_Buffer
: constant := 1_500
;
47 Buffer
: String (1 .. Max_Buffer
);
48 -- Used to bufferize output when writing to a new mapping file
50 Buffer_Last
: Natural := 0;
51 -- Index of last valid character in Buffer
53 type Mapping
is record
54 Uname
: Unit_Name_Type
;
55 Fname
: File_Name_Type
;
58 package File_Mapping
is new Table
.Table
(
59 Table_Component_Type
=> Mapping
,
60 Table_Index_Type
=> Int
,
62 Table_Initial
=> 1_000
,
63 Table_Increment
=> 1_000
,
64 Table_Name
=> "Fmap.File_Mapping");
65 -- Mapping table to map unit names to file names
67 package Path_Mapping
is new Table
.Table
(
68 Table_Component_Type
=> Mapping
,
69 Table_Index_Type
=> Int
,
71 Table_Initial
=> 1_000
,
72 Table_Increment
=> 1_000
,
73 Table_Name
=> "Fmap.Path_Mapping");
74 -- Mapping table to map file names to path names
76 type Header_Num
is range 0 .. 1_000
;
78 function Hash
(F
: Unit_Name_Type
) return Header_Num
;
79 -- Function used to compute hash of unit name
81 No_Entry
: constant Int
:= -1;
82 -- Signals no entry in following table
84 package Unit_Hash_Table
is new GNAT
.HTable
.Simple_HTable
(
85 Header_Num
=> Header_Num
,
87 No_Element
=> No_Entry
,
88 Key
=> Unit_Name_Type
,
91 -- Hash table to map unit names to file names. Used in conjunction with
92 -- table File_Mapping above.
94 function Hash
(F
: File_Name_Type
) return Header_Num
;
95 -- Function used to compute hash of file name
97 package File_Hash_Table
is new GNAT
.HTable
.Simple_HTable
(
98 Header_Num
=> Header_Num
,
100 No_Element
=> No_Entry
,
101 Key
=> File_Name_Type
,
104 -- Hash table to map file names to path names. Used in conjunction with
105 -- table Path_Mapping above.
107 Last_In_Table
: Int
:= 0;
109 package Forbidden_Names
is new GNAT
.HTable
.Simple_HTable
(
110 Header_Num
=> Header_Num
,
113 Key
=> File_Name_Type
,
117 -----------------------------
118 -- Add_Forbidden_File_Name --
119 -----------------------------
121 procedure Add_Forbidden_File_Name
(Name
: File_Name_Type
) is
123 Forbidden_Names
.Set
(Name
, True);
124 end Add_Forbidden_File_Name
;
126 ---------------------
127 -- Add_To_File_Map --
128 ---------------------
130 procedure Add_To_File_Map
131 (Unit_Name
: Unit_Name_Type
;
132 File_Name
: File_Name_Type
;
133 Path_Name
: File_Name_Type
)
135 Unit_Entry
: constant Int
:= Unit_Hash_Table
.Get
(Unit_Name
);
136 File_Entry
: constant Int
:= File_Hash_Table
.Get
(File_Name
);
138 if Unit_Entry
= No_Entry
or else
139 File_Mapping
.Table
(Unit_Entry
).Fname
/= File_Name
141 File_Mapping
.Increment_Last
;
142 Unit_Hash_Table
.Set
(Unit_Name
, File_Mapping
.Last
);
143 File_Mapping
.Table
(File_Mapping
.Last
) :=
144 (Uname
=> Unit_Name
, Fname
=> File_Name
);
147 if File_Entry
= No_Entry
or else
148 Path_Mapping
.Table
(File_Entry
).Fname
/= Path_Name
150 Path_Mapping
.Increment_Last
;
151 File_Hash_Table
.Set
(File_Name
, Path_Mapping
.Last
);
152 Path_Mapping
.Table
(Path_Mapping
.Last
) :=
153 (Uname
=> Unit_Name
, Fname
=> Path_Name
);
161 function Hash
(F
: File_Name_Type
) return Header_Num
is
163 return Header_Num
(Int
(F
) rem Header_Num
'Range_Length);
166 function Hash
(F
: Unit_Name_Type
) return Header_Num
is
168 return Header_Num
(Int
(F
) rem Header_Num
'Range_Length);
175 procedure Initialize
(File_Name
: String) is
176 Src
: Source_Buffer_Ptr
;
181 First
: Positive := 1;
184 Uname
: Unit_Name_Type
;
185 Fname
: File_Name_Type
;
186 Pname
: File_Name_Type
;
188 procedure Empty_Tables
;
189 -- Remove all entries in case of incorrect mapping file
191 function Find_File_Name
return File_Name_Type
;
192 -- Return Error_File_Name for "/", otherwise call Name_Find
193 -- What is this about, explanation required ???
195 function Find_Unit_Name
return Unit_Name_Type
;
196 -- Return Error_Unit_Name for "/", otherwise call Name_Find
197 -- Even more mysterious??? function appeared when Find_Name was split
198 -- for the two types, but this routine is definitely called!
201 -- Get a line from the mapping file
203 procedure Report_Truncated
;
204 -- Report a warning when the mapping file is truncated
205 -- (number of lines is not a multiple of 3).
211 procedure Empty_Tables
is
213 Unit_Hash_Table
.Reset
;
214 File_Hash_Table
.Reset
;
215 Path_Mapping
.Set_Last
(0);
216 File_Mapping
.Set_Last
(0);
224 -- Why is only / illegal, why not \ on windows ???
226 function Find_File_Name
return File_Name_Type
is
228 if Name_Buffer
(1 .. Name_Len
) = "/" then
229 return Error_File_Name
;
239 function Find_Unit_Name
return Unit_Name_Type
is
241 return Unit_Name_Type
(Find_File_Name
);
249 procedure Get_Line
is
255 -- If not at the end of file, skip the end of line
257 while First
< SP
'Last
258 and then (SP
(First
) = CR
259 or else SP
(First
) = LF
260 or else SP
(First
) = EOF
)
265 -- If not at the end of file, find the end of this new line
267 if First
< SP
'Last and then SP
(First
) /= EOF
then
271 and then SP
(Last
+ 1) /= CR
272 and then SP
(Last
+ 1) /= LF
273 and then SP
(Last
+ 1) /= EOF
281 ----------------------
282 -- Report_Truncated --
283 ----------------------
285 procedure Report_Truncated
is
287 Write_Str
("warning: mapping file """);
288 Write_Str
(File_Name
);
289 Write_Line
(""" is truncated");
290 end Report_Truncated
;
292 -- Start of processing for Initialize
296 Name_Len
:= File_Name
'Length;
297 Name_Buffer
(1 .. Name_Len
) := File_Name
;
298 Read_Source_File
(Name_Enter
, 0, Hi
, Src
, Config
);
301 Write_Str
("warning: could not read mapping file """);
302 Write_Str
(File_Name
);
306 BS
:= To_Big_String_Ptr
(Src
);
307 SP
:= BS
(1 .. Natural (Hi
))'Unrestricted_Access;
314 -- Exit if end of file has been reached
316 exit when First
> Last
;
318 if (Last
< First
+ 2) or else (SP
(Last
- 1) /= '%')
319 or else (SP
(Last
) /= 's' and then SP
(Last
) /= 'b')
321 Write_Str
("warning: mapping file """);
322 Write_Str
(File_Name
);
323 Write_Line
(""" is incorrectly formatted");
328 Name_Len
:= Last
- First
+ 1;
329 Name_Buffer
(1 .. Name_Len
) := SP
(First
.. Last
);
330 Uname
:= Find_Unit_Name
;
336 -- If end of line has been reached, file is truncated
344 Name_Len
:= Last
- First
+ 1;
345 Name_Buffer
(1 .. Name_Len
) := SP
(First
.. Last
);
346 Canonical_Case_File_Name
(Name_Buffer
(1 .. Name_Len
));
347 Fname
:= Find_File_Name
;
353 -- If end of line has been reached, file is truncated
361 Name_Len
:= Last
- First
+ 1;
362 Name_Buffer
(1 .. Name_Len
) := SP
(First
.. Last
);
363 Pname
:= Find_File_Name
;
365 -- Add the mappings for this unit name
367 Add_To_File_Map
(Uname
, Fname
, Pname
);
371 -- Record the length of the two mapping tables
373 Last_In_Table
:= File_Mapping
.Last
;
376 ----------------------
377 -- Mapped_File_Name --
378 ----------------------
380 function Mapped_File_Name
(Unit
: Unit_Name_Type
) return File_Name_Type
is
381 The_Index
: constant Int
:= Unit_Hash_Table
.Get
(Unit
);
384 if The_Index
= No_Entry
then
387 return File_Mapping
.Table
(The_Index
).Fname
;
389 end Mapped_File_Name
;
391 ----------------------
392 -- Mapped_Path_Name --
393 ----------------------
395 function Mapped_Path_Name
(File
: File_Name_Type
) return File_Name_Type
is
396 Index
: Int
:= No_Entry
;
399 if Forbidden_Names
.Get
(File
) then
400 return Error_File_Name
;
403 Index
:= File_Hash_Table
.Get
(File
);
405 if Index
= No_Entry
then
408 return Path_Mapping
.Table
(Index
).Fname
;
410 end Mapped_Path_Name
;
412 --------------------------------
413 -- Remove_Forbidden_File_Name --
414 --------------------------------
416 procedure Remove_Forbidden_File_Name
(Name
: File_Name_Type
) is
418 Forbidden_Names
.Set
(Name
, False);
419 end Remove_Forbidden_File_Name
;
425 procedure Reset_Tables
is
429 Unit_Hash_Table
.Reset
;
430 File_Hash_Table
.Reset
;
431 Forbidden_Names
.Reset
;
435 -------------------------
436 -- Update_Mapping_File --
437 -------------------------
439 procedure Update_Mapping_File
(File_Name
: String) is
440 File
: File_Descriptor
;
446 -- For the call to Close
448 procedure Put_Line
(Name
: Name_Id
);
449 -- Put Name as a line in the Mapping File
455 procedure Put_Line
(Name
: Name_Id
) is
457 Get_Name_String
(Name
);
459 -- If the Buffer is full, write it to the file
461 if Buffer_Last
+ Name_Len
+ 1 > Buffer
'Last then
462 N_Bytes
:= Write
(File
, Buffer
(1)'Address, Buffer_Last
);
464 if N_Bytes
< Buffer_Last
then
471 -- Add the line to the Buffer
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
;
479 -- Start of Update_Mapping_File
483 -- Only Update if there are new entries in the mappings
485 if Last_In_Table
< File_Mapping
.Last
then
487 -- If the tables have been emptied, recreate the file.
488 -- Otherwise, append to it.
490 if Last_In_Table
= 0 then
493 pragma Warnings
(Off
, Discard
);
495 Delete_File
(File_Name
, Discard
);
498 File
:= Create_File
(File_Name
, Binary
);
501 File
:= Open_Read_Write
(Name
=> File_Name
, Fmode
=> Binary
);
504 if File
/= Invalid_FD
then
505 if Last_In_Table
> 0 then
506 Lseek
(File
, 0, Seek_End
);
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
));
513 File_Hash_Table
.Get
(File_Mapping
.Table
(Unit
).Fname
);
514 Put_Line
(Name_Id
(Path_Mapping
.Table
(File_Entry
).Fname
));
517 -- Before closing the file, write the buffer to the file. It is
518 -- guaranteed that the Buffer is not empty, because Put_Line has
519 -- been called at least 3 times, and after a call to Put_Line, the
520 -- Buffer is not empty.
522 N_Bytes
:= Write
(File
, Buffer
(1)'Address, Buffer_Last
);
524 if N_Bytes
< Buffer_Last
then
528 Close
(File
, Status
);
534 elsif not Quiet_Output
then
535 Write_Str
("warning: could not open mapping file """);
536 Write_Str
(File_Name
);
537 Write_Line
(""" for update");
541 end Update_Mapping_File
;