Skip to content

Commit 91d5f1a

Browse files
committed
Use C99 restrict via pg_restrict, rather than restrict directly.
Unfortunately using 'restrict' plainly causes problems with MSVC, which supports restrict only as '__restrict'. Defining 'restrict' to '__restrict' unfortunately causes a conflict with MSVC's usage of __declspec(restrict) in headers. Therefore define pg_restrict to the appropriate keyword instead, and replace existing usages. This replaces the temporary workaround introduced in 36b4b91. Author: Andres Freund Discussion: https://postgr.es/m/2656.1507830907@sss.pgh.pa.us
1 parent 1c497fa commit 91d5f1a

File tree

5 files changed

+101
-71
lines changed

5 files changed

+101
-71
lines changed

configure

Lines changed: 61 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11545,52 +11545,6 @@ _ACEOF
1154511545
;;
1154611546
esac
1154711547

11548-
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for C/C++ restrict keyword" >&5
11549-
$as_echo_n "checking for C/C++ restrict keyword... " >&6; }
11550-
if ${ac_cv_c_restrict+:} false; then :
11551-
$as_echo_n "(cached) " >&6
11552-
else
11553-
ac_cv_c_restrict=no
11554-
# The order here caters to the fact that C++ does not require restrict.
11555-
for ac_kw in __restrict __restrict__ _Restrict restrict; do
11556-
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
11557-
/* end confdefs.h. */
11558-
typedef int * int_ptr;
11559-
int foo (int_ptr $ac_kw ip) {
11560-
return ip[0];
11561-
}
11562-
int
11563-
main ()
11564-
{
11565-
int s[1];
11566-
int * $ac_kw t = s;
11567-
t[0] = 0;
11568-
return foo(t)
11569-
;
11570-
return 0;
11571-
}
11572-
_ACEOF
11573-
if ac_fn_c_try_compile "$LINENO"; then :
11574-
ac_cv_c_restrict=$ac_kw
11575-
fi
11576-
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
11577-
test "$ac_cv_c_restrict" != no && break
11578-
done
11579-
11580-
fi
11581-
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_c_restrict" >&5
11582-
$as_echo "$ac_cv_c_restrict" >&6; }
11583-
11584-
case $ac_cv_c_restrict in
11585-
restrict) ;;
11586-
no) $as_echo "#define restrict /**/" >>confdefs.h
11587-
;;
11588-
*) cat >>confdefs.h <<_ACEOF
11589-
#define restrict $ac_cv_c_restrict
11590-
_ACEOF
11591-
;;
11592-
esac
11593-
1159411548
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for printf format archetype" >&5
1159511549
$as_echo_n "checking for printf format archetype... " >&6; }
1159611550
if ${pgac_cv_printf_archetype+:} false; then :
@@ -12508,6 +12462,67 @@ $as_echo "#define LOCALE_T_IN_XLOCALE 1" >>confdefs.h
1250812462

1250912463
fi
1251012464

12465+
# MSVC doesn't cope well with defining restrict to __restrict, the
12466+
# spelling it understands, because it conflicts with
12467+
# __declspec(restrict). Therefore we define pg_restrict to the
12468+
# appropriate definition, which presumably won't conflict.
12469+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for C/C++ restrict keyword" >&5
12470+
$as_echo_n "checking for C/C++ restrict keyword... " >&6; }
12471+
if ${ac_cv_c_restrict+:} false; then :
12472+
$as_echo_n "(cached) " >&6
12473+
else
12474+
ac_cv_c_restrict=no
12475+
# The order here caters to the fact that C++ does not require restrict.
12476+
for ac_kw in __restrict __restrict__ _Restrict restrict; do
12477+
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
12478+
/* end confdefs.h. */
12479+
typedef int * int_ptr;
12480+
int foo (int_ptr $ac_kw ip) {
12481+
return ip[0];
12482+
}
12483+
int
12484+
main ()
12485+
{
12486+
int s[1];
12487+
int * $ac_kw t = s;
12488+
t[0] = 0;
12489+
return foo(t)
12490+
;
12491+
return 0;
12492+
}
12493+
_ACEOF
12494+
if ac_fn_c_try_compile "$LINENO"; then :
12495+
ac_cv_c_restrict=$ac_kw
12496+
fi
12497+
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
12498+
test "$ac_cv_c_restrict" != no && break
12499+
done
12500+
12501+
fi
12502+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_c_restrict" >&5
12503+
$as_echo "$ac_cv_c_restrict" >&6; }
12504+
12505+
case $ac_cv_c_restrict in
12506+
restrict) ;;
12507+
no) $as_echo "#define restrict /**/" >>confdefs.h
12508+
;;
12509+
*) cat >>confdefs.h <<_ACEOF
12510+
#define restrict $ac_cv_c_restrict
12511+
_ACEOF
12512+
;;
12513+
esac
12514+
12515+
if test "$ac_cv_c_restrict" = "no" ; then
12516+
pg_restrict=""
12517+
else
12518+
pg_restrict="$ac_cv_c_restrict"
12519+
fi
12520+
12521+
cat >>confdefs.h <<_ACEOF
12522+
#define pg_restrict $pg_restrict
12523+
_ACEOF
12524+
12525+
1251112526
ac_fn_c_check_type "$LINENO" "struct cmsgcred" "ac_cv_type_struct_cmsgcred" "#include <sys/socket.h>
1251212527
#include <sys/param.h>
1251312528
#ifdef HAVE_SYS_UCRED_H

