[flang-commits] [flang] [flang][Semantics] Adding check integer kind on STAT= specifier #230463 (PR #231103)

Jean-Didier PAILLEUX via flang-commits flang-commits at lists.llvm.org
Mon Oct 12 02:15:44 PDT 2026


https://github.com/JDPailleux created https://github.com/llvm/llvm-project/pull/231103

This PR adds a pedantic warning to check whether the specified STAT is an integer of at least 16-bit because 
 flang currently silently accepts 8-bit integers to `STAT=`: and F2023 9.7.4 says:

> R946 _stat-variable_ is _scalar-int-variable_
>
> A stat-variable should have a decimal exponent range of at least four; otherwise the 
> processor-dependent error code might not be representable in the variable.


>From 12ff96e5f825467b03cf0530b59a8e375c5ac584 Mon Sep 17 00:00:00 2001
From: Jean-Didier PAILLEUX <jean-didier.pailleux at sipearl.com>
Date: Fri, 9 Oct 2026 18:59:58 +0200
Subject: [PATCH] [flang][Semantics] Adding -pedantic warning to check integer
 kind on STAT= specifier

---
 .../include/flang/Support/Fortran-features.h  |  2 +-
 flang/lib/Semantics/check-coarray.cpp         | 27 +++++++++++++++++++
 flang/test/Semantics/check-stat.f90           | 27 +++++++++++++++++++
 3 files changed, 55 insertions(+), 1 deletion(-)
 create mode 100644 flang/test/Semantics/check-stat.f90

diff --git a/flang/include/flang/Support/Fortran-features.h b/flang/include/flang/Support/Fortran-features.h
index 021d157ae1f8d..ac551a04c39fc 100644
--- a/flang/include/flang/Support/Fortran-features.h
+++ b/flang/include/flang/Support/Fortran-features.h
@@ -63,7 +63,7 @@ ENUM_CLASS(LanguageFeature, BackslashEscapes, OldDebugLines,
     CUDAImplicitDataAttrSpelling, OpenAccDefaultNoneScalarsStrict,
     OpenACCMultipleNamesInRoutine, EnumerationType, CUDAInit,
     PreferIntrinsicModuleUseAssociation, MultipleCommonBlockInit,
-    OutOfBoundsSubscripts)
+    OutOfBoundsSubscripts, StatKind)
 
 // Portability and suspicious usage warnings
 ENUM_CLASS(UsageWarning, Portability, PointerToUndefinable,
diff --git a/flang/lib/Semantics/check-coarray.cpp b/flang/lib/Semantics/check-coarray.cpp
index 00b97162ff0d4..4fdd181674778 100644
--- a/flang/lib/Semantics/check-coarray.cpp
+++ b/flang/lib/Semantics/check-coarray.cpp
@@ -115,10 +115,18 @@ static void CheckTeamType(
 static void CheckTeamStat(
     SemanticsContext &context, const parser::ImageSelectorSpec::Stat &stat) {
   const auto &var{parser::UnwrapRef<parser::Variable>(stat)};
+  auto at{parser::FindSourceLocation(var)};
   if (parser::GetCoindexedNamedObject(var)) {
     context.Say(parser::FindSourceLocation(var), // C931
         "Image selector STAT variable must not be a coindexed "
         "object"_err_en_US);
+  } else if (const auto *expr{GetExpr(context, var)}) {
+    if (auto type{expr->GetType()};
+        type && type->category() == TypeCategory::Integer &&
+        type->kind() < 2 && context.ShouldWarn(common::LanguageFeature::StatKind)) { // F2023 9.7.4 p1
+      context.Say(at,
+          "The stat-variable must have an integer kind of at least 16-bit"_warn_en_US);
+    }
   }
 }
 
@@ -145,6 +153,15 @@ static void CheckSyncStat(SemanticsContext &context,
               context.Say( // C1172
                   "The stat-variable in a sync-stat-list may not be repeated"_err_en_US);
             }
+            if (const auto *expr{GetExpr(context, stat)})
+              if (auto type{expr->GetType()}; type &&
+                  type->category() == TypeCategory::Integer &&
+                  type->kind() < 2 &&
+                  context.ShouldWarn(
+                      common::LanguageFeature::StatKind)) { // F2023 9.7.4 p1
+                context.Say(
+                    "The stat-variable must have an integer kind of at least 16-bit"_warn_en_US);
+              }
             gotStat = true;
           },
           [&](const parser::MsgVariable &var) {
@@ -258,6 +275,16 @@ static void CheckEventWaitSpecList(SemanticsContext &context,
                           context.Say( // C1178
                               "A stat-variable in a event-wait-spec-list may not be repeated"_err_en_US);
                         }
+                        if (const auto *expr{GetExpr(context, stat)})
+                          if (auto type{expr->GetType()}; type &&
+                              type->category() == TypeCategory::Integer &&
+                              type->kind() < 2 &&
+                              context.ShouldWarn(
+                                  common::LanguageFeature::StatKind)) { // F2023 9.7.4
+                                                                        // p1
+                            context.Say(
+                                "The stat-variable must have an integer kind of at least 16-bit"_warn_en_US);
+                          }
                         gotStat = true;
                       },
                       [&](const parser::MsgVariable &var) {
diff --git a/flang/test/Semantics/check-stat.f90 b/flang/test/Semantics/check-stat.f90
new file mode 100644
index 0000000000000..a975679635cbc
--- /dev/null
+++ b/flang/test/Semantics/check-stat.f90
@@ -0,0 +1,27 @@
+! RUN: %flang -fsyntax-only -pedantic %s 2>&1 | FileCheck %s
+
+program stat_test
+  use iso_c_binding
+  use iso_fortran_env
+  integer :: coarray[*]
+  type(event_type) :: e[*]
+  integer(C_INT8_T) :: myint8
+  integer(C_INT16_T) :: myint16
+ 
+  ! CHECK: warning:  The stat-variable must have an integer kind of at least 16-bit
+  coarray[1, stat=myint8] = 0 ! violates F2023 9.7.4
+  
+  ! CHECK: warning: The stat-variable must have an integer kind of at least 16-bit
+  sync all(stat=myint8) ! violates F2023 9.7.4
+  
+  ! CHECK: warning: The stat-variable must have an integer kind of at least 16-bit
+  event wait(e, stat=myint8) ! violates F2023 9.7.4
+  
+  ! CHECK-NOT: warning: The stat-variable must have an integer kind of at least 16-bit
+  coarray[1, stat=myint16] = 0 ! ok
+  
+  sync all(stat=myint16) ! ok
+  
+  event wait(e, stat=myint16) ! ok
+
+end program



More information about the flang-commits mailing list