Skip to content

Commit 91c72e8

Browse files
[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 ^ ```
1 parent 44b7abc commit 91c72e8

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

flang/include/flang/Support/Fortran-features.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ ENUM_CLASS(UsageWarning, Portability, PointerToUndefinable,
7373
ZeroDoStep, UnusedForallIndex, OpenMPUsage, DataLength, IgnoredDirective,
7474
HomonymousSpecific, HomonymousResult, IgnoredIntrinsicFunctionType,
7575
PreviousScalarUse, RedeclaredInaccessibleComponent, ImplicitShared,
76-
IndexVarRedefinition, IncompatibleImplicitInterfaces,
76+
IndexVarRedefinition, IncompatibleImplicitInterfaces, CdefinedInit,
7777
VectorSubscriptFinalization, UndefinedFunctionResult, UselessIomsg,
7878
MismatchingDummyProcedure, SubscriptedEmptyArray, UnsignedLiteralTruncation,
7979
CompatibleDeclarationsFromDistinctModules,

flang/lib/Semantics/resolve-names.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9118,6 +9118,9 @@ void DeclarationVisitor::NonPointerInitialization(
91189118
if (details->init()) {
91199119
SayWithDecl(name, *name.symbol,
91209120
"'%s' has already been initialized"_err_en_US);
9121+
} else if (details->isCDefined()) {
9122+
context().Warn(common::UsageWarning::CdefinedInit, name.source,
9123+
"CDEFINED variable should not have an initializer"_warn_en_US);
91219124
} else if (IsAllocatable(ultimate)) {
91229125
Say(name, "Allocatable object '%s' cannot be initialized"_err_en_US);
91239126
} else if (ultimate.owner().IsParameterizedDerivedType()) {

flang/test/Semantics/cdefined.f90

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic -Werror
2+
module m
3+
use iso_c_binding
4+
!WARNING: CDEFINED variable should not have an initializer [-Wcdefined-init]
5+
integer(c_int), bind(C, name='c_global', CDEFINED) :: c = 42
6+
end

0 commit comments

Comments
 (0)