| 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-2025, 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 |
------------------------------------------------------------------------------ |
| 25 |
|
| 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 Fname; use Fname; |
| 33 |
with Scng; |
| 34 |
with Sinput.C; |
| 35 |
with Stringt; |
| 36 |
with Styleg; |
| 37 |
|
| 38 |
with System.OS_Lib; use System.OS_Lib; |
| 39 |
|
| 40 |
package body ALI.Util is |
| 41 |
|
| 42 |
-- Empty procedures needed to instantiate Scng. Error procedures are |
| 43 |
-- empty, because we don't want to report any errors when computing |
| 44 |
-- a source checksum. |
| 45 |
|
| 46 |
procedure Post_Scan is null; |
| 47 |
|
| 48 |
procedure Error_Msg (Msg : String; Flag_Location : Source_Ptr) is null; |
| 49 |
procedure Error_Msg_S (Msg : String) is null; |
| 50 |
procedure Error_Msg_SC (Msg : String) is null; |
| 51 |
procedure Error_Msg_SP (Msg : String) is null; |
| 52 |
|
| 53 |
-- Instantiation of Styleg, needed to instantiate Scng |
| 54 |
|
| 55 |
package Style is new Styleg |
| 56 |
(Error_Msg, Error_Msg_S, Error_Msg_SC, Error_Msg_SP); |
| 57 |
|
| 58 |
-- A Scanner is needed to get checksum of a source (procedure |
| 59 |
-- Get_File_Checksum). |
| 60 |
|
| 61 |
package Scanner is new Scng |
| 62 |
(Post_Scan, Error_Msg, Error_Msg_S, Error_Msg_SC, Error_Msg_SP, Style); |
| 63 |
|
| 64 |
type Header_Num is range 0 .. 1_000; |
| 65 |
|
| 66 |
function Hash (F : File_Name_Type) return Header_Num; |
| 67 |
-- Function used to compute hash of ALI file name |
| 68 |
|
| 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 => "="); |
| 76 |
|
| 77 |
--------------------- |
| 78 |
-- Checksums_Match -- |
| 79 |
--------------------- |
| 80 |
|
| 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; |
| 85 |
|
| 86 |
----------------------- |
| 87 |
-- Get_File_Checksum -- |
| 88 |
----------------------- |
| 89 |
|
| 90 |
function Get_File_Checksum (Fname : File_Name_Type) return Word is |
| 91 |
Full_Name : File_Name_Type; |
| 92 |
Source_Index : Source_File_Index; |
| 93 |
Ada_Version_Current : Ada_Version_Type; |
| 94 |
Internal_Unit : constant Boolean := Is_Internal_File_Name (Fname); |
| 95 |
|
| 96 |
begin |
| 97 |
Full_Name := Find_File (Fname, Osint.Source); |
| 98 |
|
| 99 |
-- If we cannot find the file, then return an impossible checksum, |
| 100 |
-- impossible because checksums have the high order bit zero, so |
| 101 |
-- that checksums do not match. |
| 102 |
|
| 103 |
if Full_Name = No_File then |
| 104 |
return Checksum_Error; |
| 105 |
end if; |
| 106 |
|
| 107 |
Source_Index := Sinput.C.Load_File (Get_Name_String (Full_Name)); |
| 108 |
|
| 109 |
if Source_Index <= No_Source_File then |
| 110 |
return Checksum_Error; |
| 111 |
end if; |
| 112 |
|
| 113 |
Scanner.Initialize_Scanner (Source_Index); |
| 114 |
|
| 115 |
-- The runtime files are precompiled with an implicitly defined Ada |
| 116 |
-- version that we set here to improve the parsing required to compute |
| 117 |
-- the checksum. |
| 118 |
|
| 119 |
if Internal_Unit then |
| 120 |
Ada_Version_Current := Ada_Version; |
| 121 |
Ada_Version := Ada_Version_Runtime; |
| 122 |
end if; |
| 123 |
|
| 124 |
-- Scan the complete file to compute its checksum |
| 125 |
|
| 126 |
loop |
| 127 |
Scanner.Scan; |
| 128 |
exit when Token = Tok_EOF; |
| 129 |
end loop; |
| 130 |
|
| 131 |
-- Restore the Ada version if we changed it |
| 132 |
|
| 133 |
if Internal_Unit then |
| 134 |
Ada_Version := Ada_Version_Current; |
| 135 |
end if; |
| 136 |
|
| 137 |
return Scans.Checksum; |
| 138 |
end Get_File_Checksum; |
| 139 |
|
| 140 |
---------- |
| 141 |
-- Hash -- |
| 142 |
---------- |
| 143 |
|
| 144 |
function Hash (F : File_Name_Type) return Header_Num is |
| 145 |
begin |
| 146 |
return Header_Num (Int (F) mod Header_Num'Range_Length); |
| 147 |
end Hash; |
| 148 |
|
| 149 |
--------------------------- |
| 150 |
-- Initialize_ALI_Source -- |
| 151 |
--------------------------- |
| 152 |
|
| 153 |
procedure Initialize_ALI_Source is |
| 154 |
begin |
| 155 |
-- When (re)initializing ALI data structures the ALI user expects to |
| 156 |
-- get a fresh set of data structures. Thus we first need to erase the |
| 157 |
-- marks put in the name table by the previous set of ALI routine calls. |
| 158 |
-- This loop is empty and harmless the first time in. |
| 159 |
|
| 160 |
for J in Source.First .. Source.Last loop |
| 161 |
Set_Name_Table_Int (Source.Table (J).Sfile, 0); |
| 162 |
Source.Table (J).Source_Found := False; |
| 163 |
end loop; |
| 164 |
|
| 165 |
Source.Init; |
| 166 |
Interfaces.Reset; |
| 167 |
end Initialize_ALI_Source; |
| 168 |
|
| 169 |
---------------------- |
| 170 |
-- Read_Withed_ALIs -- |
| 171 |
---------------------- |
| 172 |
|
| 173 |
procedure Read_Withed_ALIs (Id : ALI_Id) is |
| 174 |
Afile : File_Name_Type; |
| 175 |
Text : Text_Buffer_Ptr; |
| 176 |
Idread : ALI_Id; |
| 177 |
|
| 178 |
begin |
| 179 |
-- Process all dependent units |
| 180 |
|
| 181 |
for U in ALIs.Table (Id).First_Unit .. ALIs.Table (Id).Last_Unit loop |
| 182 |
for W in Units.Table (U).First_With .. Units.Table (U).Last_With loop |
| 183 |
Afile := Withs.Table (W).Afile; |
| 184 |
|
| 185 |
-- Only process if not a generic (Afile /= No_File) and if |
| 186 |
-- file has not been processed already. |
| 187 |
|
| 188 |
if Afile /= No_File |
| 189 |
and then Get_Name_Table_Int (Afile) = 0 |
| 190 |
then |
| 191 |
Text := Read_Library_Info (Afile); |
| 192 |
|
| 193 |
-- Unless in GNATprove mode, return with an error if source |
| 194 |
-- cannot be found. We used to skip this check when we did not |
| 195 |
-- compile library generics separately, but we now always do, |
| 196 |
-- so there is no special case here anymore. |
| 197 |
|
| 198 |
if Text = null then |
| 199 |
|
| 200 |
if not GNATprove_Mode then |
| 201 |
Error_Msg_File_1 := Afile; |
| 202 |
Error_Msg_File_2 := Withs.Table (W).Sfile; |
| 203 |
Error_Msg ("{ not found, { must be compiled"); |
| 204 |
Set_Name_Table_Int (Afile, Int (No_Unit_Id)); |
| 205 |
return; |
| 206 |
end if; |
| 207 |
|
| 208 |
else |
| 209 |
-- Enter in ALIs table |
| 210 |
|
| 211 |
Idread := |
| 212 |
Scan_ALI |
| 213 |
(F => Afile, |
| 214 |
T => Text, |
| 215 |
Err => False); |
| 216 |
|
| 217 |
Free (Text); |
| 218 |
|
| 219 |
if ALIs.Table (Idread).Compile_Errors |
| 220 |
and then not GNATprove_Mode |
| 221 |
then |
| 222 |
Error_Msg_File_1 := Withs.Table (W).Sfile; |
| 223 |
Error_Msg ("{ had errors, must be fixed, and recompiled"); |
| 224 |
Set_Name_Table_Int (Afile, Int (No_Unit_Id)); |
| 225 |
|
| 226 |
-- In GNATprove mode, object files are never generated, so |
| 227 |
-- No_Object=True is not considered an error. |
| 228 |
|
| 229 |
elsif ALIs.Table (Idread).No_Object |
| 230 |
and then not GNATprove_Mode |
| 231 |
then |
| 232 |
Error_Msg_File_1 := Withs.Table (W).Sfile; |
| 233 |
Error_Msg ("{ must be recompiled"); |
| 234 |
Set_Name_Table_Int (Afile, Int (No_Unit_Id)); |
| 235 |
end if; |
| 236 |
|
| 237 |
-- If the Unit is an Interface to a Stand-Alone Library, |
| 238 |
-- set the Interface flag in the Withs table, so that its |
| 239 |
-- dependant are not considered for elaboration order. |
| 240 |
|
| 241 |
if ALIs.Table (Idread).SAL_Interface then |
| 242 |
Withs.Table (W).SAL_Interface := True; |
| 243 |
Interface_Library_Unit := True; |
| 244 |
|
| 245 |
-- Set the entry in the Interfaces hash table, so that |
| 246 |
-- other units that import this unit will set the flag |
| 247 |
-- in their entry in the Withs table. |
| 248 |
|
| 249 |
Interfaces.Set (Afile, True); |
| 250 |
|
| 251 |
else |
| 252 |
-- Otherwise, recurse to get new dependents |
| 253 |
|
| 254 |
Read_Withed_ALIs (Idread); |
| 255 |
end if; |
| 256 |
end if; |
| 257 |
|
| 258 |
-- If the ALI file has already been processed and is an interface, |
| 259 |
-- set the flag in the entry of the Withs table. |
| 260 |
|
| 261 |
elsif Interface_Library_Unit and then Interfaces.Get (Afile) then |
| 262 |
Withs.Table (W).SAL_Interface := True; |
| 263 |
end if; |
| 264 |
end loop; |
| 265 |
end loop; |
| 266 |
end Read_Withed_ALIs; |
| 267 |
|
| 268 |
---------------------- |
| 269 |
-- Set_Source_Table -- |
| 270 |
---------------------- |
| 271 |
|
| 272 |
procedure Set_Source_Table (A : ALI_Id) is |
| 273 |
F : File_Name_Type; |
| 274 |
S : Source_Id; |
| 275 |
Stamp : Time_Stamp_Type; |
| 276 |
|
| 277 |
begin |
| 278 |
Sdep_Loop : for D in |
| 279 |
ALIs.Table (A).First_Sdep .. ALIs.Table (A).Last_Sdep |
| 280 |
loop |
| 281 |
F := Sdep.Table (D).Sfile; |
| 282 |
|
| 283 |
if F /= No_File then |
| 284 |
|
| 285 |
-- If this is the first time we are seeing this source file, |
| 286 |
-- then make a new entry in the source table. |
| 287 |
|
| 288 |
if Get_Name_Table_Int (F) = 0 then |
| 289 |
Source.Increment_Last; |
| 290 |
S := Source.Last; |
| 291 |
Set_Name_Table_Int (F, Int (S)); |
| 292 |
Source.Table (S).Sfile := F; |
| 293 |
Source.Table (S).All_Timestamps_Match := True; |
| 294 |
|
| 295 |
-- Initialize checksum fields |
| 296 |
|
| 297 |
Source.Table (S).Checksum := Sdep.Table (D).Checksum; |
| 298 |
Source.Table (S).All_Checksums_Match := True; |
| 299 |
|
| 300 |
-- In check source files mode, try to get time stamp from file |
| 301 |
|
| 302 |
if Opt.Check_Source_Files then |
| 303 |
Stamp := Source_File_Stamp (F); |
| 304 |
|
| 305 |
-- If we got the stamp, then set the stamp in the source |
| 306 |
-- table entry and mark it as set from the source so that |
| 307 |
-- it does not get subsequently changed. |
| 308 |
|
| 309 |
if Stamp (Stamp'First) /= ' ' then |
| 310 |
Source.Table (S).Stamp := Stamp; |
| 311 |
Source.Table (S).Source_Found := True; |
| 312 |
Source.Table (S).Stamp_File := F; |
| 313 |
|
| 314 |
-- If we could not find the file, then the stamp is set |
| 315 |
-- from the dependency table entry (to be possibly reset |
| 316 |
-- if we find a later stamp in subsequent processing) |
| 317 |
|
| 318 |
else |
| 319 |
Source.Table (S).Stamp := Sdep.Table (D).Stamp; |
| 320 |
Source.Table (S).Source_Found := False; |
| 321 |
Source.Table (S).Stamp_File := ALIs.Table (A).Afile; |
| 322 |
|
| 323 |
-- In All_Sources mode, flag error of file not found |
| 324 |
|
| 325 |
if Opt.All_Sources then |
| 326 |
Error_Msg_File_1 := F; |
| 327 |
Error_Msg ("cannot locate {"); |
| 328 |
end if; |
| 329 |
end if; |
| 330 |
|
| 331 |
-- First time for this source file, but Check_Source_Files |
| 332 |
-- is off, so simply initialize the stamp from the Sdep entry |
| 333 |
|
| 334 |
else |
| 335 |
Source.Table (S).Stamp := Sdep.Table (D).Stamp; |
| 336 |
Source.Table (S).Source_Found := False; |
| 337 |
Source.Table (S).Stamp_File := ALIs.Table (A).Afile; |
| 338 |
end if; |
| 339 |
|
| 340 |
-- Here if this is not the first time for this source file, |
| 341 |
-- so that the source table entry is already constructed. |
| 342 |
|
| 343 |
else |
| 344 |
S := Source_Id (Get_Name_Table_Int (F)); |
| 345 |
|
| 346 |
-- Update checksum flag |
| 347 |
|
| 348 |
if not Checksums_Match |
| 349 |
(Sdep.Table (D).Checksum, Source.Table (S).Checksum) |
| 350 |
then |
| 351 |
Source.Table (S).All_Checksums_Match := False; |
| 352 |
end if; |
| 353 |
|
| 354 |
-- Check for time stamp mismatch |
| 355 |
|
| 356 |
if Sdep.Table (D).Stamp /= Source.Table (S).Stamp then |
| 357 |
Source.Table (S).All_Timestamps_Match := False; |
| 358 |
|
| 359 |
-- When we have a time stamp mismatch, we go look for the |
| 360 |
-- source file even if Check_Source_Files is false, since |
| 361 |
-- if we find it, then we can use it to resolve which of the |
| 362 |
-- two timestamps in the ALI files is likely to be correct. |
| 363 |
-- We only look in the current directory, because when |
| 364 |
-- Check_Source_Files is false, other search directories are |
| 365 |
-- likely to be incorrect. |
| 366 |
|
| 367 |
if not Check_Source_Files |
| 368 |
and then Is_Regular_File (Get_Name_String (F)) |
| 369 |
then |
| 370 |
Stamp := Source_File_Stamp (F); |
| 371 |
|
| 372 |
if Stamp (Stamp'First) /= ' ' then |
| 373 |
Source.Table (S).Stamp := Stamp; |
| 374 |
Source.Table (S).Source_Found := True; |
| 375 |
Source.Table (S).Stamp_File := F; |
| 376 |
end if; |
| 377 |
end if; |
| 378 |
|
| 379 |
-- If the stamp in the source table entry was set from the |
| 380 |
-- source file, then we do not change it (the stamp in the |
| 381 |
-- source file is always taken as the "right" one). |
| 382 |
|
| 383 |
if Source.Table (S).Source_Found then |
| 384 |
null; |
| 385 |
|
| 386 |
-- Otherwise, we have no source file available, so we guess |
| 387 |
-- that the later of the two timestamps is the right one. |
| 388 |
-- Note that this guess only affects which error messages |
| 389 |
-- are issued later on, not correct functionality. |
| 390 |
|
| 391 |
else |
| 392 |
if Sdep.Table (D).Stamp > Source.Table (S).Stamp then |
| 393 |
Source.Table (S).Stamp := Sdep.Table (D).Stamp; |
| 394 |
Source.Table (S).Stamp_File := ALIs.Table (A).Afile; |
| 395 |
end if; |
| 396 |
end if; |
| 397 |
end if; |
| 398 |
end if; |
| 399 |
|
| 400 |
-- Set the checksum value in the source table |
| 401 |
|
| 402 |
S := Source_Id (Get_Name_Table_Int (F)); |
| 403 |
Source.Table (S).Checksum := Sdep.Table (D).Checksum; |
| 404 |
end if; |
| 405 |
|
| 406 |
end loop Sdep_Loop; |
| 407 |
end Set_Source_Table; |
| 408 |
|
| 409 |
---------------------- |
| 410 |
-- Set_Source_Table -- |
| 411 |
---------------------- |
| 412 |
|
| 413 |
procedure Set_Source_Table is |
| 414 |
begin |
| 415 |
for A in ALIs.First .. ALIs.Last loop |
| 416 |
Set_Source_Table (A); |
| 417 |
end loop; |
| 418 |
end Set_Source_Table; |
| 419 |
|
| 420 |
------------------------- |
| 421 |
-- Time_Stamp_Mismatch -- |
| 422 |
------------------------- |
| 423 |
|
| 424 |
function Time_Stamp_Mismatch |
| 425 |
(A : ALI_Id; |
| 426 |
Read_Only : Boolean := False) return File_Name_Type |
| 427 |
is |
| 428 |
Src : Source_Id; |
| 429 |
-- Source file Id for the current Sdep entry |
| 430 |
|
| 431 |
begin |
| 432 |
for D in ALIs.Table (A).First_Sdep .. ALIs.Table (A).Last_Sdep loop |
| 433 |
Src := Source_Id (Get_Name_Table_Int (Sdep.Table (D).Sfile)); |
| 434 |
|
| 435 |
if Opt.Minimal_Recompilation |
| 436 |
and then Sdep.Table (D).Stamp /= Source.Table (Src).Stamp |
| 437 |
then |
| 438 |
-- If minimal recompilation is in action, replace the stamp |
| 439 |
-- of the source file in the table if checksums match. |
| 440 |
|
| 441 |
-- ??? It is probably worth updating the ALI file with a new |
| 442 |
-- field to avoid recomputing it each time. In any case we ensure |
| 443 |
-- that we don't gobble up string table space by doing a mark |
| 444 |
-- release around this computation. |
| 445 |
|
| 446 |
Stringt.Mark; |
| 447 |
|
| 448 |
if Checksums_Match |
| 449 |
(Get_File_Checksum (Sdep.Table (D).Sfile), |
| 450 |
Source.Table (Src).Checksum) |
| 451 |
then |
| 452 |
if Verbose_Mode then |
| 453 |
Write_Str (" "); |
| 454 |
Write_Str (Get_Name_String (Sdep.Table (D).Sfile)); |
| 455 |
Write_Str (": up to date, different timestamps " & |
| 456 |
"but same checksum"); |
| 457 |
Write_Eol; |
| 458 |
end if; |
| 459 |
|
| 460 |
Sdep.Table (D).Stamp := Source.Table (Src).Stamp; |
| 461 |
end if; |
| 462 |
|
| 463 |
Stringt.Release; |
| 464 |
end if; |
| 465 |
|
| 466 |
if not Read_Only or else Source.Table (Src).Source_Found then |
| 467 |
if not Source.Table (Src).Source_Found |
| 468 |
or else Sdep.Table (D).Stamp /= Source.Table (Src).Stamp |
| 469 |
then |
| 470 |
-- If -dt debug flag set, output time stamp found/expected |
| 471 |
|
| 472 |
if Source.Table (Src).Source_Found and then Debug_Flag_T then |
| 473 |
Write_Str ("Source: """); |
| 474 |
Get_Name_String (Sdep.Table (D).Sfile); |
| 475 |
Write_Str (Name_Buffer (1 .. Name_Len)); |
| 476 |
Write_Line (""""); |
| 477 |
|
| 478 |
Write_Str (" time stamp expected: "); |
| 479 |
Write_Line (String (Sdep.Table (D).Stamp)); |
| 480 |
|
| 481 |
Write_Str (" time stamp found: "); |
| 482 |
Write_Line (String (Source.Table (Src).Stamp)); |
| 483 |
end if; |
| 484 |
|
| 485 |
-- Return the source file |
| 486 |
|
| 487 |
return Source.Table (Src).Sfile; |
| 488 |
end if; |
| 489 |
end if; |
| 490 |
end loop; |
| 491 |
|
| 492 |
return No_File; |
| 493 |
end Time_Stamp_Mismatch; |
| 494 |
|
| 495 |
end ALI.Util; |