[COMMITTED,36/51] ada: Simplify host and target suffix queries

Message ID 20260602084541.3829876-36-poulhies@adacore.com
State Committed
Headers
Series [COMMITTED,01/51] ada: Rename Private_Component function |

Commit Message

Marc Poulhiès June 2, 2026, 8:45 a.m. UTC
  From: Piotr Trojanek <trojanek@adacore.com>

Code cleanup for host and target suffixes, which are stored in C runtime code.

gcc/ada/ChangeLog:

	* libgnat/s-os_lib.adb (Get_Debuggable_Suffix, Get_Executable_Suffix,
	Get_Object_Suffix, Get_Target_Debuggable_Suffix,
	Get_Target_Executable_Suffix, Get_Target_Object_Suffix): Access strings
	like we do in Gnatlink.
	* adaint.c (__gnat_object_suffix, __gnat_executable_suffix,
	__gnat_debuggable_suffix): Now constants, just like the ones for target
	suffixes.

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/adaint.c             |  45 ++-------------
 gcc/ada/libgnat/s-os_lib.adb | 106 +++++++----------------------------
 2 files changed, 25 insertions(+), 126 deletions(-)
  

Patch

diff --git a/gcc/ada/adaint.c b/gcc/ada/adaint.c
index 5fb25571774..cf885126067 100644
--- a/gcc/ada/adaint.c
+++ b/gcc/ada/adaint.c
@@ -698,51 +698,18 @@  __gnat_get_current_dir (char *dir, int *length)
    dir[*length] = '\0';
 }
 
-/* Return the suffix for object files.  */
+/* Suffix for object files.  */
 
-void
-__gnat_get_object_suffix_ptr (int *len, const char **value)
-{
-  *value = HOST_OBJECT_SUFFIX;
+const char *__gnat_object_suffix = HOST_OBJECT_SUFFIX;
 
-  if (*value == 0)
-    *len = 0;
-  else
-    *len = strlen (*value);
+/* Suffix for executable files.  */
 
-  return;
-}
+const char *__gnat_executable_suffix = HOST_EXECUTABLE_SUFFIX;
 
-/* Return the suffix for executable files.  */
-
-void
-__gnat_get_executable_suffix_ptr (int *len, const char **value)
-{
-  *value = HOST_EXECUTABLE_SUFFIX;
-
-  if (!*value)
-    *len = 0;
-  else
-    *len = strlen (*value);
-
-  return;
-}
-
-/* Return the suffix for debuggable files. Usually this is the same as the
+/* Suffix for debuggable files. Usually this is the same as the
    executable extension.  */
 
-void
-__gnat_get_debuggable_suffix_ptr (int *len, const char **value)
-{
-  *value = HOST_EXECUTABLE_SUFFIX;
-
-  if (*value == 0)
-    *len = 0;
-  else
-    *len = strlen (*value);
-
-  return;
-}
+const char *__gnat_debuggable_suffix = HOST_EXECUTABLE_SUFFIX;
 
 /* Returns the OS filename and corresponding encoding.  */
 
diff --git a/gcc/ada/libgnat/s-os_lib.adb b/gcc/ada/libgnat/s-os_lib.adb
index ae8ef2b1a08..0caab7e045e 100644
--- a/gcc/ada/libgnat/s-os_lib.adb
+++ b/gcc/ada/libgnat/s-os_lib.adb
@@ -35,6 +35,7 @@  with System.Case_Util;
 with System.CRTL;
 with System.Soft_Links;
 with Interfaces.C;
+with Interfaces.C.Strings;
 
 package body System.OS_Lib is
 
@@ -1034,22 +1035,11 @@  package body System.OS_Lib is
    ---------------------------
 
    function Get_Debuggable_Suffix return String_Access is
-      procedure Get_Suffix_Ptr (Length, Ptr : Address);
-      pragma Import (C, Get_Suffix_Ptr, "__gnat_get_debuggable_suffix_ptr");
-
-      Result        : String_Access;
-      Suffix_Length : Integer;
-      Suffix_Ptr    : Address;
+      Suffix : constant Interfaces.C.Strings.chars_ptr;
+      pragma Import (C, Suffix, "__gnat_debuggable_suffix");
 
    begin
