Skip to content

Commit e4dd3fd

Browse files
akuhlensGeorgeARM
authored andcommitted
[flang] fix scoping of cray pointer declarations and add check for initialization (llvm#136776)
This PR: - makes Cray pointer declarations shadow previous bindings instead of modifying them, - errors when the pointee of a cray pointee has the SAVE attribute, and - adds a missing newline after dumping the list of cray pointers in a scope. Closes llvm#135579
1 parent 2564b9d commit e4dd3fd

File tree

6 files changed

+85
-3
lines changed

6 files changed

+85
-3
lines changed

flang/lib/Semantics/check-declarations.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,18 @@ void CheckHelper::CheckObjectEntity(
963963
"'%s' is a data object and may not be EXTERNAL"_err_en_US,
964964
symbol.name());
965965
}
966-
966+
if (symbol.test(Symbol::Flag::CrayPointee)) {
967+
// NB, IsSaved was too smart here.
968+
if (details.init()) {
969+
messages_.Say(
970+
"Cray pointee '%s' may not be initialized"_err_en_US, symbol.name());
971+
}
972+
if (symbol.attrs().test(Attr::SAVE)) {
973+
messages_.Say(
974+
"Cray pointee '%s' may not have the SAVE attribute"_err_en_US,
975+
symbol.name());
976+
}
977+
}
967978
if (derived) {
968979
bool isUnsavedLocal{
969980
isLocalVariable && !IsAllocatable(symbol) && !IsSaved(symbol)};

flang/lib/Semantics/resolve-names.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6650,7 +6650,7 @@ bool DeclarationVisitor::Pre(const parser::BasedPointer &) {
66506650

66516651
void DeclarationVisitor::Post(const parser::BasedPointer &bp) {
66526652
const parser::ObjectName &pointerName{std::get<0>(bp.t)};
6653-
auto *pointer{FindSymbol(pointerName)};
6653+
auto *pointer{FindInScope(pointerName)};
66546654
if (!pointer) {
66556655
pointer = &MakeSymbol(pointerName, ObjectEntityDetails{});
66566656
} else if (!ConvertToObjectEntity(*pointer)) {

flang/lib/Semantics/semantics.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,7 @@ void DoDumpSymbols(llvm::raw_ostream &os, const Scope &scope, int indent) {
731731
for (const auto &[pointee, pointer] : scope.crayPointers()) {
732732
os << " (" << pointer->name() << ',' << pointee << ')';
733733
}
734+
os << '\n';
734735
}
735736
for (const auto &pair : scope.commonBlocks()) {
736737
const auto &symbol{*pair.second};

flang/test/Lower/OpenMP/cray-pointers01.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ subroutine set_cray_pointer
3333
end module
3434

3535
program test_cray_pointers_01
36-
real*8, save :: var(*)
36+
real*8 :: var(*)
3737
! CHECK: %[[BOX_ALLOCA:.*]] = fir.alloca !fir.box<!fir.ptr<!fir.array<?xf64>>>
3838
! CHECK: %[[IVAR_ALLOCA:.*]] = fir.alloca i64 {bindc_name = "ivar", uniq_name = "_QFEivar"}
3939
! CHECK: %[[IVAR_DECL_01:.*]]:2 = hlfir.declare %[[IVAR_ALLOCA]] {uniq_name = "_QFEivar"} : (!fir.ref<i64>) -> (!fir.ref<i64>, !fir.ref<i64>)

flang/test/Semantics/declarations08.f90

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,10 @@
55
!ERROR: Cray pointee 'x' may not be a member of a COMMON block
66
common x
77
equivalence(y,z)
8+
!ERROR: Cray pointee 'v' may not be initialized
9+
real :: v = 42.0
10+
pointer(p,v)
11+
!ERROR: Cray pointee 'u' may not have the SAVE attribute
12+
save u
13+
pointer(p, u)
814
end

flang/test/Semantics/resolve125.f90

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
! RUN: %flang_fc1 -fdebug-dump-symbols %s 2>&1 | FileCheck %s
2+
3+
!CHECK: Module scope: m1
4+
!CHECK: i, PUBLIC size={{[0-9]+}} offset={{[0-9]+}}: ObjectEntity type: REAL({{[0-9]+}}) init:{{.+}}
5+
!CHECK: init, PUBLIC (Subroutine): Subprogram ()
6+
!CHECK: o, PUBLIC (CrayPointee) size={{[0-9]+}} offset={{[0-9]+}}: ObjectEntity type: REAL({{[0-9]+}})
7+
!CHECK: ptr, PUBLIC (CrayPointer) size={{[0-9]+}} offset={{[0-9]+}}: ObjectEntity type: INTEGER({{[0-9]+}})
8+
module m1
9+
implicit none
10+
real:: o
11+
real:: i = 42.0
12+
pointer (ptr, o)
13+
contains
14+
!CHECK: Subprogram scope: init
15+
subroutine init
16+
implicit none
17+
ptr=loc(i)
18+
print *, "init : o= ", o
19+
end subroutine init
20+
end module m1
21+
22+
!CHECK: Module scope: m2
23+
!CHECK: i, PUBLIC: Use from i in m1
24+
!CHECK: i2, PUBLIC size={{[0-9]+}} offset={{[0-9]+}}: ObjectEntity type: REAL({{[0-9]+}}) init:{{.+}}
25+
!CHECK: init, PUBLIC (Subroutine): Use from init in m1
26+
!CHECK: o, PUBLIC (CrayPointee): Use from o in m1
27+
!CHECK: ptr, PUBLIC (CrayPointer): Use from ptr in m1
28+
!CHECK: reset, PUBLIC (Subroutine): Subprogram ()
29+
module m2
30+
use m1
31+
implicit none
32+
real:: i2 = 777.0
33+
contains
34+
!CHECK: Subprogram scope: reset
35+
!CHECK: o2 (CrayPointee) size={{[0-9]+}} offset={{[0-9]+}}: ObjectEntity type: REAL({{[0-9]+}})
36+
!CHECK: ptr (CrayPointer) size={{[0-9]+}} offset={{[0-9]+}}: ObjectEntity type: INTEGER({{[0-9]+}})
37+
subroutine reset
38+
real::o2
39+
pointer (ptr, o2)
40+
ptr=loc(i2)
41+
print *, "reset : o= ", o, " o2 = ", o2
42+
o2 = 666.0
43+
end subroutine reset
44+
end module m2
45+
46+
!CHECK: MainProgram scope: main
47+
!CHECK: i: Use from i in m2
48+
!CHECK: i2: Use from i2 in m2
49+
!CHECK: init (Subroutine): Use from init in m2
50+
!CHECK: o (CrayPointee): Use from o in m2
51+
!CHECK: ptr (CrayPointer): Use from ptr in m2
52+
!CHECK: reset (Subroutine): Use from reset in m2
53+
program main
54+
use m2
55+
implicit none
56+
call init
57+
call reset
58+
write(6,*) "main : o = ", o
59+
if (o == 42.0) then
60+
print *, "pass"
61+
else
62+
print *, "fail"
63+
end if
64+
end program main

0 commit comments

Comments
 (0)