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

via flang-commits flang-commits at lists.llvm.org
Mon Oct 27 15:26:12 PDT 2025


Author: Eugene Epshteyn
Date: 2025-10-27T18:26:08-04:00
New Revision: 9702ec056b5509238353bff71c8f8bb664d95e4c

URL: https://github.com/llvm/llvm-project/commit/9702ec056b5509238353bff71c8f8bb664d95e4c
DIFF: https://github.com/llvm/llvm-project/commit/9702ec056b5509238353bff71c8f8bb664d95e4c.diff

LOG: [flang] Fixed regression with CDEFINED linkage (#164616)

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.

Also, based on the code review feedback, made CDEFINED variable
initialization a hard error and updated tests accordingly.

Added: 
    

Modified: 
    flang/include/flang/Support/Fortran-features.h
    flang/lib/Semantics/resolve-names.cpp
    flang/test/Lower/cdefined.f90
    flang/test/Semantics/cdefined.f90

Removed: 
    


################################################################################
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 4af6cf6a91239..561ebd2a982c3 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -9194,11 +9194,11 @@ 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.
+          Say(name, "CDEFINED variable cannot be initialized"_err_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..748f8f701e58b 100644
--- a/flang/test/Lower/cdefined.f90
+++ b/flang/test/Lower/cdefined.f90
@@ -1,9 +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: fir.zero_bits
+  ! CHECK-NOT: arith.constant
 end

diff  --git a/flang/test/Semantics/cdefined.f90 b/flang/test/Semantics/cdefined.f90
index 84103ce661d0b..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 should not have an initializer [-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