-      Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
-      Result := new String (1 .. Suffix_Length);
-
-      if Suffix_Length > 0 then
-         Strncpy (Result.all'Address, Suffix_Ptr, size_t (Suffix_Length));
-      end if;
-
-      return Result;
+      return new String'(Interfaces.C.Strings.Value (Suffix));
    end Get_Debuggable_Suffix;
 
    ---------------------------
@@ -1057,22 +1047,11 @@  package body System.OS_Lib is
    ---------------------------
 
    function Get_Executable_Suffix return String_Access is
-      procedure Get_Suffix_Ptr (Length, Ptr : Address);
-      pragma Import (C, Get_Suffix_Ptr, "__gnat_get_executable_suffix_ptr");
-
-      Result        : String_Access;
-      Suffix_Length : Integer;
-      Suffix_Ptr    : Address;
+      Suffix : constant Interfaces.C.Strings.chars_ptr;
+      pragma Import (C, Suffix, "__gnat_executable_suffix");
 
    begin
-      Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
-      Result := new String (1 .. Suffix_Length);
-
-      if Suffix_Length > 0 then
-         Strncpy (Result.all'Address, Suffix_Ptr, size_t (Suffix_Length));
-      end if;
-
-      return Result;
+      return new String'(Interfaces.C.Strings.Value (Suffix));
    end Get_Executable_Suffix;
 
    -----------------------
@@ -1080,22 +1059,11 @@  package body System.OS_Lib is
    -----------------------
 
    function Get_Object_Suffix return String_Access is
-      procedure Get_Suffix_Ptr (Length, Ptr : Address);
-      pragma Import (C, Get_Suffix_Ptr, "__gnat_get_object_suffix_ptr");
-
-      Result        : String_Access;
-      Suffix_Length : Integer;
-      Suffix_Ptr    : Address;
+      Suffix : constant Interfaces.C.Strings.chars_ptr;
+      pragma Import (C, Suffix, "__gnat_object_suffix");
 
    begin
-      Get_Suffix_Ptr (Suffix_Length'Address, Suffix_Ptr'Address);
-      Result := new String (1 .. Suffix_Length);
-
-      if Suffix_Length > 0 then
-         Strncpy (Result.all'Address, Suffix_Ptr, size_t (Suffix_Length));
-      end if;
-
-      return Result;
+      return new String'(Interfaces.C.Strings.Value (Suffix));
    end Get_Object_Suffix;
 
    ----------------------------------
@@ -1103,23 +1071,11 @@  package body System.OS_Lib is
    ----------------------------------
 
    function Get_Target_Debuggable_Suffix return String_Access is
-      Target_Exec_Ext_Ptr : Address;
-      pragma Import
-        (C, Target_Exec_Ext_Ptr, "__gnat_target_debuggable_extension");
-
-      Result        : String_Access;
-      Suffix_Length : Integer;
+      Suffix : constant Interfaces.C.Strings.chars_ptr;
+      pragma Import (C, Suffix, "__gnat_target_debuggable_extension");
 
    begin
-      Suffix_Length := Integer (CRTL.strlen (Target_Exec_Ext_Ptr));
-      Result := new String (1 .. Suffix_Length);
-
-      if Suffix_Length > 0 then
-         Strncpy
-           (Result.all'Address, Target_Exec_Ext_Ptr, size_t (Suffix_Length));
-      end if;
-
-      return Result;
+      return new String'(Interfaces.C.Strings.Value (Suffix));
    end Get_Target_Debuggable_Suffix;
 
    ----------------------------------
@@ -1127,23 +1083,11 @@  package body System.OS_Lib is
    ----------------------------------
 
    function Get_Target_Executable_Suffix return String_Access is
-      Target_Exec_Ext_Ptr : Address;
-      pragma Import
-        (C, Target_Exec_Ext_Ptr, "__gnat_target_executable_extension");
-
-      Result        : String_Access;
-      Suffix_Length : Integer;
+      Suffix : constant Interfaces.C.Strings.chars_ptr;
+      pragma Import (C, Suffix, "__gnat_target_executable_extension");
 
    begin
-      Suffix_Length := Integer (CRTL.strlen (Target_Exec_Ext_Ptr));
-      Result := new String (1 .. Suffix_Length);
-
-      if Suffix_Length > 0 then
-         Strncpy
-           (Result.all'Address, Target_Exec_Ext_Ptr, size_t (Suffix_Length));
-      end if;
-
-      return Result;
+      return new String'(Interfaces.C.Strings.Value (Suffix));
    end Get_Target_Executable_Suffix;
 
    ------------------------------
@@ -1151,23 +1095,11 @@  package body System.OS_Lib is
    ------------------------------
 
    function Get_Target_Object_Suffix return String_Access is
-      Target_Object_Ext_Ptr : Address;
-      pragma Import
-        (C, Target_Object_Ext_Ptr, "__gnat_target_object_extension");
-
-      Result        : String_Access;
-      Suffix_Length : Integer;
+      Suffix : constant Interfaces.C.Strings.chars_ptr;
+      pragma Import (C, Suffix, "__gnat_target_object_extension");
 
    begin
-      Suffix_Length := Integer (CRTL.strlen (Target_Object_Ext_Ptr));
-      Result := new String (1 .. Suffix_Length);
-
-      if Suffix_Length > 0 then
-         Strncpy
-           (Result.all'Address, Target_Object_Ext_Ptr, size_t (Suffix_Length));
-      end if;
-
-      return Result;
+      return new String'(Interfaces.C.Strings.Value (Suffix));
    end Get_Target_Object_Suffix;
 
    ------------