Like all other Gnulib header files, stat-time.h is supposed to be usable
in C++ mode. But it isn't: there are compilation errors on MSVC:
../../gltests/../gllib\stat-time.h(127): error C2059: syntax error: '.'
../../gltests/../gllib\stat-time.h(139): error C2059: syntax error: '.'
../../gltests/../gllib\stat-time.h(142): error C2059: syntax error: '}'
../../gltests/../gllib\stat-time.h(142): error C2143: syntax error: missing ';'
before '}'
../../gltests/../gllib\stat-time.h(147): error C2143: syntax error: missing ';'
before '{'
../../gltests/../gllib\stat-time.h(147): error C2447: '{': missing function
header (old-style formal list?)
../../gltests/../gllib\stat-time.h(176): error C2059: syntax error: '.'
../../gltests/../gllib\stat-time.h(195): error C2059: syntax error: 'return'
../../gltests/../gllib\stat-time.h(196): error C2059: syntax error: '}'
../../gltests/../gllib\stat-time.h(196): error C2143: syntax error: missing ';'
before '}'
../../gltests/../gllib\stat-time.h(205): error C2143: syntax error: missing ';'
before '{'
../../gltests/../gllib\stat-time.h(205): error C2447: '{': missing function
header (old-style formal list?)
../../gltests/../gllib\stat-time.h(240): error C2059: syntax error: '}'
../../gltests/../gllib\stat-time.h(240): error C2143: syntax error: missing ';'
before '}'
These patches fix the problem and add a unit test.
2025-05-31 Bruno Haible <[email protected]>
stat-time tests: Add C++ tests.
* tests/test-stat-time-c++.cc: New file.
* modules/stat-time-c++-tests: New file.
* modules/stat-time-tests (Depends-on): Add stat-time-c++-tests.
stat-time: Fix syntax errors in C++ mode on MSVC (regr. 2023-05-14).
* lib/stat-time.h (_gl_make_timespec): New macro / function.
(get_stat_atime, get_stat_ctime, get_stat_mtime, get_stat_birthtime):
Use it instead of a compound literal expression.
>From 0143999972f1565ac40887393e20a7a8237785a7 Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email protected]>
Date: Sun, 1 Jun 2025 01:07:53 +0200
Subject: [PATCH 1/2] stat-time: Fix syntax errors in C++ mode on MSVC (regr.
2023-05-14).
* lib/stat-time.h (_gl_make_timespec): New macro / function.
(get_stat_atime, get_stat_ctime, get_stat_mtime, get_stat_birthtime):
Use it instead of a compound literal expression.
---
ChangeLog | 7 +++++++
lib/stat-time.h | 43 ++++++++++++++++++++++++++++++++-----------
2 files changed, 39 insertions(+), 11 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index c5edd007b9..1ffa32c753 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2025-05-31 Bruno Haible <[email protected]>
+
+ stat-time: Fix syntax errors in C++ mode on MSVC (regr. 2023-05-14).
+ * lib/stat-time.h (_gl_make_timespec): New macro / function.
+ (get_stat_atime, get_stat_ctime, get_stat_mtime, get_stat_birthtime):
+ Use it instead of a compound literal expression.
+
2025-05-31 Bruno Haible <[email protected]>
gettext-h: Fix compilation error on Solaris 11 (regr. 2025-05-28).
diff --git a/lib/stat-time.h b/lib/stat-time.h
index 69813932d5..38315b9f56 100644
--- a/lib/stat-time.h
+++ b/lib/stat-time.h
@@ -117,6 +117,31 @@ get_stat_birthtime_ns (_GL_UNUSED struct stat const *st)
# endif
}
+/* Constructs a 'struct timespec' with the given contents.
+ This macro / function is private to stat-time.h. */
+#if !defined __cplusplus
+/* Use a C99 compound literal.
+ This is guaranteed to initialize also the padding bits, for example on
+ platforms where tv_sec is 64 bits and tv_nsec is 32 bits, thus avoiding
+ gcc -Wuse-of-uninitialized-value warnings. */
+# define _gl_make_timespec(sec,nsec) \
+ (struct timespec) { .tv_sec = (sec), .tv_nsec = (nsec) }
+#else
+/* C++ does not have C99 compound literals.
+ A constructor invocation
+ timespec { (sec), (nsec) }
+ would make assumptions about the order of the fields of 'struct timespec',
+ which are not guaranteed by POSIX. So, use an inline function. */
+static inline struct timespec
+_gl_make_timespec (time_t sec, long nsec)
+{
+ struct timespec ts;
+ ts.tv_sec = sec;
+ ts.tv_nsec = nsec;
+ return ts;
+}
+#endif
+
/* Return *ST's access time. */
_GL_STAT_TIME_INLINE struct timespec _GL_ATTRIBUTE_PURE
get_stat_atime (struct stat const *st)
@@ -124,8 +149,7 @@ get_stat_atime (struct stat const *st)
#ifdef STAT_TIMESPEC
return STAT_TIMESPEC (st, st_atim);
#else
- return (struct timespec) { .tv_sec = st->st_atime,
- .tv_nsec = get_stat_atime_ns (st) };
+ return _gl_make_timespec (st->st_atime, get_stat_atime_ns (st));
#endif
}
@@ -136,8 +160,7 @@ get_stat_ctime (struct stat const *st)
#ifdef STAT_TIMESPEC
return STAT_TIMESPEC (st, st_ctim);
#else
- return (struct timespec) { .tv_sec = st->st_ctime,
- .tv_nsec = get_stat_ctime_ns (st) };
+ return _gl_make_timespec (st->st_ctime, get_stat_ctime_ns (st));
#endif
}
@@ -148,8 +171,7 @@ get_stat_mtime (struct stat const *st)
#ifdef STAT_TIMESPEC
return STAT_TIMESPEC (st, st_mtim);
#else
- return (struct timespec) { .tv_sec = st->st_mtime,
- .tv_nsec = get_stat_mtime_ns (st) };
+ return _gl_make_timespec (st->st_mtime, get_stat_mtime_ns (st));
#endif
}
@@ -164,8 +186,7 @@ get_stat_birthtime (_GL_UNUSED struct stat const *st)
|| defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC)
t = STAT_TIMESPEC (st, st_birthtim);
#elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
- t = (struct timespec) { .tv_sec = st->st_birthtime,
- .tv_nsec = st->st_birthtimensec };
+ t = _gl_make_timespec (st->st_birthtime, st->st_birthtimensec);
#elif defined _WIN32 && ! defined __CYGWIN__
/* Native Windows platforms (but not Cygwin) put the "file creation
time" in st_ctime (!). See
@@ -173,11 +194,11 @@ get_stat_birthtime (_GL_UNUSED struct stat const *st)
# if _GL_WINDOWS_STAT_TIMESPEC
t = st->st_ctim;
# else
- t = (struct timespec) { .tv_sec = st->st_ctime };
+ t = _gl_make_timespec (st->st_ctime, 0);
# endif
#else
/* Birth time is not supported. */
- t = (struct timespec) { .tv_sec = -1, .tv_nsec = -1 };
+ t = _gl_make_timespec (-1, -1);
#endif
#if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
@@ -189,7 +210,7 @@ get_stat_birthtime (_GL_UNUSED struct stat const *st)
sometimes returns junk in the birth time fields; work around this
bug if it is detected. */
if (! (t.tv_sec && 0 <= t.tv_nsec && t.tv_nsec < 1000000000))
- t = (struct timespec) { .tv_sec = -1, .tv_nsec = -1 };
+ t = _gl_make_timespec (-1, -1);
#endif
return t;
--
2.43.0
>From c384a44fc201e0ae6d23138727ad9080fc314fe9 Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email protected]>
Date: Sat, 31 May 2025 23:43:24 +0200
Subject: [PATCH 2/2] stat-time tests: Add C++ tests.
* tests/test-stat-time-c++.cc: New file.
* modules/stat-time-c++-tests: New file.
* modules/stat-time-tests (Depends-on): Add stat-time-c++-tests.
---
ChangeLog | 5 +++++
modules/stat-time-c++-tests | 19 +++++++++++++++++++
modules/stat-time-tests | 1 +
tests/test-stat-time-c++.cc | 33 +++++++++++++++++++++++++++++++++
4 files changed, 58 insertions(+)
create mode 100644 modules/stat-time-c++-tests
create mode 100644 tests/test-stat-time-c++.cc
diff --git a/ChangeLog b/ChangeLog
index 1ffa32c753..e9b9bd011f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
2025-05-31 Bruno Haible <[email protected]>
+ stat-time tests: Add C++ tests.
+ * tests/test-stat-time-c++.cc: New file.
+ * modules/stat-time-c++-tests: New file.
+ * modules/stat-time-tests (Depends-on): Add stat-time-c++-tests.
+
stat-time: Fix syntax errors in C++ mode on MSVC (regr. 2023-05-14).
* lib/stat-time.h (_gl_make_timespec): New macro / function.
(get_stat_atime, get_stat_ctime, get_stat_mtime, get_stat_birthtime):
diff --git a/modules/stat-time-c++-tests b/modules/stat-time-c++-tests
new file mode 100644
index 0000000000..e4dfb485c6
--- /dev/null
+++ b/modules/stat-time-c++-tests
@@ -0,0 +1,19 @@
+Files:
+tests/test-stat-time-c++.cc
+tests/signature.h
+
+Status:
+c++-test
+
+Depends-on:
+ansi-c++-opt
+
+configure.ac:
+
+Makefile.am:
+if ANSICXX
+TESTS += test-stat-time-c++
+check_PROGRAMS += test-stat-time-c++
+test_stat_time_c___SOURCES = test-stat-time-c++.cc
+test_stat_time_c___LDADD = $(LDADD) $(LIBINTL) $(NANOSLEEP_LIB) $(CLOCK_TIME_LIB) $(MBRTOWC_LIB) $(GETRANDOM_LIB)
+endif
diff --git a/modules/stat-time-tests b/modules/stat-time-tests
index c1f962c243..367a1f6ba9 100644
--- a/modules/stat-time-tests
+++ b/modules/stat-time-tests
@@ -10,6 +10,7 @@ time-h
unistd-h
xconcat-filename
xgetcwd-lgpl
+stat-time-c++-tests
configure.ac:
diff --git a/tests/test-stat-time-c++.cc b/tests/test-stat-time-c++.cc
new file mode 100644
index 0000000000..2f2488d89b
--- /dev/null
+++ b/tests/test-stat-time-c++.cc
@@ -0,0 +1,33 @@
+/* Test of <stat-time.h> in C++ mode.
+ Copyright (C) 2025 Free Software Foundation, Inc.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>. */
+
+#define GNULIB_NAMESPACE gnulib
+#include <config.h>
+
+#include "stat-time.h"
+
+struct stat statinfo;
+
+int
+main (void)
+{
+ struct timespec a, c, m, b;
+ a = get_stat_atime (&statinfo);
+ c = get_stat_ctime (&statinfo);
+ m = get_stat_mtime (&statinfo);
+ b = get_stat_birthtime (&statinfo);
+ (void) (a.tv_sec + c.tv_sec + m.tv_sec + b.tv_sec);
+}
--
2.43.0