[flang-commits] [flang] a94d943 - [flang] Fix actions at end of output record
peter klausler via flang-commits
flang-commits at lists.llvm.org
Thu Oct 1 17:18:43 PDT 2020
Author: peter klausler
Date: 2020-10-01T17:18:20-07:00
New Revision: a94d943f1a3f42efede7e908bb250c84f9f442b1
URL: https://github.com/llvm/llvm-project/commit/a94d943f1a3f42efede7e908bb250c84f9f442b1
DIFF: https://github.com/llvm/llvm-project/commit/a94d943f1a3f42efede7e908bb250c84f9f442b1.diff
LOG: [flang] Fix actions at end of output record
It turns out that unformatted fixed-size output records
do need to be padded out if short, in order to avoid a
spurious EOF crash on a short record at the end of the file.
While here in AdvanceRecord(), move the unformatted
variable-length record header/footer writing code to here
from EndIoStatement().
Differential revision: https://reviews.llvm.org/D88685
Added:
Modified:
flang/runtime/io-stmt.cpp
flang/runtime/io-stmt.h
flang/runtime/unit.cpp
Removed:
################################################################################
diff --git a/flang/runtime/io-stmt.cpp b/flang/runtime/io-stmt.cpp
index 45b5f2a95060..7474dd94b982 100644
--- a/flang/runtime/io-stmt.cpp
+++ b/flang/runtime/io-stmt.cpp
@@ -698,32 +698,6 @@ bool UnformattedIoStatementState<DIR>::Emit(
return ExternalIoStatementState<DIR>::Emit(data, bytes, elementBytes);
}
-template <Direction DIR>
-int UnformattedIoStatementState<DIR>::EndIoStatement() {
- ExternalFileUnit &unit{this->unit()};
- if constexpr (DIR == Direction::Output) {
- if (unit.access == Access::Sequential && !unit.isFixedRecordLength) {
- // Append the length of a sequential unformatted variable-length record
- // as its footer, then overwrite the reserved first four bytes of the
- // record with its length as its header. These four bytes were skipped
- // over in BeginUnformattedOutput().
- // TODO: Break very large records up into subrecords with negative
- // headers &/or footers
- union {
- std::uint32_t u;
- char c[sizeof u];
- } u;
- u.u = unit.furthestPositionInRecord - sizeof u;
- // TODO: Convert record length to little-endian on big-endian host?
- if (!(this->Emit(u.c, sizeof u) &&
- (this->HandleAbsolutePosition(0), this->Emit(u.c, sizeof u)))) {
- return false;
- }
- }
- }
- return ExternalIoStatementState<DIR>::EndIoStatement();
-}
-
template class InternalIoStatementState<Direction::Output>;
template class InternalIoStatementState<Direction::Input>;
template class InternalFormattedIoStatementState<Direction::Output>;
diff --git a/flang/runtime/io-stmt.h b/flang/runtime/io-stmt.h
index 343619bc121c..b5d3caff04f0 100644
--- a/flang/runtime/io-stmt.h
+++ b/flang/runtime/io-stmt.h
@@ -322,7 +322,6 @@ class UnformattedIoStatementState : public ExternalIoStatementState<DIR> {
using ExternalIoStatementState<DIR>::ExternalIoStatementState;
bool Receive(char *, std::size_t, std::size_t elementBytes = 0);
bool Emit(const char *, std::size_t, std::size_t elementBytes = 0);
- int EndIoStatement();
};
class OpenStatementState : public ExternalIoStatementBase {
diff --git a/flang/runtime/unit.cpp b/flang/runtime/unit.cpp
index 77b7a74551d8..85d83ec50bd9 100644
--- a/flang/runtime/unit.cpp
+++ b/flang/runtime/unit.cpp
@@ -406,15 +406,32 @@ bool ExternalFileUnit::AdvanceRecord(IoErrorHandler &handler) {
FinishReadingRecord(handler);
BeginReadingRecord(handler);
} else { // Direction::Output
- if (!isUnformatted) {
- if (isFixedRecordLength && recordLength) {
- if (furthestPositionInRecord < *recordLength) {
- WriteFrame(frameOffsetInFile_, *recordLength, handler);
- std::memset(Frame() + recordOffsetInFrame_ + furthestPositionInRecord,
- ' ', *recordLength - furthestPositionInRecord);
- }
+ if (isFixedRecordLength && recordLength) {
+ // Pad remainder of fixed length record
+ if (furthestPositionInRecord < *recordLength) {
+ WriteFrame(
+ frameOffsetInFile_, recordOffsetInFrame_ + *recordLength, handler);
+ std::memset(Frame() + recordOffsetInFrame_ + furthestPositionInRecord,
+ isUnformatted ? 0 : ' ', *recordLength - furthestPositionInRecord);
+ }
+ } else {
+ positionInRecord = furthestPositionInRecord;
+ if (isUnformatted) {
+ // Append the length of a sequential unformatted variable-length record
+ // as its footer, then overwrite the reserved first four bytes of the
+ // record with its length as its header. These four bytes were skipped
+ // over in BeginUnformattedIO<Output>().
+ // TODO: Break very large records up into subrecords with negative
+ // headers &/or footers
+ std::uint32_t length;
+ length = furthestPositionInRecord - sizeof length;
+ ok &= Emit(reinterpret_cast<const char *>(&length), sizeof length,
+ sizeof length, handler);
+ positionInRecord = 0;
+ ok &= Emit(reinterpret_cast<const char *>(&length), sizeof length,
+ sizeof length, handler);
} else {
- positionInRecord = furthestPositionInRecord;
+ // Terminate formatted variable length record
ok &= Emit("\n", 1, 1, handler); // TODO: Windows CR+LF
}
}
More information about the flang-commits
mailing list