Skip to content

Commit 0e8d40e

Browse files
[flang] Fixed regression with CDEFINED linkage
#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.
1 parent 1fbfac3 commit 0e8d40e

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

flang/lib/Semantics/resolve-names.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9194,11 +9194,12 @@ bool DeclarationVisitor::CheckNonPointerInitialization(
91949194
"'%s' has already been initialized"_err_en_US);
91959195
} else if (IsAllocatable(ultimate)) {
91969196
Say(name, "Allocatable object '%s' cannot be initialized"_err_en_US);
9197+
} else if (details->isCDefined()) {
9198+
// CDEFINED variables cannot have initializer, because their storage
9199+
// may come outside of Fortran.
9200+
context().Warn(common::UsageWarning::CdefinedInit, name.source,
9201+
"CDEFINED variable cannot be initialized"_warn_en_US);
91979202
} else {
9198-
if (details->isCDefined()) {
9199-
context().Warn(common::UsageWarning::CdefinedInit, name.source,
9200-
"CDEFINED variable should not have an initializer"_warn_en_US);
9201-
}
92029203
return true;
92039204
}
92049205
} else {

flang/test/Lower/cdefined.f90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ module m
66
integer(c_int), bind(C, name='c_global', CDEFINED) :: c = 42
77
! CHECK: fir.global @c_global : i32
88
! CHECK-NOT: fir.zero_bits
9+
! CHECK-NOT: arith.constant 42
910
end

flang/test/Semantics/cdefined.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic -Werror
22
module m
33
use iso_c_binding
4-
!WARNING: CDEFINED variable should not have an initializer [-Wcdefined-init]
4+
!WARNING: CDEFINED variable cannot be initialized [-Wcdefined-init]
55
integer(c_int), bind(C, name='c_global', CDEFINED) :: c = 42
66
end

0 commit comments

Comments
 (0)