bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] C++: "#define timeval rpl_timeval" -> typedef in GNULIB_NAMESPAC


From: Pedro Alves
Subject: [PATCH] C++: "#define timeval rpl_timeval" -> typedef in GNULIB_NAMESPACE
Date: Tue, 15 Nov 2016 02:36:00 +0000

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  <address@hidden>

        * 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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]