Thanks to visit codestin.com
Credit goes to github.com

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

Conversation

akuhlens
Copy link
Contributor

closes #135805

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels Apr 22, 2025
@akuhlens akuhlens requested a review from klausler April 22, 2025 22:17
@llvmbot
Copy link
Member

llvmbot commented Apr 22, 2025

@llvm/pr-subscribers-flang-semantics

Author: Andre Kuhlenschmidt (akuhlens)

Changes

closes #135805


Full diff: https://github.com/llvm/llvm-project/pull/136778.diff

5 Files Affected:

  • (modified) flang/include/flang/Support/Fortran-features.h (+1-1)
  • (modified) flang/lib/Semantics/pointer-assignment.cpp (+14)
  • (modified) flang/lib/Support/Fortran-features.cpp (+1)
  • (modified) flang/test/Semantics/assign02.f90 (+34)
  • (modified) flang/test/Semantics/call03.f90 (+2)
diff --git a/flang/include/flang/Support/Fortran-features.h b/flang/include/flang/Support/Fortran-features.h
index 5b22313754a0f..6cb1bcdb0003f 100644
--- a/flang/include/flang/Support/Fortran-features.h
+++ b/flang/include/flang/Support/Fortran-features.h
@@ -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>;
diff --git a/flang/lib/Semantics/pointer-assignment.cpp b/flang/lib/Semantics/pointer-assignment.cpp
index ab3771c808761..36c9c5b845706 100644
--- a/flang/lib/Semantics/pointer-assignment.cpp
+++ b/flang/lib/Semantics/pointer-assignment.cpp
@@ -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);
diff --git a/flang/lib/Support/Fortran-features.cpp b/flang/lib/Support/Fortran-features.cpp
index b3cb62e62f5fb..49a5989849eaa 100644
--- a/flang/lib/Support/Fortran-features.cpp
+++ b/flang/lib/Support/Fortran-features.cpp
@@ -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);
diff --git a/flang/test/Semantics/assign02.f90 b/flang/test/Semantics/assign02.f90
index 6775506c21a3b..d83d126e2734c 100644
--- a/flang/test/Semantics/assign02.f90
+++ b/flang/test/Semantics/assign02.f90
@@ -9,6 +9,9 @@ module m1
     sequence
     real :: t2Field
   end type
+  type t3
+    type(t2) :: t3Field
+  end type
 contains
 
   ! C852
@@ -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
@@ -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
diff --git a/flang/test/Semantics/call03.f90 b/flang/test/Semantics/call03.f90
index 8f1be1ebff4eb..59513557324e5 100644
--- a/flang/test/Semantics/call03.f90
+++ b/flang/test/Semantics/call03.f90
@@ -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

@akuhlens akuhlens merged commit 42f5d71 into llvm:main May 1, 2025
14 checks passed
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[flang] Non-volatile pointer can point to volatile target
5 participants