Skip to content

Commit

Permalink
[flang] Don't blank-fill remaining lines in internal output
Browse files Browse the repository at this point in the history
Internal writes to character arrays should not blank-fill
records (elements) past the last one that was written to.

Differential Revision: https://reviews.llvm.org/D117342
  • Loading branch information
klausler committed Jan 14, 2022
1 parent 533fbae commit b77fd01
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
28 changes: 14 additions & 14 deletions flang/runtime/internal-unit.cpp
Expand Up @@ -38,15 +38,9 @@ InternalDescriptorUnit<DIR>::InternalDescriptorUnit(
}

template <Direction DIR> void InternalDescriptorUnit<DIR>::EndIoStatement() {
if constexpr (DIR == Direction::Output) { // blank fill
while (char *record{CurrentRecord()}) {
if (furthestPositionInRecord <
recordLength.value_or(furthestPositionInRecord)) {
std::fill_n(record + furthestPositionInRecord,
*recordLength - furthestPositionInRecord, ' ');
}
furthestPositionInRecord = 0;
++currentRecordNumber;
if constexpr (DIR == Direction::Output) {
if (furthestPositionInRecord > 0) {
BlankFillOutputRecord();
}
}
}
Expand Down Expand Up @@ -127,18 +121,24 @@ bool InternalDescriptorUnit<DIR>::AdvanceRecord(IoErrorHandler &handler) {
handler.SignalEnd();
return false;
}
if constexpr (DIR == Direction::Output) { // blank fill
if constexpr (DIR == Direction::Output) {
BlankFillOutputRecord();
}
++currentRecordNumber;
BeginRecord();
return true;
}

template <Direction DIR>
void InternalDescriptorUnit<DIR>::BlankFillOutputRecord() {
if constexpr (DIR == Direction::Output) {
if (furthestPositionInRecord <
recordLength.value_or(furthestPositionInRecord)) {
char *record{CurrentRecord()};
RUNTIME_CHECK(handler, record != nullptr);
std::fill_n(record + furthestPositionInRecord,
*recordLength - furthestPositionInRecord, ' ');
}
}
++currentRecordNumber;
BeginRecord();
return true;
}

template <Direction DIR>
Expand Down
2 changes: 2 additions & 0 deletions flang/runtime/internal-unit.h
Expand Up @@ -45,6 +45,8 @@ template <Direction DIR> class InternalDescriptorUnit : public ConnectionState {
return descriptor().template ZeroBasedIndexedElement<char>(
currentRecordNumber - 1);
}
void BlankFillOutputRecord();

StaticDescriptor<maxRank, true /*addendum*/> staticDescriptor_;
};

Expand Down
5 changes: 4 additions & 1 deletion flang/unittests/Runtime/NumericalFormatTest.cpp
Expand Up @@ -118,6 +118,9 @@ TEST(IOApiTests, MultilineOutputTest) {
auto cookie{IONAME(BeginInternalArrayFormattedOutput)(
section, format, std::strlen(format))};

// Fill last line with periods
std::memset(buffer[numLines - 1], '.', lineLength);

// Write data to buffer
IONAME(OutputAscii)(cookie, "WORLD", 5);
IONAME(OutputAscii)(cookie, "HELLO", 5);
Expand All @@ -135,7 +138,7 @@ TEST(IOApiTests, MultilineOutputTest) {
" "
"789 abcd 666 777"
" 888 999 "
" "};
"................................"};
// Ensure formatted string matches expected output
EXPECT_TRUE(
CompareFormattedStrings(expect, std::string{buffer[0], sizeof buffer}))
Expand Down

0 comments on commit b77fd01

Please sign in to comment.