The pid_t type on 32-bit Solaris differs from gcc's builtin.

<sys/types.h> has

#if defined(_LP64) || defined(_I32LPx)
typedef int     pid_t;                  /* process id type      */
#else
typedef long    pid_t;                  /* (historical version) */
#endif

while gcc/defaults.h uses

#define PID_TYPE "int"

on all targets.

This patch fixes this by providing a Solaris definition instead.  At the
same time, it introduces __PID_TYPE__ etc. macros matching e.g. wint_t
as well as two testcases.

g++.dg/lookup/extern-c-redecl3.C had to be adjusted to use the new
__PID_TYPE__.

Bootstrapped without regressions on i386-pc-solaris2.11 and
x86_64-pc-linux-gnu.

Ok for trunk?


A couple of frontends (Ada, D, LTO, Rust) provide their own definitions
of pid_type_node, always using int_type_node.  Since wint_type_node is
handled similarly since the type width is always 32-bit, it seems there
are no changes needed.

Thanks to Jonathan and Andrew for pointing this out.

        Rainer

-- 
-----------------------------------------------------------------------------
Rainer Orth, Center for Biotechnology, Bielefeld University


2025-11-17  Rainer Orth  <[email protected]>

        gcc:
        * config/sol2.h (PID_TYPE): Define.
        * doc/cpp.texi (Common Predefined Macros): Document __PID_TYPE__,
        __PID_MAX__, __PID_MIN__, __PID_WIDTH__.
        * doc/tm.texi.in (Type Layout): Document PID_TYPE.
        * doc/tm.texi: Regenerate.

        gcc/c-family:
        * c-common.cc (c_stddef_cpp_builtins): Define __PID_TYPE__.
        * c-cppbuiltin.cc (builtin_define_stdint_macros): Define
        __PID_MIN__, __PID_MAX__, __PID_WIDTH__.

        gcc/testsuite:
        * g++.dg/lookup/extern-c-redecl3.C (fork): Use __PID_TYPE__
        instead of int.
        * g++.dg/pid_t-1.C: New test.
        * gcc.dg/pid_t-1.c: Likewise.

# HG changeset patch
# Parent  7e5e4234fe66fc86716d62458d8fcd12d8638b99
Fix PID_TYPE on Solaris

diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc
--- a/gcc/c-family/c-common.cc
+++ b/gcc/c-family/c-common.cc
@@ -5581,6 +5581,8 @@ c_stddef_cpp_builtins(void)
   builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
   if (SIG_ATOMIC_TYPE)
     builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
+  if (PID_TYPE)
+    builtin_define_with_value ("__PID_TYPE__", PID_TYPE, 0);
   if (INT8_TYPE)
     builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
   if (INT16_TYPE)
diff --git a/gcc/c-family/c-cppbuiltin.cc b/gcc/c-family/c-cppbuiltin.cc
--- a/gcc/c-family/c-cppbuiltin.cc
+++ b/gcc/c-family/c-cppbuiltin.cc
@@ -452,6 +452,13 @@ builtin_define_stdint_macros (void)
       builtin_define_type_width ("__SIG_ATOMIC_WIDTH__", sig_atomic_type_node,
 				 NULL_TREE);
     }
+  if (pid_type_node)
+    {
+      builtin_define_type_minmax ("__PID_MIN__", "__PID_MAX__",
+				  pid_type_node);
+      builtin_define_type_width ("__PID_WIDTH__", pid_type_node,
+				 NULL_TREE);
+    }
   if (int8_type_node)
     builtin_define_type_max ("__INT8_MAX__", int8_type_node);
   if (int16_type_node)
diff --git a/gcc/config/sol2.h b/gcc/config/sol2.h
--- a/gcc/config/sol2.h
+++ b/gcc/config/sol2.h
@@ -41,6 +41,12 @@ along with GCC; see the file COPYING3.  
 #undef WINT_TYPE_SIZE
 #define WINT_TYPE_SIZE 32
 
