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