Revert "Replace pg_restrict by standard restrict"
authorPeter Eisentraut <peter@eisentraut.org>
Wed, 14 Jan 2026 14:12:25 +0000 (15:12 +0100)
committerPeter Eisentraut <peter@eisentraut.org>
Wed, 14 Jan 2026 14:12:25 +0000 (15:12 +0100)
This reverts commit f0f2c0c1aef95757c4e7f144d5577e2b0d814279.

The original problem that led to the use of pg_restrict was that MSVC
couldn't handle plain restrict, and defining it to something else
would conflict with its __declspec(restrict) that is used in system
header files.  In C11 mode, this is no longer a problem, as MSVC
handles plain restrict.  This led to the commit to replace pg_restrict
with restrict.  But this did not take C++ into account.  Standard C++
does not have restrict, so we defined it as something else (for
example, MSVC supports __restrict).  But this then again conflicts
with __declspec(restrict) in system header files.  So we have to
revert this attempt.  The comments are updated to clarify that the
reason for this is now C++ only.

Reported-by: Jelte Fennema-Nio <postgres@jeltef.nl>
Reviewed-by: Andres Freund <andres@anarazel.de>
Discussion: https://www.postgresql.org/message-id/CAGECzQRoD7chJP1-dneSrhxUJv%2BBRcigoGOO4UwGzaShLot2Yw%40mail.gmail.com

12 files changed:
configure
configure.ac
meson.build
src/bin/pg_verifybackup/pg_verifybackup.c
src/bin/pg_verifybackup/pg_verifybackup.h
src/common/logging.c
src/common/string.c
src/include/c.h
src/include/common/logging.h
src/include/common/string.h
src/include/libpq/pqformat.h
src/include/pg_config.h.in

index 045c913865d8c85fae1a65845ee2d07b7c6db1f2..fb6a4914b06bf470a1f6142115c785c48d1f8d05 100755 (executable)
--- a/configure
+++ b/configure
@@ -15131,10 +15131,10 @@ _ACEOF
 fi
 
 
-# Even though restrict is in C99 and should be supported by all
-# supported compilers, this test is useful because it will prefer a
-# spelling that also works in C++ (often __restrict).  (restrict is
-# not part of the C++ standard.)
+# MSVC doesn't cope well with defining restrict to __restrict, the
+# spelling it understands, because it conflicts with
+# __declspec(restrict) in C++ mode. Therefore we define pg_restrict to
+# the appropriate definition, which presumably won't conflict.
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking for C/C++ restrict keyword" >&5
 $as_echo_n "checking for C/C++ restrict keyword... " >&6; }
 if ${ac_cv_c_restrict+:} false; then :
@@ -15181,6 +15181,16 @@ _ACEOF
  ;;
  esac
 
+if test "$ac_cv_c_restrict" = "no"; then
+  pg_restrict=""
+else
+  pg_restrict="$ac_cv_c_restrict"
+fi
+
+cat >>confdefs.h <<_ACEOF
+#define pg_restrict $pg_restrict
+_ACEOF
+
 
 ac_fn_c_check_type "$LINENO" "struct option" "ac_cv_type_struct_option" "#ifdef HAVE_GETOPT_H
 #include <getopt.h>
