Skip to content

Conversation

@klausler
Copy link
Contributor

There are a few well-meaning warnings for some cases of the FPTR= argument to C_F_POINTER() that can be false positives, since the restrictions in the standard are dependent on the source of the CPTR= argument. Further, there is no way to alter a program to avoid these warnings, so one cannot compile a correct and conforming program with -pedantic -Werror. Disable these warnings.

Fixes #167470.

There are a few well-meaning warnings for some cases of the
FPTR= argument to C_F_POINTER() that can be false positives,
since the restrictions in the standard are dependent on the
source of the CPTR= argument.  Further, there is no way to
alter a program to avoid these warnings, so one cannot compile
a correct and conforming program with -pedantic -Werror.
Disable these warnings.

Fixes llvm#167470.
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels Nov 12, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 12, 2025

@llvm/pr-subscribers-flang-semantics

Author: Peter Klausler (klausler)

Changes

There are a few well-meaning warnings for some cases of the FPTR= argument to C_F_POINTER() that can be false positives, since the restrictions in the standard are dependent on the source of the CPTR= argument. Further, there is no way to alter a program to avoid these warnings, so one cannot compile a correct and conforming program with -pedantic -Werror. Disable these warnings.

Fixes #167470.


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

2 Files Affected:

  • (modified) flang/lib/Evaluate/intrinsics.cpp (-22)
  • (modified) flang/test/Semantics/c_f_pointer.f90 (+8-4)
diff --git a/flang/lib/Evaluate/intrinsics.cpp b/flang/lib/Evaluate/intrinsics.cpp
index 1de5e6b53ba71..8f4204b1f9afe 100644
--- a/flang/lib/Evaluate/intrinsics.cpp
+++ b/flang/lib/Evaluate/intrinsics.cpp
@@ -3139,28 +3139,6 @@ IntrinsicProcTable::Implementation::HandleC_F_Pointer(
         if (type->HasDeferredTypeParameter()) {
           context.messages().Say(at,
               "FPTR= argument to C_F_POINTER() may not have a deferred type parameter"_err_en_US);
-        } else if (type->category() == TypeCategory::Derived) {
-          if (type->IsUnlimitedPolymorphic()) {
-            context.Warn(common::UsageWarning::Interoperability, at,
-                "FPTR= argument to C_F_POINTER() should not be unlimited polymorphic"_warn_en_US);
-          } else if (!type->GetDerivedTypeSpec().typeSymbol().attrs().test(
-                         semantics::Attr::BIND_C)) {
-            context.Warn(common::UsageWarning::Portability, at,
-                "FPTR= argument to C_F_POINTER() should not have a derived type that is not BIND(C)"_port_en_US);
-          }
-        } else if (!IsInteroperableIntrinsicType(
-                       *type, &context.languageFeatures())
-                        .value_or(true)) {
-          if (type->category() == TypeCategory::Character &&
-              type->kind() == 1) {
-            context.Warn(common::UsageWarning::CharacterInteroperability, at,
-                "FPTR= argument to C_F_POINTER() should not have the non-interoperable character length %s"_warn_en_US,
-                type->AsFortran());
-          } else {
-            context.Warn(common::UsageWarning::Interoperability, at,
-                "FPTR= argument to C_F_POINTER() should not have the non-interoperable intrinsic type or kind %s"_warn_en_US,
-                type->AsFortran());
-          }
         }
         if (ExtractCoarrayRef(*expr)) {
           context.messages().Say(at,
diff --git a/flang/test/Semantics/c_f_pointer.f90 b/flang/test/Semantics/c_f_pointer.f90
index 8a22175ffe19e..29b1127af15bd 100644
--- a/flang/test/Semantics/c_f_pointer.f90
+++ b/flang/test/Semantics/c_f_pointer.f90
@@ -46,13 +46,17 @@ program test
   call c_f_pointer(scalarC, multiDimIntF, shape=[1_8])
   !ERROR: SHAPE= argument to C_F_POINTER() must be a rank-one array.
   call c_f_pointer(scalarC, multiDimIntF, shape=rankTwoArray)
-  !WARNING: FPTR= argument to C_F_POINTER() should not be unlimited polymorphic [-Winteroperability]
+
+  !These warnings have been disabled because the C_F_POINTER's restrictions
+  !are dependent on the source of the CPTR= argument.  Each warning here
+  !might be a false positive for a valid program.
+  !!WARNING: FPTR= argument to C_F_POINTER() should not be unlimited polymorphic [-Winteroperability]
   call c_f_pointer(scalarC, unlimited)
-  !PORTABILITY: FPTR= argument to C_F_POINTER() should not have a derived type that is not BIND(C) [-Wportability]
+  !!PORTABILITY: FPTR= argument to C_F_POINTER() should not have a derived type that is not BIND(C) [-Wportability]
   call c_f_pointer(scalarC, notBindC)
-  !WARNING: FPTR= argument to C_F_POINTER() should not have the non-interoperable character length CHARACTER(KIND=1,LEN=2_8) [-Wcharacter-interoperability]
+  !!WARNING: FPTR= argument to C_F_POINTER() should not have the non-interoperable character length CHARACTER(KIND=1,LEN=2_8) [-Wcharacter-interoperability]
   call c_f_pointer(scalarC, c2ptr)
-  !WARNING: FPTR= argument to C_F_POINTER() should not have the non-interoperable intrinsic type or kind CHARACTER(KIND=4,LEN=1_8) [-Winteroperability]
+  !!WARNING: FPTR= argument to C_F_POINTER() should not have the non-interoperable intrinsic type or kind CHARACTER(KIND=4,LEN=1_8) [-Winteroperability]
   call c_f_pointer(scalarC, unicodePtr)
 
   !ERROR: SHAPE= argument to C_F_POINTER() may not appear when FPTR= is scalar

@klausler klausler merged commit 3425f22 into llvm:main Nov 14, 2025
13 checks passed
@klausler klausler deleted the bug167470 branch November 14, 2025 16:23
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] false warning for C pointer to noninteroperable derived-type

3 participants