Skip to content

Commit 510ada5

Browse files
yiwu0b11Yi Wu
authored and
Yi Wu
committed
emit fortran runtime error on failure, fill in spac
If the length is too short to fit completely, blank return. If length if larger than it requires(24), fill the rest of buffer space. hange the return type of `ctime_alloc` from char * to void, because we don't need the return value.
1 parent bf1cd99 commit 510ada5

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

flang/runtime/extensions.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,23 @@
1010
// extensions that will eventually be implemented in Fortran.
1111

1212
#include "flang/Runtime/extensions.h"
13+
#include "terminator.h"
1314
#include "flang/Runtime/command.h"
1415
#include "flang/Runtime/descriptor.h"
1516
#include "flang/Runtime/io-api.h"
1617
#include <ctime>
1718

1819
#ifdef _WIN32
19-
inline const char *ctime_alloc(
20-
char *buffer, size_t bufsize, const time_t cur_time) {
20+
inline void ctime_alloc(char *buffer, size_t bufsize, const time_t cur_time,
21+
Fortran::runtime::Terminator terminator) {
2122
int error = ctime_s(buffer, bufsize, &cur_time);
22-
assert(error == 0 && "ctime_s returned an error");
23-
return buffer;
23+
RUNTIME_CHECK(terminator, error == 0);
2424
}
2525
#else
26-
inline const char *ctime_alloc(
27-
char *buffer, size_t bufsize, const time_t cur_time) {
26+
inline void ctime_alloc(char *buffer, size_t bufsize, const time_t cur_time,
27+
Fortran::runtime::Terminator terminator) {
2828
const char *res = ctime_r(&cur_time, buffer);
29-
assert(res != nullptr && "ctime_s returned an error");
30-
return res;
29+
RUNTIME_CHECK(terminator, res != nullptr);
3130
}
3231
#endif
3332

@@ -48,15 +47,23 @@ void FORTRAN_PROCEDURE_NAME(flush)(const int &unit) {
4847
std::int32_t FORTRAN_PROCEDURE_NAME(iargc)() { return RTNAME(ArgumentCount)(); }
4948

5049
void FORTRAN_PROCEDURE_NAME(fdate)(std::int8_t *arg, std::int64_t length) {
50+
std::array<char, 26> str;
51+
// If the length is too short to fit completely, blank return.
52+
if (length < 24) {
53+
str.fill(' ');
54+
strncpy(reinterpret_cast<char *>(arg), str.data(), length);
55+
return;
56+
}
57+
58+
Terminator terminator{__FILE__, __LINE__};
5159
std::time_t current_time;
5260
std::time(&current_time);
53-
std::array<char, 26> str;
5461
// Day Mon dd hh:mm:ss yyyy\n\0 is 26 characters, e.g.
5562
// Tue May 26 21:51:03 2015\n\0
5663

57-
ctime_alloc(str.data(), str.size(), current_time);
58-
str[24] = '\0'; // remove new line
64+
ctime_alloc(str.data(), str.size(), current_time, terminator);
5965

66+
std::fill(str.begin() + 24, str.begin() + length, ' ');
6067
strncpy(reinterpret_cast<char *>(arg), str.data(), length);
6168
}
6269

0 commit comments

Comments
 (0)