Skip to content

Commit c0f6308

Browse files
committed
[flang][runtime] Clean up code to unblock development
Clean up recently-added code to avoid warnings and to eliminate a needless dependence from the Fortran runtime support library on C++ runtimes.
1 parent 253d2f9 commit c0f6308

File tree

4 files changed

+24
-47
lines changed

4 files changed

+24
-47
lines changed

flang/include/flang/Runtime/extensions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ std::int32_t FORTRAN_PROCEDURE_NAME(iargc)();
3030

3131
// GNU Fortran 77 compatibility subroutine GETARG(N, ARG).
3232
void FORTRAN_PROCEDURE_NAME(getarg)(
33-
std::int32_t &n, std::int8_t *arg, std::int64_t length);
33+
std::int32_t &n, char *arg, std::int64_t length);
3434

3535
// GNU extension subroutine GETLOG(C).
36-
void FORTRAN_PROCEDURE_NAME(getlog)(std::byte *name, std::int64_t length);
36+
void FORTRAN_PROCEDURE_NAME(getlog)(char *name, std::int64_t length);
3737

3838
} // extern "C"
3939
#endif // FORTRAN_RUNTIME_EXTENSIONS_H_

flang/runtime/execute.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,19 +151,19 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
151151

152152
// add "cmd.exe /c " to the beginning of command
153153
const char *prefix{"cmd.exe /c "};
154-
char *newCmdWin{(char *)AllocateMemoryOrCrash(
155-
terminator, std::strlen(prefix) + std::strlen(newCmd) + 1)};
154+
char *newCmdWin{static_cast<char *>(AllocateMemoryOrCrash(
155+
terminator, std::strlen(prefix) + std::strlen(newCmd) + 1))};
156156
std::strcpy(newCmdWin, prefix);
157157
std::strcat(newCmdWin, newCmd);
158158

159159
// Convert the char to wide char
160160
const size_t sizeNeeded{mbstowcs(NULL, newCmdWin, 0) + 1};
161-
wchar_t *wcmd{(wchar_t *)AllocateMemoryOrCrash(
162-
terminator, sizeNeeded * sizeof(wchar_t))};
161+
wchar_t *wcmd{static_cast<wchar_t *>(
162+
AllocateMemoryOrCrash(terminator, sizeNeeded * sizeof(wchar_t)))};
163163
if (std::mbstowcs(wcmd, newCmdWin, sizeNeeded) == static_cast<size_t>(-1)) {
164164
terminator.Crash("Char to wide char failed for newCmd");
165165
}
166-
FreeMemory((void *)newCmdWin);
166+
FreeMemory(newCmdWin);
167167

168168
if (CreateProcess(nullptr, wcmd, nullptr, nullptr, FALSE, 0, nullptr,
169169
nullptr, &si, &pi)) {
@@ -179,7 +179,7 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
179179
CheckAndCopyCharsToDescriptor(cmdmsg, "CreateProcess failed.");
180180
}
181181
}
182-
FreeMemory((void *)wcmd);
182+
FreeMemory(wcmd);
183183
#else
184184
// terminated children do not become zombies
185185
signal(SIGCHLD, SIG_IGN);
@@ -200,7 +200,7 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
200200
}
201201
// Deallocate memory if EnsureNullTerminated dynamically allocated memory
202202
if (newCmd != command.OffsetElement()) {
203-
FreeMemory((void *)newCmd);
203+
FreeMemory(newCmd);
204204
}
205205
}
206206

