cobol: Guard clock_gettime(). [PR119975]

This attempts to eliminate "'clock_gettime' not declared..." when
building on x86_64-apple-darwin15.6.0.  Calls to clock_gettime have been
reduced to two locations.  Both have been guarded with

gcc/cobol/ChangeLog:

	PR cobol/119975
	* genapi.cc (parser_intrinsic_call_0): Use get_time_nanoseconds().
	* genutil.cc (get_time_64): Rename to get_time_nanoseconds().
	(get_time_nanoseconds): Likewise.
	* genutil.h (get_time_64): Likewise.
	(get_time_nanoseconds): Likewise.
	* util.cc (class cbl_timespec): Timing routine uses
	get_time_nanoseconds().
	(operator-): Likewise.
	(parse_file): Likewise.

libgcobol/ChangeLog:

	PR cobol/119975
	* configure.ac: AC_CHECK_LIB(rt, clock_gettime).
	* config.h.in: Likewise.
	* configure: Likewise.
	* gfileio.cc: Remove in-line cppcheck-suppress.
	* intrinsic.cc (timespec_to_string): Use guarded clock_gettime().
	(__gg__current_date): Likewise.
	(__gg__seconds_past_midnight): Likewise.
	(__gg__formatted_current_date): Likewise.
	(__gg__random): Likewise.
	(__gg__random_next): Likewise.
	(__gg__when_compiled): Likewise.
	* libgcobol.cc (cobol_time): Likewise.
	(get_time_nanoseconds): Likewise.
	(__gg__clock_gettime): Likewise.
	(__gg__get_date_hhmmssff): Likewise.
	* libgcobol.h (__gg__clock_gettime): Likewise.
	(struct cbl_timespec): Likewise.
This commit is contained in:
Robert Dubner 2025-06-05 10:53:02 -04:00
parent 58fb3ba196
commit 2e334900f4
11 changed files with 130 additions and 22 deletions

View File