+/* Same for pid_t.  See SCD 2.4.2, p. 6P-12, Figure 6-59 (64-bit).  There's
+   no corresponding 32-bit definition, but this is what Solaris 8
+   <sys/types.h> uses.  */
+
+#define PID_TYPE (TARGET_64BIT ? "int" : "long int")
+
 #define SIG_ATOMIC_TYPE "int"
 
 /* ??? This definition of int8_t follows the system header but does
diff --git a/gcc/doc/cpp.texi b/gcc/doc/cpp.texi
--- a/gcc/doc/cpp.texi
+++ b/gcc/doc/cpp.texi
@@ -2101,6 +2101,7 @@ OSF/rose @option{-mno-underscores} optio
 @itemx __INTMAX_TYPE__
 @itemx __UINTMAX_TYPE__
 @itemx __SIG_ATOMIC_TYPE__
+@itemx __PID_TYPE__
 @itemx __INT8_TYPE__
 @itemx __INT16_TYPE__
 @itemx __INT32_TYPE__
@@ -2129,7 +2130,8 @@ OSF/rose @option{-mno-underscores} optio
 @itemx __UINTPTR_TYPE__
 These macros are defined to the correct underlying types for the
 @code{size_t}, @code{ptrdiff_t}, @code{wchar_t}, @code{wint_t},
-@code{intmax_t}, @code{uintmax_t}, @code{sig_atomic_t}, @code{int8_t},
+@code{intmax_t}, @code{uintmax_t}, @code{sig_atomic_t}, @code{pid_t},
+@code{int8_t},
 @code{int16_t}, @code{int32_t}, @code{int64_t}, @code{uint8_t},
 @code{uint16_t}, @code{uint32_t}, @code{uint64_t},
 @code{int_least8_t}, @code{int_least16_t}, @code{int_least32_t},
@@ -2139,7 +2141,8 @@ These macros are defined to the correct 
 @code{uint_fast8_t}, @code{uint_fast16_t}, @code{uint_fast32_t},
 @code{uint_fast64_t}, @code{intptr_t}, and @code{uintptr_t} typedefs,
 respectively.  They exist to make the standard header files
-@file{stddef.h}, @file{stdint.h}, and @file{wchar.h} work correctly.
+@file{stddef.h}, @file{stdint.h}, @file{sys/types.h} and @file{wchar.h}
+work correctly.
 You should not use these macros directly; instead, include the
 appropriate headers and use the typedefs.  Some of these macros may
 not be defined on particular systems if GCC does not provide a
@@ -2163,6 +2166,7 @@ this macro directly; instead, include th
 @itemx __INTMAX_MAX__
 @itemx __UINTMAX_MAX__
 @itemx __SIG_ATOMIC_MAX__
+@itemx __PID_MAX__
 @itemx __INT8_MAX__
 @itemx __INT16_MAX__
 @itemx __INT32_MAX__
@@ -2192,11 +2196,13 @@ this macro directly; instead, include th
 @itemx __WCHAR_MIN__
 @itemx __WINT_MIN__
 @itemx __SIG_ATOMIC_MIN__
+@itemx __PID_MIN__
 Defined to the maximum value of the @code{signed char}, @code{wchar_t},
 @code{signed short},
 @code{signed int}, @code{signed long}, @code{signed long long},
 @code{wint_t}, @code{size_t}, @code{ptrdiff_t},
-@code{intmax_t}, @code{uintmax_t}, @code{sig_atomic_t}, @code{int8_t},
+@code{intmax_t}, @code{uintmax_t}, @code{sig_atomic_t}, @code{pid_t},
+@code{int8_t},
 @code{int16_t}, @code{int32_t}, @code{int64_t}, @code{uint8_t},
 @code{uint16_t}, @code{uint32_t}, @code{uint64_t},
 @code{int_least8_t}, @code{int_least16_t}, @code{int_least32_t},
@@ -2236,6 +2242,7 @@ not provide a @file{stdint.h} header on 
 @itemx __LONG_LONG_WIDTH__
 @itemx __PTRDIFF_WIDTH__
 @itemx __SIG_ATOMIC_WIDTH__
+@itemx __PID_WIDTH__
 @itemx __SIZE_WIDTH__
 @itemx __WCHAR_WIDTH__
 @itemx __WINT_WIDTH__
@@ -2250,7 +2257,8 @@ not provide a @file{stdint.h} header on 
 @itemx __INTPTR_WIDTH__
 @itemx __INTMAX_WIDTH__
 Defined to the bit widths of the corresponding types.  They exist to
-make the implementations of @file{limits.h} and @file{stdint.h} behave
+make the implementations of @file{limits.h}, @file{stdint.h}, and
+@file{<sys/types.h>} behave
 correctly.  You should not use these macros directly; instead, include
 the appropriate headers.  Some of these macros may not be defined on
 particular systems if GCC does not provide a @file{stdint.h} header on
diff --git a/gcc/doc/tm.texi b/gcc/doc/tm.texi
--- a/gcc/doc/tm.texi
+++ b/gcc/doc/tm.texi
@@ -1799,6 +1799,7 @@ int}.
 @end defmac
 
 @defmac SIG_ATOMIC_TYPE
+@defmacx PID_TYPE
 @defmacx INT8_TYPE
 @defmacx INT16_TYPE
 @defmacx INT32_TYPE
@@ -1825,7 +1826,7 @@ int}.
 @defmacx UINT_FAST64_TYPE
 @defmacx INTPTR_TYPE
 @defmacx UINTPTR_TYPE
-C expressions for the standard types @code{sig_atomic_t},
+C expressions for the standard types @code{sig_atomic_t}, @code{pid_t},
 @code{int8_t}, @code{int16_t}, @code{int32_t}, @code{int64_t},
 @code{uint8_t}, @code{uint16_t}, @code{uint32_t}, @code{uint64_t},
 @code{int_least8_t}, @code{int_least16_t}, @code{int_least32_t},
diff --git a/gcc/doc/tm.texi.in b/gcc/doc/tm.texi.in
--- a/gcc/doc/tm.texi.in
+++ b/gcc/doc/tm.texi.in
@@ -1527,6 +1527,7 @@ int}.
 @end defmac
 
 @defmac SIG_ATOMIC_TYPE
+@defmacx PID_TYPE
 @defmacx INT8_TYPE
 @defmacx INT16_TYPE
 @defmacx INT32_TYPE
@@ -1553,7 +1554,7 @@ int}.
 @defmacx UINT_FAST64_TYPE
 @defmacx INTPTR_TYPE
 @defmacx UINTPTR_TYPE
-C expressions for the standard types @code{sig_atomic_t},
+C expressions for the standard types @code{sig_atomic_t}, @code{pid_t},
 @code{int8_t}, @code{int16_t}, @code{int32_t}, @code{int64_t},
 @code{uint8_t}, @code{uint16_t}, @code{uint32_t}, @code{uint64_t},
 @code{int_least8_t}, @code{int_least16_t}, @code{int_least32_t},
diff --git a/gcc/testsuite/g++.dg/lookup/extern-c-redecl3.C b/gcc/testsuite/g++.dg/lookup/extern-c-redecl3.C
--- a/gcc/testsuite/g++.dg/lookup/extern-c-redecl3.C
+++ b/gcc/testsuite/g++.dg/lookup/extern-c-redecl3.C
@@ -5,17 +5,17 @@
 // { dg-final { scan-assembler-not "call\[\t \]+\[^\$\]*?_Z4forkv" { target i?86-*-* x86_64-*-* } } }
 // { dg-final { scan-assembler "call\[\t \]+_?fork" { target i?86-*-* x86_64-*-* } } }
 
-extern "C" int fork (void);
+extern "C" __PID_TYPE__ fork (void);
 
 void
 foo ()
 {
-  extern int fork (void);
+  extern __PID_TYPE__ fork (void);
   fork ();
 }
 
 extern "C"
-int
+__PID_TYPE__
 fork (void)
 {
   return 0;
diff --git a/gcc/testsuite/g++.dg/pid_t-1.C b/gcc/testsuite/g++.dg/pid_t-1.C
new file mode 100644
--- /dev/null
+++ b/gcc/testsuite/g++.dg/pid_t-1.C
@@ -0,0 +1,3 @@
+/* { dg-options "-Wall" } */
+
+extern "C" int fork (void); // { dg-warning "conflicts with built-in declaration" "" { target { *-*-solaris2* && ilp32 } } }
diff --git a/gcc/testsuite/gcc.dg/pid_t-1.c b/gcc/testsuite/gcc.dg/pid_t-1.c
new file mode 100644
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pid_t-1.c
@@ -0,0 +1,17 @@
+/* { dg-do compile } */
+/* { dg-options "-Wall" } */
+/* { dg-require-fork "" } */
+
+/* Compile with -Wall to get a warning if built-in and system pid_t don't
+   match.  */
+
+#include <sys/types.h>
+
+__PID_TYPE__ __p_t__;
+pid_t *p_t_p;
+
+void
+pt (void)
+{
+  p_t_p = &__p_t__;
+}

Reply via email to