1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
27 ------------------------------------------------------------------------------
29 with Binderr; use Binderr;
30 with Namet; use Namet;
32 with Osint; use Osint;
36 package body ALI.Util is
38 -----------------------
39 -- Local Subprograms --
40 -----------------------
42 procedure Accumulate_Checksum (C : Character; Csum : in out Word);
43 pragma Inline (Accumulate_Checksum);
44 -- This routine accumulates the checksum given character C. During the
45 -- scanning of a source file, this routine is called with every character
46 -- in the source, excluding blanks, and all control characters (except
47 -- that ESC is included in the checksum). Upper case letters not in string
48 -- literals are folded by the caller. See Sinput spec for the documentation
49 -- of the checksum algorithm. Note: checksum values are only used if we
50 -- generate code, so it is not necessary to worry about making the right
51 -- sequence of calls in any error situation.
53 procedure Initialize_Checksum (Csum : out Word);
54 -- Sets initial value of Csum before any calls to Accumulate_Checksum
56 -------------------------
57 -- Accumulate_Checksum --
58 -------------------------
60 procedure Accumulate_Checksum (C : Character; Csum : in out Word) is
62 System.CRC32.Update (System.CRC32.CRC32 (Csum), C);
63 end Accumulate_Checksum;
69 function Checksums_Match (Checksum1, Checksum2 : Word) return Boolean is
71 return Checksum1 = Checksum2 and then Checksum1 /= Checksum_Error;
74 -----------------------
75 -- Get_File_Checksum --
76 -----------------------
78 function Get_File_Checksum (Fname : Name_Id) return Word is
79 Src : Source_Buffer_Ptr;
85 -- Raised if file not found, or file format error
88 -- Make control characters visible
90 procedure Free_Source;
91 -- Free source file buffer
93 procedure Free_Source is
94 procedure free (Arg : Source_Buffer_Ptr);
95 pragma Import (C, free, "free");
101 -- Start of processing for Get_File_Checksum
104 Read_Source_File (Fname, 0, Hi, Src);
106 -- If we cannot find the file, then return an impossible checksum,
107 -- impossible becaues checksums have the high order bit zero, so
108 -- that checksums do not match.
114 Initialize_Checksum (Csum);
120 -- Spaces and formatting information are ignored in checksum
122 when ' ' | CR | LF | VT | FF | HT =>
125 -- EOF is ignored unless it is the last character
135 -- Non-blank characters that are included in the checksum
137 when '#' | '&' | '*' | ':' | '(' | ',' | '.' | '=' | '>' |
138 '<' | ')' | '/' | ';' | '|' | '!' | '+' | '_' |
139 '0' .. '9' | 'a' .. 'z'
141 Accumulate_Checksum (Src (Ptr), Csum);
144 -- Upper case letters, fold to lower case
148 (Character'Val (Character'Pos (Src (Ptr)) + 32), Csum);
151 -- Left bracket, really should do wide character thing here,
152 -- but for now, don't bother.
157 -- Minus, could be comment
160 if Src (Ptr + 1) = '-' then
163 while Src (Ptr) >= ' ' or else Src (Ptr) = HT loop
168 Accumulate_Checksum ('-', Csum);
172 -- String delimited by double quote
175 Accumulate_Checksum ('"', Csum);
179 exit when Src (Ptr) = '"';
181 if Src (Ptr) < ' ' then
185 Accumulate_Checksum (Src (Ptr), Csum);
188 Accumulate_Checksum ('"', Csum);
191 -- String delimited by percent
194 Accumulate_Checksum ('%', Csum);
198 exit when Src (Ptr) = '%';
200 if Src (Ptr) < ' ' then
204 Accumulate_Checksum (Src (Ptr), Csum);
207 Accumulate_Checksum ('%', Csum);
210 -- Quote, could be character constant
213 Accumulate_Checksum (''', Csum);
215 if Src (Ptr + 2) = ''' then
216 Accumulate_Checksum (Src (Ptr + 1), Csum);
217 Accumulate_Checksum (''', Csum);
220 -- Otherwise assume attribute char. We should deal with wide
221 -- character cases here, but that's hard, so forget it.
227 -- Upper half character, more to be done here, we should worry
228 -- about folding Latin-1, folding other character sets, and
229 -- dealing with the nasty case of upper half wide encoding.
231 when Upper_Half_Character =>
232 Accumulate_Checksum (Src (Ptr), Csum);
235 -- Escape character, we should do the wide character thing here,
236 -- but for now, do not bother.
241 -- Invalid control characters
243 when NUL | SOH | STX | ETX | EOT | ENQ | ACK | BEL | BS | SO |
244 SI | DLE | DC1 | DC2 | DC3 | DC4 | NAK | SYN | ETB | CAN |
245 EM | FS | GS | RS | US | DEL
249 -- Invalid graphic characters
251 when '$' | '?' | '@' | '`' | '\' |
252 '^' | '~' | ']' | '{' | '}'
262 return Checksum_Error;
264 end Get_File_Checksum;
266 ---------------------------
267 -- Initialize_ALI_Source --
268 ---------------------------
270 procedure Initialize_ALI_Source is
272 -- When (re)initializing ALI data structures the ALI user expects to
273 -- get a fresh set of data structures. Thus we first need to erase the
274 -- marks put in the name table by the previous set of ALI routine calls.
275 -- This loop is empty and harmless the first time in.
277 for J in Source.First .. Source.Last loop
278 Set_Name_Table_Info (Source.Table (J).Sfile, 0);
279 Source.Table (J).Source_Found := False;
283 end Initialize_ALI_Source;
285 -------------------------
286 -- Initialize_Checksum --
287 -------------------------
289 procedure Initialize_Checksum (Csum : out Word) is
291 System.CRC32.Initialize (System.CRC32.CRC32 (Csum));
292 end Initialize_Checksum;
298 procedure Read_ALI (Id : ALI_Id) is
299 Afile : File_Name_Type;
300 Text : Text_Buffer_Ptr;
304 for I in ALIs.Table (Id).First_Unit .. ALIs.Table (Id).Last_Unit loop
305 for J in Units.Table (I).First_With .. Units.Table (I).Last_With loop
307 Afile := Withs.Table (J).Afile;
309 -- Only process if not a generic (Afile /= No_File) and if
310 -- file has not been processed already.
312 if Afile /= No_File and then Get_Name_Table_Info (Afile) = 0 then
314 Text := Read_Library_Info (Afile);
317 Error_Msg_Name_1 := Afile;
318 Error_Msg_Name_2 := Withs.Table (J).Sfile;
319 Error_Msg ("% not found, % must be compiled");
320 Set_Name_Table_Info (Afile, Int (No_Unit_Id));
328 Ignore_ED => Force_RM_Elaboration_Order,
333 if ALIs.Table (Idread).Compile_Errors then
334 Error_Msg_Name_1 := Withs.Table (J).Sfile;
335 Error_Msg ("% had errors, must be fixed, and recompiled");
336 Set_Name_Table_Info (Afile, Int (No_Unit_Id));
338 elsif ALIs.Table (Idread).No_Object then
339 Error_Msg_Name_1 := Withs.Table (J).Sfile;
340 Error_Msg ("% must be recompiled");
341 Set_Name_Table_Info (Afile, Int (No_Unit_Id));
344 -- Recurse to get new dependents
353 ----------------------
354 -- Set_Source_Table --
355 ----------------------
357 procedure Set_Source_Table (A : ALI_Id) is
360 Stamp : Time_Stamp_Type;
364 ALIs.Table (A).First_Sdep .. ALIs.Table (A).Last_Sdep
366 F := Sdep.Table (D).Sfile;
368 -- If this is the first time we are seeing this source file,
369 -- then make a new entry in the source table.
371 if Get_Name_Table_Info (F) = 0 then
372 Source.Increment_Last;
374 Set_Name_Table_Info (F, Int (S));
375 Source.Table (S).Sfile := F;
376 Source.Table (S).All_Timestamps_Match := True;
378 -- Initialize checksum fields
380 Source.Table (S).Checksum := Sdep.Table (D).Checksum;
381 Source.Table (S).All_Checksums_Match := True;
383 -- In check source files mode, try to get time stamp from file
385 if Opt.Check_Source_Files then
386 Stamp := Source_File_Stamp (F);
388 -- If we got the stamp, then set the stamp in the source
389 -- table entry and mark it as set from the source so that
390 -- it does not get subsequently changed.
392 if Stamp (Stamp'First) /= ' ' then
393 Source.Table (S).Stamp := Stamp;
394 Source.Table (S).Source_Found := True;
396 -- If we could not find the file, then the stamp is set
397 -- from the dependency table entry (to be possibly reset
398 -- if we find a later stamp in subsequent processing)
401 Source.Table (S).Stamp := Sdep.Table (D).Stamp;
402 Source.Table (S).Source_Found := False;
404 -- In All_Sources mode, flag error of file not found
406 if Opt.All_Sources then
407 Error_Msg_Name_1 := F;
408 Error_Msg ("cannot locate %");
412 -- First time for this source file, but Check_Source_Files
413 -- is off, so simply initialize the stamp from the Sdep entry
416 Source.Table (S).Source_Found := False;
417 Source.Table (S).Stamp := Sdep.Table (D).Stamp;
420 -- Here if this is not the first time for this source file,
421 -- so that the source table entry is already constructed.
424 S := Source_Id (Get_Name_Table_Info (F));
426 -- Update checksum flag
428 if not Checksums_Match
429 (Sdep.Table (D).Checksum, Source.Table (S).Checksum)
431 Source.Table (S).All_Checksums_Match := False;
434 -- Check for time stamp mismatch
436 if Sdep.Table (D).Stamp /= Source.Table (S).Stamp then
437 Source.Table (S).All_Timestamps_Match := False;
439 -- When we have a time stamp mismatch, we go look for the
440 -- source file even if Check_Source_Files is false, since
441 -- if we find it, then we can use it to resolve which of the
442 -- two timestamps in the ALI files is likely to be correct.
444 if not Check_Source_Files then
445 Stamp := Source_File_Stamp (F);
447 if Stamp (Stamp'First) /= ' ' then
448 Source.Table (S).Stamp := Stamp;
449 Source.Table (S).Source_Found := True;
453 -- If the stamp in the source table entry was set from the
454 -- source file, then we do not change it (the stamp in the
455 -- source file is always taken as the "right" one).
457 if Source.Table (S).Source_Found then
460 -- Otherwise, we have no source file available, so we guess
461 -- that the later of the two timestamps is the right one.
462 -- Note that this guess only affects which error messages
463 -- are issued later on, not correct functionality.
466 if Sdep.Table (D).Stamp > Source.Table (S).Stamp then
467 Source.Table (S).Stamp := Sdep.Table (D).Stamp;
473 -- Set the checksum value in the source table
475 S := Source_Id (Get_Name_Table_Info (F));
476 Source.Table (S).Checksum := Sdep.Table (D).Checksum;
480 end Set_Source_Table;
482 ----------------------
483 -- Set_Source_Table --
484 ----------------------
486 procedure Set_Source_Table is
488 for A in ALIs.First .. ALIs.Last loop
489 Set_Source_Table (A);
492 end Set_Source_Table;
494 -------------------------
495 -- Time_Stamp_Mismatch --
496 -------------------------
498 function Time_Stamp_Mismatch (A : ALI_Id) return File_Name_Type is
500 -- Source file Id for the current Sdep entry
503 for D in ALIs.Table (A).First_Sdep .. ALIs.Table (A).Last_Sdep loop
504 Src := Source_Id (Get_Name_Table_Info (Sdep.Table (D).Sfile));
506 if Opt.Minimal_Recompilation
507 and then Sdep.Table (D).Stamp /= Source.Table (Src).Stamp
510 -- If minimal recompilation is in action, replace the stamp
511 -- of the source file in the table if checksums match.
513 -- ??? It is probably worth updating the ALI file with a new
514 -- field to avoid recomputing it each time.
517 (Get_File_Checksum (Sdep.Table (D).Sfile),
518 Source.Table (Src).Checksum)
520 Sdep.Table (D).Stamp := Source.Table (Src).Stamp;
525 if not Source.Table (Src).Source_Found
526 or else Sdep.Table (D).Stamp /= Source.Table (Src).Stamp
528 return Source.Table (Src).Sfile;
534 end Time_Stamp_Mismatch;