Skip to content

Commit 438362d

Browse files
committed
move StoreToDescriptor functions to tools.h
1 parent bc6aef8 commit 438362d

File tree

4 files changed

+191
-184
lines changed

4 files changed

+191
-184
lines changed

flang/runtime/command.cpp

Lines changed: 6 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -39,106 +39,6 @@ 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 bool IsValidCharDescriptor(const Descriptor *value) {
55-
return value && value->IsAllocated() &&
56-
value->type() == TypeCode(TypeCategory::Character, 1) &&
57-
value->rank() == 0;
58-
}
59-
60-
static bool IsValidIntDescriptor(const Descriptor *length) {
61-
auto typeCode{length->type().GetCategoryAndKind()};
62-
// Check that our descriptor is allocated and is a scalar integer with
63-
// kind != 1 (i.e. with a large enough decimal exponent range).
64-
return length->IsAllocated() && length->rank() == 0 &&
65-
length->type().IsInteger() && typeCode && typeCode->second != 1;
66-
}
67-
68-
static void FillWithSpaces(const Descriptor &value, std::size_t offset = 0) {
69-
if (offset < value.ElementBytes()) {
70-
std::memset(
71-
value.OffsetElement(offset), ' ', value.ElementBytes() - offset);
72-
}
73-
}
74-
75-
static std::int32_t CopyToDescriptor(const Descriptor &value,
76-
const char *rawValue, std::int64_t rawValueLength, const Descriptor *errmsg,
77-
std::size_t offset = 0) {
78-
79-
std::int64_t toCopy{std::min(rawValueLength,
80-
static_cast<std::int64_t>(value.ElementBytes() - offset))};
81-
if (toCopy < 0) {
82-
return ToErrmsg(errmsg, StatValueTooShort);
83-
}
84-
85-
std::memcpy(value.OffsetElement(offset), rawValue, toCopy);
86-
87-
if (rawValueLength > toCopy) {
88-
return ToErrmsg(errmsg, StatValueTooShort);
89-
}
90-
91-
return StatOk;
92-
}
93-
94-
static std::int32_t CheckAndCopyToDescriptor(const Descriptor *value,
95-
const char *rawValue, const Descriptor *errmsg, std::size_t &offset) {
96-
bool haveValue{IsValidCharDescriptor(value)};
97-
98-
std::int64_t len{StringLength(rawValue)};
99-
if (len <= 0) {
100-
if (haveValue) {
101-
FillWithSpaces(*value);
102-
}
103-
return ToErrmsg(errmsg, StatMissingArgument);
104-
}
105-
106-
std::int32_t stat{StatOk};
107-
if (haveValue) {
108-
stat = CopyToDescriptor(*value, rawValue, len, errmsg, offset);
109-
}
110-
111-
offset += len;
112-
return stat;
113-
}
114-
115-
static void StoreLengthToDescriptor(
116-
const Descriptor *length, std::int64_t value, Terminator &terminator) {
117-
auto typeCode{length->type().GetCategoryAndKind()};
118-
int kind{typeCode->second};
119-
Fortran::runtime::ApplyIntegerKind<Fortran::runtime::StoreIntegerAt, void>(
120-
kind, terminator, *length, /* atIndex = */ 0, value);
121-
}
122-
123-
template <int KIND> struct FitsInIntegerKind {
124-
bool operator()([[maybe_unused]] std::int64_t value) {
125-
if constexpr (KIND >= 8) {
126-
return true;
127-
} else {
128-
return value <= std::numeric_limits<Fortran::runtime::CppTypeFor<
129-
Fortran::common::TypeCategory::Integer, KIND>>::max();
130-
}
131-
}
132-
};
133-
134-
static bool FitsInDescriptor(
135-
const Descriptor *length, std::int64_t value, Terminator &terminator) {
136-
auto typeCode{length->type().GetCategoryAndKind()};
137-
int kind{typeCode->second};
138-
return Fortran::runtime::ApplyIntegerKind<FitsInIntegerKind, bool>(
139-
kind, terminator, value);
140-
}
141-
14242
std::int32_t RTNAME(GetCommandArgument)(std::int32_t n, const Descriptor *value,
14343
const Descriptor *length, const Descriptor *errmsg, const char *sourceFile,
14444
int line) {
@@ -152,7 +52,7 @@ std::int32_t RTNAME(GetCommandArgument)(std::int32_t n, const Descriptor *value,
15252
// Store 0 in case we error out later on.
15353
if (length) {
15454
RUNTIME_CHECK(terminator, IsValidIntDescriptor(length));
155-
StoreLengthToDescriptor(length, 0, terminator);
55+
StoreIntToDescriptor(length, 0, terminator);
15656
}
15757

15858
if (n < 0 || n >= executionEnvironment.argc) {
@@ -166,7 +66,7 @@ std::int32_t RTNAME(GetCommandArgument)(std::int32_t n, const Descriptor *value,
16666
}
16767

16868
if (length && FitsInDescriptor(length, argLen, terminator)) {
169-
StoreLengthToDescriptor(length, argLen, terminator);
69+
StoreIntToDescriptor(length, argLen, terminator);
17070
}
17171

17272
if (value) {
@@ -188,7 +88,7 @@ std::int32_t RTNAME(GetCommand)(const Descriptor *value,
18888
// Store 0 in case we error out later on.
18989
if (length) {
19090
RUNTIME_CHECK(terminator, IsValidIntDescriptor(length));
191-
StoreLengthToDescriptor(length, 0, terminator);
91+
StoreIntToDescriptor(length, 0, terminator);
19292
}
19393

19494
auto shouldContinue = [&](std::int32_t stat) -> bool {
@@ -225,7 +125,7 @@ std::int32_t RTNAME(GetCommand)(const Descriptor *value,
225125
}
226126

227127
if (length && FitsInDescriptor(length, offset, terminator)) {
228-
StoreLengthToDescriptor(length, offset, terminator);
128+
StoreIntToDescriptor(length, offset, terminator);
229129
}
230130

231131
// value += spaces for padding
@@ -236,14 +136,6 @@ std::int32_t RTNAME(GetCommand)(const Descriptor *value,
236136
return stat;
237137
}
238138

239-
static std::size_t LengthWithoutTrailingSpaces(const Descriptor &d) {
240-
std::size_t s{d.ElementBytes() - 1};
241-
while (*d.OffsetElement(s) == ' ') {
242-
--s;
243-
}
244-
return s + 1;
245-
}
246-
247139
std::int32_t RTNAME(GetEnvVariable)(const Descriptor &name,
248140
const Descriptor *value, const Descriptor *length, bool trim_name,
249141
const Descriptor *errmsg, const char *sourceFile, int line) {
@@ -257,7 +149,7 @@ std::int32_t RTNAME(GetEnvVariable)(const Descriptor &name,
257149
// Store 0 in case we error out later on.
258150
if (length) {
259151
RUNTIME_CHECK(terminator, IsValidIntDescriptor(length));
260-
StoreLengthToDescriptor(length, 0, terminator);
152+
StoreIntToDescriptor(length, 0, terminator);
261153
}
262154

263155
const char *rawValue{nullptr};
@@ -273,7 +165,7 @@ std::int32_t RTNAME(GetEnvVariable)(const Descriptor &name,
273165

274166
std::int64_t varLen{StringLength(rawValue)};
275167
if (length && FitsInDescriptor(length, varLen, terminator)) {
276-
StoreLengthToDescriptor(length, varLen, terminator);
168+
StoreIntToDescriptor(length, varLen, terminator);
277169
}
278170

279171
if (value) {

flang/runtime/execute.cpp

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

46-
static bool IsValidCharDescriptor(const Descriptor *value) {
47-
return value && value->IsAllocated() &&
48-
value->type() == TypeCode(TypeCategory::Character, 1) &&
49-
value->rank() == 0;
50-
}
51-
52-
static bool IsValidIntDescriptor(const Descriptor *length) {
53-
auto typeCode{length->type().GetCategoryAndKind()};
54-
// Check that our descriptor is allocated and is a scalar integer with
55-
// kind != 1 (i.e. with a large enough decimal exponent range).
56-
return length->IsAllocated() && length->rank() == 0 &&
57-
length->type().IsInteger() && typeCode && typeCode->second != 1;
58-
}
59-
60-
void CopyToDescriptor(
61-
const Descriptor &value, const char *rawValue, std::size_t offset = 0) {
62-
auto toCopy{std::min(std::strlen(rawValue), value.ElementBytes() - offset)};
63-
std::memcpy(value.OffsetElement(offset), rawValue, toCopy);
64-
}
65-
66-
void CheckAndCopyToDescriptor(
67-
const Descriptor *value, const char *rawValue, std::size_t offset = 0) {
68-
if (value) {
69-
CopyToDescriptor(*value, rawValue, offset);
70-
}
71-
}
72-
73-
static void StoreIntToDescriptor(
74-
const Descriptor *intVal, std::int64_t value, Terminator &terminator) {
75-
auto typeCode{intVal->type().GetCategoryAndKind()};
76-
int kind{typeCode->second};
77-
Fortran::runtime::ApplyIntegerKind<Fortran::runtime::StoreIntegerAt, void>(
78-
kind, terminator, *intVal, /* atIndex = */ 0, value);
79-
}
80-
81-
static void CheckAndStoreIntToDescriptor(
82-
const Descriptor *intVal, std::int64_t value, Terminator &terminator) {
83-
if (intVal) {
84-
StoreIntToDescriptor(intVal, value, terminator);
85-
}
86-
}
87-
88-
template <int KIND> struct FitsInIntegerKind {
89-
bool operator()([[maybe_unused]] std::int64_t value) {
90-
if constexpr (KIND >= 8) {
91-
return true;
92-
} else {
93-
return value <= std::numeric_limits<Fortran::runtime::CppTypeFor<
94-
Fortran::common::TypeCategory::Integer, KIND>>::max();
95-
}
96-
}
97-
};
98-
9946
// If a condition occurs that would assign a nonzero value to CMDSTAT but
10047
// the CMDSTAT variable is not present, error termination is initiated.
10148
int TerminationCheck(int status, const Descriptor *cmdstat,
@@ -105,7 +52,7 @@ int TerminationCheck(int status, const Descriptor *cmdstat,
10552
terminator.Crash("Execution error with system status code: %d", status);
10653
} else {
10754
CheckAndStoreIntToDescriptor(cmdstat, EXECL_ERR, terminator);
108-
CopyToDescriptor(*cmdmsg, "Execution error");
55+
CopyCharToDescriptor(*cmdmsg, "Execution error");
10956
}
11057
}
11158
#ifdef _WIN32
@@ -121,7 +68,7 @@ int TerminationCheck(int status, const Descriptor *cmdstat,
12168
"Invalid command quit with exit status code: %d", exitStatusVal);
12269
} else {
12370
CheckAndStoreIntToDescriptor(cmdstat, INVALID_CL_ERR, terminator);
124-
CopyToDescriptor(*cmdmsg, "Invalid command line");
71+
CopyCharToDescriptor(*cmdmsg, "Invalid command line");
12572
}
12673
}
12774
#if defined(WIFSIGNALED) && defined(WTERMSIG)
@@ -130,7 +77,7 @@ int TerminationCheck(int status, const Descriptor *cmdstat,
13077
terminator.Crash("killed by signal: %d", WTERMSIG(status));
13178
} else {
13279
CheckAndStoreIntToDescriptor(cmdstat, SIGNAL_ERR, terminator);
133-
CopyToDescriptor(*cmdmsg, "killed by signal");
80+
CopyCharToDescriptor(*cmdmsg, "killed by signal");
13481
}
13582
}
13683
#endif
@@ -140,7 +87,7 @@ int TerminationCheck(int status, const Descriptor *cmdstat,
14087
terminator.Crash("stopped by signal: %d", WSTOPSIG(status));
14188
} else {
14289
CheckAndStoreIntToDescriptor(cmdstat, SIGNAL_ERR, terminator);
143-
CopyToDescriptor(*cmdmsg, "stopped by signal");
90+
CopyCharToDescriptor(*cmdmsg, "stopped by signal");
14491
}
14592
}
14693
#endif
@@ -209,7 +156,7 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
209156
"CreateProcess failed with error code: %lu.", GetLastError());
210157
} else {
211158
StoreIntToDescriptor(cmdstat, (uint32_t)GetLastError(), terminator);
212-
CheckAndCopyToDescriptor(cmdmsg, "CreateProcess failed.");
159+
CheckAndCopyCharToDescriptor(cmdmsg, "CreateProcess failed.");
213160
}
214161
}
215162
FreeMemory((void *)wcmd);
@@ -222,7 +169,7 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
222169
terminator.Crash("Fork failed with pid: %d.", pid);
223170
} else {
224171
StoreIntToDescriptor(cmdstat, FORK_ERR, terminator);
225-
CheckAndCopyToDescriptor(cmdmsg, "Fork failed");
172+
CheckAndCopyCharToDescriptor(cmdmsg, "Fork failed");
226173
}
227174
} else if (pid == 0) {
228175
int status{std::system(newCmd)};

0 commit comments

Comments
 (0)