flang/runtime/extensions.cpp

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ extern "C" {
4747

4848
namespace Fortran::runtime {
4949

50-
void GetUsernameEnvVar(
51-
const char *envName, std::byte *arg, std::int64_t length) {
50+
void GetUsernameEnvVar(const char *envName, char *arg, std::int64_t length) {
5251
Descriptor name{*Descriptor::Create(
5352
1, std::strlen(envName) + 1, const_cast<char *>(envName), 0)};
5453
Descriptor value{*Descriptor::Create(1, length, arg, 0)};
@@ -91,36 +90,23 @@ std::int32_t FORTRAN_PROCEDURE_NAME(iargc)() { return RTNAME(ArgumentCount)(); }
9190

9291
// CALL GETARG(N, ARG)
9392
void FORTRAN_PROCEDURE_NAME(getarg)(
94-
std::int32_t &n, std::int8_t *arg, std::int64_t length) {
93+
std::int32_t &n, char *arg, std::int64_t length) {
9594
Descriptor value{*Descriptor::Create(1, length, arg, 0)};
9695
(void)RTNAME(GetCommandArgument)(
9796
n, &value, nullptr, nullptr, __FILE__, __LINE__);
9897
}
9998

10099
// CALL GETLOG(USRNAME)
101-
void FORTRAN_PROCEDURE_NAME(getlog)(std::byte *arg, std::int64_t length) {
100+
void FORTRAN_PROCEDURE_NAME(getlog)(char *arg, std::int64_t length) {
102101
#if _REENTRANT || _POSIX_C_SOURCE >= 199506L
103-
int nameMaxLen;
104-
#ifdef LOGIN_NAME_MAX
105-
nameMaxLen = LOGIN_NAME_MAX + 1;
106-
#else
107-
nameMaxLen = sysconf(_SC_LOGIN_NAME_MAX) + 1;
108-
if (nameMaxLen == -1)
109-
nameMaxLen = _POSIX_LOGIN_NAME_MAX + 1;
110-
#endif
111-
std::vector<char> str(nameMaxLen);
112-
113-
int error{getlogin_r(str.data(), nameMaxLen)};
114-
if (error == 0) {
115-
// no error: find first \0 in string then pad from there
116-
CopyAndPad(reinterpret_cast<char *>(arg), str.data(), length,
117-
std::strlen(str.data()));
118-
} else {
119-
// error occur: get username from environment variable
120-
GetUsernameEnvVar("LOGNAME", arg, length);
102+
if (length >= 1 && getlogin_r(arg, length) == 0) {
103+
auto loginLen{std::strlen(arg)};
104+
std::memset(
105+
arg + loginLen, ' ', static_cast<std::size_t>(length) - loginLen);
106+
return;
121107
}
122-
#elif _WIN32
123-
// Get username from environment to avoid link to Advapi32.lib
108+
#endif
109+
#if _WIN32
124110
GetUsernameEnvVar("USERNAME", arg, length);
125111
#else
126112
GetUsernameEnvVar("LOGNAME", arg, length);

flang/unittests/Runtime/CommandTest.cpp

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -681,10 +681,7 @@ TEST_F(EnvironmentVariables, ErrMsgTooShort) {
681681
TEST_F(EnvironmentVariables, GetlogGetName) {
682682
const int charLen{3};
683683
char input[charLen]{"\0\0"};
684-
685-
FORTRAN_PROCEDURE_NAME(getlog)
686-
(reinterpret_cast<std::byte *>(input), charLen);
687-
684+
FORTRAN_PROCEDURE_NAME(getlog)(input, charLen);
688685
EXPECT_NE(input[0], '\0');
689686
}
690687

@@ -700,10 +697,7 @@ TEST_F(EnvironmentVariables, GetlogPadSpace) {
700697
charLen = _POSIX_LOGIN_NAME_MAX + 2;
701698
#endif
702699
std::vector<char> input(charLen);
703-
704-
FORTRAN_PROCEDURE_NAME(getlog)
705-
(reinterpret_cast<std::byte *>(input.data()), charLen);
706-
700+
FORTRAN_PROCEDURE_NAME(getlog)(input.data(), charLen);
707701
EXPECT_EQ(input[charLen - 1], ' ');
708702
}
709703
#endif
@@ -715,8 +709,7 @@ TEST_F(EnvironmentVariables, GetlogEnvGetName) {
715709
<< "Environment variable USERNAME does not exist";
716710

717711
char input[]{"XXXXXXXXX"};
718-
FORTRAN_PROCEDURE_NAME(getlog)
719-
(reinterpret_cast<std::byte *>(input), sizeof(input));
712+
FORTRAN_PROCEDURE_NAME(getlog)(input, sizeof(input));
720713

721714
CheckCharEqStr(input, "loginName");
722715
}
@@ -728,8 +721,7 @@ TEST_F(EnvironmentVariables, GetlogEnvBufferShort) {
728721
<< "Environment variable USERNAME does not exist";
729722

730723
char input[]{"XXXXXX"};
731-
FORTRAN_PROCEDURE_NAME(getlog)
732-
(reinterpret_cast<std::byte *>(input), sizeof(input));
724+
FORTRAN_PROCEDURE_NAME(getlog)(input, sizeof(input));
733725

734726
CheckCharEqStr(input, "loginN");
735727
}
@@ -741,8 +733,7 @@ TEST_F(EnvironmentVariables, GetlogEnvPadSpace) {
741733
<< "Environment variable USERNAME does not exist";
742734

743735
char input[]{"XXXXXXXXXX"};
744-
FORTRAN_PROCEDURE_NAME(getlog)
745-
(reinterpret_cast<std::byte *>(input), sizeof(input));
736+
FORTRAN_PROCEDURE_NAME(getlog)(input, sizeof(input));
746737

747738
CheckCharEqStr(input, "loginName ");
748739
}

0 commit comments

Comments
 (0)