@ -10491,7 +10491,7 @@ parser_intrinsic_call_0(cbl_field_t *tgt,
{
// Pass __gg__when_compiled() the time from right now.
struct timespec tp;
uint64_t now = get_time_64();
uint64_t now = get_time_nanoseconds();
tp.tv_sec = now / 1000000000;
tp.tv_nsec = now % 1000000000;

View File

@ -2121,7 +2121,7 @@ qualified_data_location(cbl_refer_t &refer)
}
uint64_t
get_time_64()
get_time_nanoseconds()
{
// This code was unabashedly stolen from gcc/timevar.cc.
// It returns the Unix epoch with nine decimal places.

View File

@ -155,7 +155,7 @@ void build_array_of_fourplets( int ngroup,
size_t N,
cbl_refer_t *refers);
void get_depending_on_value_from_odo(tree retval, cbl_field_t *odo);
uint64_t get_time_64();
uint64_t get_time_nanoseconds();
#endif

View File

@ -65,6 +65,7 @@
#include "inspect.h"
#include "../../libgcobol/io.h"
#include "genapi.h"
#include "genutil.h"
#pragma GCC diagnostic ignored "-Wunused-result"
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
@ -2141,22 +2142,25 @@ cobol_fileline_set( const char line[] ) {
return file.name;
}
//#define TIMING_PARSE
#ifdef TIMING_PARSE
class cbl_timespec {
struct timespec now;
uint64_t now; // Nanoseconds
public:
cbl_timespec() {
clock_gettime(CLOCK_MONOTONIC, &now);
now = get_time_nanoseconds();
}
double ns() const {
return now.tv_sec * 1000000000 + now.tv_nsec;
return now;
}
friend double operator-( const cbl_timespec& now, const cbl_timespec& then );
};
double
operator-( const cbl_timespec& then, const cbl_timespec& now ) {
operator-( const cbl_timespec& now, const cbl_timespec& then ) {
return (now.ns() - then.ns()) / 1000000000;
}
#endif
static int
parse_file( const char filename[] )
@ -2172,15 +2176,20 @@ parse_file( const char filename[] )
return 0;
}
#ifdef TIMING_PARSE
cbl_timespec start;
#endif
int erc = yyparse();
#ifdef TIMING_PARSE
cbl_timespec finish;
double dt = finish - start;
printf("Overall parse & generate time is %.6f seconds\n", dt);
#endif
parser_leave_file();
//printf("Overall parse & generate time is %.6f seconds\n", dt);
fclose (yyin);

View File

@ -3,6 +3,9 @@
/* Define to 1 if the target assembler supports thread-local storage. */
#undef HAVE_CC_TLS
/* Define to 1 if you have the `clock_gettime' function. */
#undef HAVE_CLOCK_GETTIME
/* Define to 1 if you have the <complex.h> header file. */
#undef HAVE_COMPLEX_H

53
libgcobol/configure vendored
View File

@ -17275,6 +17275,59 @@ if test "$ac_res" != no; then :
fi
# Copied from gcc/configure.ac. 2025-06-05 R.J.Dubner
# At least for glibc, clock_gettime is in librt. But don't pull that
# in if it still doesn't give us the function we want.
ac_cv_func_clock_gettime=no
if test $ac_cv_func_clock_gettime = no; then
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for clock_gettime in -lrt" >&5
$as_echo_n "checking for clock_gettime in -lrt... " >&6; }
if ${ac_cv_lib_rt_clock_gettime+:} false; then :
$as_echo_n "(cached) " >&6
else
ac_check_lib_save_LIBS=$LIBS
LIBS="-lrt $LIBS"
if test x$gcc_no_link = xyes; then
as_fn_error $? "Link tests are not allowed after GCC_NO_EXECUTABLES." "$LINENO" 5
fi
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
/* Override any GCC internal prototype to avoid an error.
Use char because int might match the return type of a GCC
builtin and then its argument prototype would still apply. */
#ifdef __cplusplus
extern "C"
#endif
char clock_gettime ();
int
main ()
{
return clock_gettime ();
;
return 0;
}
_ACEOF
if ac_fn_cxx_try_link "$LINENO"; then :
ac_cv_lib_rt_clock_gettime=yes
else
ac_cv_lib_rt_clock_gettime=no
fi
rm -f core conftest.err conftest.$ac_objext \
conftest$ac_exeext conftest.$ac_ext
LIBS=$ac_check_lib_save_LIBS
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_rt_clock_gettime" >&5
$as_echo "$ac_cv_lib_rt_clock_gettime" >&6; }
if test "x$ac_cv_lib_rt_clock_gettime" = xyes; then :
LIBS="-lrt $LIBS"
$as_echo "#define HAVE_CLOCK_GETTIME 1" >>confdefs.h
fi
fi
have_iec_60559_libc_support=no
if test "x$ac_cv_func_strtof128$ac_cv_func_strfromf128" = xyesyes \
&& test "x$libgcobol_have_sinf128$libgcobol_have_cacosf128" = xyesyes; then

View File

