Skip to content

Commit 0c3c8f4

Browse files
committed
[flang] Fix descriptor-based array data item I/O for list-directed CHARACTER & LOGICAL
These types have to distinguish list-directed I/O from formatted I/O, and the subscript incrementation call was in the formatted branch of the if() rather than after the if(). Differential revision: https://reviews.llvm.org/D88606
1 parent ae4c400 commit 0c3c8f4

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

flang/runtime/descriptor-io.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,13 @@ inline bool FormattedCharacterIO(
159159
}
160160
}
161161
}
162-
if (!descriptor.IncrementSubscripts(subscripts) && j + 1 < numElements) {
163-
io.GetIoErrorHandler().Crash(
164-
"FormattedCharacterIO: subscripts out of bounds");
165-
}
166162
} else {
167163
return false;
168164
}
165+
if (!descriptor.IncrementSubscripts(subscripts) && j + 1 < numElements) {
166+
io.GetIoErrorHandler().Crash(
167+
"FormattedCharacterIO: subscripts out of bounds");
168+
}
169169
}
170170
return true;
171171
}
@@ -198,13 +198,13 @@ inline bool FormattedLogicalIO(
198198
}
199199
}
200200
}
201-
if (!descriptor.IncrementSubscripts(subscripts) && j + 1 < numElements) {
202-
io.GetIoErrorHandler().Crash(
203-
"FormattedLogicalIO: subscripts out of bounds");
204-
}
205201
} else {
206202
return false;
207203
}
204+
if (!descriptor.IncrementSubscripts(subscripts) && j + 1 < numElements) {
205+
io.GetIoErrorHandler().Crash(
206+
"FormattedLogicalIO: subscripts out of bounds");
207+
}
208208
}
209209
return true;
210210
}

flang/unittests/Runtime/hello.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,41 @@ static void listInputTest() {
118118
}
119119
}
120120

121+
static void descrOutputTest() {
122+
char buffer[9];
123+
// Formatted
124+
const char *format{"(2A4)"};
125+
auto cookie{IONAME(BeginInternalFormattedOutput)(
126+
buffer, sizeof buffer, format, std::strlen(format))};
127+
StaticDescriptor<1> staticDescriptor;
128+
Descriptor &desc{staticDescriptor.descriptor()};
129+
SubscriptValue extent[]{2};
130+
char data[2][4];
131+
std::memcpy(data[0], "ABCD", 4);
132+
std::memcpy(data[1], "EFGH", 4);
133+
desc.Establish(TypeCode{CFI_type_char}, sizeof data[0], &data, 1, extent);
134+
desc.Dump();
135+
desc.Check();
136+
IONAME(OutputDescriptor)(cookie, desc);
137+
if (auto status{IONAME(EndIoStatement)(cookie)}) {
138+
Fail() << "descrOutputTest: '" << format << "' failed, status "
139+
<< static_cast<int>(status) << '\n';
140+
} else {
141+
test("descrOutputTest(formatted)", "ABCDEFGH ",
142+
std::string{buffer, sizeof buffer});
143+
}
144+
// List-directed
145+
cookie = IONAME(BeginInternalListOutput)(buffer, sizeof buffer);
146+
IONAME(OutputDescriptor)(cookie, desc);
147+
if (auto status{IONAME(EndIoStatement)(cookie)}) {
148+
Fail() << "descrOutputTest: list-directed failed, status "
149+
<< static_cast<int>(status) << '\n';
150+
} else {
151+
test("descrOutputTest(list)", " ABCDEFGH",
152+
std::string{buffer, sizeof buffer});
153+
}
154+
}
155+
121156
static void realTest(const char *format, double x, const char *expect) {
122157
char buffer[800];
123158
auto cookie{IONAME(BeginInternalFormattedOutput)(
@@ -485,6 +520,7 @@ int main() {
485520
realInTest("(DC,F18.0)", " 12,5", 0x4029000000000000);
486521

487522
listInputTest();
523+
descrOutputTest();
488524

489525
return EndTests();
490526
}

0 commit comments

Comments
 (0)