Tested on hppa64-hp-hpux11.11 and x86_64-pc-linux-gnu.

Okay?

Dave
---

libgfortran: Define macro to handle z length modifier in format strings

hppa64-hp-hpux* lacks the z length modifier.  It specifies the following
integer conversion corresponds to a size_t or a ssize_t argument.  The
size_t type is unsigned long on HP-UX, so we can replace the z length
modifier with l on HP-UX.

2026-06-11  John David Anglin  <[email protected]>

libgfortran/ChangeLog:

        * libgfortran.h (FLM_Z): Define.
        * caf/shmem/alloc.c (get_memory_by_id_internal): Use FLM_Z.
        (alloc_free_memory_with_id): Likewise.
        * caf/shmem/supervisor.c (get_memory_size_from_envvar): Likewise.
        * caf/shmem/thread_support.c (get_handle): Likewise.
        (thread_support_init_supervisor): Likewise.
        * intrinsics/reduce.c (reduce): Likewise.

diff --git a/libgfortran/caf/shmem/alloc.c b/libgfortran/caf/shmem/alloc.c
index 4fc2307ffec..adbab848bd2 100644
--- a/libgfortran/caf/shmem/alloc.c
+++ b/libgfortran/caf/shmem/alloc.c
@@ -85,7 +85,7 @@ get_memory_by_id_internal (alloc *iface, size_t size, memid 
id, bool *created)
        {
          allocator_unlock (&iface->alloc);
          caf_runtime_error (
-           "Size mismatch for coarray allocation id %zd: found = %lu "
+           "Size mismatch for coarray allocation id %" FLM_Z "d: found = %lu "
            "< size = %lu\n",
            id, found_size, size);
          return NULL; // The runtime_error exit()s, so this is never reached.
@@ -144,7 +144,8 @@ alloc_free_memory_with_id (alloc *iface, memid id)
   if (!hm_search_result_contains (&res))
     {
       allocator_unlock (&iface->alloc);
-      caf_runtime_error ("Error in free_memory_with_id: %zd not found.\n", id);
+      caf_runtime_error ("Error in free_memory_with_id: %" FLM_Z
+                        "d not found.\n", id);
       return;
     }
 
diff --git a/libgfortran/caf/shmem/supervisor.c 
b/libgfortran/caf/shmem/supervisor.c
index aa692a6551d..8f676c5aa0a 100644
--- a/libgfortran/caf/shmem/supervisor.c
+++ b/libgfortran/caf/shmem/supervisor.c
@@ -106,7 +106,7 @@ get_memory_size_from_envvar (void)
     {
       char suffix[2];
       int rv;
-      rv = sscanf (e, "%zu%1s", &sz, suffix);
+      rv = sscanf (e, "%" FLM_Z "u%1s", &sz, suffix);
       if (rv == 2)
        {
          switch (suffix[0])
diff --git a/libgfortran/caf/shmem/thread_support.c 
b/libgfortran/caf/shmem/thread_support.c
index 0c5f8cc2763..dc907c95b0a 100755
--- a/libgfortran/caf/shmem/thread_support.c
+++ b/libgfortran/caf/shmem/thread_support.c
@@ -122,7 +122,8 @@ get_handle (const size_t id, const char t)
 
       if (!pid)
        pid = shared_memory_get_env ();
-      snprintf (name, MAX_PATH, "Global_gfortran-%s-%c-%zd", pid, t, id);
+      snprintf (name, MAX_PATH, "Global_gfortran-%s-%c-%" FLM_Z "d",
+               pid, t, id);
       switch (t)
        {
        case 'm':
@@ -131,8 +132,8 @@ get_handle (const size_t id, const char t)
        case 'c':
          {
            handles[id] = CreateSemaphore (NULL, 0, __INT_MAX__, name);
-           snprintf (name, MAX_PATH, "Global_gfortran-%s-%c-%zd_lock", pid, t,
-                     id);
+           snprintf (name, MAX_PATH, "Global_gfortran-%s-%c-%" FLM_Z "d_lock",
+                     pid, t, id);
            handles[id + 1] = CreateSemaphore (NULL, 1, 1, name);
            this_image.supervisor->global_used_handles
              = smax (this_image.supervisor->global_used_handles, id + 2);
@@ -173,7 +174,7 @@ void
 thread_support_init_supervisor (void)
 {
   if (local->total_num_images > ULONGBITS * MAX_NUM_SIGNALED)
-    caf_runtime_error ("Maximum number of supported images is %zd.",
+    caf_runtime_error ("Maximum number of supported images is %" FLM_Z "d.",
                       ULONGBITS * MAX_NUM_SIGNALED);
   this_image.supervisor->global_used_handles = 0;
 }
diff --git a/libgfortran/intrinsics/reduce.c b/libgfortran/intrinsics/reduce.c
index f1f3b838adb..28bc834bc73 100644
--- a/libgfortran/intrinsics/reduce.c
+++ b/libgfortran/intrinsics/reduce.c
@@ -110,7 +110,7 @@ reduce (parray *ret,
        {
          int mext = (int)GFC_DESCRIPTOR_EXTENT (mask, i);
          runtime_error ("shape mismatch between ARRAY and MASK in the REDUCE "
-                        "intrinsic (%zd/%d)", ext, mext);
+                        "intrinsic (%" FLM_Z "d/%d)", ext, mext);
        }
 
       if (scalar_result)
diff --git a/libgfortran/libgfortran.h b/libgfortran/libgfortran.h
index 10c5313e1bc..8381cbd7cd4 100644
--- a/libgfortran/libgfortran.h
+++ b/libgfortran/libgfortran.h
@@ -36,6 +36,14 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If 
not, see
 #  define gfc_printf __printf__
 #endif
 
+/* Handle unsupported C99 length modifiers.  */
+#if defined(__hpux__)
+/* size_t is unsigned long on HP-UX.  */
+#define FLM_Z "l"
+#else
+#define FLM_Z "z"
+#endif
+
 /* config.h MUST be first because it can affect system headers.  */
 #include "config.h"
 

Attachment: signature.asc
Description: PGP signature

Reply via email to