https://gcc.gnu.org/g:1726eda68199ab64e327035e782865468b626edc

commit r15-6134-g1726eda68199ab64e327035e782865468b626edc
Author: Ronan Desplanques <desplanq...@adacore.com>
Date:   Thu Nov 14 16:52:55 2024 +0100

    ada: Restrict External_Initialization file lookup
    
    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.

Diff:
---
 gcc/ada/sem_ch3.adb | 41 +++++++++++++++++++++++++++++++++++++----
 1 file changed, 37 insertions(+), 4 deletions(-)

diff --git a/gcc/ada/sem_ch3.adb b/gcc/ada/sem_ch3.adb
index 4a3d020330ca..f88c5adc9296 100644
--- a/gcc/ada/sem_ch3.adb
+++ b/gcc/ada/sem_ch3.adb
@@ -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);

Reply via email to