Bug and fix, reopened: "restrict" #define fails on VC
librik@panix.com
librik at panix.com
Fri Mar 31 01:16:02 CEST 2006
I'm afraid I have to report that a configure bug, fixed four
years ago in GMP 4.1, has crept back in to the latest GMP 4.2
sources. (I know this because I reported the bug at the time,
and Kevin Ryde fixed it.)
Unfortunately most of the 2002 archives have vanished from
SWOX's list-archives, but I have the report and reply here,
and have reproduced them below.
In short: creating the "#define restrict __restrict" macro in
config.h is unsafe on compilers like Visual C 2005 that support
both the "__restrict" keyword and the "declspec(restrict)" syntax
in header files. The correct solution is to use a unique macro
name like "gmp_restrict". As Kevin noted, "restrict is in the
compiler's namespace these days."
- David Librik
librik at panix.com
My original report:
>From: <librik <at> panix.com>
>Subject: Bug and Fix: GMP_C_RESTRICT fails on VC for IA-64
>Date: 2002-09-13 03:18:34 GMT
>
> BUG DESCRIPTION:
>
> The GMP_C_RESTRICT autoconf macro causes serious problems on
> Visual C++ for Win64. This compiler supports the C99 __restrict
> keyword, but also a __declspec(restrict) declaration qualifier.
> The approach used by GMP_C_RESTRICT, redefining "restrict" as
> a C preprocessor macro whose value is "__restrict", causes a
> compile error when __declspec(restrict) appears later. It would
> be better to use a unique C macro name such as "gmp_restrict".
>
>
> BUG EXPLANATION:
>
> VC for Win64 uses the __restrict keyword to specify restricted
> pointers. Configure correctly detects this, and then puts the
> following line in config.h:
> #define restrict __restrict
> so that GMP programmers could use the bare "restrict" keyword.
>
> VC for Win64 also has the __declspec(restrict) syntax, used to
> specify that a function returns a restricted pointer. For
> instance, the declaration for malloc() in the standard compiler
> headers is:
> __declspec(noalias) __declspec(restrict) void * __cdecl malloc(size_t);
> But the config.h macro above causes the "restrict" to be rewritten
> as "__restrict", when a standard header file is #included after config.h.
> "__declspec(__restrict)" is illegal syntax.
>
>
> BUG FIX:
>
> The correct solution is to use a unique macro name, like
> GMP_RESTRICT, rather than the common keyword "restrict."
> This is easy to do, because restrict is not actually used
> anywhere in GMP 4.1. (It is in tasks.html as a "bright idea"
> for future development.)
>
> I belive this fix can be applied by changing aclocal.m4,
> GMP_C_RESTRICT macro, to remove the lines:
> if test $gmp_cv_c_restrict = restrict; then
> gmp_cv_c_restrict=yes
> fi
> and, in the following case statement, removing
> restrict | yes) ;;
> and changing the word "restrict" to, e.g., "GMP_RESTRICT" in
> the two AC_DEFINE()s -- but since I don't actually speak M4,
> I'll turn it over to you...
Kevin's reply:
>From: Kevin Ryde <user42 <at> zip.com.au>
>Subject: Re: Bug and Fix: GMP_C_RESTRICT fails on VC for IA-64
>Date: 2002-09-13 22:29:09 GMT
>
> > #define restrict __restrict
> > so that GMP programmers could use the bare "restrict" keyword.
>
> Yes.
>
> > the declaration for malloc() in the standard compiler
> > headers is:
> > __declspec(noalias) __declspec(restrict) void * __cdecl malloc(size_t);
>
> Grr. I guess "restrict" is in the compiler's namespace these days.
>
> > The correct solution is to use a unique macro name, like
> > GMP_RESTRICT, rather than the common keyword "restrict."
>
> Thanks, yes. For now I think I'll remove the whole GMP_C_RESTRICT
> test, until we start making use of it.
(I haven't seen much from Kevin in the last couple of years.
I hope he is doing well.)
More information about the gmp-bugs
mailing list