[flang-commits] [flang] [flang] Fixed regression with CDEFINED linkage (PR #164616)

Eugene Epshteyn via flang-commits flang-commits at lists.llvm.org
Wed Oct 22 11:18:40 PDT 2025


https://github.com/eugeneepshteyn updated https://github.com/llvm/llvm-project/pull/164616

>From 0e8d40eeac0a76e368255bb73b027eafbbafc9e3 Mon Sep 17 00:00:00 2001
From: Eugene Epshteyn <eepshteyn at nvidia.com>
Date: Wed, 22 Oct 2025 08:40:31 -0400
Subject: [PATCH 1/2] [flang] Fixed regression with CDEFINED linkage

https://github.com/llvm/llvm-project/pull/162722 introduced a regression that
started creating initializers for CDEFINED variables. CDEFINED variables
cannot have initializers, because their storage is expected come from
elsewhere, likely outside of Fortran. Fixed the regression and improved the
regression test to catch the incorrect initialization case.
---
 flang/lib/Semantics/resolve-names.cpp | 9 +++++----
 flang/test/Lower/cdefined.f90         | 1 +
 flang/test/Semantics/cdefined.f90     | 2 +-
 3 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index db75437708a6c..5cb1bc636a582 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -9194,11 +9194,12 @@ bool DeclarationVisitor::CheckNonPointerInitialization(
               "'%s' has already been initialized"_err_en_US);
         } else if (IsAllocatable(ultimate)) {
           Say(name, "Allocatable object '%s' cannot be initialized"_err_en_US);
+        } else if (details->isCDefined()) {
+          // CDEFINED variables cannot have initializer, because their storage
+          // may come outside of Fortran.
+          context().Warn(common::UsageWarning::CdefinedInit, name.source,
+              "CDEFINED variable cannot be initialized"_warn_en_US);
         } else {
-          if (details->isCDefined()) {
-            context().Warn(common::UsageWarning::CdefinedInit, name.source,
-                "CDEFINED variable should not have an initializer"_warn_en_US);
-          }
           return true;
         }
       } else {
diff --git a/flang/test/Lower/cdefined.f90 b/flang/test/Lower/cdefined.f90
index 89599442589eb..87a01b51460fc 100644
--- a/flang/test/Lower/cdefined.f90
+++ b/flang/test/Lower/cdefined.f90
@@ -6,4 +6,5 @@ module m
   integer(c_int), bind(C, name='c_global', CDEFINED) :: c  = 42
   ! CHECK: fir.global @c_global : i32
   ! CHECK-NOT: fir.zero_bits 
+  ! CHECK-NOT: arith.constant 42
 end
diff --git a/flang/test/Semantics/cdefined.f90 b/flang/test/Semantics/cdefined.f90
index 84103ce661d0b..9e0b0a485ee15 100644
--- a/flang/test/Semantics/cdefined.f90
+++ b/flang/test/Semantics/cdefined.f90
@@ -1,6 +1,6 @@
 ! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic -Werror
 module m
   use iso_c_binding
-  !WARNING: CDEFINED variable should not have an initializer [-Wcdefined-init]
+  !WARNING: CDEFINED variable cannot be initialized [-Wcdefined-init]
   integer(c_int), bind(C, name='c_global', CDEFINED) :: c  = 42
 end

>From b973c07fa656bb8f907606fa28c9212590eb0e7a Mon Sep 17 00:00:00 2001
From: Eugene Epshteyn <eepshteyn at nvidia.com>
Date: Wed, 22 Oct 2025 14:18:29 -0400
Subject: [PATCH 2/2] Removed CdefinedInit warning and changed CDEFINED
 initialization to hard error

---
 flang/include/flang/Support/Fortran-features.h | 2 +-
 flang/lib/Semantics/resolve-names.cpp          | 3 +--
 flang/test/Lower/cdefined.f90                  | 8 ++++----
 flang/test/Semantics/cdefined.f90              | 4 ++--
 4 files changed, 8 insertions(+), 9 deletions(-)

diff --git a/flang/include/flang/Support/Fortran-features.h b/flang/include/flang/Support/Fortran-features.h
index 51364d552be64..c7d0b7fca1d59 100644
--- a/flang/include/flang/Support/Fortran-features.h
+++ b/flang/include/flang/Support/Fortran-features.h
@@ -73,7 +73,7 @@ ENUM_CLASS(UsageWarning, Portability, PointerToUndefinable,
     ZeroDoStep, UnusedForallIndex, OpenMPUsage, DataLength, IgnoredDirective,
     HomonymousSpecific, HomonymousResult, IgnoredIntrinsicFunctionType,
     PreviousScalarUse, RedeclaredInaccessibleComponent, ImplicitShared,
-    IndexVarRedefinition, IncompatibleImplicitInterfaces, CdefinedInit,
+    IndexVarRedefinition, IncompatibleImplicitInterfaces,
     VectorSubscriptFinalization, UndefinedFunctionResult, UselessIomsg,
     MismatchingDummyProcedure, SubscriptedEmptyArray, UnsignedLiteralTruncation,
     CompatibleDeclarationsFromDistinctModules, ConstantIsContiguous,
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 5cb1bc636a582..f5f6037bcd8bd 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -9197,8 +9197,7 @@ bool DeclarationVisitor::CheckNonPointerInitialization(
         } else if (details->isCDefined()) {
           // CDEFINED variables cannot have initializer, because their storage
           // may come outside of Fortran.
-          context().Warn(common::UsageWarning::CdefinedInit, name.source,
-              "CDEFINED variable cannot be initialized"_warn_en_US);
+          Say(name, "CDEFINED variable cannot be initialized"_err_en_US);
         } else {
           return true;
         }
diff --git a/flang/test/Lower/cdefined.f90 b/flang/test/Lower/cdefined.f90
index 87a01b51460fc..748f8f701e58b 100644
--- a/flang/test/Lower/cdefined.f90
+++ b/flang/test/Lower/cdefined.f90
@@ -1,10 +1,10 @@
 ! RUN: bbc -emit-hlfir -o - %s | FileCheck %s
 ! Ensure that CDEFINED variable has external (default) linkage and that
-! it doesn't have an initializer
+! it doesn't have either zero or constant initializer.
 module m
   use iso_c_binding
-  integer(c_int), bind(C, name='c_global', CDEFINED) :: c  = 42
+  integer(c_int), bind(C, name='c_global', CDEFINED) :: c
   ! CHECK: fir.global @c_global : i32
-  ! CHECK-NOT: fir.zero_bits 
-  ! CHECK-NOT: arith.constant 42
+  ! CHECK-NOT: fir.zero_bits
+  ! CHECK-NOT: arith.constant
 end
diff --git a/flang/test/Semantics/cdefined.f90 b/flang/test/Semantics/cdefined.f90
index 9e0b0a485ee15..ef59e43c6b37e 100644
--- a/flang/test/Semantics/cdefined.f90
+++ b/flang/test/Semantics/cdefined.f90
@@ -1,6 +1,6 @@
-! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic -Werror
+! RUN: %python %S/test_errors.py %s %flang_fc1
 module m
   use iso_c_binding
-  !WARNING: CDEFINED variable cannot be initialized [-Wcdefined-init]
+  !ERROR: CDEFINED variable cannot be initialized
   integer(c_int), bind(C, name='c_global', CDEFINED) :: c  = 42
 end



More information about the flang-commits mailing list