[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 17:25:56 PST 2023


This revision was automatically updated to reflect the committed changes.
Closed by commit rG73eb5dbd5f0b: [flang] Catch out-of-range constant arguments to CHAR/ACHAR (authored by klausler).

Changed prior to commit:
  https://reviews.llvm.org/D142754?vs=492864&id=492955#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D142754/new/

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
@@ -103,6 +103,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 s12(x,y)
     class(t), intent(in) :: x
     class(*), intent(in) :: y
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.492955.patch
Type: text/x-patch
Size: 3040 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230128/5069d698/attachment.bin>


More information about the flang-commits mailing list