In gcc/testsuite/: 2010-09-30 Nicola Pero <nicola.pero@meta-innovation.com>
[official-gcc.git] / gcc / ada / ali-util.adb
blob8c837b4177d3161b7cc20633835eb5958fb78faa
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- A L I . U T I L --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2010, 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 Debug; use Debug;
27 with Binderr; use Binderr;
28 with Opt; use Opt;
29 with Output; use Output;
30 with Osint; use Osint;
31 with Scans; use Scans;
32 with Scng;
33 with Sinput.C;
34 with Snames; use Snames;
35 with Styleg;
37 package body ALI.Util is
39 -- Empty procedures needed to instantiate Scng. Error procedures are
40 -- empty, because we don't want to report any errors when computing
41 -- a source checksum.
43 procedure Post_Scan;
45 procedure Error_Msg (Msg : String; Flag_Location : Source_Ptr);
47 procedure Error_Msg_S (Msg : String);
49 procedure Error_Msg_SC (Msg : String);
51 procedure Error_Msg_SP (Msg : String);
53 -- Instantiation of Styleg, needed to instantiate Scng
55 package Style is new Styleg
56 (Error_Msg, Error_Msg_S, Error_Msg_SC, Error_Msg_SP);
58 -- A Scanner is needed to get checksum of a source (procedure
59 -- Get_File_Checksum).
61 package Scanner is new Scng
62 (Post_Scan, Error_Msg, Error_Msg_S, Error_Msg_SC, Error_Msg_SP, Style);
64 type Header_Num is range 0 .. 1_000;
66 function Hash (F : File_Name_Type) return Header_Num;
67 -- Function used to compute hash of ALI file name
69 package Interfaces is new Simple_HTable (
70 Header_Num => Header_Num,
71 Element => Boolean,
72 No_Element => False,
73 Key => File_Name_Type,
74 Hash => Hash,
75 Equal => "=");
77 ---------------------
78 -- Checksums_Match --
79 ---------------------
81 function Checksums_Match (Checksum1, Checksum2 : Word) return Boolean is
82 begin
83 return Checksum1 = Checksum2 and then Checksum1 /= Checksum_Error;
84 end Checksums_Match;
86 ---------------
87 -- Error_Msg --
88 ---------------
90 procedure Error_Msg (Msg : String; Flag_Location : Source_Ptr) is
91 pragma Warnings (Off, Msg);
92 pragma Warnings (Off, Flag_Location);
93 begin
94 null;
95 end Error_Msg;
97 -----------------
98 -- Error_Msg_S --
99 -----------------
101 procedure Error_Msg_S (Msg : String) is
102 pragma Warnings (Off, Msg);
103 begin
104 null;
105 end Error_Msg_S;
107 ------------------
108 -- Error_Msg_SC --
109 ------------------
111 procedure Error_Msg_SC (Msg : String) is
112 pragma Warnings (Off, Msg);
113 begin
114 null;
115 end Error_Msg_SC;
117 ------------------
118 -- Error_Msg_SP --
119 ------------------
121 procedure Error_Msg_SP (Msg : String) is
122 pragma Warnings (Off, Msg);
123 begin
124 null;
125 end Error_Msg_SP;
127 -----------------------
128 -- Get_File_Checksum --
129 -----------------------
131 function Get_File_Checksum (Fname : File_Name_Type) return Word is
132 Full_Name : File_Name_Type;
133 Source_Index : Source_File_Index;
135 begin
136 Full_Name := Find_File (Fname, Osint.Source);
138 -- If we cannot find the file, then return an impossible checksum,
139 -- impossible because checksums have the high order bit zero, so
140 -- that checksums do not match.
142 if Full_Name = No_File then
143 return Checksum_Error;
144 end if;
146 Source_Index := Sinput.C.Load_File (Get_Name_String (Full_Name));
148 if Source_Index = No_Source_File then
149 return Checksum_Error;
150 end if;
152 Scanner.Initialize_Scanner (Source_Index);
154 -- Make sure that the project language reserved words are not
155 -- recognized as reserved words, but as identifiers. The byte info for
156 -- those names have been set if we are in gnatmake.
158 Set_Name_Table_Byte (Name_Project, 0);
159 Set_Name_Table_Byte (Name_Extends, 0);
160 Set_Name_Table_Byte (Name_External, 0);
162 -- Scan the complete file to compute its checksum
164 loop
165 Scanner.Scan;
166 exit when Token = Tok_EOF;
167 end loop;
169 return Scans.Checksum;
170 end Get_File_Checksum;
172 ----------
173 -- Hash --
174 ----------
176 function Hash (F : File_Name_Type) return Header_Num is
177 begin
178 return Header_Num (Int (F) rem Header_Num'Range_Length);
179 end Hash;
181 ---------------------------
182 -- Initialize_ALI_Source --
183 ---------------------------
185 procedure Initialize_ALI_Source is
186 begin
187 -- When (re)initializing ALI data structures the ALI user expects to
188 -- get a fresh set of data structures. Thus we first need to erase the
189 -- marks put in the name table by the previous set of ALI routine calls.
190 -- This loop is empty and harmless the first time in.
192 for J in Source.First .. Source.Last loop
193 Set_Name_Table_Info (Source.Table (J).Sfile, 0);
194 Source.Table (J).Source_Found := False;
195 end loop;
197 Source.Init;
198 Interfaces.Reset;
199 end Initialize_ALI_Source;
201 ---------------
202 -- Post_Scan --
203 ---------------
205 procedure Post_Scan is
206 begin
207 null;
208 end Post_Scan;
210 ----------------------
211 -- Read_Withed_ALIs --
212 ----------------------
214 procedure Read_Withed_ALIs (Id : ALI_Id) is
215 Afile : File_Name_Type;
216 Text : Text_Buffer_Ptr;
217 Idread : ALI_Id;
219 begin
220 -- Process all dependent units
222 for U in ALIs.Table (Id).First_Unit .. ALIs.Table (Id).Last_Unit loop
224 W in Units.Table (U).First_With .. Units.Table (U).Last_With
225 loop
226 Afile := Withs.Table (W).Afile;
228 -- Only process if not a generic (Afile /= No_File) and if
229 -- file has not been processed already.
231 if Afile /= No_File
232 and then Get_Name_Table_Info (Afile) = 0
233 then
234 Text := Read_Library_Info (Afile);
236 -- Return with an error if source cannot be found. We used to
237 -- skip this check when we did not compile library generics
238 -- separately, but we now always do, so there is no special
239 -- case here anymore.
241 if Text = null then
242 Error_Msg_File_1 := Afile;
243 Error_Msg_File_2 := Withs.Table (W).Sfile;
244 Error_Msg ("{ not found, { must be compiled");
245 Set_Name_Table_Info (Afile, Int (No_Unit_Id));
246 return;
247 end if;
249 -- Enter in ALIs table
251 Idread :=
252 Scan_ALI
253 (F => Afile,
254 T => Text,
255 Ignore_ED => False,
256 Err => False);
258 Free (Text);
260 if ALIs.Table (Idread).Compile_Errors then
261 Error_Msg_File_1 := Withs.Table (W).Sfile;
262 Error_Msg ("{ had errors, must be fixed, and recompiled");
263 Set_Name_Table_Info (Afile, Int (No_Unit_Id));
265 elsif ALIs.Table (Idread).No_Object then
266 Error_Msg_File_1 := Withs.Table (W).Sfile;
267 Error_Msg ("{ must be recompiled");
268 Set_Name_Table_Info (Afile, Int (No_Unit_Id));
269 end if;
271 -- If the Unit is an Interface to a Stand-Alone Library,
272 -- set the Interface flag in the Withs table, so that its
273 -- dependant are not considered for elaboration order.
275 if ALIs.Table (Idread).SAL_Interface then
276 Withs.Table (W).SAL_Interface := True;
277 Interface_Library_Unit := True;
279 -- Set the entry in the Interfaces hash table, so that other
280 -- units that import this unit will set the flag in their
281 -- entry in the Withs table.
283 Interfaces.Set (Afile, True);
285 else
286 -- Otherwise, recurse to get new dependents
288 Read_Withed_ALIs (Idread);
289 end if;
291 -- If the ALI file has already been processed and is an interface,
292 -- set the flag in the entry of the Withs table.
294 elsif Interface_Library_Unit and then Interfaces.Get (Afile) then
295 Withs.Table (W).SAL_Interface := True;
296 end if;
297 end loop;
298 end loop;
299 end Read_Withed_ALIs;
301 ----------------------
302 -- Set_Source_Table --
303 ----------------------
305 procedure Set_Source_Table (A : ALI_Id) is
306 F : File_Name_Type;
307 S : Source_Id;
308 Stamp : Time_Stamp_Type;
310 begin
311 Sdep_Loop : for D in
312 ALIs.Table (A).First_Sdep .. ALIs.Table (A).Last_Sdep
313 loop
314 F := Sdep.Table (D).Sfile;
316 if F /= No_File then
318 -- If this is the first time we are seeing this source file,
319 -- then make a new entry in the source table.
321 if Get_Name_Table_Info (F) = 0 then
322 Source.Increment_Last;
323 S := Source.Last;
324 Set_Name_Table_Info (F, Int (S));
325 Source.Table (S).Sfile := F;
326 Source.Table (S).All_Timestamps_Match := True;
328 -- Initialize checksum fields
330 Source.Table (S).Checksum := Sdep.Table (D).Checksum;
331 Source.Table (S).All_Checksums_Match := True;
333 -- In check source files mode, try to get time stamp from file
335 if Opt.Check_Source_Files then
336 Stamp := Source_File_Stamp (F);
338 -- If we got the stamp, then set the stamp in the source
339 -- table entry and mark it as set from the source so that
340 -- it does not get subsequently changed.
342 if Stamp (Stamp'First) /= ' ' then
343 Source.Table (S).Stamp := Stamp;
344 Source.Table (S).Source_Found := True;
346 -- If we could not find the file, then the stamp is set
347 -- from the dependency table entry (to be possibly reset
348 -- if we find a later stamp in subsequent processing)
350 else
351 Source.Table (S).Stamp := Sdep.Table (D).Stamp;
352 Source.Table (S).Source_Found := False;
354 -- In All_Sources mode, flag error of file not found
356 if Opt.All_Sources then
357 Error_Msg_File_1 := F;
358 Error_Msg ("cannot locate {");
359 end if;
360 end if;
362 -- First time for this source file, but Check_Source_Files
363 -- is off, so simply initialize the stamp from the Sdep entry
365 else
366 Source.Table (S).Source_Found := False;
367 Source.Table (S).Stamp := Sdep.Table (D).Stamp;
368 end if;
370 -- Here if this is not the first time for this source file,
371 -- so that the source table entry is already constructed.
373 else
374 S := Source_Id (Get_Name_Table_Info (F));
376 -- Update checksum flag
378 if not Checksums_Match
379 (Sdep.Table (D).Checksum, Source.Table (S).Checksum)
380 then
381 Source.Table (S).All_Checksums_Match := False;
382 end if;
384 -- Check for time stamp mismatch
386 if Sdep.Table (D).Stamp /= Source.Table (S).Stamp then
387 Source.Table (S).All_Timestamps_Match := False;
389 -- When we have a time stamp mismatch, we go look for the
390 -- source file even if Check_Source_Files is false, since
391 -- if we find it, then we can use it to resolve which of the
392 -- two timestamps in the ALI files is likely to be correct.
394 if not Check_Source_Files then
395 Stamp := Source_File_Stamp (F);
397 if Stamp (Stamp'First) /= ' ' then
398 Source.Table (S).Stamp := Stamp;
399 Source.Table (S).Source_Found := True;
400 end if;
401 end if;
403 -- If the stamp in the source table entry was set from the
404 -- source file, then we do not change it (the stamp in the
405 -- source file is always taken as the "right" one).
407 if Source.Table (S).Source_Found then
408 null;
410 -- Otherwise, we have no source file available, so we guess
411 -- that the later of the two timestamps is the right one.
412 -- Note that this guess only affects which error messages
413 -- are issued later on, not correct functionality.
415 else
416 if Sdep.Table (D).Stamp > Source.Table (S).Stamp then
417 Source.Table (S).Stamp := Sdep.Table (D).Stamp;
418 end if;
419 end if;
420 end if;
421 end if;
423 -- Set the checksum value in the source table
425 S := Source_Id (Get_Name_Table_Info (F));
426 Source.Table (S).Checksum := Sdep.Table (D).Checksum;
427 end if;
429 end loop Sdep_Loop;
430 end Set_Source_Table;
432 ----------------------
433 -- Set_Source_Table --
434 ----------------------
436 procedure Set_Source_Table is
437 begin
438 for A in ALIs.First .. ALIs.Last loop
439 Set_Source_Table (A);
440 end loop;
441 end Set_Source_Table;
443 -------------------------
444 -- Time_Stamp_Mismatch --
445 -------------------------
447 function Time_Stamp_Mismatch
448 (A : ALI_Id;
449 Read_Only : Boolean := False) return File_Name_Type
451 Src : Source_Id;
452 -- Source file Id for the current Sdep entry
454 begin
455 for D in ALIs.Table (A).First_Sdep .. ALIs.Table (A).Last_Sdep loop
456 Src := Source_Id (Get_Name_Table_Info (Sdep.Table (D).Sfile));
458 if Opt.Minimal_Recompilation
459 and then Sdep.Table (D).Stamp /= Source.Table (Src).Stamp
460 then
461 -- If minimal recompilation is in action, replace the stamp
462 -- of the source file in the table if checksums match.
464 -- ??? It is probably worth updating the ALI file with a new
465 -- field to avoid recomputing it each time.
467 if Checksums_Match
468 (Get_File_Checksum (Sdep.Table (D).Sfile),
469 Source.Table (Src).Checksum)
470 then
471 if Verbose_Mode then
472 Write_Str (" ");
473 Write_Str (Get_Name_String (Sdep.Table (D).Sfile));
474 Write_Str (": up to date, different timestamps " &
475 "but same checksum");
476 Write_Eol;
477 end if;
479 Sdep.Table (D).Stamp := Source.Table (Src).Stamp;
480 end if;
482 end if;
484 if (not Read_Only) or else Source.Table (Src).Source_Found then
485 if not Source.Table (Src).Source_Found
486 or else Sdep.Table (D).Stamp /= Source.Table (Src).Stamp
487 then
488 -- If -dt debug flag set, output time stamp found/expected
490 if Source.Table (Src).Source_Found and then Debug_Flag_T then
491 Write_Str ("Source: """);
492 Get_Name_String (Sdep.Table (D).Sfile);
493 Write_Str (Name_Buffer (1 .. Name_Len));
494 Write_Line ("""");
496 Write_Str (" time stamp expected: ");
497 Write_Line (String (Sdep.Table (D).Stamp));
499 Write_Str (" time stamp found: ");
500 Write_Line (String (Source.Table (Src).Stamp));
501 end if;
503 -- Return the source file
505 return Source.Table (Src).Sfile;
506 end if;
507 end if;
508 end loop;
510 return No_File;
511 end Time_Stamp_Mismatch;
513 end ALI.Util;