From 5f04866f4c867e67b1c5573b86a7e20ef890e535 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Mon, 8 Mar 2010 01:45:46 +0100 Subject: [PATCH] time: Avoid #define replacements in C++ mode. --- ChangeLog | 14 ++++++ lib/time.in.h | 125 ++++++++++++++++++++++++++++++++++++++++-------------- m4/time_h.m4 | 35 +++++++++------ modules/mktime | 1 + modules/nanosleep | 1 + modules/strptime | 1 + modules/time | 26 ++++++++---- modules/timegm | 1 + 8 files changed, 151 insertions(+), 53 deletions(-) diff --git a/ChangeLog b/ChangeLog index e99715c8f..def832c0b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,19 @@ 2010-03-07 Bruno Haible + time: Avoid #define replacements in C++ mode. + * lib/time.in.h: Include c++defs.h, warn-on-use.h. + (nanosleep, mktime, localtime_r, gmtime_r, strptime, timegm): In C++, + define a namespaced alias symbol. + * m4/time_h.m4 (gl_TIME_MODULE_INDICATOR): New macro. + (gl_HEADER_TIME_H_DEFAULTS): Initialize also GNULIB_MKTIME, + GNULIB_NANOSLEEP, GNULIB_STRPTIME, GNULIB_TIMEGM. + * modules/time (Depends-on): Add c++defs, warn-on-use. + (Makefile.am): Update time.h rule. + * modules/mktime (configure.ac): Invoke gl_TIME_MODULE_INDICATOR. + * modules/nanosleep (configure.ac): Likewise. + * modules/strptime (configure.ac): Likewise. + * modules/timegm (configure.ac): Likewise. + sys_time: Avoid #define replacements in C++ mode. * lib/sys_time.in.h: Include c++defs.h. (gettimeofday): In C++, define a namespaced alias symbol. diff --git a/lib/time.in.h b/lib/time.in.h index 984b0b323..d2cf84be6 100644 --- a/lib/time.in.h +++ b/lib/time.in.h @@ -40,8 +40,12 @@ /* NetBSD 5.0 mis-defines NULL. */ #include +/* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */ + /* The definition of _GL_ARG_NONNULL is copied here. */ +/* The definition of _GL_WARN_ON_USE is copied here. */ + # ifdef __cplusplus extern "C" { # endif @@ -63,54 +67,115 @@ struct timespec # endif # endif +# ifdef __cplusplus +} +# endif + /* Sleep for at least RQTP seconds unless interrupted, If interrupted, return -1 and store the remaining time into RMTP. See . */ -# if @REPLACE_NANOSLEEP@ -# define nanosleep rpl_nanosleep -extern int nanosleep (struct timespec const *__rqtp, struct timespec *__rmtp) - _GL_ARG_NONNULL ((1)); +# if @GNULIB_NANOSLEEP@ +# if @REPLACE_NANOSLEEP@ +# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# define nanosleep rpl_nanosleep +# endif +_GL_FUNCDECL_RPL (nanosleep, int, + (struct timespec const *__rqtp, struct timespec *__rmtp) + _GL_ARG_NONNULL ((1))); +_GL_CXXALIAS_RPL (nanosleep, int, + (struct timespec const *__rqtp, struct timespec *__rmtp)); +# else +_GL_CXXALIAS_SYS (nanosleep, int, + (struct timespec const *__rqtp, struct timespec *__rmtp)); +# endif +_GL_CXXALIASWARN (nanosleep); # endif /* Return the 'time_t' representation of TP and normalize TP. */ -# if @REPLACE_MKTIME@ -# define mktime rpl_mktime -extern time_t mktime (struct tm *__tp) _GL_ARG_NONNULL ((1)); +# if @GNULIB_MKTIME@ +# if @REPLACE_MKTIME@ +# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# define mktime rpl_mktime +# endif +_GL_FUNCDECL_RPL (mktime, time_t, (struct tm *__tp) _GL_ARG_NONNULL ((1))); +_GL_CXXALIAS_RPL (mktime, time_t, (struct tm *__tp)); +# else +_GL_CXXALIAS_SYS (mktime, time_t, (struct tm *__tp)); +# endif +_GL_CXXALIASWARN (mktime); # endif /* Convert TIMER to RESULT, assuming local time and UTC respectively. See and . */ # if @REPLACE_LOCALTIME_R@ -# undef localtime_r -# define localtime_r rpl_localtime_r -# undef gmtime_r -# define gmtime_r rpl_gmtime_r -extern struct tm *localtime_r (time_t const *restrict __timer, - struct tm *restrict __result) - _GL_ARG_NONNULL ((1, 2)); -extern struct tm *gmtime_r (time_t const *restrict __timer, - struct tm *restrict __result) - _GL_ARG_NONNULL ((1, 2)); +# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# undef localtime_r +# define localtime_r rpl_localtime_r +# endif +_GL_FUNCDECL_RPL (localtime_r, struct tm *, (time_t const *restrict __timer, + struct tm *restrict __result) + _GL_ARG_NONNULL ((1, 2))); +_GL_CXXALIAS_RPL (localtime_r, struct tm *, (time_t const *restrict __timer, + struct tm *restrict __result)); +# else +_GL_CXXALIAS_SYS (localtime_r, struct tm *, (time_t const *restrict __timer, + struct tm *restrict __result)); +# endif +_GL_CXXALIASWARN (localtime_r); +# if @REPLACE_LOCALTIME_R@ +# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# undef gmtime_r +# define gmtime_r rpl_gmtime_r +# endif +_GL_FUNCDECL_RPL (gmtime_r, struct tm *, (time_t const *restrict __timer, + struct tm *restrict __result) + _GL_ARG_NONNULL ((1, 2))); +_GL_CXXALIAS_RPL (gmtime_r, struct tm *, (time_t const *restrict __timer, + struct tm *restrict __result)); +# else +_GL_CXXALIAS_SYS (gmtime_r, struct tm *, (time_t const *restrict __timer, + struct tm *restrict __result)); # endif +_GL_CXXALIASWARN (gmtime_r); /* Parse BUF as a time stamp, assuming FORMAT specifies its layout, and store the resulting broken-down time into TM. See . */ -# if @REPLACE_STRPTIME@ -# undef strptime -# define strptime rpl_strptime -extern char *strptime (char const *restrict __buf, - char const *restrict __format, - struct tm *restrict __tm) - _GL_ARG_NONNULL ((1, 2, 3)); +# if @GNULIB_STRPTIME@ +# if @REPLACE_STRPTIME@ +# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# undef strptime +# define strptime rpl_strptime +# endif +_GL_FUNCDECL_RPL (strptime, char *, (char const *restrict __buf, + char const *restrict __format, + struct tm *restrict __tm) + _GL_ARG_NONNULL ((1, 2, 3))); +_GL_CXXALIAS_RPL (strptime, char *, (char const *restrict __buf, + char const *restrict __format, + struct tm *restrict __tm)); +# else +_GL_CXXALIAS_SYS (strptime, char *, (char const *restrict __buf, + char const *restrict __format, + struct tm *restrict __tm)); +# endif +_GL_CXXALIASWARN (strptime); # endif /* Convert TM to a time_t value, assuming UTC. */ -# if @REPLACE_TIMEGM@ -# undef timegm -# define timegm rpl_timegm -extern time_t timegm (struct tm *__tm) _GL_ARG_NONNULL ((1)); +# if @GNULIB_TIMEGM@ +# if @REPLACE_TIMEGM@ +# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# undef timegm +# define timegm rpl_timegm +# endif +_GL_FUNCDECL_RPL (timegm, time_t, (struct tm *__tm) _GL_ARG_NONNULL ((1))); +_GL_CXXALIAS_RPL (timegm, time_t, (struct tm *__tm)); +# else +_GL_CXXALIAS_SYS (timegm, time_t, (struct tm *__tm)); +# endif +_GL_CXXALIASWARN (timegm); # endif /* Encourage applications to avoid unsafe functions that can overrun @@ -127,8 +192,4 @@ extern time_t timegm (struct tm *__tm) _GL_ARG_NONNULL ((1)); # define ctime_r eschew_ctime_r # endif -# ifdef __cplusplus -} -# endif - #endif diff --git a/m4/time_h.m4 b/m4/time_h.m4 index c00bfae92..4bc266931 100644 --- a/m4/time_h.m4 +++ b/m4/time_h.m4 @@ -23,18 +23,6 @@ AC_DEFUN([gl_HEADER_TIME_H_BODY], AC_REQUIRE([gl_CHECK_TYPE_STRUCT_TIMESPEC]) ]) -AC_DEFUN([gl_HEADER_TIME_H_DEFAULTS], -[ - dnl If another module says to replace or to not replace, do that. - dnl Otherwise, replace only if someone compiles with -DGNULIB_PORTCHECK; - dnl this lets maintainers check for portability. - REPLACE_LOCALTIME_R=GNULIB_PORTCHECK; AC_SUBST([REPLACE_LOCALTIME_R]) - REPLACE_MKTIME=GNULIB_PORTCHECK; AC_SUBST([REPLACE_MKTIME]) - REPLACE_NANOSLEEP=GNULIB_PORTCHECK; AC_SUBST([REPLACE_NANOSLEEP]) - REPLACE_STRPTIME=GNULIB_PORTCHECK; AC_SUBST([REPLACE_STRPTIME]) - REPLACE_TIMEGM=GNULIB_PORTCHECK; AC_SUBST([REPLACE_TIMEGM]) -]) - dnl Define HAVE_STRUCT_TIMESPEC if `struct timespec' is declared dnl in time.h or sys/time.h. @@ -72,3 +60,26 @@ AC_DEFUN([gl_CHECK_TYPE_STRUCT_TIMESPEC], AC_SUBST([TIME_H_DEFINES_STRUCT_TIMESPEC]) AC_SUBST([SYS_TIME_H_DEFINES_STRUCT_TIMESPEC]) ]) + +AC_DEFUN([gl_TIME_MODULE_INDICATOR], +[ + dnl Use AC_REQUIRE here, so that the default settings are expanded once only. + AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS]) + GNULIB_[]m4_translit([$1],[abcdefghijklmnopqrstuvwxyz./-],[ABCDEFGHIJKLMNOPQRSTUVWXYZ___])=1 +]) + +AC_DEFUN([gl_HEADER_TIME_H_DEFAULTS], +[ + GNULIB_MKTIME=0; AC_SUBST([GNULIB_MKTIME]) + GNULIB_NANOSLEEP=0; AC_SUBST([GNULIB_NANOSLEEP]) + GNULIB_STRPTIME=0; AC_SUBST([GNULIB_STRPTIME]) + GNULIB_TIMEGM=0; AC_SUBST([GNULIB_TIMEGM]) + dnl If another module says to replace or to not replace, do that. + dnl Otherwise, replace only if someone compiles with -DGNULIB_PORTCHECK; + dnl this lets maintainers check for portability. + REPLACE_LOCALTIME_R=GNULIB_PORTCHECK; AC_SUBST([REPLACE_LOCALTIME_R]) + REPLACE_MKTIME=GNULIB_PORTCHECK; AC_SUBST([REPLACE_MKTIME]) + REPLACE_NANOSLEEP=GNULIB_PORTCHECK; AC_SUBST([REPLACE_NANOSLEEP]) + REPLACE_STRPTIME=GNULIB_PORTCHECK; AC_SUBST([REPLACE_STRPTIME]) + REPLACE_TIMEGM=GNULIB_PORTCHECK; AC_SUBST([REPLACE_TIMEGM]) +]) diff --git a/modules/mktime b/modules/mktime index 037f4e475..d5b0ee795 100644 --- a/modules/mktime +++ b/modules/mktime @@ -12,6 +12,7 @@ time_r configure.ac: gl_FUNC_MKTIME +gl_TIME_MODULE_INDICATOR([mktime]) Makefile.am: diff --git a/modules/nanosleep b/modules/nanosleep index d457b6f04..dfe1c7acb 100644 --- a/modules/nanosleep +++ b/modules/nanosleep @@ -19,6 +19,7 @@ verify configure.ac: gl_FUNC_NANOSLEEP +gl_TIME_MODULE_INDICATOR([nanosleep]) Makefile.am: diff --git a/modules/strptime b/modules/strptime index 74570f5a8..667d55245 100644 --- a/modules/strptime +++ b/modules/strptime @@ -17,6 +17,7 @@ time_r configure.ac: gl_FUNC_STRPTIME +gl_TIME_MODULE_INDICATOR([strptime]) Makefile.am: diff --git a/modules/time b/modules/time index ed89cf9bd..02c7a20b2 100644 --- a/modules/time +++ b/modules/time @@ -9,6 +9,8 @@ Depends-on: extensions include_next arg-nonnull +c++defs +warn-on-use stddef configure.ac: @@ -19,20 +21,26 @@ BUILT_SOURCES += time.h # We need the following in order to create when the system # doesn't have one that works with the given compiler. -time.h: time.in.h $(ARG_NONNULL_H) +time.h: time.in.h $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H) $(AM_V_GEN)rm -f $@-t $@ && \ { echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \ sed -e 's|@''INCLUDE_NEXT''@|$(INCLUDE_NEXT)|g' \ -e 's|@''PRAGMA_SYSTEM_HEADER''@|@PRAGMA_SYSTEM_HEADER@|g' \ - -e 's|@NEXT_TIME_H''@|$(NEXT_TIME_H)|g' \ - -e 's|@REPLACE_LOCALTIME_R''@|$(REPLACE_LOCALTIME_R)|g' \ - -e 's|@REPLACE_MKTIME''@|$(REPLACE_MKTIME)|g' \ - -e 's|@REPLACE_NANOSLEEP''@|$(REPLACE_NANOSLEEP)|g' \ - -e 's|@REPLACE_STRPTIME''@|$(REPLACE_STRPTIME)|g' \ - -e 's|@REPLACE_TIMEGM''@|$(REPLACE_TIMEGM)|g' \ - -e 's|@SYS_TIME_H_DEFINES_STRUCT_TIMESPEC''@|$(SYS_TIME_H_DEFINES_STRUCT_TIMESPEC)|g' \ - -e 's|@TIME_H_DEFINES_STRUCT_TIMESPEC''@|$(TIME_H_DEFINES_STRUCT_TIMESPEC)|g' \ + -e 's|@''NEXT_TIME_H''@|$(NEXT_TIME_H)|g' \ + -e 's|@''GNULIB_MKTIME''@|$(GNULIB_MKTIME)|g' \ + -e 's|@''GNULIB_NANOSLEEP''@|$(GNULIB_NANOSLEEP)|g' \ + -e 's|@''GNULIB_STRPTIME''@|$(GNULIB_STRPTIME)|g' \ + -e 's|@''GNULIB_TIMEGM''@|$(GNULIB_TIMEGM)|g' \ + -e 's|@''REPLACE_LOCALTIME_R''@|$(REPLACE_LOCALTIME_R)|g' \ + -e 's|@''REPLACE_MKTIME''@|$(REPLACE_MKTIME)|g' \ + -e 's|@''REPLACE_NANOSLEEP''@|$(REPLACE_NANOSLEEP)|g' \ + -e 's|@''REPLACE_STRPTIME''@|$(REPLACE_STRPTIME)|g' \ + -e 's|@''REPLACE_TIMEGM''@|$(REPLACE_TIMEGM)|g' \ + -e 's|@''SYS_TIME_H_DEFINES_STRUCT_TIMESPEC''@|$(SYS_TIME_H_DEFINES_STRUCT_TIMESPEC)|g' \ + -e 's|@''TIME_H_DEFINES_STRUCT_TIMESPEC''@|$(TIME_H_DEFINES_STRUCT_TIMESPEC)|g' \ + -e '/definitions of _GL_FUNCDECL_RPL/r $(CXXDEFS_H)' \ -e '/definition of _GL_ARG_NONNULL/r $(ARG_NONNULL_H)' \ + -e '/definition of _GL_WARN_ON_USE/r $(WARN_ON_USE_H)' \ < $(srcdir)/time.in.h; \ } > $@-t && \ mv $@-t $@ diff --git a/modules/timegm b/modules/timegm index f1de72643..91623cf75 100644 --- a/modules/timegm +++ b/modules/timegm @@ -13,6 +13,7 @@ time_r configure.ac: gl_FUNC_TIMEGM +gl_TIME_MODULE_INDICATOR([timegm]) Makefile.am: -- 2.11.0