From f9307065ef1a5b77edafb318661d57788b851dfd Mon Sep 17 00:00:00 2001 From: Eugene Epshteyn Date: Wed, 17 Sep 2025 17:12:14 -0400 Subject: [PATCH 1/2] [flang] Add a warning for CDEFINED declarations that have initializers 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 ^ ``` --- flang/include/flang/Support/Fortran-features.h | 2 +- flang/lib/Semantics/resolve-names.cpp | 4 ++++ flang/test/Semantics/cdefined.f90 | 6 ++++++ 3 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 flang/test/Semantics/cdefined.f90 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..a47e9e98174fb 100644 --- a/flang/lib/Semantics/resolve-names.cpp +++ b/flang/lib/Semantics/resolve-names.cpp @@ -9118,6 +9118,10 @@ 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 From ae22529aead2e2b651c1d4f56b0913a8f58c8487 Mon Sep 17 00:00:00 2001 From: Eugene Epshteyn Date: Wed, 17 Sep 2025 17:15:14 -0400 Subject: [PATCH 2/2] clang-format --- flang/lib/Semantics/resolve-names.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp index a47e9e98174fb..cdd8d6ff2f60e 100644 --- a/flang/lib/Semantics/resolve-names.cpp +++ b/flang/lib/Semantics/resolve-names.cpp @@ -9119,8 +9119,7 @@ void DeclarationVisitor::NonPointerInitialization( SayWithDecl(name, *name.symbol, "'%s' has already been initialized"_err_en_US); } else if (details->isCDefined()) { - context().Warn(common::UsageWarning::CdefinedInit, - name.source, + 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);