Skip to content

Commit 573ac29

Browse files
committed
Revert "move StoreToDescriptor functions to tools.h"
This reverts commit 438362d.
1 parent d2242c7 commit 573ac29

File tree

4 files changed

+114
-123
lines changed

4 files changed

+114
-123
lines changed

flang/runtime/command.cpp

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,65 @@ std::int32_t RTNAME(ArgumentCount)() {
3939

4040
pid_t RTNAME(GetPID)() { return getpid(); }
4141

42+
// Returns the length of the \p string. Assumes \p string is valid.
43+
static std::int64_t StringLength(const char *string) {
44+
std::size_t length{std::strlen(string)};
45+
if constexpr (sizeof(std::size_t) < sizeof(std::int64_t)) {
46+
return static_cast<std::int64_t>(length);
47+
} else {
48+
std::size_t max{std::numeric_limits<std::int64_t>::max()};
49+
return length > max ? 0 // Just fail.
50+
: static_cast<std::int64_t>(length);
51+
}
52+
}
53+
54+
static void FillWithSpaces(const Descriptor &value, std::size_t offset = 0) {
55+
if (offset < value.ElementBytes()) {
56+
std::memset(
57+
value.OffsetElement(offset), ' ', value.ElementBytes() - offset);
58+
}
59+
}
60+
61+
static std::int32_t CheckAndCopyCharsToDescriptor(const Descriptor *value,
62+
const char *rawValue, const Descriptor *errmsg, std::size_t &offset) {
63+
bool haveValue{IsValidCharDescriptor(value)};
64+
65+
std::int64_t len{StringLength(rawValue)};
66+
if (len <= 0) {
67+
if (haveValue) {
68+
FillWithSpaces(*value);
69+
}
70+
return ToErrmsg(errmsg, StatMissingArgument);
71+
}
72+
73+
std::int32_t stat{StatOk};
74+
if (haveValue) {
75+
stat = CopyCharsToDescriptor(*value, rawValue, len, errmsg, offset);
76+
}
77+
78+
offset += len;
79+
return stat;
80+
}
81+
82+
template <int KIND> struct FitsInIntegerKind {
83+
bool operator()([[maybe_unused]] std::int64_t value) {
84+
if constexpr (KIND >= 8) {
85+
return true;
86+
} else {
87+
return value <= std::numeric_limits<Fortran::runtime::CppTypeFor<
88+
Fortran::common::TypeCategory::Integer, KIND>>::max();
89+
}
90+
}
91+
};
92+
93+
static bool FitsInDescriptor(
94+
const Descriptor *length, std::int64_t value, Terminator &terminator) {
95+
auto typeCode{length->type().GetCategoryAndKind()};
96+
int kind{typeCode->second};
97+
return Fortran::runtime::ApplyIntegerKind<FitsInIntegerKind, bool>(
98+
kind, terminator, value);
99+
}
100+
42101
std::int32_t RTNAME(GetCommandArgument)(std::int32_t n, const Descriptor *value,
43102
const Descriptor *length, const Descriptor *errmsg, const char *sourceFile,
44103
int line) {
@@ -70,7 +129,7 @@ std::int32_t RTNAME(GetCommandArgument)(std::int32_t n, const Descriptor *value,
70129
}
71130

72131
if (value) {
73-
return CopyToDescriptor(*value, arg, argLen, errmsg);
132+
return CopyCharsToDescriptor(*value, arg, argLen, errmsg);
74133
}
75134

76135
return StatOk;
@@ -100,24 +159,24 @@ std::int32_t RTNAME(GetCommand)(const Descriptor *value,
100159
std::size_t offset{0};
101160

102161
if (executionEnvironment.argc == 0) {
103-
return CheckAndCopyToDescriptor(value, "", errmsg, offset);
162+
return CheckAndCopyCharsToDescriptor(value, "", errmsg, offset);
104163
}
105164

106165
// value = argv[0]
107-
std::int32_t stat{CheckAndCopyToDescriptor(
166+
std::int32_t stat{CheckAndCopyCharsToDescriptor(
108167
value, executionEnvironment.argv[0], errmsg, offset)};
109168
if (!shouldContinue(stat)) {
110169
return stat;
111170
}
112171

113172
// value += " " + argv[1:n]
114173
for (std::int32_t i{1}; i < executionEnvironment.argc; ++i) {
115-
stat = CheckAndCopyToDescriptor(value, " ", errmsg, offset);
174+
stat = CheckAndCopyCharsToDescriptor(value, " ", errmsg, offset);
116175
if (!shouldContinue(stat)) {
117176
return stat;
118177
}
119178

120-
stat = CheckAndCopyToDescriptor(
179+
stat = CheckAndCopyCharsToDescriptor(
121180
value, executionEnvironment.argv[i], errmsg, offset);
122181
if (!shouldContinue(stat)) {
123182
return stat;
@@ -136,6 +195,14 @@ std::int32_t RTNAME(GetCommand)(const Descriptor *value,
136195
return stat;
137196
}
138197

198+
static std::size_t LengthWithoutTrailingSpaces(const Descriptor &d) {
199+
std::size_t s{d.ElementBytes() - 1};
200+
while (*d.OffsetElement(s) == ' ') {
201+
--s;
202+
}
203+
return s + 1;
204+
}
205+
139206
std::int32_t RTNAME(GetEnvVariable)(const Descriptor &name,
140207
const Descriptor *value, const Descriptor *length, bool trim_name,
141208
const Descriptor *errmsg, const char *sourceFile, int line) {
@@ -169,7 +236,7 @@ std::int32_t RTNAME(GetEnvVariable)(const Descriptor &name,
169236
}
170237

171238
if (value) {
172-
return CopyToDescriptor(*value, rawValue, varLen, errmsg);
239+
return CopyCharsToDescriptor(*value, rawValue, varLen, errmsg);
173240
}
174241
return StatOk;
175242
}

flang/runtime/execute.cpp

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,25 @@ enum CMD_STAT {
4343
SIGNAL_ERR = 4
4444
};
4545

46+
// Override CopyCharsToDescriptor in tools.h
47+
void CopyCharsToDescriptor(const Descriptor &value, const char *rawValue){
48+
CopyCharsToDescriptor(value, rawValue, std::strlen(rawValue));
49+
}
50+
51+
void CheckAndCopyCharsToDescriptor(
52+
const Descriptor *value, const char *rawValue) {
53+
if (value) {
54+
CopyCharsToDescriptor(*value, rawValue);
55+
}
56+
}
57+
58+
void CheckAndStoreIntToDescriptor(
59+
const Descriptor *intVal, std::int64_t value, Terminator &terminator) {
60+
if (intVal) {
61+
StoreIntToDescriptor(intVal, value, terminator);
62+
}
63+
}
64+
4665
// If a condition occurs that would assign a nonzero value to CMDSTAT but
4766
// the CMDSTAT variable is not present, error termination is initiated.
4867
int TerminationCheck(int status, const Descriptor *cmdstat,
@@ -52,7 +71,7 @@ int TerminationCheck(int status, const Descriptor *cmdstat,
5271
terminator.Crash("Execution error with system status code: %d", status);
5372
} else {
5473
CheckAndStoreIntToDescriptor(cmdstat, EXECL_ERR, terminator);
55-
CopyCharToDescriptor(*cmdmsg, "Execution error");
74+
CopyCharsToDescriptor(*cmdmsg, "Execution error", 16);
5675
}
5776
}
5877
#ifdef _WIN32
@@ -68,7 +87,7 @@ int TerminationCheck(int status, const Descriptor *cmdstat,
6887
"Invalid command quit with exit status code: %d", exitStatusVal);
6988
} else {
7089
CheckAndStoreIntToDescriptor(cmdstat, INVALID_CL_ERR, terminator);
71-
CopyCharToDescriptor(*cmdmsg, "Invalid command line");
90+
CopyCharsToDescriptor(*cmdmsg, "Invalid command line");
7291
}
7392
}
7493
#if defined(WIFSIGNALED) && defined(WTERMSIG)
@@ -77,7 +96,7 @@ int TerminationCheck(int status, const Descriptor *cmdstat,
7796
terminator.Crash("killed by signal: %d", WTERMSIG(status));
7897
} else {
7998
CheckAndStoreIntToDescriptor(cmdstat, SIGNAL_ERR, terminator);
80-
CopyCharToDescriptor(*cmdmsg, "killed by signal");
99+
CopyCharsToDescriptor(*cmdmsg, "killed by signal");
81100
}
82101
}
83102
#endif
@@ -87,7 +106,7 @@ int TerminationCheck(int status, const Descriptor *cmdstat,
87106
terminator.Crash("stopped by signal: %d", WSTOPSIG(status));
88107
} else {
89108
CheckAndStoreIntToDescriptor(cmdstat, SIGNAL_ERR, terminator);
90-
CopyCharToDescriptor(*cmdmsg, "stopped by signal");
109+
CopyCharsToDescriptor(*cmdmsg, "stopped by signal");
91110
}
92111
}
93112
#endif
@@ -156,7 +175,7 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
156175
"CreateProcess failed with error code: %lu.", GetLastError());
157176
} else {
158177
StoreIntToDescriptor(cmdstat, (uint32_t)GetLastError(), terminator);
159-
CheckAndCopyCharToDescriptor(cmdmsg, "CreateProcess failed.");
178+
CheckAndCopyCharsToDescriptor(cmdmsg, "CreateProcess failed.");
160179
}
161180
}
162181
FreeMemory((void *)wcmd);
@@ -169,7 +188,7 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
169188
terminator.Crash("Fork failed with pid: %d.", pid);
170189
} else {
171190
StoreIntToDescriptor(cmdstat, FORK_ERR, terminator);
172-
CheckAndCopyCharToDescriptor(cmdmsg, "Fork failed");
191+
CheckAndCopyCharsToDescriptor(cmdmsg, "Fork failed");
173192
}
174193
} else if (pid == 0) {
175194
int status{std::system(newCmd)};

flang/runtime/tools.cpp

Lines changed: 4 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -185,44 +185,21 @@ RT_API_ATTRS const char *EnsureNullTerminated(
185185
}
186186
}
187187

