Skip to content

Commit 73eb5db

Browse files
committed
[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
1 parent d68e568 commit 73eb5db

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

flang/lib/Evaluate/fold-character.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ Expr<Type<TypeCategory::Character, KIND>> FoldIntrinsicFunction(
5656
if (name == "achar" || name == "char") {
5757
using IntT = SubscriptInteger;
5858
return FoldElementalIntrinsic<T, IntT>(context, std::move(funcRef),
59-
ScalarFunc<T, IntT>([](const Scalar<IntT> &i) {
59+
ScalarFunc<T, IntT>([&](const Scalar<IntT> &i) {
60+
if (i.IsNegative() || i.BGE(Scalar<IntT>{0}.IBSET(8 * KIND))) {
61+
context.messages().Say(
62+
"%s(I=%jd) is out of range for CHARACTER(KIND=%d)"_warn_en_US,
63+
parser::ToUpperCaseLetters(name),
64+
static_cast<std::intmax_t>(i.ToInt64()), KIND);
65+
}
6066
return CharacterUtils<KIND>::CHAR(i.ToUInt64());
6167
}));
6268
} else if (name == "adjustl") {

flang/test/Evaluate/errors01.f90

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,32 @@ subroutine s9
103103
!CHECK: error: DIM=4 argument to SPREAD must be between 1 and 3
104104
integer, parameter :: bad3 = spread(matrix, 4, 1)
105105
end subroutine
106+
subroutine s10
107+
!CHECK: warning: CHAR(I=-1) is out of range for CHARACTER(KIND=1)
108+
character(kind=1), parameter :: badc11 = char(-1,kind=1)
109+
!CHECK: warning: ACHAR(I=-1) is out of range for CHARACTER(KIND=1)
110+
character(kind=1), parameter :: bada11 = achar(-1,kind=1)
111+
!CHECK: warning: CHAR(I=-1) is out of range for CHARACTER(KIND=2)
112+
character(kind=2), parameter :: badc21 = char(-1,kind=2)
113+
!CHECK: warning: ACHAR(I=-1) is out of range for CHARACTER(KIND=2)
114+
character(kind=2), parameter :: bada21 = achar(-1,kind=2)
115+
!CHECK: warning: CHAR(I=-1) is out of range for CHARACTER(KIND=4)
116+
character(kind=4), parameter :: badc41 = char(-1,kind=4)
117+
!CHECK: warning: ACHAR(I=-1) is out of range for CHARACTER(KIND=4)
118+
character(kind=4), parameter :: bada41 = achar(-1,kind=4)
119+
!CHECK: warning: CHAR(I=256) is out of range for CHARACTER(KIND=1)
120+
character(kind=1), parameter :: badc12 = char(256,kind=1)
121+
!CHECK: warning: ACHAR(I=256) is out of range for CHARACTER(KIND=1)
122+
character(kind=1), parameter :: bada12 = achar(256,kind=1)
123+
!CHECK: warning: CHAR(I=65536) is out of range for CHARACTER(KIND=2)
124+
character(kind=2), parameter :: badc22 = char(65536,kind=2)
125+
!CHECK: warning: ACHAR(I=65536) is out of range for CHARACTER(KIND=2)
126+
character(kind=2), parameter :: bada22 = achar(65536,kind=2)
127+
!CHECK: warning: CHAR(I=4294967296) is out of range for CHARACTER(KIND=4)
128+
character(kind=4), parameter :: badc42 = char(4294967296_8,kind=4)
129+
!CHECK: warning: ACHAR(I=4294967296) is out of range for CHARACTER(KIND=4)
130+
character(kind=4), parameter :: bada42 = achar(4294967296_8,kind=4)
131+
end subroutine
106132
subroutine s12(x,y)
107133
class(t), intent(in) :: x
108134
class(*), intent(in) :: y

flang/test/Evaluate/folding05.f90

491 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)