Skip to content

Commit 4f2c6d3

Browse files
eugeneepshteynLukacma
authored andcommitted
[flang] Fixed regression with CDEFINED linkage (llvm#164616)
llvm#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. Also, based on the code review feedback, made CDEFINED variable initialization a hard error and updated tests accordingly.
1 parent 507f5f1 commit 4f2c6d3

File tree

4 files changed

+11
-10
lines changed

4 files changed

+11
-10
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, CdefinedInit,
76+
IndexVarRedefinition, IncompatibleImplicitInterfaces,
7777
VectorSubscriptFinalization, UndefinedFunctionResult, UselessIomsg,
7878
MismatchingDummyProcedure, SubscriptedEmptyArray, UnsignedLiteralTruncation,
7979
CompatibleDeclarationsFromDistinctModules, ConstantIsContiguous,

flang/lib/Semantics/resolve-names.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9194,11 +9194,11 @@ 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+
Say(name, "CDEFINED variable cannot be initialized"_err_en_US);
91979201
} 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-
}
92029202
return true;
92039203
}
92049204
} else {

flang/test/Lower/cdefined.f90

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
! RUN: bbc -emit-hlfir -o - %s | FileCheck %s
22
! Ensure that CDEFINED variable has external (default) linkage and that
3-
! it doesn't have an initializer
3+
! it doesn't have either zero or constant initializer.
44
module m
55
use iso_c_binding
6-
integer(c_int), bind(C, name='c_global', CDEFINED) :: c = 42
6+
integer(c_int), bind(C, name='c_global', CDEFINED) :: c
77
! CHECK: fir.global @c_global : i32
8-
! CHECK-NOT: fir.zero_bits
8+
! CHECK-NOT: fir.zero_bits
9+
! CHECK-NOT: arith.constant
910
end

flang/test/Semantics/cdefined.f90

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

0 commit comments

Comments
 (0)