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

via flang-commits flang-commits at lists.llvm.org
Wed Sep 17 15:50:40 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-semantics

Author: Eugene Epshteyn (eugeneepshteyn)

<details>
<summary>Changes</summary>

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
                                                          ^
```


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


3 Files Affected:

- (modified) flang/include/flang/Support/Fortran-features.h (+1-1) 
- (modified) flang/lib/Semantics/resolve-names.cpp (+3) 
- (added) flang/test/Semantics/cdefined.f90 (+6) 


``````````diff
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

``````````

</details>


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


More information about the flang-commits mailing list