[flang-commits] [flang] [flang] Adjust checks of ICHAR/IACHAR argument length (PR #72312)

via flang-commits flang-commits at lists.llvm.org
Tue Nov 14 13:30:40 PST 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-semantics

Author: Peter Klausler (klausler)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/72312.diff


2 Files Affected:

- (modified) flang/lib/Evaluate/fold-integer.cpp (+8-2) 
- (added) flang/test/Semantics/ichar01.f90 (+13) 


``````````diff
diff --git a/flang/lib/Evaluate/fold-integer.cpp b/flang/lib/Evaluate/fold-integer.cpp
index dedfc20a491cd88..489102f5277f1f0 100644
--- a/flang/lib/Evaluate/fold-integer.cpp
+++ b/flang/lib/Evaluate/fold-integer.cpp
@@ -676,10 +676,16 @@ Expr<Type<TypeCategory::Integer, KIND>> FoldIntrinsicFunction(
     auto *someChar{UnwrapExpr<Expr<SomeCharacter>>(args[0])};
     CHECK(someChar);
     if (auto len{ToInt64(someChar->LEN())}) {
-      if (len.value() != 1) {
+      if (len.value() < 1) {
+        context.messages().Say(
+            "Character in intrinsic function %s must have length one"_err_en_US,
+            name);
+      } else if (len.value() > 1 &&
+          context.languageFeatures().ShouldWarn(
+              common::UsageWarning::Portability)) {
         // Do not die, this was not checked before
         context.messages().Say(
-            "Character in intrinsic function %s must have length one"_warn_en_US,
+            "Character in intrinsic function %s should have length one"_port_en_US,
             name);
       } else {
         return common::visit(
diff --git a/flang/test/Semantics/ichar01.f90 b/flang/test/Semantics/ichar01.f90
new file mode 100644
index 000000000000000..5eb25a97e6d939e
--- /dev/null
+++ b/flang/test/Semantics/ichar01.f90
@@ -0,0 +1,13 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic
+!ERROR: Character in intrinsic function ichar must have length one
+print *, ichar('')
+!ERROR: Character in intrinsic function iachar must have length one
+print *, iachar('')
+print *, ichar('a')
+print *, iachar('a')
+!PORTABILITY: Character in intrinsic function ichar should have length one
+print *, ichar('ab')
+!PORTABILITY: Character in intrinsic function iachar should have length one
+print *, iachar('ab')
+end
+

``````````

</details>


https://github.com/llvm/llvm-project/pull/72312


More information about the flang-commits mailing list