Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions flang-rt/include/flang-rt/runtime/environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ struct ExecutionEnvironment {
bool noStopMessage{false}; // NO_STOP_MESSAGE=1 inhibits "Fortran STOP"
bool defaultUTF8{false}; // DEFAULT_UTF8
bool checkPointerDeallocation{true}; // FORT_CHECK_POINTER_DEALLOCATION
bool truncateStream{true}; // FORT_TRUNCATE_STREAM

enum InternalDebugging { WorkQueue = 1 };
int internalDebugging{0}; // FLANG_RT_DEBUG
Expand Down
11 changes: 11 additions & 0 deletions flang-rt/lib/runtime/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,17 @@ void ExecutionEnvironment::Configure(int ac, const char *av[],
}
}

if (auto *x{std::getenv("FORT_TRUNCATE_STREAM")}) {
char *end;
auto n{std::strtol(x, &end, 10)};
if (n >= 0 && n <= 1 && *end == '\0') {
truncateStream = n != 0;
} else {
std::fprintf(stderr,
"Fortran runtime: FORT_TRUNCATE_STREAM=%s is invalid; ignored\n", x);
}
}

if (auto *x{std::getenv("NO_STOP_MESSAGE")}) {
char *end;
auto n{std::strtol(x, &end, 10)};
Expand Down
7 changes: 5 additions & 2 deletions flang-rt/lib/runtime/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -783,8 +783,11 @@ void ExternalFileUnit::DoEndfile(IoErrorHandler &handler) {
frameOffsetInFile_ += recordOffsetInFrame_ + furthestPositionInRecord;
recordOffsetInFrame_ = 0;
FlushOutput(handler);
Truncate(frameOffsetInFile_, handler);
TruncateFrame(frameOffsetInFile_, handler);
if (access != Access::Stream || executionEnvironment.truncateStream) {
// Stream output after positioning truncates with some compilers.
Truncate(frameOffsetInFile_, handler);
TruncateFrame(frameOffsetInFile_, handler);
}
BeginRecord();
impliedEndfile_ = false;
anyWriteSinceLastPositioning_ = false;
Expand Down
11 changes: 11 additions & 0 deletions flang/docs/Extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,17 @@ print *, [(j,j=1,10)]
This design allows format-driven input with `DT` editing to retain
control over advancement in child input, while otherwise allowing it.

* When output takes place to a file under `ACCESS="STREAM"` after
repositioning it to an earlier position, some compilers will
truncate the file; this behavior is similar to the implicit
`ENDFILE` that takes place under sequential output after a
`BACKSPACE` or `REWIND` statement.
Truncation of streams is not specified in the standard, however,
and it does not take place with all compilers.
In this one, truncation is optional; it occurs by default,
but it can be disabled via `FORT_TRUNCATE_STREAM=0` in the
environment at execution time.

## De Facto Standard Features

* `EXTENDS_TYPE_OF()` returns `.TRUE.` if both of its arguments have the
Expand Down
11 changes: 11 additions & 0 deletions flang/docs/RuntimeEnvironment.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,14 @@ The default is 72.
Set `NO_STOP_MESSAGE=1` to disable the extra information about
IEEE floating-point exception flags that the Fortran language
standard requires for `STOP` and `ERROR STOP` statements.

## `FORT_TRUNCATE_STREAM`

Set `FORT_TRUNCATE_STREAM=1` to make output to a formatted unit
with `ACCESS="STREAM"` truncate the file when the unit has been
repositioned via `POS=` to an earlier point in the file.
This behavior is analogous to the implicit writing of an ENDFILE record
when output takes place to a sequential unit after
executing a `BACKSPACE` or `REWIND` statement.
Truncation of a stream-access unit is common to several other
compilers, but it is not mentioned in the standard.
Loading