[flang-commits] [PATCH] D142754: [flang] Catch out-of-range constant arguments to CHAR/ACHAR

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Fri Jan 27 09:33:26 PST 2023


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

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.


https://reviews.llvm.org/D142754

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


Index: flang/test/Evaluate/errors01.f90
===================================================================
--- flang/test/Evaluate/errors01.f90
+++ flang/test/Evaluate/errors01.f90
@@ -100,6 +100,32 @@
     !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 warnings
     real, parameter :: ok1 = scale(0.0, 99999) ! 0.0
     real, parameter :: ok2 = scale(1.0, -99999) ! 0.0
Index: flang/lib/Evaluate/fold-character.cpp
===================================================================
--- flang/lib/Evaluate/fold-character.cpp
+++ flang/lib/Evaluate/fold-character.cpp
@@ -56,7 +56,13 @@
   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") {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D142754.492809.patch
Type: text/x-patch
Size: 3087 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230127/84ace34e/attachment-0001.bin>


More information about the flang-commits mailing list