index 145197e6bd63f444b09aba5fd5f67d464b379269..d3febfe58f1a100c613d3f1c9b7f9a239c511042 100644 (file)
@@ -1687,11 +1687,19 @@ PGAC_UNION_SEMUN
 AC_CHECK_TYPES(socklen_t, [], [], [#include <sys/socket.h>])
 PGAC_STRUCT_SOCKADDR_SA_LEN
 
-# Even though restrict is in C99 and should be supported by all
-# supported compilers, this test is useful because it will prefer a
-# spelling that also works in C++ (often __restrict).  (restrict is
-# not part of the C++ standard.)
+# MSVC doesn't cope well with defining restrict to __restrict, the
+# spelling it understands, because it conflicts with
+# __declspec(restrict) in C++ mode. Therefore we define pg_restrict to
+# the appropriate definition, which presumably won't conflict.
 AC_C_RESTRICT
+if test "$ac_cv_c_restrict" = "no"; then
+  pg_restrict=""
+else
+  pg_restrict="$ac_cv_c_restrict"
+fi
+AC_DEFINE_UNQUOTED([pg_restrict], [$pg_restrict],
+[Define to keyword to use for C99 restrict support, or to nothing if not
+supported])
 
 AC_CHECK_TYPES([struct option], [], [],
 [#ifdef HAVE_GETOPT_H
index d8a701440dab5268dc910c31acddcacf809dc93e..eedd40b8137c1c6a685836295dcf2f4243819215 100644 (file)
@@ -2847,12 +2847,13 @@ int main(void)
 endforeach
 
 
-# Even though restrict is in C99 and should be supported by all
-# supported compilers, this indirection is useful because __restrict
-# also works in C++ in all supported compilers.  (If not, then we
-# might have to write a real test.)  (restrict is not part of the C++
-# standard.)
-cdata.set('restrict', '__restrict')
+# MSVC doesn't cope well with defining restrict to __restrict, the
+# spelling it understands, because it conflicts with
+# __declspec(restrict) in C++ mode.  Therefore we define pg_restrict
+# to the appropriate definition, which presumably won't conflict.
+#
+# We assume C99 support, so we don't need to make this conditional.
+cdata.set('pg_restrict', '__restrict')
 
 
 # Most libraries are included only if they demonstrably provide a function we
index 456e0375e133cf0ff236f01ffe7e0368abce1888..f9f2d457f2fb6212313517cb1cc6af1afc6481de 100644 (file)
@@ -1228,7 +1228,7 @@ parse_required_wal(verifier_context *context, char *pg_waldump_path,
  * context says we should.
  */
 void
-report_backup_error(verifier_context *context, const char *restrict fmt,...)
+report_backup_error(verifier_context *context, const char *pg_restrict fmt,...)
 {
    va_list     ap;
 
@@ -1245,7 +1245,7 @@ report_backup_error(verifier_context *context, const char *restrict fmt,...)
  * Report a fatal error and exit
  */
 void
-report_fatal_error(const char *restrict fmt,...)
+report_fatal_error(const char *pg_restrict fmt,...)
 {
    va_list     ap;
 
index 1cd76a8826bc2433ad0ad78585a78b839566b260..a99d9bfd58171623ca9b97a74f14bb4a71e585d1 100644 (file)
@@ -96,9 +96,9 @@ typedef struct verifier_context
 } verifier_context;
 
 extern void report_backup_error(verifier_context *context,
-                               const char *restrict fmt,...)
+                               const char *pg_restrict fmt,...)
            pg_attribute_printf(2, 3);
-pg_noreturn extern void report_fatal_error(const char *restrict fmt,...)
+pg_noreturn extern void report_fatal_error(const char *pg_restrict fmt,...)
            pg_attribute_printf(1, 2);
 extern bool should_ignore_relpath(verifier_context *context,
                                  const char *relpath);
index 214a87f21892202bab62ef7656344638157bd0eb..5206949e5d8c19a30a5cf5f59d0934f800e7b73c 100644 (file)
@@ -206,7 +206,7 @@ pg_logging_set_locus_callback(void (*cb) (const char **filename, uint64 *lineno)
 
 void
 pg_log_generic(enum pg_log_level level, enum pg_log_part part,
-              const char *restrict fmt,...)
+              const char *pg_restrict fmt,...)
 {
    va_list     ap;
 
@@ -217,7 +217,7 @@ pg_log_generic(enum pg_log_level level, enum pg_log_part part,
 
 void
 pg_log_generic_v(enum pg_log_level level, enum pg_log_part part,
-                const char *restrict fmt, va_list ap)
+                const char *pg_restrict fmt, va_list ap)
 {
    int         save_errno = errno;
    const char *filename = NULL;
index fff14d3facfdea777cf7801a4431adc7c2d8a6a9..41c74a1502dfb4c31e55e386c73ac2b98cecb0ca 100644 (file)
@@ -47,7 +47,7 @@ pg_str_endswith(const char *str, const char *end)
  * strtoint --- just like strtol, but returns int not long
  */
 int
-strtoint(const char *restrict str, char **restrict endptr, int base)
+strtoint(const char *pg_restrict str, char **pg_restrict endptr, int base)
 {
    long        val;
 
index 335af3352d1a32bc2fbbccad8b03345638cc6713..7136102e5ff2a900deef409845d22f4948149d19 100644 (file)
 #define inline
 #endif
 
-/*
- * Previously used PostgreSQL-specific spelling, for backward compatibility
- * for extensions.
- */
-#define pg_restrict restrict
-
 /*
  * Attribute macros
  *
index b5caec0bff1439fdfdd8b82df942893fe155f92c..bccba4ac07b51b0362f9891521c9bac5e9ca72ef 100644 (file)
@@ -93,10 +93,10 @@ void        pg_logging_set_pre_callback(void (*cb) (void));
 void       pg_logging_set_locus_callback(void (*cb) (const char **filename, uint64 *lineno));
 
 void       pg_log_generic(enum pg_log_level level, enum pg_log_part part,
-                          const char *restrict fmt,...)
+                          const char *pg_restrict fmt,...)
            pg_attribute_printf(3, 4);
 void       pg_log_generic_v(enum pg_log_level level, enum pg_log_part part,
-                            const char *restrict fmt, va_list ap)
+                            const char *pg_restrict fmt, va_list ap)
            pg_attribute_printf(3, 0);
 
 /*
index 5e1bd61c710af31abfa5e3652632c1b674bb2beb..2a7c31ea74e66d790295a6c81f06c37acb0f0c6e 100644 (file)
@@ -25,7 +25,7 @@ typedef struct PromptInterruptContext
 
 /* functions in src/common/string.c */
 extern bool pg_str_endswith(const char *str, const char *end);
-extern int strtoint(const char *restrict str, char **restrict endptr,
+extern int strtoint(const char *pg_restrict str, char **pg_restrict endptr,
                     int base);
 extern char *pg_clean_ascii(const char *str, int alloc_flags);
 extern int pg_strip_crlf(char *str);
index 911d9397f5e7f6de08e967d6d2b15ba63cda9929..bc4ab1381a917307e171641d523e7ca19d1b3590 100644 (file)
@@ -34,7 +34,7 @@ extern void pq_sendfloat8(StringInfo buf, float8 f);
  * Append a [u]int8 to a StringInfo buffer, which already has enough space
  * preallocated.
  *
- * The use of restrict allows the compiler to optimize the code based on
+ * The use of pg_restrict allows the compiler to optimize the code based on
  * the assumption that buf, buf->len, buf->data and *buf->data don't
  * overlap. Without the annotation buf->len etc cannot be kept in a register
  * over subsequent pq_writeintN calls.
@@ -43,7 +43,7 @@ extern void pq_sendfloat8(StringInfo buf, float8 f);
  * overly picky and demanding a * before a restrict.
  */
 static inline void
-pq_writeint8(StringInfoData *restrict buf, uint8 i)
+pq_writeint8(StringInfoData *pg_restrict buf, uint8 i)
 {
    uint8       ni = i;
 
@@ -57,7 +57,7 @@ pq_writeint8(StringInfoData *restrict buf, uint8 i)
  * preallocated.
  */
 static inline void
-pq_writeint16(StringInfoData *restrict buf, uint16 i)
+pq_writeint16(StringInfoData *pg_restrict buf, uint16 i)
 {
    uint16      ni = pg_hton16(i);
 
@@ -71,7 +71,7 @@ pq_writeint16(StringInfoData *restrict buf, uint16 i)
  * preallocated.
  */
 static inline void
-pq_writeint32(StringInfoData *restrict buf, uint32 i)
+pq_writeint32(StringInfoData *pg_restrict buf, uint32 i)
 {
    uint32      ni = pg_hton32(i);
 
@@ -85,7 +85,7 @@ pq_writeint32(StringInfoData *restrict buf, uint32 i)
  * preallocated.
  */
 static inline void
-pq_writeint64(StringInfoData *restrict buf, uint64 i)
+pq_writeint64(StringInfoData *pg_restrict buf, uint64 i)
 {
    uint64      ni = pg_hton64(i);
 
@@ -105,7 +105,7 @@ pq_writeint64(StringInfoData *restrict buf, uint64 i)
  * sent to the frontend.
  */
 static inline void
-pq_writestring(StringInfoData *restrict buf, const char *restrict str)
+pq_writestring(StringInfoData *pg_restrict buf, const char *pg_restrict str)
 {
    int         slen = strlen(str);
    char       *p;
index 10fa85e78c262b0c15978c15c64fca108d4c4d94..339268dc8ef44674370ff8e789a4fe5e25d1dc6f 100644 (file)
 #undef inline
 #endif
 
+/* Define to keyword to use for C99 restrict support, or to nothing if not
+   supported */
+#undef pg_restrict
+
 /* Define to the equivalent of the C99 'restrict' keyword, or to
    nothing if this is not supported.  Do not define if restrict is
    supported directly.  */