@ -232,6 +232,17 @@ AC_SEARCH_LIBS([sinf128], [c m], libgcobol_have_sinf128=yes)
libgcobol_have_cacosf128=no
AC_SEARCH_LIBS([cacosf128], [c m], libgcobol_have_cacosf128=yes)
# Copied from gcc/configure.ac. 2025-06-05 R.J.Dubner
# At least for glibc, clock_gettime is in librt. But don't pull that
# in if it still doesn't give us the function we want.
ac_cv_func_clock_gettime=no
if test $ac_cv_func_clock_gettime = no; then
AC_CHECK_LIB(rt, clock_gettime,
[LIBS="-lrt $LIBS"
AC_DEFINE(HAVE_CLOCK_GETTIME, 1,
[Define to 1 if you have the `clock_gettime' function.])])
fi
have_iec_60559_libc_support=no
if test "x$ac_cv_func_strtof128$ac_cv_func_strfromf128" = xyesyes \
&& test "x$libgcobol_have_sinf128$libgcobol_have_cacosf128" = xyesyes; then

View File

@ -28,8 +28,6 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// cppcheck-suppress-file postfixOperator
#include <err.h>
#include <fcntl.h>
#include <unistd.h>

View File

@ -167,7 +167,7 @@ JD_to_DOW(double JD)
static
char *
timespec_to_string(char *retval, struct timespec &tp)
timespec_to_string(char *retval, struct cbl_timespec &tp)
{
/*
Returns a 21-character string:
@ -1218,7 +1218,7 @@ void
__gg__current_date(cblc_field_t *dest)
{
// FUNCTION CURRENT-DATE
struct timespec tp = {};
struct cbl_timespec tp = {};
__gg__clock_gettime(CLOCK_REALTIME, &tp); // time_t tv_sec; long tv_nsec
char retval[DATE_STRING_BUFFER_SIZE];
@ -1232,7 +1232,7 @@ void
__gg__seconds_past_midnight(cblc_field_t *dest)
{
// SECONDS-PAST-MIDNIGHT
struct timespec tp = {};
struct cbl_timespec tp = {};
struct tm tm;
__int128 retval=0;
@ -1484,7 +1484,7 @@ __gg__formatted_current_date( cblc_field_t *dest, // Destination string
}
}
struct timespec ts = {};
struct cbl_timespec ts = {};
__gg__clock_gettime(CLOCK_REALTIME, &ts);
struct tm tm = {};
@ -3432,7 +3432,7 @@ __gg__random( cblc_field_t *dest,
buf->state = NULL;
state = (char *)malloc(state_len);
struct timespec ts;
struct cbl_timespec ts;
__gg__clock_gettime(CLOCK_REALTIME, &ts);
initstate_r( ts.tv_nsec, state, state_len, buf);
}
@ -3472,7 +3472,7 @@ __gg__random_next(cblc_field_t *dest)
buf = (random_data *)malloc(sizeof(struct random_data));
buf->state = NULL;
state = (char *)malloc(state_len);
struct timespec ts;
struct cbl_timespec ts;
__gg__clock_gettime(CLOCK_REALTIME, &ts);
initstate_r( ts.tv_nsec, state, state_len, buf);
}
@ -3776,7 +3776,7 @@ extern "C"
void
__gg__when_compiled(cblc_field_t *dest, size_t tv_sec, long tv_nsec)
{
struct timespec tp = {};
struct cbl_timespec tp = {};
tp.tv_sec = tv_sec;
tp.tv_nsec = tv_nsec;
char retval[DATE_STRING_BUFFER_SIZE];

View File

@ -2289,7 +2289,7 @@ get_binary_value_local( int *rdigits,
static time_t
cobol_time()
{
struct timespec tp;
struct cbl_timespec tp;
__gg__clock_gettime(CLOCK_REALTIME, &tp);
return tp.tv_sec;
}
@ -2402,10 +2402,32 @@ int_from_digits(const char * &p, int ndigits)
return retval;
}
uint64_t
get_time_nanoseconds()
{
// This code was unabashedly stolen from gcc/timevar.cc.
// It returns the Unix epoch with nine decimal places.
uint64_t retval = 0;
#ifdef HAVE_CLOCK_GETTIME
struct timespec ts;
clock_gettime (CLOCK_REALTIME, &ts);
retval = ts.tv_sec * 1000000000 + ts.tv_nsec;
return retval;
#endif
#ifdef HAVE_GETTIMEOFDAY
struct timeval tv;
gettimeofday (&tv, NULL);
retval = tv.tv_sec * 1000000000 + tv.tv_usec * 1000;
return retval;
#endif
return retval;
}
extern "C"
void
__gg__clock_gettime(clockid_t clk_id, struct timespec *tp)
__gg__clock_gettime(clockid_t clk_id, struct cbl_timespec *tp)
{
const char *p = getenv("GCOBOL_CURRENT_DATE");
@ -2435,7 +2457,11 @@ __gg__clock_gettime(clockid_t clk_id, struct timespec *tp)
}
else
{
clock_gettime(clk_id, tp);
timespec tm;
clock_gettime(clk_id, &tm);
uint64_t ns = get_time_nanoseconds();
tp->tv_sec = ns/1000000000;
tp->tv_nsec = ns%1000000000;
}
}
@ -2445,7 +2471,7 @@ __gg__get_date_hhmmssff()
{
char ach[32];
struct timespec tv;
struct cbl_timespec tv;
__gg__clock_gettime(CLOCK_REALTIME, &tv);
struct tm tm;

View File

@ -104,7 +104,15 @@ extern "C" char __gg__get_decimal_separator();
extern "C" char __gg__get_decimal_point();
extern "C" char * __gg__get_default_currency_string();
extern "C" void __gg__clock_gettime(clockid_t clk_id, struct timespec *tp);
struct cbl_timespec
{
/* You keep using that word "portability". I do not think it means what
you think it means. */
time_t tv_sec; // Seconds.
long tv_nsec; // Nanoseconds.
} ;
extern "C" void __gg__clock_gettime(clockid_t clk_id, struct cbl_timespec *tp);
extern "C" GCOB_FP128 __gg__float128_from_location(
const cblc_field_t *var,