[flang-commits] [PATCH] D143819: [flang] Warn on mismatched DATA substring sizes rather than crashing

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Sat Feb 11 10:43:08 PST 2023


klausler created this revision.
klausler added a reviewer: vdonaldson.
klausler added a project: Flang.
Herald added subscribers: sunshaoce, jdoerfert.
Herald added a reviewer: sscalpone.
Herald added a project: All.
klausler requested review of this revision.

When a DATA statement initializes a substring with a character constant
of the wrong length, do the right thing with blank padding or truncation,
and emit a warning.  Current code is crashing due to an unhandled error
reported from the low-level data image initialization framework.


https://reviews.llvm.org/D143819

Files:
  flang/include/flang/Evaluate/initial-image.h
  flang/lib/Semantics/data-to-inits.cpp
  flang/test/Semantics/data17.f90


Index: flang/test/Semantics/data17.f90
===================================================================
--- /dev/null
+++ flang/test/Semantics/data17.f90
@@ -0,0 +1,11 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1
+character(4) a, b, c, d, e
+!WARNING: DATA statement value '"abcde"' for 'a' has the wrong length
+data a(1:4)/'abcde'/
+!WARNING: DATA statement value '"abc"' for 'b' has the wrong length
+data b(1:4)/'abc'/
+data c/'abcde'/ ! not a substring, conforms
+data d/'abc'/ ! not a substring, conforms
+!ERROR: DATA statement designator 'e(1_8:5_8)' is out of range
+data e(1:5)/'xyz'/
+end
Index: flang/lib/Semantics/data-to-inits.cpp
===================================================================
--- flang/lib/Semantics/data-to-inits.cpp
+++ flang/lib/Semantics/data-to-inits.cpp
@@ -448,6 +448,11 @@
       case evaluate::InitialImage::OutOfRange:
         OutOfRangeError();
         break;
+      case evaluate::InitialImage::SizeMismatch:
+        exprAnalyzer_.Say(
+            "DATA statement value '%s' for '%s' has the wrong length"_warn_en_US,
+            folded.AsFortran(), DescribeElement());
+        break;
       default:
         CHECK(exprAnalyzer_.context().AnyFatalError());
         break;
Index: flang/include/flang/Evaluate/initial-image.h
===================================================================
--- flang/include/flang/Evaluate/initial-image.h
+++ flang/include/flang/Evaluate/initial-image.h
@@ -72,20 +72,22 @@
       } else if (bytes == 0) {
         return Ok;
       } else {
+        Result result{Ok};
         for (auto at{x.lbounds()}; elements-- > 0; x.IncrementSubscripts(at)) {
           auto scalar{x.At(at)}; // this is a std string; size() in chars
-          // Subtle: an initializer for a substring may have been
-          // expanded to the length of the entire string.
           auto scalarBytes{scalar.size() * KIND};
-          if (scalarBytes < elementBytes ||
-              (scalarBytes > elementBytes && elements != 0)) {
-            return SizeMismatch;
+          if (scalarBytes != elementBytes) {
+            result = SizeMismatch;
+          }
+          // Blank padding when short
+          for (; scalarBytes < elementBytes; scalarBytes += KIND) {
+            scalar += ' ';
           }
           // TODO endianness
           std::memcpy(&data_.at(offset), scalar.data(), elementBytes);
           offset += elementBytes;
         }
-        return Ok;
+        return result;
       }
     }
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143819.496699.patch
Type: text/x-patch
Size: 2505 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230211/21f5a757/attachment-0001.bin>


More information about the flang-commits mailing list