[flang-commits] [flang] 4acd8f7 - [flang] Detect and rewrite ambiguous READ(CVAR)[, item-list]

peter klausler via flang-commits flang-commits at lists.llvm.org
Fri Oct 30 15:47:49 PDT 2020


Author: peter klausler
Date: 2020-10-30T15:47:28-07:00
New Revision: 4acd8f7f0a7ef4009fe039297335b931ab9e62d0

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

LOG: [flang] Detect and rewrite ambiguous READ(CVAR)[,item-list]

READ(CVAR)[,item-list] with a character variable CVAR
could be parsed as an unformatted READ from an internal
unit or as a formatted READ from the default external unit
with a needlessly parenthesized variable format.  We parse
it as the former, but Fortran doesn't have unformatted
internal I/O.

Differential revision: https://reviews.llvm.org/D90493

Added: 
    flang/test/Semantics/rewrite01.f90

Modified: 
    flang/lib/Parser/io-parsers.cpp
    flang/lib/Semantics/rewrite-parse-tree.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Parser/io-parsers.cpp b/flang/lib/Parser/io-parsers.cpp
index 3615501a98ed..0aab883ea8f7 100644
--- a/flang/lib/Parser/io-parsers.cpp
+++ b/flang/lib/Parser/io-parsers.cpp
@@ -139,6 +139,10 @@ TYPE_CONTEXT_PARSER("CLOSE statement"_en_US,
 // R1210 read-stmt ->
 //         READ ( io-control-spec-list ) [input-item-list] |
 //         READ format [, input-item-list]
+// The ambiguous READ(CVAR) is parsed as if CVAR were the unit.
+// As Fortran doesn't have internal unformatted I/O, it should
+// be parsed as if (CVAR) were a format; this is corrected by
+// rewriting in semantics when we know that CVAR is character.
 constexpr auto inputItemList{
     extension<LanguageFeature::IOListLeadingComma>(
         some("," >> inputItem)) || // legacy extension: leading comma

diff  --git a/flang/lib/Semantics/rewrite-parse-tree.cpp b/flang/lib/Semantics/rewrite-parse-tree.cpp
index 42962c8694f1..706e28c722c0 100644
--- a/flang/lib/Semantics/rewrite-parse-tree.cpp
+++ b/flang/lib/Semantics/rewrite-parse-tree.cpp
@@ -146,7 +146,25 @@ void FixMisparsedUntaggedNamelistName(READ_OR_WRITE &x) {
   }
 }
 
+// READ(CVAR) [, ...] will be misparsed as UNIT=CVAR; correct
+// it to READ CVAR [,...] with CVAR as a format rather than as
+// an internal I/O unit for unformatted I/O, which Fortran does
+// not support.
 void RewriteMutator::Post(parser::ReadStmt &x) {
+  if (x.iounit && !x.format && x.controls.empty()) {
+    if (auto *var{std::get_if<parser::Variable>(&x.iounit->u)}) {
+      const parser::Name &last{parser::GetLastName(*var)};
+      DeclTypeSpec *type{last.symbol ? last.symbol->GetType() : nullptr};
+      if (type && type->category() == DeclTypeSpec::Character) {
+        x.format = std::visit(
+            [](auto &&indirection) {
+              return parser::Expr{std::move(indirection)};
+            },
+            std::move(var->u));
+        x.iounit.reset();
+      }
+    }
+  }
   FixMisparsedUntaggedNamelistName(x);
 }
 

diff  --git a/flang/test/Semantics/rewrite01.f90 b/flang/test/Semantics/rewrite01.f90
new file mode 100644
index 000000000000..221994593422
--- /dev/null
+++ b/flang/test/Semantics/rewrite01.f90
@@ -0,0 +1,10 @@
+! RUN: %f18 -fparse-only -fdebug-dump-parse-tree %s 2>&1 | FileCheck %s
+! Ensure that READ(CVAR) [, item-list] is corrected when CVAR is a
+! character variable so as to be a formatted read from the default
+! unit, not an unformatted read from an internal unit (which is not
+! possible in Fortran).
+character :: cvar
+! CHECK-NOT: IoUnit -> Variable -> Designator -> DataRef -> Name = 'cvar'
+! CHECK: Format -> Expr = 'cvar'
+read(cvar)
+end


        


More information about the flang-commits mailing list