configure.in

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1299,7 +1299,6 @@ fi
12991299
m4_defun([AC_PROG_CC_STDC], []) dnl We don't want that.
13001300
AC_C_BIGENDIAN
13011301
AC_C_INLINE
1302-
AC_C_RESTRICT
13031302
PGAC_PRINTF_ARCHETYPE
13041303
AC_C_FLEXIBLE_ARRAY_MEMBER
13051304
PGAC_C_SIGNED
@@ -1326,6 +1325,20 @@ AC_TYPE_LONG_LONG_INT
13261325

13271326
PGAC_TYPE_LOCALE_T
13281327

1328+
# MSVC doesn't cope well with defining restrict to __restrict, the
1329+
# spelling it understands, because it conflicts with
1330+
# __declspec(restrict). Therefore we define pg_restrict to the
1331+
# appropriate definition, which presumably won't conflict.
1332+
AC_C_RESTRICT
1333+
if test "$ac_cv_c_restrict" = "no" ; then
1334+
pg_restrict=""
1335+
else
1336+
pg_restrict="$ac_cv_c_restrict"
1337+
fi
1338+
AC_DEFINE_UNQUOTED([pg_restrict], [$pg_restrict],
1339+
[Define to keyword to use for C99 restrict support, or to nothing if not
1340+
supported])
1341+
13291342
AC_CHECK_TYPES([struct cmsgcred], [], [],
13301343
[#include <sys/socket.h>
13311344
#include <sys/param.h>

src/include/libpq/pqformat.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,21 @@ extern void pq_sendfloat8(StringInfo buf, float8 f);
3838
* Append a int8 to a StringInfo buffer, which already has enough space
3939
* preallocated.
4040
*
41-
* The use of restrict allows the compiler to optimize the code based on the
42-
* assumption that buf, buf->len, buf->data and *buf->data don't
41+
* The use of pg_restrict allows the compiler to optimize the code based on
42+
* the assumption that buf, buf->len, buf->data and *buf->data don't
4343
* overlap. Without the annotation buf->len etc cannot be kept in a register
4444
* over subsequent pq_writeint* calls.
4545
*
4646
* The use of StringInfoData * rather than StringInfo is due to MSVC being
4747
* overly picky and demanding a * before a restrict.
4848
*/
4949
static inline void
50-
pq_writeint8(StringInfoData * restrict buf, int8 i)
50+
pq_writeint8(StringInfoData *pg_restrict buf, int8 i)
5151
{
5252
int8 ni = i;
5353

5454
Assert(buf->len + sizeof(i) <= buf->maxlen);
55-
memcpy((char *restrict) (buf->data + buf->len), &ni, sizeof(ni));
55+
memcpy((char *pg_restrict) (buf->data + buf->len), &ni, sizeof(ni));
5656
buf->len += sizeof(i);
5757
}
5858

@@ -61,12 +61,12 @@ pq_writeint8(StringInfoData * restrict buf, int8 i)
6161
* preallocated.
6262
*/
6363
static inline void
64-
pq_writeint16(StringInfoData * restrict buf, int16 i)
64+
pq_writeint16(StringInfoData *pg_restrict buf, int16 i)
6565
{
6666
int16 ni = pg_hton16(i);
6767

6868
Assert(buf->len + sizeof(ni) <= buf->maxlen);
69-
memcpy((char *restrict) (buf->data + buf->len), &ni, sizeof(i));
69+
memcpy((char *pg_restrict) (buf->data + buf->len), &ni, sizeof(i));
7070
buf->len += sizeof(i);
7171
}
7272

@@ -75,12 +75,12 @@ pq_writeint16(StringInfoData * restrict buf, int16 i)
7575
* preallocated.
7676
*/
7777
static inline void
78-
pq_writeint32(StringInfoData * restrict buf, int32 i)
78+
pq_writeint32(StringInfoData *pg_restrict buf, int32 i)
7979
{
8080
int32 ni = pg_hton32(i);
8181

8282
Assert(buf->len + sizeof(i) <= buf->maxlen);
83-
memcpy((char *restrict) (buf->data + buf->len), &ni, sizeof(i));
83+
memcpy((char *pg_restrict) (buf->data + buf->len), &ni, sizeof(i));
8484
buf->len += sizeof(i);
8585
}
8686

@@ -89,12 +89,12 @@ pq_writeint32(StringInfoData * restrict buf, int32 i)
8989
* preallocated.
9090
*/
9191
static inline void
92-
pq_writeint64(StringInfoData * restrict buf, int64 i)
92+
pq_writeint64(StringInfoData *pg_restrict buf, int64 i)
9393
{
9494
int64 ni = pg_hton64(i);
9595

9696
Assert(buf->len + sizeof(i) <= buf->maxlen);
97-
memcpy((char *restrict) (buf->data + buf->len), &ni, sizeof(i));
97+
memcpy((char *pg_restrict) (buf->data + buf->len), &ni, sizeof(i));
9898
buf->len += sizeof(i);
9999
}
100100

@@ -109,7 +109,7 @@ pq_writeint64(StringInfoData * restrict buf, int64 i)
109109
* sent to the frontend.
110110
*/
111111
static inline void
112-
pq_writestring(StringInfoData * restrict buf, const char *restrict str)
112+
pq_writestring(StringInfoData *pg_restrict buf, const char *pg_restrict str)
113113
{
114114
int slen = strlen(str);
115115
char *p;
@@ -120,7 +120,7 @@ pq_writestring(StringInfoData * restrict buf, const char *restrict str)
120120

121121
Assert(buf->len + slen + 1 <= buf->maxlen);
122122

123-
memcpy(((char *restrict) buf->data + buf->len), p, slen + 1);
123+
memcpy(((char *pg_restrict) buf->data + buf->len), p, slen + 1);
124124
buf->len += slen + 1;
125125

126126
if (p != str)

src/include/pg_config.h.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,10 @@
923923
if such a type exists, and if the system does not define it. */
924924
#undef intptr_t
925925

926+
/* Define to keyword to use for C99 restrict support, or to nothing if not
927+
supported */
928+
#undef pg_restrict
929+
926930
/* Define to the equivalent of the C99 'restrict' keyword, or to
927931
nothing if this is not supported. Do not define if restrict is
928932
supported directly. */

src/include/pg_config.h.win32

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -681,22 +681,20 @@
681681
#define inline __inline
682682
#endif
683683

684-
/* Define to the equivalent of the C99 'restrict' keyword, or to
685-
nothing if this is not supported. Do not define if restrict is
686-
supported directly. */
687-
/* Visual Studio 2008 and upwards */
684+
/* Define to keyword to use for C99 restrict support, or to nothing if this is
685+
not supported */
686+
/* Works for C and C++ in Visual Studio 2008 and upwards */
688687
#if (_MSC_VER >= 1500)
689-
/* works for C and C++ in msvc */
690-
/*
691-
* Temporary attempt at a workaround for stdlib.h's use of
692-
* declspec(restrict), conflicting with below define.
693-
*/
694-
#include <stdlib.h>
695-
#define restrict __restrict
688+
#define pg_restrict __restrict
696689
#else
697-
#define restrict
690+
#define pg_restrict
698691
#endif
699692

693+
/* Define to the equivalent of the C99 'restrict' keyword, or to
694+
nothing if this is not supported. Do not define if restrict is
695+
supported directly. */
696+
/* not defined, because it'd conflict with __declspec(restrict) */
697+
700698
/* Define to empty if the C compiler does not understand signed types. */
701699
/* #undef signed */
702700

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy