1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2015, 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 pragma Warnings
(Off
);
33 -- This package is used also by gnatcoll
34 with System
.OS_Lib
; use System
.OS_Lib
;
37 with Unchecked_Conversion
;
43 No_Mapping_File
: Boolean := False;
44 -- Set to True when the specified mapping file cannot be read in
45 -- procedure Initialize, so that no attempt is made to open the mapping
46 -- file in procedure Update_Mapping_File.
48 function To_Big_String_Ptr
is new Unchecked_Conversion
49 (Source_Buffer_Ptr
, Big_String_Ptr
);
51 Max_Buffer
: constant := 1_500
;
52 Buffer
: String (1 .. Max_Buffer
);
53 -- Used to buffer output when writing to a new mapping file
55 Buffer_Last
: Natural := 0;
56 -- Index of last valid character in Buffer
58 type Mapping
is record
59 Uname
: Unit_Name_Type
;
60 Fname
: File_Name_Type
;
63 package File_Mapping
is new Table
.Table
(
64 Table_Component_Type
=> Mapping
,
65 Table_Index_Type
=> Int
,
67 Table_Initial
=> 1_000
,
68 Table_Increment
=> 1_000
,
69 Table_Name
=> "Fmap.File_Mapping");
70 -- Mapping table to map unit names to file names
72 package Path_Mapping
is new Table
.Table
(
73 Table_Component_Type
=> Mapping
,
74 Table_Index_Type
=> Int
,
76 Table_Initial
=> 1_000
,
77 Table_Increment
=> 1_000
,
78 Table_Name
=> "Fmap.Path_Mapping");
79 -- Mapping table to map file names to path names
81 type Header_Num
is range 0 .. 1_000
;
83 function Hash
(F
: Unit_Name_Type
) return Header_Num
;
84 -- Function used to compute hash of unit name
86 No_Entry
: constant Int
:= -1;
87 -- Signals no entry in following table
89 package Unit_Hash_Table
is new GNAT
.HTable
.Simple_HTable
(
90 Header_Num
=> Header_Num
,
92 No_Element
=> No_Entry
,
93 Key
=> Unit_Name_Type
,
96 -- Hash table to map unit names to file names. Used in conjunction with
97 -- table File_Mapping above.
99 function Hash
(F
: File_Name_Type
) return Header_Num
;
100 -- Function used to compute hash of file name
102 package File_Hash_Table
is new GNAT
.HTable
.Simple_HTable
(
103 Header_Num
=> Header_Num
,
105 No_Element
=> No_Entry
,
106 Key
=> File_Name_Type
,
109 -- Hash table to map file names to path names. Used in conjunction with
110 -- table Path_Mapping above.
112 Last_In_Table
: Int
:= 0;
114 package Forbidden_Names
is new GNAT
.HTable
.Simple_HTable
(
115 Header_Num
=> Header_Num
,
118 Key
=> File_Name_Type
,
122 -----------------------------
123 -- Add_Forbidden_File_Name --
124 -----------------------------
126 procedure Add_Forbidden_File_Name
(Name
: File_Name_Type
) is
128 Forbidden_Names
.Set
(Name
, True);
129 end Add_Forbidden_File_Name
;
131 ---------------------
132 -- Add_To_File_Map --
133 ---------------------
135 procedure Add_To_File_Map
136 (Unit_Name
: Unit_Name_Type
;
137 File_Name
: File_Name_Type
;
138 Path_Name
: File_Name_Type
)
140 Unit_Entry
: constant Int
:= Unit_Hash_Table
.Get
(Unit_Name
);
141 File_Entry
: constant Int
:= File_Hash_Table
.Get
(File_Name
);
143 if Unit_Entry
= No_Entry
or else
144 File_Mapping
.Table
(Unit_Entry
).Fname
/= File_Name
146 File_Mapping
.Increment_Last
;
147 Unit_Hash_Table
.Set
(Unit_Name
, File_Mapping
.Last
);
148 File_Mapping
.Table
(File_Mapping
.Last
) :=
149 (Uname
=> Unit_Name
, Fname
=> File_Name
);
152 if File_Entry
= No_Entry
or else
153 Path_Mapping
.Table
(File_Entry
).Fname
/= Path_Name
155 Path_Mapping
.Increment_Last
;
156 File_Hash_Table
.Set
(File_Name
, Path_Mapping
.Last
);
157 Path_Mapping
.Table
(Path_Mapping
.Last
) :=
158 (Uname
=> Unit_Name
, Fname
=> Path_Name
);
166 function Hash
(F
: File_Name_Type
) return Header_Num
is
168 return Header_Num
(Int
(F
) rem Header_Num
'Range_Length);
171 function Hash
(F
: Unit_Name_Type
) return Header_Num
is
173 return Header_Num
(Int
(F
) rem Header_Num
'Range_Length);
180 procedure Initialize
(File_Name
: String) is
181 Src
: Source_Buffer_Ptr
;
186 First
: Positive := 1;
189 Uname
: Unit_Name_Type
;
190 Fname
: File_Name_Type
;
191 Pname
: File_Name_Type
;
193 procedure Empty_Tables
;
194 -- Remove all entries in case of incorrect mapping file
196 function Find_File_Name
return File_Name_Type
;
197 -- Return Error_File_Name if the name buffer contains "/", otherwise
198 -- call Name_Find. "/" is the path name in the mapping file to indicate
199 -- that a source has been suppressed, and thus should not be found by
202 function Find_Unit_Name
return Unit_Name_Type
;
203 -- Return the unit name in the name buffer. Return Error_Unit_Name if
204 -- the name buffer contains "/".
207 -- Get a line from the mapping file, where a line is SP (First .. Last)
209 procedure Report_Truncated
;
210 -- Report a warning when the mapping file is truncated
211 -- (number of lines is not a multiple of 3).
217 procedure Empty_Tables
is
219 Unit_Hash_Table
.Reset
;
220 File_Hash_Table
.Reset
;
221 Path_Mapping
.Set_Last
(0);
222 File_Mapping
.Set_Last
(0);
230 function Find_File_Name
return File_Name_Type
is
232 if Name_Buffer
(1 .. Name_Len
) = "/" then
234 -- A path name of "/" is the indication that the source has been
235 -- "suppressed". Return Error_File_Name so that the compiler does
236 -- not find the source, even if it is in the include path.
238 return Error_File_Name
;
249 function Find_Unit_Name
return Unit_Name_Type
is
251 return Unit_Name_Type
(Find_File_Name
);
258 procedure Get_Line
is
264 -- If not at the end of file, skip the end of line
266 while First
< SP
'Last
267 and then (SP
(First
) = CR
268 or else SP
(First
) = LF
269 or else SP
(First
) = EOF
)
274 -- If not at the end of file, find the end of this new line
276 if First
< SP
'Last and then SP
(First
) /= EOF
then
280 and then SP
(Last
+ 1) /= CR
281 and then SP
(Last
+ 1) /= LF
282 and then SP
(Last
+ 1) /= EOF
290 ----------------------
291 -- Report_Truncated --
292 ----------------------
294 procedure Report_Truncated
is
296 Write_Str
("warning: mapping file """);
297 Write_Str
(File_Name
);
298 Write_Line
(""" is truncated");
299 end Report_Truncated
;
301 -- Start of processing for Initialize
305 Name_Len
:= File_Name
'Length;
306 Name_Buffer
(1 .. Name_Len
) := File_Name
;
307 Read_Source_File
(Name_Enter
, 0, Hi
, Src
, Config
);
310 Write_Str
("warning: could not read mapping file """);
311 Write_Str
(File_Name
);
313 No_Mapping_File
:= True;
316 BS
:= To_Big_String_Ptr
(Src
);
317 SP
:= BS
(1 .. Natural (Hi
))'Unrestricted_Access;
324 -- Exit if end of file has been reached
326 exit when First
> Last
;
328 if (Last
< First
+ 2) or else (SP
(Last
- 1) /= '%')
329 or else (SP
(Last
) /= 's' and then SP
(Last
) /= 'b')
332 ("warning: mapping file """ & File_Name
&
333 """ is incorrectly formatted");
334 Write_Line
("Line = """ & SP
(First
.. Last
) & '"');
339 Name_Len
:= Last
- First
+ 1;
340 Name_Buffer
(1 .. Name_Len
) := SP
(First
.. Last
);
341 Uname
:= Find_Unit_Name
;
347 -- If end of line has been reached, file is truncated
355 Name_Len
:= Last
- First
+ 1;
356 Name_Buffer
(1 .. Name_Len
) := SP
(First
.. Last
);
357 Canonical_Case_File_Name
(Name_Buffer
(1 .. Name_Len
));
358 Fname
:= Find_File_Name
;
364 -- If end of line has been reached, file is truncated
372 Name_Len
:= Last
- First
+ 1;
373 Name_Buffer
(1 .. Name_Len
) := SP
(First
.. Last
);
374 Pname
:= Find_File_Name
;
376 -- Add the mappings for this unit name
378 Add_To_File_Map
(Uname
, Fname
, Pname
);
382 -- Record the length of the two mapping tables
384 Last_In_Table
:= File_Mapping
.Last
;
387 ----------------------
388 -- Mapped_File_Name --
389 ----------------------
391 function Mapped_File_Name
(Unit
: Unit_Name_Type
) return File_Name_Type
is
392 The_Index
: constant Int
:= Unit_Hash_Table
.Get
(Unit
);
395 if The_Index
= No_Entry
then
398 return File_Mapping
.Table
(The_Index
).Fname
;
400 end Mapped_File_Name
;
402 ----------------------
403 -- Mapped_Path_Name --
404 ----------------------
406 function Mapped_Path_Name
(File
: File_Name_Type
) return File_Name_Type
is
407 Index
: Int
:= No_Entry
;
410 if Forbidden_Names
.Get
(File
) then
411 return Error_File_Name
;
414 Index
:= File_Hash_Table
.Get
(File
);
416 if Index
= No_Entry
then
419 return Path_Mapping
.Table
(Index
).Fname
;
421 end Mapped_Path_Name
;
427 procedure Reset_Tables
is
431 Unit_Hash_Table
.Reset
;
432 File_Hash_Table
.Reset
;
433 Forbidden_Names
.Reset
;
437 -------------------------
438 -- Update_Mapping_File --
439 -------------------------
441 procedure Update_Mapping_File
(File_Name
: String) is
442 File
: File_Descriptor
;
448 -- For the call to Close
450 procedure Put_Line
(Name
: Name_Id
);
451 -- Put Name as a line in the Mapping File
457 procedure Put_Line
(Name
: Name_Id
) is
459 Get_Name_String
(Name
);
461 -- If the Buffer is full, write it to the file
463 if Buffer_Last
+ Name_Len
+ 1 > Buffer
'Last then
464 N_Bytes
:= Write
(File
, Buffer
(1)'Address, Buffer_Last
);
466 if N_Bytes
< Buffer_Last
then
473 -- Add the line to the Buffer
475 Buffer
(Buffer_Last
+ 1 .. Buffer_Last
+ Name_Len
) :=
476 Name_Buffer
(1 .. Name_Len
);
477 Buffer_Last
:= Buffer_Last
+ Name_Len
+ 1;
478 Buffer
(Buffer_Last
) := ASCII
.LF
;
481 -- Start of processing for Update_Mapping_File
484 -- If the mapping file could not be read, then it will not be possible
487 if No_Mapping_File
then
490 -- Only Update if there are new entries in the mappings
492 if Last_In_Table
< File_Mapping
.Last
then
494 File
:= Open_Read_Write
(Name
=> File_Name
, Fmode
=> Binary
);
496 if File
/= Invalid_FD
then
497 if Last_In_Table
> 0 then
498 Lseek
(File
, 0, Seek_End
);
501 for Unit
in Last_In_Table
+ 1 .. File_Mapping
.Last
loop
502 Put_Line
(Name_Id
(File_Mapping
.Table
(Unit
).Uname
));
503 Put_Line
(Name_Id
(File_Mapping
.Table
(Unit
).Fname
));
505 File_Hash_Table
.Get
(File_Mapping
.Table
(Unit
).Fname
);
506 Put_Line
(Name_Id
(Path_Mapping
.Table
(File_Entry
).Fname
));
509 -- Before closing the file, write the buffer to the file. It is
510 -- guaranteed that the Buffer is not empty, because Put_Line has
511 -- been called at least 3 times, and after a call to Put_Line, the
512 -- Buffer is not empty.
514 N_Bytes
:= Write
(File
, Buffer
(1)'Address, Buffer_Last
);
516 if N_Bytes
< Buffer_Last
then
520 Close
(File
, Status
);
526 elsif not Quiet_Output
then
527 Write_Str
("warning: could not open mapping file """);
528 Write_Str
(File_Name
);
529 Write_Line
(""" for update");
533 end Update_Mapping_File
;