1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2000 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;
34 package body ALI.Util is
36 -----------------------
37 -- Local Subprograms --
38 -----------------------
40 procedure Accumulate_Checksum (C : Character; Csum : in out Word);
41 pragma Inline (Accumulate_Checksum);
42 -- This routine accumulates the checksum given character C. During the
43 -- scanning of a source file, this routine is called with every character
44 -- in the source, excluding blanks, and all control characters (except
45 -- that ESC is included in the checksum). Upper case letters not in string
46 -- literals are folded by the caller. See Sinput spec for the documentation
47 -- of the checksum algorithm. Note: checksum values are only used if we
48 -- generate code, so it is not necessary to worry about making the right
49 -- sequence of calls in any error situation.
51 -------------------------
52 -- Accumulate_Checksum --
53 -------------------------
55 procedure Accumulate_Checksum (C : Character; Csum : in out Word) is
57 Csum := Csum + Csum + Character'Pos (C);
59 if Csum > 16#8000_0000# then
60 Csum := (Csum + 1) and 16#7FFF_FFFF#;
62 end Accumulate_Checksum;
64 -----------------------
65 -- Get_File_Checksum --
66 -----------------------
68 function Get_File_Checksum (Fname : Name_Id) return Word is
69 Src : Source_Buffer_Ptr;
75 -- Raised if file not found, or file format error
78 -- Make control characters visible
80 procedure Free_Source;
81 -- Free source file buffer
83 procedure Free_Source is
84 procedure free (Arg : Source_Buffer_Ptr);
85 pragma Import (C, free, "free");
91 -- Start of processing for Get_File_Checksum
94 Read_Source_File (Fname, 0, Hi, Src);
96 -- If we cannot find the file, then return an impossible checksum,
97 -- impossible becaues checksums have the high order bit zero, so
98 -- that checksums do not match.
110 -- Spaces and formatting information are ignored in checksum
112 when ' ' | CR | LF | VT | FF | HT =>
115 -- EOF is ignored unless it is the last character
125 -- Non-blank characters that are included in the checksum
127 when '#' | '&' | '*' | ':' | '(' | ',' | '.' | '=' | '>' |
128 '<' | ')' | '/' | ';' | '|' | '!' | '+' | '_' |
129 '0' .. '9' | 'a' .. 'z'
131 Accumulate_Checksum (Src (Ptr), Csum);
134 -- Upper case letters, fold to lower case
138 (Character'Val (Character'Pos (Src (Ptr)) + 32), Csum);
141 -- Left bracket, really should do wide character thing here,
142 -- but for now, don't bother.
147 -- Minus, could be comment
150 if Src (Ptr + 1) = '-' then
153 while Src (Ptr) >= ' ' or else Src (Ptr) = HT loop
158 Accumulate_Checksum ('-', Csum);
162 -- String delimited by double quote
165 Accumulate_Checksum ('"', Csum);
169 exit when Src (Ptr) = '"';
171 if Src (Ptr) < ' ' then
175 Accumulate_Checksum (Src (Ptr), Csum);
178 Accumulate_Checksum ('"', Csum);
181 -- String delimited by percent
184 Accumulate_Checksum ('%', Csum);
188 exit when Src (Ptr) = '%';
190 if Src (Ptr) < ' ' then
194 Accumulate_Checksum (Src (Ptr), Csum);
197 Accumulate_Checksum ('%', Csum);
200 -- Quote, could be character constant
203 Accumulate_Checksum (''', Csum);
205 if Src (Ptr + 2) = ''' then
206 Accumulate_Checksum (Src (Ptr + 1), Csum);
207 Accumulate_Checksum (''', Csum);
210 -- Otherwise assume attribute char. We should deal with wide
211 -- character cases here, but that's hard, so forget it.
217 -- Upper half character, more to be done here, we should worry
218 -- about folding Latin-1, folding other character sets, and
219 -- dealing with the nasty case of upper half wide encoding.
221 when Upper_Half_Character =>
222 Accumulate_Checksum (Src (Ptr), Csum);
225 -- Escape character, we should do the wide character thing here,
226 -- but for now, do not bother.
231 -- Invalid control characters
233 when NUL | SOH | STX | ETX | EOT | ENQ | ACK | BEL | BS | SO |
234 SI | DLE | DC1 | DC2 | DC3 | DC4 | NAK | SYN | ETB | CAN |
235 EM | FS | GS | RS | US | DEL
239 -- Invalid graphic characters
241 when '$' | '?' | '@' | '`' | '\' |
242 '^' | '~' | ']' | '{' | '}'
252 return 16#FFFF_FFFF#;
254 end Get_File_Checksum;
256 ---------------------------
257 -- Initialize_ALI_Source --
258 ---------------------------
260 procedure Initialize_ALI_Source is
262 -- When (re)initializing ALI data structures the ALI user expects to
263 -- get a fresh set of data structures. Thus we first need to erase the
264 -- marks put in the name table by the previous set of ALI routine calls.
265 -- This loop is empty and harmless the first time in.
267 for J in Source.First .. Source.Last loop
268 Set_Name_Table_Info (Source.Table (J).Sfile, 0);
269 Source.Table (J).Source_Found := False;
273 end Initialize_ALI_Source;
279 procedure Read_ALI (Id : ALI_Id) is
280 Afile : File_Name_Type;
281 Text : Text_Buffer_Ptr;
285 for I in ALIs.Table (Id).First_Unit .. ALIs.Table (Id).Last_Unit loop
286 for J in Units.Table (I).First_With .. Units.Table (I).Last_With loop
288 Afile := Withs.Table (J).Afile;
290 -- Only process if not a generic (Afile /= No_File) and if
291 -- file has not been processed already.
293 if Afile /= No_File and then Get_Name_Table_Info (Afile) = 0 then
295 Text := Read_Library_Info (Afile);
298 Error_Msg_Name_1 := Afile;
299 Error_Msg_Name_2 := Withs.Table (J).Sfile;
300 Error_Msg ("% not found, % must be compiled");
301 Set_Name_Table_Info (Afile, Int (No_Unit_Id));
309 Ignore_ED => Force_RM_Elaboration_Order,
314 if ALIs.Table (Idread).Compile_Errors then
315 Error_Msg_Name_1 := Withs.Table (J).Sfile;
316 Error_Msg ("% had errors, must be fixed, and recompiled");
317 Set_Name_Table_Info (Afile, Int (No_Unit_Id));
319 elsif ALIs.Table (Idread).No_Object then
320 Error_Msg_Name_1 := Withs.Table (J).Sfile;
321 Error_Msg ("% must be recompiled");
322 Set_Name_Table_Info (Afile, Int (No_Unit_Id));
325 -- Recurse to get new dependents
334 ----------------------
335 -- Set_Source_Table --
336 ----------------------
338 procedure Set_Source_Table (A : ALI_Id) is
341 Stamp : Time_Stamp_Type;
345 ALIs.Table (A).First_Sdep .. ALIs.Table (A).Last_Sdep
347 F := Sdep.Table (D).Sfile;
349 -- If this is the first time we are seeing this source file,
350 -- then make a new entry in the source table.
352 if Get_Name_Table_Info (F) = 0 then
353 Source.Increment_Last;
355 Set_Name_Table_Info (F, Int (S));
356 Source.Table (S).Sfile := F;
357 Source.Table (S).All_Timestamps_Match := True;
359 -- Initialize checksum fields
361 Source.Table (S).Checksum := Sdep.Table (D).Checksum;
362 Source.Table (S).All_Checksums_Match := True;
364 -- In check source files mode, try to get time stamp from file
366 if Opt.Check_Source_Files then
367 Stamp := Source_File_Stamp (F);
369 -- If we got the stamp, then set the stamp in the source
370 -- table entry and mark it as set from the source so that
371 -- it does not get subsequently changed.
373 if Stamp (Stamp'First) /= ' ' then
374 Source.Table (S).Stamp := Stamp;
375 Source.Table (S).Source_Found := True;
377 -- If we could not find the file, then the stamp is set
378 -- from the dependency table entry (to be possibly reset
379 -- if we find a later stamp in subsequent processing)
382 Source.Table (S).Stamp := Sdep.Table (D).Stamp;
383 Source.Table (S).Source_Found := False;
385 -- In All_Sources mode, flag error of file not found
387 if Opt.All_Sources then
388 Error_Msg_Name_1 := F;
389 Error_Msg ("cannot locate %");
393 -- First time for this source file, but Check_Source_Files
394 -- is off, so simply initialize the stamp from the Sdep entry
397 Source.Table (S).Source_Found := False;
398 Source.Table (S).Stamp := Sdep.Table (D).Stamp;
401 -- Here if this is not the first time for this source file,
402 -- so that the source table entry is already constructed.
405 S := Source_Id (Get_Name_Table_Info (F));
407 -- Update checksum flag
409 if Sdep.Table (D).Checksum /= Source.Table (S).Checksum then
410 Source.Table (S).All_Checksums_Match := False;
413 -- Check for time stamp mismatch
415 if Sdep.Table (D).Stamp /= Source.Table (S).Stamp then
416 Source.Table (S).All_Timestamps_Match := False;
418 -- When we have a time stamp mismatch, we go look for the
419 -- source file even if Check_Source_Files is false, since
420 -- if we find it, then we can use it to resolve which of the
421 -- two timestamps in the ALI files is likely to be correct.
423 if not Check_Source_Files then
424 Stamp := Source_File_Stamp (F);
426 if Stamp (Stamp'First) /= ' ' then
427 Source.Table (S).Stamp := Stamp;
428 Source.Table (S).Source_Found := True;
432 -- If the stamp in the source table entry was set from the
433 -- source file, then we do not change it (the stamp in the
434 -- source file is always taken as the "right" one).
436 if Source.Table (S).Source_Found then
439 -- Otherwise, we have no source file available, so we guess
440 -- that the later of the two timestamps is the right one.
441 -- Note that this guess only affects which error messages
442 -- are issued later on, not correct functionality.
445 if Sdep.Table (D).Stamp > Source.Table (S).Stamp then
446 Source.Table (S).Stamp := Sdep.Table (D).Stamp;
452 -- Set the checksum value in the source table
454 S := Source_Id (Get_Name_Table_Info (F));
455 Source.Table (S).Checksum := Sdep.Table (D).Checksum;
459 end Set_Source_Table;
461 ----------------------
462 -- Set_Source_Table --
463 ----------------------
465 procedure Set_Source_Table is
467 for A in ALIs.First .. ALIs.Last loop
468 Set_Source_Table (A);
471 end Set_Source_Table;
473 -------------------------
474 -- Time_Stamp_Mismatch --
475 -------------------------
477 function Time_Stamp_Mismatch (A : ALI_Id) return File_Name_Type is
479 -- Source file Id for the current Sdep entry
482 for D in ALIs.Table (A).First_Sdep .. ALIs.Table (A).Last_Sdep loop
483 Src := Source_Id (Get_Name_Table_Info (Sdep.Table (D).Sfile));
485 if Opt.Minimal_Recompilation
486 and then Sdep.Table (D).Stamp /= Source.Table (Src).Stamp
489 -- If minimal recompilation is in action, replace the stamp
490 -- of the source file in the table if checksums match.
492 -- ??? It is probably worth updating the ALI file with a new
493 -- field to avoid recomputing it each time.
495 if Get_File_Checksum (Sdep.Table (D).Sfile) =
496 Source.Table (Src).Checksum
498 Sdep.Table (D).Stamp := Source.Table (Src).Stamp;
503 if not Source.Table (Src).Source_Found
504 or else Sdep.Table (D).Stamp /= Source.Table (Src).Stamp
506 return Source.Table (Src).Sfile;
512 end Time_Stamp_Mismatch;