Discussion:
[PATCH] C++: "#define timeval rpl_timeval" -> typedef in GNULIB_NAMESPACE
(too old to reply)
Pedro Alves
2016-11-15 02:36:00 UTC
Permalink
Raw Message
Working on making GDB use GNULIB_NAMESPACE, I got this:

../../src/gdb/remote-fileio.c: In function 'void remote_fileio_func_fstat(char*)':
../../src/gdb/remote-fileio.c:958:43: error: no match for call to '(const gnulib::_gl_gettimeofday_wrapper) (timeval*, NULL)'
if (!gnulib::gettimeofday (&tv, NULL))
^
../../src/gdb/remote-fileio.c:958:43: note: candidate: gnulib::_gl_gettimeofday_wrapper::type {aka int (*)(rpl_timeval*, void*)} <conversion>
../../src/gdb/remote-fileio.c:958:43: note: candidate expects 3 arguments, 3 provided
../../src/gdb/remote-fileio.c: In function 'void remote_fileio_func_gettimeofday(char*)':

The problem above is that gnulib::gettimeofday was passed a "struct
timeval" pointer, instead of a rpl_timeval (gnulib's replacement
struct) pointer.

This happens because of this in a wrapper header for sys/time.h that
GDB has:

/* On MinGW-w64, gnulib's sys/time.h replaces 'struct timeval' and
gettimeofday with versions that support 64-bit time_t, for POSIX
compliance. However, the gettimeofday replacement does not ever
return time_t values larger than 31-bit, as it simply returns the
system's gettimeofday's (signed) 32-bit result as (signed) 64-bit.
Because we don't really need the POSIX compliance, and it ends up
causing conflicts with other libraries we use that don't use gnulib
and thus work with the native struct timeval, such as Winsock2's
native 'select' and libiberty, simply undefine away gnulib's
replacements. */
#if GNULIB_defined_struct_timeval
# undef timeval
# undef gettimeofday
#endif

With GNULIB_NAMESPACE, we can do better: Make gnulib define
GNULIB_NAMESPACE::timeval as a typedef to whatever "struct timeval" is
(which is either the system's timeval or gnulib's rpl_timeval
replacement), and #undef away the "#define timeval rpl_timeval".

With this, you now write:

gnulib::timeval tv;
gnulib::gettimeofday (&tv, NULL);

Instead of:

struct timeval tv;
gnulib::gettimeofday (&tv, NULL);

And now "struct timeval" refers to the system's timeval struct. (That
GDB wrapper for sys/time.h can then be removed, as having no effect.)

Tested with
./gnulib-tool --test --with-c++-tests cond gettimeofday cond-tests getrusage-tests gettime nonblocking-pipe-tests nonblocking-socket-tests select-tests tempname time tzset
against gcc 4.7, 4.8, 4.9, 5.3, and 7/trunk on Fedora 23.

2016-11-15 Pedro Alves <***@redhat.com>

* lib/sys_time.in.h [__cplusplus && defined GNULIB_NAMESPACE]:
Define "timeval" in the GNULIB_NAMESPACE namespace, and #undef any
timeval macro.
---
lib/sys_time.in.h | 7 +++++++
1 file changed, 7 insertions(+)

diff --git a/lib/sys_time.in.h b/lib/sys_time.in.h
index 4dc0cc4..d3adf58 100644
--- a/lib/sys_time.in.h
+++ b/lib/sys_time.in.h
@@ -109,6 +109,13 @@ _GL_CXXALIAS_SYS_CAST (gettimeofday, int,
(struct timeval *restrict, void *restrict));
# endif
_GL_CXXALIASWARN (gettimeofday);
+# if defined __cplusplus && defined GNULIB_NAMESPACE
+namespace GNULIB_NAMESPACE {
+ typedef ::timeval
+#undef timeval
+ timeval;
+}
+# endif
#elif defined GNULIB_POSIXCHECK
# undef gettimeofday
# if HAVE_RAW_DECL_GETTIMEOFDAY
--
2.5.5
Paul Eggert
2016-11-15 17:24:52 UTC
Permalink
Raw Message
Thanks again. I installed the attached.
Bruno Haible
2017-05-05 22:19:25 UTC
Permalink
Raw Message
Post by Pedro Alves
With GNULIB_NAMESPACE, we can do better: Make gnulib define
GNULIB_NAMESPACE::timeval as a typedef to whatever "struct timeval" is
(which is either the system's timeval or gnulib's rpl_timeval
replacement), and #undef away the "#define timeval rpl_timeval".
gnulib::timeval tv;
gnulib::gettimeofday (&tv, NULL);
struct timeval tv;
gnulib::gettimeofday (&tv, NULL);
And now "struct timeval" refers to the system's timeval struct. (That
GDB wrapper for sys/time.h can then be removed, as having no effect.)
However, we (now? still?) have a compilation error on Mac OS X of the
signature tests of a C++ testdir:
$ ./gnulib-tool --create-testdir --dir=/tmp/testdir-posix --with-tests --with-c++-tests --single-configure `./posix-modules`

Namely:

In file included from test-fcntl-h-c++.cc:22:
In file included from ../gllib/fcntl.h:67:
In file included from ../gllib/unistd.h:40:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/unistd.h:638:
../gllib/sys/select.h:728:27: error: elaborated type refers to a typedef
struct timeval *restrict));
^
../gllib/sys/time.h:568:5: note: declared here
timeval;
^

In file included from test-stdlib-c++.cc:22:
In file included from ../gllib/stdlib.h:96:
In file included from ../gllib/unistd.h:40:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/unistd.h:638:
../gllib/sys/select.h:728:27: error: elaborated type refers to a typedef
struct timeval *restrict));
^
../gllib/sys/time.h:568:5: note: declared here
timeval;
^

In file included from test-sys_ioctl-c++.cc:22:
In file included from ../gllib/sys/ioctl.h:37:
In file included from ../gllib/unistd.h:40:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/unistd.h:638:
../gllib/sys/select.h:728:27: error: elaborated type refers to a typedef
struct timeval *restrict));
^
../gllib/sys/time.h:568:5: note: declared here
timeval;
^

In file included from test-sys_select-c++.cc:22:
../gllib/sys/select.h:728:27: error: elaborated type refers to a typedef
struct timeval *restrict));
^
../gllib/sys/time.h:568:5: note: declared here
timeval;
^

In file included from test-unistd-c++.cc:22:
In file included from ../gllib/unistd.h:40:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/unistd.h:638:
../gllib/sys/select.h:728:27: error: elaborated type refers to a typedef
struct timeval *restrict));
^
../gllib/sys/time.h:568:5: note: declared here
timeval;
^

Any ideas how to fix this?

Bruno

Loading...