1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, 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 ------------------------------------------------------------------------------
26 with Debug
; use Debug
;
27 with Binderr
; use Binderr
;
29 with Output
; use Output
;
30 with Osint
; use Osint
;
31 with Scans
; use Scans
;
34 with Snames
; use Snames
;
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
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
,
73 Key
=> File_Name_Type
,
81 function Checksums_Match
(Checksum1
, Checksum2
: Word
) return Boolean is
83 return Checksum1
= Checksum2
and then Checksum1
/= Checksum_Error
;
90 procedure Error_Msg
(Msg
: String; Flag_Location
: Source_Ptr
) is
91 pragma Warnings
(Off
, Msg
);
92 pragma Warnings
(Off
, Flag_Location
);
101 procedure Error_Msg_S
(Msg
: String) is
102 pragma Warnings
(Off
, Msg
);
111 procedure Error_Msg_SC
(Msg
: String) is
112 pragma Warnings
(Off
, Msg
);
121 procedure Error_Msg_SP
(Msg
: String) is
122 pragma Warnings
(Off
, Msg
);
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
;
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
;
146 Source_Index
:= Sinput
.C
.Load_File
(Get_Name_String
(Full_Name
));
148 if Source_Index
= No_Source_File
then
149 return Checksum_Error
;
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);
161 Set_Name_Table_Byte
(Name_External_As_List
, 0);
163 -- Scan the complete file to compute its checksum
167 exit when Token
= Tok_EOF
;
170 return Scans
.Checksum
;
171 end Get_File_Checksum
;
177 function Hash
(F
: File_Name_Type
) return Header_Num
is
179 return Header_Num
(Int
(F
) rem Header_Num
'Range_Length);
182 ---------------------------
183 -- Initialize_ALI_Source --
184 ---------------------------
186 procedure Initialize_ALI_Source
is
188 -- When (re)initializing ALI data structures the ALI user expects to
189 -- get a fresh set of data structures. Thus we first need to erase the
190 -- marks put in the name table by the previous set of ALI routine calls.
191 -- This loop is empty and harmless the first time in.
193 for J
in Source
.First
.. Source
.Last
loop
194 Set_Name_Table_Info
(Source
.Table
(J
).Sfile
, 0);
195 Source
.Table
(J
).Source_Found
:= False;
200 end Initialize_ALI_Source
;
206 procedure Post_Scan
is
211 ----------------------
212 -- Read_Withed_ALIs --
213 ----------------------
215 procedure Read_Withed_ALIs
(Id
: ALI_Id
) is
216 Afile
: File_Name_Type
;
217 Text
: Text_Buffer_Ptr
;
221 -- Process all dependent units
223 for U
in ALIs
.Table
(Id
).First_Unit
.. ALIs
.Table
(Id
).Last_Unit
loop
225 W
in Units
.Table
(U
).First_With
.. Units
.Table
(U
).Last_With
227 Afile
:= Withs
.Table
(W
).Afile
;
229 -- Only process if not a generic (Afile /= No_File) and if
230 -- file has not been processed already.
233 and then Get_Name_Table_Info
(Afile
) = 0
235 Text
:= Read_Library_Info
(Afile
);
237 -- Return with an error if source cannot be found. We used to
238 -- skip this check when we did not compile library generics
239 -- separately, but we now always do, so there is no special
240 -- case here anymore.
243 Error_Msg_File_1
:= Afile
;
244 Error_Msg_File_2
:= Withs
.Table
(W
).Sfile
;
245 Error_Msg
("{ not found, { must be compiled");
246 Set_Name_Table_Info
(Afile
, Int
(No_Unit_Id
));
250 -- Enter in ALIs table
261 if ALIs
.Table
(Idread
).Compile_Errors
then
262 Error_Msg_File_1
:= Withs
.Table
(W
).Sfile
;
263 Error_Msg
("{ had errors, must be fixed, and recompiled");
264 Set_Name_Table_Info
(Afile
, Int
(No_Unit_Id
));
266 elsif ALIs
.Table
(Idread
).No_Object
then
267 Error_Msg_File_1
:= Withs
.Table
(W
).Sfile
;
268 Error_Msg
("{ must be recompiled");
269 Set_Name_Table_Info
(Afile
, Int
(No_Unit_Id
));
272 -- If the Unit is an Interface to a Stand-Alone Library,
273 -- set the Interface flag in the Withs table, so that its
274 -- dependant are not considered for elaboration order.
276 if ALIs
.Table
(Idread
).SAL_Interface
then
277 Withs
.Table
(W
).SAL_Interface
:= True;
278 Interface_Library_Unit
:= True;
280 -- Set the entry in the Interfaces hash table, so that other
281 -- units that import this unit will set the flag in their
282 -- entry in the Withs table.
284 Interfaces
.Set
(Afile
, True);
287 -- Otherwise, recurse to get new dependents
289 Read_Withed_ALIs
(Idread
);
292 -- If the ALI file has already been processed and is an interface,
293 -- set the flag in the entry of the Withs table.
295 elsif Interface_Library_Unit
and then Interfaces
.Get
(Afile
) then
296 Withs
.Table
(W
).SAL_Interface
:= True;
300 end Read_Withed_ALIs
;
302 ----------------------
303 -- Set_Source_Table --
304 ----------------------
306 procedure Set_Source_Table
(A
: ALI_Id
) is
309 Stamp
: Time_Stamp_Type
;
313 ALIs
.Table
(A
).First_Sdep
.. ALIs
.Table
(A
).Last_Sdep
315 F
:= Sdep
.Table
(D
).Sfile
;
319 -- If this is the first time we are seeing this source file,
320 -- then make a new entry in the source table.
322 if Get_Name_Table_Info
(F
) = 0 then
323 Source
.Increment_Last
;
325 Set_Name_Table_Info
(F
, Int
(S
));
326 Source
.Table
(S
).Sfile
:= F
;
327 Source
.Table
(S
).All_Timestamps_Match
:= True;
329 -- Initialize checksum fields
331 Source
.Table
(S
).Checksum
:= Sdep
.Table
(D
).Checksum
;
332 Source
.Table
(S
).All_Checksums_Match
:= True;
334 -- In check source files mode, try to get time stamp from file
336 if Opt
.Check_Source_Files
then
337 Stamp
:= Source_File_Stamp
(F
);
339 -- If we got the stamp, then set the stamp in the source
340 -- table entry and mark it as set from the source so that
341 -- it does not get subsequently changed.
343 if Stamp
(Stamp
'First) /= ' ' then
344 Source
.Table
(S
).Stamp
:= Stamp
;
345 Source
.Table
(S
).Source_Found
:= True;
347 -- If we could not find the file, then the stamp is set
348 -- from the dependency table entry (to be possibly reset
349 -- if we find a later stamp in subsequent processing)
352 Source
.Table
(S
).Stamp
:= Sdep
.Table
(D
).Stamp
;
353 Source
.Table
(S
).Source_Found
:= False;
355 -- In All_Sources mode, flag error of file not found
357 if Opt
.All_Sources
then
358 Error_Msg_File_1
:= F
;
359 Error_Msg
("cannot locate {");
363 -- First time for this source file, but Check_Source_Files
364 -- is off, so simply initialize the stamp from the Sdep entry
367 Source
.Table
(S
).Source_Found
:= False;
368 Source
.Table
(S
).Stamp
:= Sdep
.Table
(D
).Stamp
;
371 -- Here if this is not the first time for this source file,
372 -- so that the source table entry is already constructed.
375 S
:= Source_Id
(Get_Name_Table_Info
(F
));
377 -- Update checksum flag
379 if not Checksums_Match
380 (Sdep
.Table
(D
).Checksum
, Source
.Table
(S
).Checksum
)
382 Source
.Table
(S
).All_Checksums_Match
:= False;
385 -- Check for time stamp mismatch
387 if Sdep
.Table
(D
).Stamp
/= Source
.Table
(S
).Stamp
then
388 Source
.Table
(S
).All_Timestamps_Match
:= False;
390 -- When we have a time stamp mismatch, we go look for the
391 -- source file even if Check_Source_Files is false, since
392 -- if we find it, then we can use it to resolve which of the
393 -- two timestamps in the ALI files is likely to be correct.
395 if not Check_Source_Files
then
396 Stamp
:= Source_File_Stamp
(F
);
398 if Stamp
(Stamp
'First) /= ' ' then
399 Source
.Table
(S
).Stamp
:= Stamp
;
400 Source
.Table
(S
).Source_Found
:= True;
404 -- If the stamp in the source table entry was set from the
405 -- source file, then we do not change it (the stamp in the
406 -- source file is always taken as the "right" one).
408 if Source
.Table
(S
).Source_Found
then
411 -- Otherwise, we have no source file available, so we guess
412 -- that the later of the two timestamps is the right one.
413 -- Note that this guess only affects which error messages
414 -- are issued later on, not correct functionality.
417 if Sdep
.Table
(D
).Stamp
> Source
.Table
(S
).Stamp
then
418 Source
.Table
(S
).Stamp
:= Sdep
.Table
(D
).Stamp
;
424 -- Set the checksum value in the source table
426 S
:= Source_Id
(Get_Name_Table_Info
(F
));
427 Source
.Table
(S
).Checksum
:= Sdep
.Table
(D
).Checksum
;
431 end Set_Source_Table
;
433 ----------------------
434 -- Set_Source_Table --
435 ----------------------
437 procedure Set_Source_Table
is
439 for A
in ALIs
.First
.. ALIs
.Last
loop
440 Set_Source_Table
(A
);
442 end Set_Source_Table
;
444 -------------------------
445 -- Time_Stamp_Mismatch --
446 -------------------------
448 function Time_Stamp_Mismatch
450 Read_Only
: Boolean := False) return File_Name_Type
453 -- Source file Id for the current Sdep entry
456 for D
in ALIs
.Table
(A
).First_Sdep
.. ALIs
.Table
(A
).Last_Sdep
loop
457 Src
:= Source_Id
(Get_Name_Table_Info
(Sdep
.Table
(D
).Sfile
));
459 if Opt
.Minimal_Recompilation
460 and then Sdep
.Table
(D
).Stamp
/= Source
.Table
(Src
).Stamp
462 -- If minimal recompilation is in action, replace the stamp
463 -- of the source file in the table if checksums match.
465 -- ??? It is probably worth updating the ALI file with a new
466 -- field to avoid recomputing it each time.
469 (Get_File_Checksum
(Sdep
.Table
(D
).Sfile
),
470 Source
.Table
(Src
).Checksum
)
474 Write_Str
(Get_Name_String
(Sdep
.Table
(D
).Sfile
));
475 Write_Str
(": up to date, different timestamps " &
476 "but same checksum");
480 Sdep
.Table
(D
).Stamp
:= Source
.Table
(Src
).Stamp
;
485 if (not Read_Only
) or else Source
.Table
(Src
).Source_Found
then
486 if not Source
.Table
(Src
).Source_Found
487 or else Sdep
.Table
(D
).Stamp
/= Source
.Table
(Src
).Stamp
489 -- If -dt debug flag set, output time stamp found/expected
491 if Source
.Table
(Src
).Source_Found
and then Debug_Flag_T
then
492 Write_Str
("Source: """);
493 Get_Name_String
(Sdep
.Table
(D
).Sfile
);
494 Write_Str
(Name_Buffer
(1 .. Name_Len
));
497 Write_Str
(" time stamp expected: ");
498 Write_Line
(String (Sdep
.Table
(D
).Stamp
));
500 Write_Str
(" time stamp found: ");
501 Write_Line
(String (Source
.Table
(Src
).Stamp
));
504 -- Return the source file
506 return Source
.Table
(Src
).Sfile
;
512 end Time_Stamp_Mismatch
;