]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Restrict External_Initialization file lookup
authorRonan Desplanques <desplanques@adacore.com>
Thu, 14 Nov 2024 15:52:55 +0000 (16:52 +0100)
committerMarc Poulhiès <dkm@gcc.gnu.org>
Thu, 12 Dec 2024 09:57:54 +0000 (10:57 +0100)
Before this patch, External_Initialization looked for files in all
directories of the source search path, which led to inconsistencies in
some cases. This patch restricts the file lookup so the argument is
interpreted as relative to the current source file's directory only.

gcc/ada/ChangeLog:

* sem_ch3.adb (Apply_External_Initialization): Restrict File lookup.

gcc/ada/sem_ch3.adb

index 4a3d020330ca5c7a34806d5cb909d78af2a27a27..f88c5adc92962a10755251fb610afb33c6cd50a2 100644 (file)
@@ -42,6 +42,7 @@ with Exp_Dist;       use Exp_Dist;
 with Exp_Tss;        use Exp_Tss;
 with Exp_Util;       use Exp_Util;
 with Expander;       use Expander;
+with Fmap;
 with Freeze;         use Freeze;
 with Ghost;          use Ghost;
 with Itypes;         use Itypes;
@@ -54,6 +55,7 @@ with Namet;          use Namet;
 with Nlists;         use Nlists;
 with Nmake;          use Nmake;
 with Opt;            use Opt;
+with Osint;
 with Restrict;       use Restrict;
 with Rident;         use Rident;
 with Rtsfind;        use Rtsfind;
@@ -87,6 +89,7 @@ with Sinput.L;
 with Snames;         use Snames;
 with Stringt;
 with Strub;          use Strub;
+with System.OS_Lib;
 with Targparm;       use Targparm;
 with Tbuild;         use Tbuild;
 with Ttypes;         use Ttypes;
@@ -3885,6 +3888,7 @@ package body Sem_Ch3 is
 
          Expr : N_Subexpr_Id;
 
+         Data_Path : File_Name_Type;
       begin
          Remove (Specification);
 
@@ -3919,13 +3923,42 @@ package body Sem_Ch3 is
             return;
          end if;
 
+         declare
+            S : constant String := Stringt.To_String (Strval (Def));
          begin
-            declare
-               Name : constant Valid_Name_Id :=
-                 Stringt.String_To_Name (Strval (Def));
+            if System.OS_Lib.Is_Absolute_Path (S) then
+               Data_Path := Name_Find (S);
+            else
+               declare
+                  Current_File_Name : constant File_Name_Type :=
+                    Unit_File_Name (Current_Sem_Unit);
+
+                  Current_File_Path : constant File_Name_Type :=
+                    Fmap.Mapped_Path_Name (Current_File_Name);
+
+                  Current_File_Directory : constant File_Name_Type :=
+                    Osint.Get_Directory (Current_File_Path);
+
+                  Absolute_Dir : constant String :=
+                    System.OS_Lib.Normalize_Pathname
+                      (Get_Name_String (Current_File_Directory),
+                       Resolve_Links => False);
+
+                  Data_Path_String : constant String :=
+                    Absolute_Dir
+                    & System.OS_Lib.Directory_Separator
+                    & Stringt.To_String (Strval (Def));
 
+               begin
+                  Data_Path := Name_Find (Data_Path_String);
+               end;
+            end if;
+         end;
+
+         begin
+            declare
                Source_File_I : constant Source_File_Index :=
-                 Sinput.L.Load_Source_File (File_Name_Type (Name));
+                 Sinput.L.Load_Source_File (Data_Path);
             begin
                if Source_File_I <= No_Source_File then
                   Error_Msg_N ("cannot find input file", Specification);