Skip to content

Commit aea94c9

Browse files
authored
[flang] Adjust checks of ICHAR/IACHAR argument length (#72312)
The compiler will now emit an error for length == 0 and an off-by-default portability warning for length > 1. Previously, the message was an unconditional warning for length /= 1.
1 parent ae485e6 commit aea94c9

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

flang/lib/Evaluate/fold-integer.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -677,10 +677,16 @@ Expr<Type<TypeCategory::Integer, KIND>> FoldIntrinsicFunction(
677677
auto *someChar{UnwrapExpr<Expr<SomeCharacter>>(args[0])};
678678
CHECK(someChar);
679679
if (auto len{ToInt64(someChar->LEN())}) {
680-
if (len.value() != 1) {
680+
if (len.value() < 1) {
681+
context.messages().Say(
682+
"Character in intrinsic function %s must have length one"_err_en_US,
683+
name);
684+
} else if (len.value() > 1 &&
685+
context.languageFeatures().ShouldWarn(
686+
common::UsageWarning::Portability)) {
681687
// Do not die, this was not checked before
682688
context.messages().Say(
683-
"Character in intrinsic function %s must have length one"_warn_en_US,
689+
"Character in intrinsic function %s should have length one"_port_en_US,
684690
name);
685691
} else {
686692
return common::visit(

flang/test/Semantics/ichar01.f90

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic
2+
!ERROR: Character in intrinsic function ichar must have length one
3+
print *, ichar('')
4+
!ERROR: Character in intrinsic function iachar must have length one
5+
print *, iachar('')
6+
print *, ichar('a')
7+
print *, iachar('a')
8+
!PORTABILITY: Character in intrinsic function ichar should have length one
9+
print *, ichar('ab')
10+
!PORTABILITY: Character in intrinsic function iachar should have length one
11+
print *, iachar('ab')
12+
end
13+

0 commit comments

Comments
 (0)