[flang-commits] [flang] ea8cdf3 - [flang] Fix a bug in INQUIRE(IOLENGTH=) output

V Donaldson via flang-commits flang-commits at lists.llvm.org
Tue Nov 16 11:33:25 PST 2021


Author: V Donaldson
Date: 2021-11-16T11:33:15-08:00
New Revision: ea8cdf322f3962765218829a0aece71e0bb5e989

URL: https://github.com/llvm/llvm-project/commit/ea8cdf322f3962765218829a0aece71e0bb5e989
DIFF: https://github.com/llvm/llvm-project/commit/ea8cdf322f3962765218829a0aece71e0bb5e989.diff

LOG: [flang] Fix a bug in INQUIRE(IOLENGTH=) output

The inquire by output list form of the INQUIRE statement calculates the
number of file storage units that would be required to store the data
of an output list in an unformatted file.  Currently, the result is
incorrectly multiplied by the number of bytes for a data type.  A query
for "INTEGER(KIND=4) A(10)" should be 40, not 160.

Update formatting.

Added: 
    

Modified: 
    flang/runtime/io-stmt.cpp
    flang/unittests/Runtime/ExternalIOTest.cpp

Removed: 
    


################################################################################
diff  --git a/flang/runtime/io-stmt.cpp b/flang/runtime/io-stmt.cpp
index 5d5714ac2b69..acb8e43cf624 100644
--- a/flang/runtime/io-stmt.cpp
+++ b/flang/runtime/io-stmt.cpp
@@ -25,17 +25,11 @@ bool IoStatementBase::Emit(const char *, std::size_t, std::size_t) {
   return false;
 }
 
-bool IoStatementBase::Emit(const char *, std::size_t) {
-  return false;
-}
+bool IoStatementBase::Emit(const char *, std::size_t) { return false; }
 
-bool IoStatementBase::Emit(const char16_t *, std::size_t) {
-  return false;
-}
+bool IoStatementBase::Emit(const char16_t *, std::size_t) { return false; }
 
-bool IoStatementBase::Emit(const char32_t *, std::size_t) {
-  return false;
-}
+bool IoStatementBase::Emit(const char32_t *, std::size_t) { return false; }
 
 std::size_t IoStatementBase::GetNextInputBytes(const char *&p) {
   p = nullptr;
@@ -71,9 +65,7 @@ bool IoStatementBase::Inquire(InquiryKeywordHash, char *, std::size_t) {
   return false;
 }
 
-bool IoStatementBase::Inquire(InquiryKeywordHash, bool &) {
-  return false;
-}
+bool IoStatementBase::Inquire(InquiryKeywordHash, bool &) { return false; }
 
 bool IoStatementBase::Inquire(InquiryKeywordHash, std::int64_t, bool &) {
   return false;
@@ -1248,9 +1240,8 @@ InquireIOLengthState::InquireIOLengthState(
     const char *sourceFile, int sourceLine)
     : NoUnitIoStatementState{sourceFile, sourceLine, *this} {}
 
-bool InquireIOLengthState::Emit(
-    const char *, std::size_t n, std::size_t elementBytes) {
-  bytes_ += n * elementBytes;
+bool InquireIOLengthState::Emit(const char *, std::size_t n, std::size_t) {
+  bytes_ += n;
   return true;
 }
 

diff  --git a/flang/unittests/Runtime/ExternalIOTest.cpp b/flang/unittests/Runtime/ExternalIOTest.cpp
index 070950c6132b..568a559fcd7f 100644
--- a/flang/unittests/Runtime/ExternalIOTest.cpp
+++ b/flang/unittests/Runtime/ExternalIOTest.cpp
@@ -12,6 +12,7 @@
 
 #include "CrashHandlerFixture.h"
 #include "gtest/gtest.h"
+#include "flang/Runtime/descriptor.h"
 #include "flang/Runtime/io-api.h"
 #include "flang/Runtime/main.h"
 #include "flang/Runtime/stop.h"
@@ -19,6 +20,7 @@
 #include <cstring>
 #include <string_view>
 
+using namespace Fortran::runtime;
 using namespace Fortran::runtime::io;
 
 struct ExternalIOTests : public CrashHandlerFixture {};
@@ -44,7 +46,7 @@ TEST(ExternalIOTests, TestDirectUnformatted) {
   // INQUIRE(IOLENGTH=) j
   io = IONAME(BeginInquireIoLength)(__FILE__, __LINE__);
   ASSERT_TRUE(IONAME(OutputUnformattedBlock)(
-      io, reinterpret_cast<const char *>(&buffer), 1, recl))
+      io, reinterpret_cast<const char *>(&buffer), recl, 1))
       << "OutputUnformattedBlock() for InquireIoLength";
   ASSERT_EQ(IONAME(GetIoLength)(io), recl) << "GetIoLength";
   ASSERT_EQ(IONAME(EndIoStatement)(io), IostatOk)
@@ -171,13 +173,28 @@ TEST(ExternalIOTests, TestSequentialFixedUnformatted) {
   io = IONAME(BeginInquireIoLength)(__FILE__, __LINE__);
   for (int j{1}; j <= 3; ++j) {
     ASSERT_TRUE(IONAME(OutputUnformattedBlock)(
-        io, reinterpret_cast<const char *>(&buffer), 1, recl))
+        io, reinterpret_cast<const char *>(&buffer), recl, 1))
         << "OutputUnformattedBlock() for InquireIoLength";
   }
   ASSERT_EQ(IONAME(GetIoLength)(io), 3 * recl) << "GetIoLength";
   ASSERT_EQ(IONAME(EndIoStatement)(io), IostatOk)
       << "EndIoStatement() for InquireIoLength";
 
+  // INQUIRE(IOLENGTH=) j, ...
+  StaticDescriptor<0> staticDescriptor;
+  Descriptor &desc{staticDescriptor.descriptor()};
+  desc.Establish(TypeCode{CFI_type_int64_t}, recl, &buffer, 0);
+  desc.Dump(stderr);
+  desc.Check();
+  io = IONAME(BeginInquireIoLength)(__FILE__, __LINE__);
+  for (int j{1}; j <= 3; ++j) {
+    ASSERT_TRUE(IONAME(OutputDescriptor)(io, desc))
+        << "OutputDescriptor() for InquireIoLength";
+  }
+  ASSERT_EQ(IONAME(GetIoLength)(io), 3 * recl) << "GetIoLength";
+  ASSERT_EQ(IONAME(EndIoStatement)(io), IostatOk)
+      << "EndIoStatement() for InquireIoLength";
+
   static const int records{10};
   for (int j{1}; j <= records; ++j) {
     // DO J=1,RECORDS; WRITE(UNIT=unit) j; END DO


        


More information about the flang-commits mailing list