Skip to content

Commit 85d42d4

Browse files
committed
move and use CopyAndPad from character.h
The compiler must have access to the implementations of templated functions at the points where they're instantiated.
1 parent d1470b0 commit 85d42d4

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

flang/include/flang/Runtime/character.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,36 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
// character.h
10+
#ifndef CHARACTER_H
11+
#define CHARACTER_H
12+
13+
#include <cstddef>
14+
#include <algorithm>
15+
16+
template <typename TO, typename FROM>
17+
void CopyAndPad(
18+
TO *to, const FROM *from, std::size_t toChars, std::size_t fromChars) {
19+
if constexpr (sizeof(TO) != sizeof(FROM)) {
20+
std::size_t copyChars{std::min(toChars, fromChars)};
21+
for (std::size_t j{0}; j < copyChars; ++j) {
22+
to[j] = from[j];
23+
}
24+
for (std::size_t j{copyChars}; j < toChars; ++j) {
25+
to[j] = static_cast<TO>(' ');
26+
}
27+
} else if (toChars <= fromChars) {
28+
std::memcpy(to, from, toChars * sizeof(TO));
29+
} else {
30+
std::memcpy(to, from, fromChars * sizeof(TO));
31+
for (std::size_t j{fromChars}; j < toChars; ++j) {
32+
to[j] = static_cast<TO>(' ');
33+
}
34+
}
35+
}
36+
37+
#endif // CHARACTER_H
38+
939
// Defines API between compiled code and the CHARACTER
1040
// support functions in the runtime library.
1141

flang/runtime/character.cpp

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "tools.h"
1212
#include "flang/Common/bit-population-count.h"
1313
#include "flang/Common/uint128.h"
14+
#include "flang/Runtime/character.h"
1415
#include "flang/Runtime/cpp-type.h"
1516
#include "flang/Runtime/descriptor.h"
1617
#include <algorithm>
@@ -461,27 +462,6 @@ static void GeneralCharFuncKind(Descriptor &result, const Descriptor &string,
461462
}
462463
}
463464

464-
template <typename TO, typename FROM>
465-
static void CopyAndPad(
466-
TO *to, const FROM *from, std::size_t toChars, std::size_t fromChars) {
467-
if constexpr (sizeof(TO) != sizeof(FROM)) {
468-
std::size_t copyChars{std::min(toChars, fromChars)};
469-
for (std::size_t j{0}; j < copyChars; ++j) {
470-
to[j] = from[j];
471-
}
472-
for (std::size_t j{copyChars}; j < toChars; ++j) {
473-
to[j] = static_cast<TO>(' ');
474-
}
475-
} else if (toChars <= fromChars) {
476-
std::memcpy(to, from, toChars * sizeof(TO));
477-
} else {
478-
std::memcpy(to, from, fromChars * sizeof(TO));
479-
for (std::size_t j{fromChars}; j < toChars; ++j) {
480-
to[j] = static_cast<TO>(' ');
481-
}
482-
}
483-
}
484-
485465
template <typename CHAR, bool ISMIN>
486466
static void MaxMinHelper(Descriptor &accumulator, const Descriptor &x,
487467
const Terminator &terminator) {

flang/runtime/extensions.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111

1212
#include "flang/Runtime/extensions.h"
1313
#include "terminator.h"
14+
#include "flang/Runtime/character.h"
1415
#include "flang/Runtime/command.h"
1516
#include "flang/Runtime/descriptor.h"
1617
#include "flang/Runtime/io-api.h"
17-
#include <cstring>
1818

1919
#ifdef _WIN32
2020
#define WIN32_LEAN_AND_MEAN
@@ -81,14 +81,15 @@ void FORTRAN_PROCEDURE_NAME(getarg)(
8181
}
8282

8383
void FORTRAN_PROCEDURE_NAME(getlog)(std::int8_t *arg, std::int64_t length) {
84-
int charLen{LOGIN_NAME_MAX + 1};
85-
char str[charLen];
84+
const int nameMaxLen = LOGIN_NAME_MAX + 1;
85+
char str[nameMaxLen];
8686

87-
int error{getlogin_r(*str, charLen)};
87+
int error{getlogin_r(str, nameMaxLen)};
8888
Terminator terminator{__FILE__, __LINE__};
8989
RUNTIME_CHECK(terminator, error == 0);
9090

91-
CopyBufferAndPad(reinterpret_cast<char *>(arg), length, *str, charLen);
91+
// find first \0 in string then pad from there
92+
CopyAndPad(reinterpret_cast<char *>(arg), str, length, std::strlen(str));
9293
}
9394

9495
} // namespace Fortran::runtime

0 commit comments

Comments
 (0)