188-
RT_API_ATTRS std::size_t LengthWithoutTrailingSpaces(const Descriptor &d) {
189-
std::size_t s{d.ElementBytes() - 1};
190-
while (*d.OffsetElement(s) == ' ') {
191-
--s;
192-
}
193-
return s + 1;
194-
}
195-
196-
// Returns the length of the \p string. Assumes \p string is valid.
197-
RT_API_ATTRS std::int64_t StringLength(const char *string) {
198-
return static_cast<std::int64_t>(std::strlen(string));
199-
}
200-
201-
// Assumes Descriptor \p value is not nullptr.
202188
RT_API_ATTRS bool IsValidCharDescriptor(const Descriptor *value) {
203189
return value && value->IsAllocated() &&
204190
value->type() == TypeCode(TypeCategory::Character, 1) &&
205191
value->rank() == 0;
206192
}
207193

208-
// Assumes Descriptor \p intVal is not nullptr.
209194
RT_API_ATTRS bool IsValidIntDescriptor(const Descriptor *intVal) {
210-
auto typeCode{intVal->type().GetCategoryAndKind()};
211195
// Check that our descriptor is allocated and is a scalar integer with
212196
// kind != 1 (i.e. with a large enough decimal exponent range).
213-
return intVal->IsAllocated() && intVal->rank() == 0 &&
214-
intVal->type().IsInteger() && typeCode && typeCode->second != 1;
215-
}
216-
217-
// Assume Descriptor \p value is valid: pass IsValidCharDescriptor check.
218-
RT_API_ATTRS void FillWithSpaces(const Descriptor &value, std::size_t offset) {
219-
if (offset < value.ElementBytes()) {
220-
std::memset(
221-
value.OffsetElement(offset), ' ', value.ElementBytes() - offset);
222-
}
197+
return intVal && intVal->IsAllocated() && intVal->rank() == 0 &&
198+
intVal->type().IsInteger() && intVal->type().GetCategoryAndKind() &&
199+
intVal->type().GetCategoryAndKind()->second != 1;
223200
}
224201

