Skip to content

Commit b77fd01

Browse files
committed
[flang] Don't blank-fill remaining lines in internal output
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
1 parent 533fbae commit b77fd01

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

flang/runtime/internal-unit.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,9 @@ InternalDescriptorUnit<DIR>::InternalDescriptorUnit(
3838
}
3939

4040
template <Direction DIR> void InternalDescriptorUnit<DIR>::EndIoStatement() {
41-
if constexpr (DIR == Direction::Output) { // blank fill
42-
while (char *record{CurrentRecord()}) {
43-
if (furthestPositionInRecord <
44-
recordLength.value_or(furthestPositionInRecord)) {
45-
std::fill_n(record + furthestPositionInRecord,
46-
*recordLength - furthestPositionInRecord, ' ');
47-
}
48-
furthestPositionInRecord = 0;
49-
++currentRecordNumber;
41+
if constexpr (DIR == Direction::Output) {
42+
if (furthestPositionInRecord > 0) {
43+
BlankFillOutputRecord();
5044
}
5145
}
5246
}
@@ -127,18 +121,24 @@ bool InternalDescriptorUnit<DIR>::AdvanceRecord(IoErrorHandler &handler) {
127121
handler.SignalEnd();
128122
return false;
129123
}
130-
if constexpr (DIR == Direction::Output) { // blank fill
124+
if constexpr (DIR == Direction::Output) {
125+
BlankFillOutputRecord();
126+
}
127+
++currentRecordNumber;
128+
BeginRecord();
129+
return true;
130+
}
131+
132+
template <Direction DIR>
133+
void InternalDescriptorUnit<DIR>::BlankFillOutputRecord() {
134+
if constexpr (DIR == Direction::Output) {
131135
if (furthestPositionInRecord <
132136
recordLength.value_or(furthestPositionInRecord)) {
133137
char *record{CurrentRecord()};
134-
RUNTIME_CHECK(handler, record != nullptr);
135138
std::fill_n(record + furthestPositionInRecord,
136139
*recordLength - furthestPositionInRecord, ' ');
137140
}
138141
}
139-
++currentRecordNumber;
140-
BeginRecord();
141-
return true;
142142
}
143143

144144
template <Direction DIR>

flang/runtime/internal-unit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ template <Direction DIR> class InternalDescriptorUnit : public ConnectionState {
4545
return descriptor().template ZeroBasedIndexedElement<char>(
4646
currentRecordNumber - 1);
4747
}
48+
void BlankFillOutputRecord();
49+
4850
StaticDescriptor<maxRank, true /*addendum*/> staticDescriptor_;
4951
};
5052

flang/unittests/Runtime/NumericalFormatTest.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ TEST(IOApiTests, MultilineOutputTest) {
118118
auto cookie{IONAME(BeginInternalArrayFormattedOutput)(
119119
section, format, std::strlen(format))};
120120

121+
// Fill last line with periods
122+
std::memset(buffer[numLines - 1], '.', lineLength);
123+
121124
// Write data to buffer
122125
IONAME(OutputAscii)(cookie, "WORLD", 5);
123126
IONAME(OutputAscii)(cookie, "HELLO", 5);
@@ -135,7 +138,7 @@ TEST(IOApiTests, MultilineOutputTest) {
135138
" "
136139
"789 abcd 666 777"
137140
" 888 999 "
138-
" "};
141+
"................................"};
139142
// Ensure formatted string matches expected output
140143
EXPECT_TRUE(
141144
CompareFormattedStrings(expect, std::string{buffer[0], sizeof buffer}))

0 commit comments

Comments
 (0)