Skip to content

Commit 42f5d71

Browse files
authored
[flang][frontend] warn when a volatile target is pointer associated with an non-volatile pointer (llvm#136778)
closes llvm#135805
1 parent 8d02529 commit 42f5d71

File tree

5 files changed

+52
-1
lines changed

5 files changed

+52
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ ENUM_CLASS(UsageWarning, Portability, PointerToUndefinable,
7676
MismatchingDummyProcedure, SubscriptedEmptyArray, UnsignedLiteralTruncation,
7777
CompatibleDeclarationsFromDistinctModules,
7878
NullActualForDefaultIntentAllocatable, UseAssociationIntoSameNameSubprogram,
79-
HostAssociatedIntentOutInSpecExpr)
79+
HostAssociatedIntentOutInSpecExpr, NonVolatilePointerToVolatile)
8080

8181
using LanguageFeatures = EnumSet<LanguageFeature, LanguageFeature_enumSize>;
8282
using UsageWarnings = EnumSet<UsageWarning, UsageWarning_enumSize>;

flang/lib/Semantics/pointer-assignment.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,20 @@ bool PointerAssignmentChecker::Check(const evaluate::Designator<T> &d) {
360360
} else {
361361
Say(std::get<MessageFormattedText>(*msg));
362362
}
363+
}
364+
365+
// Show warnings after errors
366+
367+
// 8.5.20(3) A pointer should have the VOLATILE attribute if its target has
368+
// the VOLATILE attribute
369+
// 8.5.20(4) If an object has the VOLATILE attribute, then all of its
370+
// subobjects also have the VOLATILE attribute.
371+
if (!isVolatile_ && base->attrs().test(Attr::VOLATILE)) {
372+
Warn(common::UsageWarning::NonVolatilePointerToVolatile,
373+
"VOLATILE target associated with non-VOLATILE pointer"_warn_en_US);
374+
}
375+
376+
if (msg) {
363377
return false;
364378
} else {
365379
context_.NoteDefinedSymbol(*base);

flang/lib/Support/Fortran-features.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ LanguageFeatureControl::LanguageFeatureControl() {
8787
warnUsage_.set(UsageWarning::NullActualForDefaultIntentAllocatable);
8888
warnUsage_.set(UsageWarning::UseAssociationIntoSameNameSubprogram);
8989
warnUsage_.set(UsageWarning::HostAssociatedIntentOutInSpecExpr);
90+
warnUsage_.set(UsageWarning::NonVolatilePointerToVolatile);
9091
// New warnings, on by default
9192
warnLanguage_.set(LanguageFeature::SavedLocalInSpecExpr);
9293
warnLanguage_.set(LanguageFeature::NullActualForAllocatable);

flang/test/Semantics/assign02.f90

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ module m1
99
sequence
1010
real :: t2Field
1111
end type
12+
type t3
13+
type(t2) :: t3Field
14+
end type
1215
contains
1316

1417
! C852
@@ -80,6 +83,7 @@ subroutine s5
8083
real, pointer, volatile :: q
8184
p => x
8285
!ERROR: Pointer must be VOLATILE when target is a VOLATILE coarray
86+
!ERROR: VOLATILE target associated with non-VOLATILE pointer
8387
p => y
8488
!ERROR: Pointer may not be VOLATILE when target is a non-VOLATILE coarray
8589
q => x
@@ -165,6 +169,36 @@ subroutine s11
165169
ca[1]%p => x
166170
end
167171

172+
subroutine s12
173+
real, volatile, target :: x
174+
real, pointer :: p
175+
real, pointer, volatile :: q
176+
!ERROR: VOLATILE target associated with non-VOLATILE pointer
177+
p => x
178+
q => x
179+
end
180+
181+
subroutine s13
182+
type(t3), target, volatile :: y = t3(t2(4.4))
183+
real, pointer :: p1
184+
type(t2), pointer :: p2
185+
type(t3), pointer :: p3
186+
real, pointer, volatile :: q1
187+
type(t2), pointer, volatile :: q2
188+
type(t3), pointer, volatile :: q3
189+
!ERROR: VOLATILE target associated with non-VOLATILE pointer
190+
p1 => y%t3Field%t2Field
191+
!ERROR: VOLATILE target associated with non-VOLATILE pointer
192+
p2 => y%t3Field
193+
!ERROR: VOLATILE target associated with non-VOLATILE pointer
194+
p3 => y
195+
!OK:
196+
q1 => y%t3Field%t2Field
197+
!OK:
198+
q2 => y%t3Field
199+
!OK:
200+
q3 => y
201+
end
168202
end
169203

170204
module m2

flang/test/Semantics/call03.f90

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,9 @@ subroutine test16() ! C1540
386386
call contiguous(a) ! ok
387387
call pointer(a) ! ok
388388
call pointer(b) ! ok
389+
!ERROR: VOLATILE target associated with non-VOLATILE pointer
389390
call pointer(c) ! ok
391+
!ERROR: VOLATILE target associated with non-VOLATILE pointer
390392
call pointer(d) ! ok
391393
call valueassumedsize(a) ! ok
392394
call valueassumedsize(b) ! ok

0 commit comments

Comments
 (0)