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-2020, 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 Ignore_ED => False, 253 Err => False); 254 255 Free (Text); 256 257 if ALIs.Table (Idread).Compile_Errors 258 and then not GNATprove_Mode 259 then 260 Error_Msg_File_1 := Withs.Table (W).Sfile; 261 Error_Msg ("{ had errors, must be fixed, and recompiled"); 262 Set_Name_Table_Int (Afile, Int (No_Unit_Id)); 263 264 -- In GNATprove mode, object files are never generated, so 265 -- No_Object=True is not considered an error. 266 267 elsif ALIs.Table (Idread).No_Object 268 and then not GNATprove_Mode 269 then 270 Error_Msg_File_1 := Withs.Table (W).Sfile; 271 Error_Msg ("{ must be recompiled"); 272 Set_Name_Table_Int (Afile, Int (No_Unit_Id)); 273 end if; 274 275 -- If the Unit is an Interface to a Stand-Alone Library, 276 -- set the Interface flag in the Withs table, so that its 277 -- dependant are not considered for elaboration order. 278 279 if ALIs.Table (Idread).SAL_Interface then 280 Withs.Table (W).SAL_Interface := True; 281 Interface_Library_Unit := True; 282 283 -- Set the entry in the Interfaces hash table, so that 284 -- other units that import this unit will set the flag 285 -- in their entry in the Withs table. 286 287 Interfaces.Set (Afile, True); 288 289 else 290 -- Otherwise, recurse to get new dependents 291 292 Read_Withed_ALIs (Idread); 293 end if; 294 end if; 295 296 -- If the ALI file has already been processed and is an interface, 297 -- set the flag in the entry of the Withs table. 298 299 elsif Interface_Library_Unit and then Interfaces.Get (Afile) then 300 Withs.Table (W).SAL_Interface := True; 301 end if; 302 end loop; 303 end loop; 304 end Read_Withed_ALIs; 305 306 ---------------------- 307 -- Set_Source_Table -- 308 ---------------------- 309 310 procedure Set_Source_Table (A : ALI_Id) is 311 F : File_Name_Type; 312 S : Source_Id; 313 Stamp : Time_Stamp_Type; 314 315 begin 316 Sdep_Loop : for D in 317 ALIs.Table (A).First_Sdep .. ALIs.Table (A).Last_Sdep 318 loop 319 F := Sdep.Table (D).Sfile; 320 321 if F /= No_File then 322 323 -- If this is the first time we are seeing this source file, 324 -- then make a new entry in the source table. 325 326 if Get_Name_Table_Int (F) = 0 then 327 Source.Increment_Last; 328 S := Source.Last; 329 Set_Name_Table_Int (F, Int (S)); 330 Source.Table (S).Sfile := F; 331 Source.Table (S).All_Timestamps_Match := True; 332 333 -- Initialize checksum fields 334 335 Source.Table (S).Checksum := Sdep.Table (D).Checksum; 336 Source.Table (S).All_Checksums_Match := True; 337 338 -- In check source files mode, try to get time stamp from file 339 340 if Opt.Check_Source_Files then 341 Stamp := Source_File_Stamp (F); 342 343 -- If we got the stamp, then set the stamp in the source 344 -- table entry and mark it as set from the source so that 345 -- it does not get subsequently changed. 346 347 if Stamp (Stamp'First) /= ' ' then 348 Source.Table (S).Stamp := Stamp; 349 Source.Table (S).Source_Found := True; 350 Source.Table (S).Stamp_File := F; 351 352 -- If we could not find the file, then the stamp is set 353 -- from the dependency table entry (to be possibly reset 354 -- if we find a later stamp in subsequent processing) 355 356 else 357 Source.Table (S).Stamp := Sdep.Table (D).Stamp; 358 Source.Table (S).Source_Found := False; 359 Source.Table (S).Stamp_File := ALIs.Table (A).Afile; 360 361 -- In All_Sources mode, flag error of file not found 362 363 if Opt.All_Sources then 364 Error_Msg_File_1 := F; 365 Error_Msg ("cannot locate {"); 366 end if; 367 end if; 368 369 -- First time for this source file, but Check_Source_Files 370 -- is off, so simply initialize the stamp from the Sdep entry 371 372 else 373 Source.Table (S).Stamp := Sdep.Table (D).Stamp; 374 Source.Table (S).Source_Found := False; 375 Source.Table (S).Stamp_File := ALIs.Table (A).Afile; 376 end if; 377 378 -- Here if this is not the first time for this source file, 379 -- so that the source table entry is already constructed. 380 381 else 382 S := Source_Id (Get_Name_Table_Int (F)); 383 384 -- Update checksum flag 385 386 if not Checksums_Match 387 (Sdep.Table (D).Checksum, Source.Table (S).Checksum) 388 then 389 Source.Table (S).All_Checksums_Match := False; 390 end if; 391 392 -- Check for time stamp mismatch 393 394 if Sdep.Table (D).Stamp /= Source.Table (S).Stamp then 395 Source.Table (S).All_Timestamps_Match := False; 396 397 -- When we have a time stamp mismatch, we go look for the 398 -- source file even if Check_Source_Files is false, since 399 -- if we find it, then we can use it to resolve which of the 400 -- two timestamps in the ALI files is likely to be correct. 401 -- We only look in the current directory, because when 402 -- Check_Source_Files is false, other search directories are 403 -- likely to be incorrect. 404 405 if not Check_Source_Files 406 and then Is_Regular_File (Get_Name_String (F)) 407 then 408 Stamp := Source_File_Stamp (F); 409 410 if Stamp (Stamp'First) /= ' ' then 411 Source.Table (S).Stamp := Stamp; 412 Source.Table (S).Source_Found := True; 413 Source.Table (S).Stamp_File := F; 414 end if; 415 end if; 416 417 -- If the stamp in the source table entry was set from the 418 -- source file, then we do not change it (the stamp in the 419 -- source file is always taken as the "right" one). 420 421 if Source.Table (S).Source_Found then 422 null; 423 424 -- Otherwise, we have no source file available, so we guess 425 -- that the later of the two timestamps is the right one. 426 -- Note that this guess only affects which error messages 427 -- are issued later on, not correct functionality. 428 429 else 430 if Sdep.Table (D).Stamp > Source.Table (S).Stamp then 431 Source.Table (S).Stamp := Sdep.Table (D).Stamp; 432 Source.Table (S).Stamp_File := ALIs.Table (A).Afile; 433 end if; 434 end if; 435 end if; 436 end if; 437 438 -- Set the checksum value in the source table 439 440 S := Source_Id (Get_Name_Table_Int (F)); 441 Source.Table (S).Checksum := Sdep.Table (D).Checksum; 442 end if; 443 444 end loop Sdep_Loop; 445 end Set_Source_Table; 446 447 ---------------------- 448 -- Set_Source_Table -- 449 ---------------------- 450 451 procedure Set_Source_Table is 452 begin 453 for A in ALIs.First .. ALIs.Last loop 454 Set_Source_Table (A); 455 end loop; 456 end Set_Source_Table; 457 458 ------------------------- 459 -- Time_Stamp_Mismatch -- 460 ------------------------- 461 462 function Time_Stamp_Mismatch 463 (A : ALI_Id; 464 Read_Only : Boolean := False) return File_Name_Type 465 is 466 Src : Source_Id; 467 -- Source file Id for the current Sdep entry 468 469 begin 470 for D in ALIs.Table (A).First_Sdep .. ALIs.Table (A).Last_Sdep loop 471 Src := Source_Id (Get_Name_Table_Int (Sdep.Table (D).Sfile)); 472 473 if Opt.Minimal_Recompilation 474 and then Sdep.Table (D).Stamp /= Source.Table (Src).Stamp 475 then 476 -- If minimal recompilation is in action, replace the stamp 477 -- of the source file in the table if checksums match. 478 479 -- ??? It is probably worth updating the ALI file with a new 480 -- field to avoid recomputing it each time. In any case we ensure 481 -- that we don't gobble up string table space by doing a mark 482 -- release around this computation. 483 484 Stringt.Mark; 485 486 if Checksums_Match 487 (Get_File_Checksum (Sdep.Table (D).Sfile), 488 Source.Table (Src).Checksum) 489 then 490 if Verbose_Mode then 491 Write_Str (" "); 492 Write_Str (Get_Name_String (Sdep.Table (D).Sfile)); 493 Write_Str (": up to date, different timestamps " & 494 "but same checksum"); 495 Write_Eol; 496 end if; 497 498 Sdep.Table (D).Stamp := Source.Table (Src).Stamp; 499 end if; 500 501 Stringt.Release; 502 end if; 503 504 if (not Read_Only) or else Source.Table (Src).Source_Found then 505 if not Source.Table (Src).Source_Found 506 or else Sdep.Table (D).Stamp /= Source.Table (Src).Stamp 507 then 508 -- If -dt debug flag set, output time stamp found/expected 509 510 if Source.Table (Src).Source_Found and then Debug_Flag_T then 511 Write_Str ("Source: """); 512 Get_Name_String (Sdep.Table (D).Sfile); 513 Write_Str (Name_Buffer (1 .. Name_Len)); 514 Write_Line (""""); 515 516 Write_Str (" time stamp expected: "); 517 Write_Line (String (Sdep.Table (D).Stamp)); 518 519 Write_Str (" time stamp found: "); 520 Write_Line (String (Source.Table (Src).Stamp)); 521 end if; 522 523 -- Return the source file 524 525 return Source.Table (Src).Sfile; 526 end if; 527 end if; 528 end loop; 529 530 return No_File; 531 end Time_Stamp_Mismatch; 532 533end ALI.Util; 534