[flang-commits] [flang] 91c72e8 - [flang] Add a warning for CDEFINED declarations that have initializers (#159456)

via flang-commits flang-commits at lists.llvm.org
Wed Sep 17 19:06:20 PDT 2025


Author: Eugene Epshteyn
Date: 2025-09-17T22:06:16-04:00
New Revision: 91c72e8169208099f85d0129f25c3a706265bc19

URL: https://github.com/llvm/llvm-project/commit/91c72e8169208099f85d0129f25c3a706265bc19
DIFF: https://github.com/llvm/llvm-project/commit/91c72e8169208099f85d0129f25c3a706265bc19.diff

LOG: [flang] Add a warning for CDEFINED declarations that have initializers (#159456)

CDEFINED declarations are similar to "extern" declarations in C. If they
have initializers, this could lead to linker errors. clang warns about
"extern" declarations with initializers. Add similar warning to flang:
```
$ flang -c cdefined.f90 -pedantic
./cdefined.f90:3:57: warning: CDEFINED variable should not have an initializer [-Wcdefined-init]
    integer(c_int), bind(C, name='c_global', CDEFINED) :: c  = 4
                                                          ^
```

Added: 
    flang/test/Semantics/cdefined.f90

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

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Support/Fortran-features.h b/flang/include/flang/Support/Fortran-features.h
index 83a75b0efcb55..89d1dba7a2dc8 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,
+    IndexVarRedefinition, IncompatibleImplicitInterfaces, CdefinedInit,
     VectorSubscriptFinalization, UndefinedFunctionResult, UselessIomsg,
     MismatchingDummyProcedure, SubscriptedEmptyArray, UnsignedLiteralTruncation,
     CompatibleDeclarationsFromDistinctModules,

diff  --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index d0d3b0e1caa5a..cdd8d6ff2f60e 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -9118,6 +9118,9 @@ void DeclarationVisitor::NonPointerInitialization(
         if (details->init()) {
           SayWithDecl(name, *name.symbol,
               "'%s' has already been 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);
         } else if (IsAllocatable(ultimate)) {
           Say(name, "Allocatable object '%s' cannot be initialized"_err_en_US);
         } else if (ultimate.owner().IsParameterizedDerivedType()) {

diff  --git a/flang/test/Semantics/cdefined.f90 b/flang/test/Semantics/cdefined.f90
new file mode 100644
index 0000000000000..84103ce661d0b
--- /dev/null
+++ b/flang/test/Semantics/cdefined.f90
@@ -0,0 +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]
+  integer(c_int), bind(C, name='c_global', CDEFINED) :: c  = 42
+end


        


More information about the flang-commits mailing list