Skip to content

[flang][frontend] warn when a volatile target is pointer associated with an non-volatile pointer #136778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion flang/include/flang/Support/Fortran-features.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ ENUM_CLASS(UsageWarning, Portability, PointerToUndefinable,
MismatchingDummyProcedure, SubscriptedEmptyArray, UnsignedLiteralTruncation,
CompatibleDeclarationsFromDistinctModules,
NullActualForDefaultIntentAllocatable, UseAssociationIntoSameNameSubprogram,
HostAssociatedIntentOutInSpecExpr)
HostAssociatedIntentOutInSpecExpr, NonVolatilePointerToVolatile)

using LanguageFeatures = EnumSet<LanguageFeature, LanguageFeature_enumSize>;
using UsageWarnings = EnumSet<UsageWarning, UsageWarning_enumSize>;
Expand Down
14 changes: 14 additions & 0 deletions flang/lib/Semantics/pointer-assignment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,20 @@ bool PointerAssignmentChecker::Check(const evaluate::Designator<T> &d) {
} else {
Say(std::get<MessageFormattedText>(*msg));
}
}

// Show warnings after errors

// 8.5.20(3) A pointer should have the VOLATILE attribute if its target has
// the VOLATILE attribute
// 8.5.20(4) If an object has the VOLATILE attribute, then all of its
// subobjects also have the VOLATILE attribute.
if (!isVolatile_ && base->attrs().test(Attr::VOLATILE)) {
Warn(common::UsageWarning::NonVolatilePointerToVolatile,
"VOLATILE target associated with non-VOLATILE pointer"_warn_en_US);
}

if (msg) {
return false;
} else {
context_.NoteDefinedSymbol(*base);
Expand Down
1 change: 1 addition & 0 deletions flang/lib/Support/Fortran-features.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ LanguageFeatureControl::LanguageFeatureControl() {
warnUsage_.set(UsageWarning::NullActualForDefaultIntentAllocatable);
warnUsage_.set(UsageWarning::UseAssociationIntoSameNameSubprogram);
warnUsage_.set(UsageWarning::HostAssociatedIntentOutInSpecExpr);
warnUsage_.set(UsageWarning::NonVolatilePointerToVolatile);
// New warnings, on by default
warnLanguage_.set(LanguageFeature::SavedLocalInSpecExpr);
warnLanguage_.set(LanguageFeature::NullActualForAllocatable);
Expand Down
34 changes: 34 additions & 0 deletions flang/test/Semantics/assign02.f90
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ module m1
sequence
real :: t2Field
end type
type t3
type(t2) :: t3Field
end type
contains

! C852
Expand Down Expand Up @@ -80,6 +83,7 @@ subroutine s5
real, pointer, volatile :: q
p => x
!ERROR: Pointer must be VOLATILE when target is a VOLATILE coarray
!ERROR: VOLATILE target associated with non-VOLATILE pointer
p => y
!ERROR: Pointer may not be VOLATILE when target is a non-VOLATILE coarray
q => x
Expand Down Expand Up @@ -165,6 +169,36 @@ subroutine s11
ca[1]%p => x
end

subroutine s12
real, volatile, target :: x
real, pointer :: p
real, pointer, volatile :: q
!ERROR: VOLATILE target associated with non-VOLATILE pointer
p => x
q => x
end

subroutine s13
type(t3), target, volatile :: y = t3(t2(4.4))
real, pointer :: p1
type(t2), pointer :: p2
type(t3), pointer :: p3
real, pointer, volatile :: q1
type(t2), pointer, volatile :: q2
type(t3), pointer, volatile :: q3
!ERROR: VOLATILE target associated with non-VOLATILE pointer
p1 => y%t3Field%t2Field
!ERROR: VOLATILE target associated with non-VOLATILE pointer
p2 => y%t3Field
!ERROR: VOLATILE target associated with non-VOLATILE pointer
p3 => y
!OK:
q1 => y%t3Field%t2Field
!OK:
q2 => y%t3Field
!OK:
q3 => y
end
end

module m2
Expand Down
2 changes: 2 additions & 0 deletions flang/test/Semantics/call03.f90
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,9 @@ subroutine test16() ! C1540
call contiguous(a) ! ok
call pointer(a) ! ok
call pointer(b) ! ok
!ERROR: VOLATILE target associated with non-VOLATILE pointer
call pointer(c) ! ok
!ERROR: VOLATILE target associated with non-VOLATILE pointer
call pointer(d) ! ok
call valueassumedsize(a) ! ok
call valueassumedsize(b) ! ok
Expand Down
Loading