225-
RT_API_ATTRS std::int32_t CopyToDescriptor(const Descriptor &value,
202+
RT_API_ATTRS std::int32_t CopyCharsToDescriptor(const Descriptor &value,
226203
const char *rawValue, std::int64_t rawValueLength, const Descriptor *errmsg,
227204
std::size_t offset) {
228205

@@ -241,40 +218,6 @@ RT_API_ATTRS std::int32_t CopyToDescriptor(const Descriptor &value,
241218
return StatOk;
242219
}
243220

244-
RT_API_ATTRS void CopyCharToDescriptor(
245-
const Descriptor &value, const char *rawValue, std::size_t offset) {
246-
auto toCopy{std::min(std::strlen(rawValue), value.ElementBytes() - offset)};
247-
std::memcpy(value.OffsetElement(offset), rawValue, toCopy);
248-
}
249-
250-
RT_API_ATTRS std::int32_t CheckAndCopyToDescriptor(const Descriptor *value,
251-
const char *rawValue, const Descriptor *errmsg, std::size_t &offset) {
252-
bool haveValue{IsValidCharDescriptor(value)};
253-
254-
std::int64_t len{StringLength(rawValue)};
255-
if (len <= 0) {
256-
if (haveValue) {
257-
FillWithSpaces(*value);
258-
}
259-
return ToErrmsg(errmsg, StatMissingArgument);
260-
}
261-
262-
std::int32_t stat{StatOk};
263-
if (haveValue) {
264-
stat = CopyToDescriptor(*value, rawValue, len, errmsg, offset);
265-
}
266-
267-
offset += len;
268-
return stat;
269-
}
270-
271-
RT_API_ATTRS void CheckAndCopyCharToDescriptor(
272-
const Descriptor *value, const char *rawValue, std::size_t offset) {
273-
if (value) {
274-
CopyCharToDescriptor(*value, rawValue, offset);
275-
}
276-
}
277-
278221
RT_API_ATTRS void StoreIntToDescriptor(
279222
const Descriptor *length, std::int64_t value, Terminator &terminator) {
280223
auto typeCode{length->type().GetCategoryAndKind()};
@@ -283,13 +226,6 @@ RT_API_ATTRS void StoreIntToDescriptor(
283226
kind, terminator, *length, /* atIndex = */ 0, value);
284227
}
285228

286-
RT_API_ATTRS void CheckAndStoreIntToDescriptor(
287-
const Descriptor *intVal, std::int64_t value, Terminator &terminator) {
288-
if (intVal) {
289-
StoreIntToDescriptor(intVal, value, terminator);
290-
}
291-
}
292-
293229
template <int KIND> struct FitsInIntegerKind {
294230
RT_API_ATTRS bool operator()([[maybe_unused]] std::int64_t value) {
295231
if constexpr (KIND >= 8) {
@@ -302,12 +238,5 @@ template <int KIND> struct FitsInIntegerKind {
302238
}
303239
};
304240

305-
RT_API_ATTRS bool FitsInDescriptor(
306-
const Descriptor *length, std::int64_t value, Terminator &terminator) {
307-
auto typeCode{length->type().GetCategoryAndKind()};
308-
int kind{typeCode->second};
309-
return ApplyIntegerKind<FitsInIntegerKind, bool>(kind, terminator, value);
310-
}
311-
312241
RT_OFFLOAD_API_GROUP_END
313242
} // namespace Fortran::runtime

0 commit comments

Comments
 (0)