[flang-commits] [flang] 73eb5db - [flang] Catch out-of-range constant arguments to CHAR/ACHAR

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Fri Jan 27 17:25:46 PST 2023


Author: Peter Klausler
Date: 2023-01-27T17:25:33-08:00
New Revision: 73eb5dbd5f0b039332f3399b26a7415a45821590

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

LOG: [flang] Catch out-of-range constant arguments to CHAR/ACHAR

When folding the intrinsic functions CHAR and ACHAR, emit an
error message if the argument is out of the valid range for the
kind of the result.

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

Added: 
    

Modified: 
    flang/lib/Evaluate/fold-character.cpp
    flang/test/Evaluate/errors01.f90
    flang/test/Evaluate/folding05.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Evaluate/fold-character.cpp b/flang/lib/Evaluate/fold-character.cpp
index de058b69625d..56214ea14ef3 100644
--- a/flang/lib/Evaluate/fold-character.cpp
+++ b/flang/lib/Evaluate/fold-character.cpp
@@ -56,7 +56,13 @@ Expr<Type<TypeCategory::Character, KIND>> FoldIntrinsicFunction(
   if (name == "achar" || name == "char") {
     using IntT = SubscriptInteger;
     return FoldElementalIntrinsic<T, IntT>(context, std::move(funcRef),
-        ScalarFunc<T, IntT>([](const Scalar<IntT> &i) {
+        ScalarFunc<T, IntT>([&](const Scalar<IntT> &i) {
+          if (i.IsNegative() || i.BGE(Scalar<IntT>{0}.IBSET(8 * KIND))) {
+            context.messages().Say(
+                "%s(I=%jd) is out of range for CHARACTER(KIND=%d)"_warn_en_US,
+                parser::ToUpperCaseLetters(name),
+                static_cast<std::intmax_t>(i.ToInt64()), KIND);
+          }
           return CharacterUtils<KIND>::CHAR(i.ToUInt64());
         }));
   } else if (name == "adjustl") {

diff  --git a/flang/test/Evaluate/errors01.f90 b/flang/test/Evaluate/errors01.f90
index ce5e40c28294..d2fcc95cfdab 100644
--- a/flang/test/Evaluate/errors01.f90
+++ b/flang/test/Evaluate/errors01.f90
@@ -103,6 +103,32 @@ subroutine s9
     !CHECK: error: DIM=4 argument to SPREAD must be between 1 and 3
     integer, parameter :: bad3 = spread(matrix, 4, 1)
   end subroutine
+  subroutine s10
+    !CHECK: warning: CHAR(I=-1) is out of range for CHARACTER(KIND=1)
+    character(kind=1), parameter :: badc11 = char(-1,kind=1)
+    !CHECK: warning: ACHAR(I=-1) is out of range for CHARACTER(KIND=1)
+    character(kind=1), parameter :: bada11 = achar(-1,kind=1)
+    !CHECK: warning: CHAR(I=-1) is out of range for CHARACTER(KIND=2)
+    character(kind=2), parameter :: badc21 = char(-1,kind=2)
+    !CHECK: warning: ACHAR(I=-1) is out of range for CHARACTER(KIND=2)
+    character(kind=2), parameter :: bada21 = achar(-1,kind=2)
+    !CHECK: warning: CHAR(I=-1) is out of range for CHARACTER(KIND=4)
+    character(kind=4), parameter :: badc41 = char(-1,kind=4)
+    !CHECK: warning: ACHAR(I=-1) is out of range for CHARACTER(KIND=4)
+    character(kind=4), parameter :: bada41 = achar(-1,kind=4)
+    !CHECK: warning: CHAR(I=256) is out of range for CHARACTER(KIND=1)
+    character(kind=1), parameter :: badc12 = char(256,kind=1)
+    !CHECK: warning: ACHAR(I=256) is out of range for CHARACTER(KIND=1)
+    character(kind=1), parameter :: bada12 = achar(256,kind=1)
+    !CHECK: warning: CHAR(I=65536) is out of range for CHARACTER(KIND=2)
+    character(kind=2), parameter :: badc22 = char(65536,kind=2)
+    !CHECK: warning: ACHAR(I=65536) is out of range for CHARACTER(KIND=2)
+    character(kind=2), parameter :: bada22 = achar(65536,kind=2)
+    !CHECK: warning: CHAR(I=4294967296) is out of range for CHARACTER(KIND=4)
+    character(kind=4), parameter :: badc42 = char(4294967296_8,kind=4)
+    !CHECK: warning: ACHAR(I=4294967296) is out of range for CHARACTER(KIND=4)
+    character(kind=4), parameter :: bada42 = achar(4294967296_8,kind=4)
+  end subroutine
   subroutine s12(x,y)
     class(t), intent(in) :: x
     class(*), intent(in) :: y

diff  --git a/flang/test/Evaluate/folding05.f90 b/flang/test/Evaluate/folding05.f90
index 568912e1ab75..eb762775e641 100644
Binary files a/flang/test/Evaluate/folding05.f90 and b/flang/test/Evaluate/folding05.f90 
diff er


        


More information about the flang-commits mailing list