* config/riscv/riscv.c: Remove unnecessary includes. Reorder
[official-gcc.git] / gcc / ada / fmap.adb
blobd517c2a4ddc5783f93c997349647012f6a8d089c
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- F M A P --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2001-2017, 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 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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Opt; use Opt;
27 with Osint; use Osint;
28 with Output; use Output;
29 with Table;
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;
35 pragma Warnings (On);
37 with Unchecked_Conversion;
39 with GNAT.HTable;
41 package body Fmap is
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 Max_Buffer : constant := 1_500;
49 Buffer : String (1 .. Max_Buffer);
50 -- Used to buffer output when writing to a new mapping file
52 Buffer_Last : Natural := 0;
53 -- Index of last valid character in Buffer
55 type Mapping is record
56 Uname : Unit_Name_Type;
57 Fname : File_Name_Type;
58 end record;
60 package File_Mapping is new Table.Table (
61 Table_Component_Type => Mapping,
62 Table_Index_Type => Int,
63 Table_Low_Bound => 0,
64 Table_Initial => 1_000,
65 Table_Increment => 1_000,
66 Table_Name => "Fmap.File_Mapping");
67 -- Mapping table to map unit names to file names
69 package Path_Mapping is new Table.Table (
70 Table_Component_Type => Mapping,
71 Table_Index_Type => Int,
72 Table_Low_Bound => 0,
73 Table_Initial => 1_000,
74 Table_Increment => 1_000,
75 Table_Name => "Fmap.Path_Mapping");
76 -- Mapping table to map file names to path names
78 type Header_Num is range 0 .. 1_000;
80 function Hash (F : Unit_Name_Type) return Header_Num;
81 -- Function used to compute hash of unit name
83 No_Entry : constant Int := -1;
84 -- Signals no entry in following table
86 package Unit_Hash_Table is new GNAT.HTable.Simple_HTable (
87 Header_Num => Header_Num,
88 Element => Int,
89 No_Element => No_Entry,
90 Key => Unit_Name_Type,
91 Hash => Hash,
92 Equal => "=");
93 -- Hash table to map unit names to file names. Used in conjunction with
94 -- table File_Mapping above.
96 function Hash (F : File_Name_Type) return Header_Num;
97 -- Function used to compute hash of file name
99 package File_Hash_Table is new GNAT.HTable.Simple_HTable (
100 Header_Num => Header_Num,
101 Element => Int,
102 No_Element => No_Entry,
103 Key => File_Name_Type,
104 Hash => Hash,
105 Equal => "=");
106 -- Hash table to map file names to path names. Used in conjunction with
107 -- table Path_Mapping above.
109 Last_In_Table : Int := 0;
111 package Forbidden_Names is new GNAT.HTable.Simple_HTable (
112 Header_Num => Header_Num,
113 Element => Boolean,
114 No_Element => False,
115 Key => File_Name_Type,
116 Hash => Hash,
117 Equal => "=");
119 -----------------------------
120 -- Add_Forbidden_File_Name --
121 -----------------------------
123 procedure Add_Forbidden_File_Name (Name : File_Name_Type) is
124 begin
125 Forbidden_Names.Set (Name, True);
126 end Add_Forbidden_File_Name;
128 ---------------------
129 -- Add_To_File_Map --
130 ---------------------
132 procedure Add_To_File_Map
133 (Unit_Name : Unit_Name_Type;
134 File_Name : File_Name_Type;
135 Path_Name : File_Name_Type)
137 Unit_Entry : constant Int := Unit_Hash_Table.Get (Unit_Name);
138 File_Entry : constant Int := File_Hash_Table.Get (File_Name);
139 begin
140 if Unit_Entry = No_Entry or else
141 File_Mapping.Table (Unit_Entry).Fname /= File_Name
142 then
143 File_Mapping.Increment_Last;
144 Unit_Hash_Table.Set (Unit_Name, File_Mapping.Last);
145 File_Mapping.Table (File_Mapping.Last) :=
146 (Uname => Unit_Name, Fname => File_Name);
147 end if;
149 if File_Entry = No_Entry or else
150 Path_Mapping.Table (File_Entry).Fname /= Path_Name
151 then
152 Path_Mapping.Increment_Last;
153 File_Hash_Table.Set (File_Name, Path_Mapping.Last);
154 Path_Mapping.Table (Path_Mapping.Last) :=
155 (Uname => Unit_Name, Fname => Path_Name);
156 end if;
157 end Add_To_File_Map;
159 ----------
160 -- Hash --
161 ----------
163 function Hash (F : File_Name_Type) return Header_Num is
164 begin
165 return Header_Num (Int (F) rem Header_Num'Range_Length);
166 end Hash;
168 function Hash (F : Unit_Name_Type) return Header_Num is
169 begin
170 return Header_Num (Int (F) rem Header_Num'Range_Length);
171 end Hash;
173 ----------------
174 -- Initialize --
175 ----------------
177 procedure Initialize (File_Name : String) is
178 Src : Source_Buffer_Ptr;
179 Hi : Source_Ptr;
181 First : Source_Ptr := 1;
182 Last : Source_Ptr := 0;
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 if the name buffer contains "/", otherwise
193 -- call Name_Find. "/" is the path name in the mapping file to indicate
194 -- that a source has been suppressed, and thus should not be found by
195 -- the compiler.
197 function Find_Unit_Name return Unit_Name_Type;
198 -- Return the unit name in the name buffer. Return Error_Unit_Name if
199 -- the name buffer contains "/".
201 procedure Get_Line;
202 -- Get a line from the mapping file, where a line is Src (First .. Last)
204 procedure Report_Truncated;
205 -- Report a warning when the mapping file is truncated
206 -- (number of lines is not a multiple of 3).
208 ------------------
209 -- Empty_Tables --
210 ------------------
212 procedure Empty_Tables is
213 begin
214 Unit_Hash_Table.Reset;
215 File_Hash_Table.Reset;
216 Path_Mapping.Set_Last (0);
217 File_Mapping.Set_Last (0);
218 Last_In_Table := 0;
219 end Empty_Tables;
221 --------------------
222 -- Find_File_Name --
223 --------------------
225 function Find_File_Name return File_Name_Type is
226 begin
227 if Name_Buffer (1 .. Name_Len) = "/" then
229 -- A path name of "/" is the indication that the source has been
230 -- "suppressed". Return Error_File_Name so that the compiler does
231 -- not find the source, even if it is in the include path.
233 return Error_File_Name;
235 else
236 return Name_Find;
237 end if;
238 end Find_File_Name;
240 --------------------
241 -- Find_Unit_Name --
242 --------------------
244 function Find_Unit_Name return Unit_Name_Type is
245 begin
246 return Unit_Name_Type (Find_File_Name);
247 end Find_Unit_Name;
249 --------------
250 -- Get_Line --
251 --------------
253 procedure Get_Line is
254 use ASCII;
256 begin
257 First := Last + 1;
259 -- If not at the end of file, skip the end of line
261 while First < Src'Last
262 and then (Src (First) = CR
263 or else Src (First) = LF
264 or else Src (First) = EOF)
265 loop
266 First := First + 1;
267 end loop;
269 -- If not at the end of file, find the end of this new line
271 if First < Src'Last and then Src (First) /= EOF then
272 Last := First;
274 while Last < Src'Last
275 and then Src (Last + 1) /= CR
276 and then Src (Last + 1) /= LF
277 and then Src (Last + 1) /= EOF
278 loop
279 Last := Last + 1;
280 end loop;
282 end if;
283 end Get_Line;
285 ----------------------
286 -- Report_Truncated --
287 ----------------------
289 procedure Report_Truncated is
290 begin
291 Write_Str ("warning: mapping file """);
292 Write_Str (File_Name);
293 Write_Line (""" is truncated");
294 end Report_Truncated;
296 -- Start of processing for Initialize
298 begin
299 Empty_Tables;
300 Read_Source_File (Name_Enter (File_Name), 1, Hi, Src, Config);
302 if Null_Source_Buffer_Ptr (Src) then
303 Write_Str ("warning: could not read mapping file """);
304 Write_Str (File_Name);
305 Write_Line ("""");
306 No_Mapping_File := True;
308 else
309 loop
310 -- Get the unit name
312 Get_Line;
314 -- Exit if end of file has been reached
316 exit when First > Last;
318 if (Last < First + 2) or else (Src (Last - 1) /= '%')
319 or else (Src (Last) /= 's' and then Src (Last) /= 'b')
320 then
321 Write_Line
322 ("warning: mapping file """ & File_Name &
323 """ is incorrectly formatted");
324 Write_Line ("Line = """ & String (Src (First .. Last)) & '"');
325 Empty_Tables;
326 return;
327 end if;
329 Name_Len := Integer (Last - First + 1);
330 Name_Buffer (1 .. Name_Len) := String (Src (First .. Last));
331 Uname := Find_Unit_Name;
333 -- Get the file name
335 Get_Line;
337 -- If end of line has been reached, file is truncated
339 if First > Last then
340 Report_Truncated;
341 Empty_Tables;
342 return;
343 end if;
345 Name_Len := Integer (Last - First + 1);
346 Name_Buffer (1 .. Name_Len) := String (Src (First .. Last));
347 Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
348 Fname := Find_File_Name;
350 -- Get the path name
352 Get_Line;
354 -- If end of line has been reached, file is truncated
356 if First > Last then
357 Report_Truncated;
358 Empty_Tables;
359 return;
360 end if;
362 Name_Len := Integer (Last - First + 1);
363 Name_Buffer (1 .. Name_Len) := String (Src (First .. Last));
364 Pname := Find_File_Name;
366 -- Add the mappings for this unit name
368 Add_To_File_Map (Uname, Fname, Pname);
369 end loop;
370 end if;
372 -- Record the length of the two mapping tables
374 Last_In_Table := File_Mapping.Last;
375 end Initialize;
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);
384 begin
385 if The_Index = No_Entry then
386 return No_File;
387 else
388 return File_Mapping.Table (The_Index).Fname;
389 end if;
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;
399 begin
400 if Forbidden_Names.Get (File) then
401 return Error_File_Name;
402 end if;
404 Index := File_Hash_Table.Get (File);
406 if Index = No_Entry then
407 return No_File;
408 else
409 return Path_Mapping.Table (Index).Fname;
410 end if;
411 end Mapped_Path_Name;
413 ------------------
414 -- Reset_Tables --
415 ------------------
417 procedure Reset_Tables is
418 begin
419 File_Mapping.Init;
420 Path_Mapping.Init;
421 Unit_Hash_Table.Reset;
422 File_Hash_Table.Reset;
423 Forbidden_Names.Reset;
424 Last_In_Table := 0;
425 end Reset_Tables;
427 -------------------------
428 -- Update_Mapping_File --
429 -------------------------
431 procedure Update_Mapping_File (File_Name : String) is
432 File : File_Descriptor;
433 N_Bytes : Integer;
435 File_Entry : Int;
437 Status : Boolean;
438 -- For the call to Close
440 procedure Put_Line (Name : Name_Id);
441 -- Put Name as a line in the Mapping File
443 --------------
444 -- Put_Line --
445 --------------
447 procedure Put_Line (Name : Name_Id) is
448 begin
449 Get_Name_String (Name);
451 -- If the Buffer is full, write it to the file
453 if Buffer_Last + Name_Len + 1 > Buffer'Last then
454 N_Bytes := Write (File, Buffer (1)'Address, Buffer_Last);
456 if N_Bytes < Buffer_Last then
457 Fail ("disk full");
458 end if;
460 Buffer_Last := 0;
461 end if;
463 -- Add the line to the Buffer
465 Buffer (Buffer_Last + 1 .. Buffer_Last + Name_Len) :=
466 Name_Buffer (1 .. Name_Len);
467 Buffer_Last := Buffer_Last + Name_Len + 1;
468 Buffer (Buffer_Last) := ASCII.LF;
469 end Put_Line;
471 -- Start of processing for Update_Mapping_File
473 begin
474 -- If the mapping file could not be read, then it will not be possible
475 -- to update it.
477 if No_Mapping_File then
478 return;
479 end if;
480 -- Only Update if there are new entries in the mappings
482 if Last_In_Table < File_Mapping.Last then
484 File := Open_Read_Write (Name => File_Name, Fmode => Binary);
486 if File /= Invalid_FD then
487 if Last_In_Table > 0 then
488 Lseek (File, 0, Seek_End);
489 end if;
491 for Unit in Last_In_Table + 1 .. File_Mapping.Last loop
492 Put_Line (Name_Id (File_Mapping.Table (Unit).Uname));
493 Put_Line (Name_Id (File_Mapping.Table (Unit).Fname));
494 File_Entry :=
495 File_Hash_Table.Get (File_Mapping.Table (Unit).Fname);
496 Put_Line (Name_Id (Path_Mapping.Table (File_Entry).Fname));
497 end loop;
499 -- Before closing the file, write the buffer to the file. It is
500 -- guaranteed that the Buffer is not empty, because Put_Line has
501 -- been called at least 3 times, and after a call to Put_Line, the
502 -- Buffer is not empty.
504 N_Bytes := Write (File, Buffer (1)'Address, Buffer_Last);
506 if N_Bytes < Buffer_Last then
507 Fail ("disk full");
508 end if;
510 Close (File, Status);
512 if not Status then
513 Fail ("disk full");
514 end if;
516 elsif not Quiet_Output then
517 Write_Str ("warning: could not open mapping file """);
518 Write_Str (File_Name);
519 Write_Line (""" for update");
520 end if;
522 end if;
523 end Update_Mapping_File;
525 end Fmap;