[flang-commits] [flang] dd3eb3f - [flang] Substrings with lower bound greater than upper bound

Peter Steinfeld via flang-commits flang-commits at lists.llvm.org
Wed Sep 16 14:57:20 PDT 2020


Author: Peter Steinfeld
Date: 2020-09-16T14:56:23-07:00
New Revision: dd3eb3f33239b23a12dd8864ae236390adf79550

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

LOG: [flang] Substrings with lower bound greater than upper bound

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.

Differential Revision: https://reviews.llvm.org/D87799

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/flang/lib/Evaluate/variable.cpp b/flang/lib/Evaluate/variable.cpp
index d87c71688f1a..c81f2b175ed5 100644
--- a/flang/lib/Evaluate/variable.cpp
+++ b/flang/lib/Evaluate/variable.cpp
@@ -204,9 +204,11 @@ std::optional<Expr<SomeCharacter>> Substring::Fold(FoldingContext &context) {
       *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};

diff  --git a/flang/test/Semantics/resolve49.f90 b/flang/test/Semantics/resolve49.f90
index b0bca059c041..5ead0784603b 100644
--- a/flang/test/Semantics/resolve49.f90
+++ b/flang/test/Semantics/resolve49.f90
@@ -17,6 +17,7 @@ program p2
   end type
   character :: a(10)
   character :: b(5)
+  character :: c(0)
   integer :: n
   n = 3
   b = a(n:7)
@@ -26,6 +27,7 @@ program p2
   a(n+3:) = b
   a(:n+2) = b
   n = iachar(1_'ABCDEFGHIJ'(1:1))
+  c = 'ABCDEFGHIJ'(1:0)
 end
 
 ! Test pointer assignment with bounds


        


More information about the flang-commits mailing list