[PATCH] D87799: [flang] Substrings with lower bound greater than upper bound

Pete Steinfeld via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 16 14:49:57 PDT 2020


PeteSteinfeld created this revision.
PeteSteinfeld added reviewers: klausler, tskeith.
Herald added a reviewer: DavidTruby.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
PeteSteinfeld requested review of this revision.

According to section 9.4.1, paragraph 3,
 If the starting point is greater than the ending point, the substring has
 length zero

But the compilers code for substring processing was failing a call to `CHECK()`
in this case.  I fixed this by just setting the number of items in the
resulting string to 0 for this situation.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D87799

Files:
  flang/lib/Evaluate/variable.cpp
  flang/test/Semantics/resolve49.f90


Index: flang/test/Semantics/resolve49.f90
===================================================================
--- flang/test/Semantics/resolve49.f90
+++ flang/test/Semantics/resolve49.f90
@@ -17,6 +17,7 @@
   end type
   character :: a(10)
   character :: b(5)
+  character :: c(0)
   integer :: n
   n = 3
   b = a(n:7)
@@ -26,6 +27,7 @@
   a(n+3:) = b
   a(:n+2) = b
   n = iachar(1_'ABCDEFGHIJ'(1:1))
+  c = 'ABCDEFGHIJ'(1:0)
 end
 
 ! Test pointer assignment with bounds
Index: flang/lib/Evaluate/variable.cpp
===================================================================
--- flang/lib/Evaluate/variable.cpp
+++ flang/lib/Evaluate/variable.cpp
@@ -204,9 +204,11 @@
       *ubi = *length;
     }
     if (lbi && literal) {
-      CHECK(*ubi >= *lbi);
       auto newStaticData{StaticDataObject::Create()};
-      auto items{*ubi - *lbi + 1};
+      auto items{0}; // If the lower bound is greater, the length is 0
+      if (*ubi >= *lbi) {
+        items = *ubi - *lbi + 1;
+      }
       auto width{(*literal)->itemBytes()};
       auto bytes{items * width};
       auto startByte{(*lbi - 1) * width};


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D87799.292355.patch
Type: text/x-patch
Size: 1114 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200916/824b1841/attachment.bin>


More information about the llvm-commits mailing list