[flang-commits] [flang] [flang] Add EXECUTE_COMMAND_LINE runtime and lowering intrinsics implementation (PR #74077)

Yi Wu via flang-commits flang-commits at lists.llvm.org
Fri Dec 15 07:19:27 PST 2023


================
@@ -173,5 +173,145 @@ RT_API_ATTRS void ShallowCopy(const Descriptor &to, const Descriptor &from) {
   ShallowCopy(to, from, to.IsContiguous(), from.IsContiguous());
 }
 
+RT_API_ATTRS const char *EnsureNullTerminated(
+    const char *str, size_t length, Terminator &terminator) {
+  if (length <= std::strlen(str)) {
+    char *newCmd{(char *)AllocateMemoryOrCrash(terminator, length + 1)};
+    std::memcpy(newCmd, str, length);
+    newCmd[length] = '\0';
+    return newCmd;
+  } else {
+    return str;
+  }
+}
+
+RT_API_ATTRS std::size_t LengthWithoutTrailingSpaces(const Descriptor &d) {
+  std::size_t s{d.ElementBytes() - 1};
+  while (*d.OffsetElement(s) == ' ') {
+    --s;
+  }
+  return s + 1;
+}
+
+// Returns the length of the \p string. Assumes \p string is valid.
+RT_API_ATTRS std::int64_t StringLength(const char *string) {
+  std::size_t length{std::strlen(string)};
+  if constexpr (sizeof(std::size_t) < sizeof(std::int64_t)) {
+    return static_cast<std::int64_t>(length);
+  } else {
+    std::size_t max{std::numeric_limits<std::int64_t>::max()};
+    return length > max ? 0 // Just fail.
+                        : static_cast<std::int64_t>(length);
+  }
+}
+
+RT_API_ATTRS bool IsValidCharDescriptor(const Descriptor *value) {
+  return value && value->IsAllocated() &&
+      value->type() == TypeCode(TypeCategory::Character, 1) &&
+      value->rank() == 0;
+}
+
+RT_API_ATTRS bool IsValidIntDescriptor(const Descriptor *length) {
----------------
yi-wu-arm wrote:

Typically this function is called after checking if the Descriptor is null, so there is not need to add a check. e.g.
```cpp
if (value){
    RUNTIME_CHECK(terminator, IsValidCharDescriptor(value));
    // modify the descriptor
    FillWithSpaces(value)
}
```
But I can add a comment stating that it assumes the Descriptor exist.

https://github.com/llvm/llvm-project/pull/74077


More information about the flang-commits mailing list