[flang-commits] [flang] [flang] Allow a few irrelevant attributes, with warning (PR #117374)
Peter Klausler via flang-commits
flang-commits at lists.llvm.org
Fri Nov 22 12:38:27 PST 2024
https://github.com/klausler created https://github.com/llvm/llvm-project/pull/117374
INTENT, VALUE, and OPTIONAL attributes apply only to dummy arguments. A couple older compilers accept them, usually with a warning, if they are applied to names that are not dummy arguments, and they show up in some older non-portable source code. Change these cases into stern warnings by default.
>From 28456df53b3f5c02f9838f03cf0ce702f056de66 Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Fri, 22 Nov 2024 12:34:48 -0800
Subject: [PATCH] [flang] Allow a few irrelevant attributes, with warning
INTENT, VALUE, and OPTIONAL attributes apply only to dummy arguments.
A couple older compilers accept them, usually with a warning, if
they are applied to names that are not dummy arguments, and they
show up in some older non-portable source code. Change these
cases into stern warnings by default.
---
flang/include/flang/Common/Fortran-features.h | 3 ++-
flang/lib/Common/Fortran-features.cpp | 1 +
flang/lib/Semantics/check-declarations.cpp | 10 ++++++++--
flang/test/Semantics/call14.f90 | 2 +-
flang/test/Semantics/resolve58.f90 | 8 ++++----
5 files changed, 16 insertions(+), 8 deletions(-)
diff --git a/flang/include/flang/Common/Fortran-features.h b/flang/include/flang/Common/Fortran-features.h
index c6ab846cce2fc0..9e3fd0dcca8e17 100644
--- a/flang/include/flang/Common/Fortran-features.h
+++ b/flang/include/flang/Common/Fortran-features.h
@@ -53,7 +53,8 @@ ENUM_CLASS(LanguageFeature, BackslashEscapes, OldDebugLines,
NonBindCInteroperability, CudaManaged, CudaUnified,
PolymorphicActualAllocatableOrPointerToMonomorphicDummy, RelaxedPureDummy,
UndefinableAsynchronousOrVolatileActual, AutomaticInMainProgram, PrintCptr,
- SavedLocalInSpecExpr, PrintNamelist, AssumedRankPassedToNonAssumedRank)
+ SavedLocalInSpecExpr, PrintNamelist, AssumedRankPassedToNonAssumedRank,
+ IgnoreIrrelevantAttributes)
// Portability and suspicious usage warnings
ENUM_CLASS(UsageWarning, Portability, PointerToUndefinable,
diff --git a/flang/lib/Common/Fortran-features.cpp b/flang/lib/Common/Fortran-features.cpp
index f47a4f17a6ba48..c86432874b8d83 100644
--- a/flang/lib/Common/Fortran-features.cpp
+++ b/flang/lib/Common/Fortran-features.cpp
@@ -43,6 +43,7 @@ LanguageFeatureControl::LanguageFeatureControl() {
warnLanguage_.set(LanguageFeature::BadBranchTarget);
warnLanguage_.set(LanguageFeature::HollerithPolymorphic);
warnLanguage_.set(LanguageFeature::ListDirectedSize);
+ warnLanguage_.set(LanguageFeature::IgnoreIrrelevantAttributes);
warnUsage_.set(UsageWarning::ShortArrayActual);
warnUsage_.set(UsageWarning::FoldingException);
warnUsage_.set(UsageWarning::FoldingAvoidsRuntimeCrash);
diff --git a/flang/lib/Semantics/check-declarations.cpp b/flang/lib/Semantics/check-declarations.cpp
index c9656d031b2e17..99c6e16c4260f1 100644
--- a/flang/lib/Semantics/check-declarations.cpp
+++ b/flang/lib/Semantics/check-declarations.cpp
@@ -320,8 +320,14 @@ void CheckHelper::Check(const Symbol &symbol) {
if (symbol.attrs().HasAny({Attr::INTENT_IN, Attr::INTENT_INOUT,
Attr::INTENT_OUT, Attr::OPTIONAL, Attr::VALUE}) &&
!IsDummy(symbol)) {
- messages_.Say(
- "Only a dummy argument may have an INTENT, VALUE, or OPTIONAL attribute"_err_en_US);
+ if (context_.IsEnabled(
+ common::LanguageFeature::IgnoreIrrelevantAttributes)) {
+ context_.Warn(common::LanguageFeature::IgnoreIrrelevantAttributes,
+ "Only a dummy argument should have an INTENT, VALUE, or OPTIONAL attribute"_warn_en_US);
+ } else {
+ messages_.Say(
+ "Only a dummy argument may have an INTENT, VALUE, or OPTIONAL attribute"_err_en_US);
+ }
} else if (symbol.attrs().test(Attr::VALUE)) {
CheckValue(symbol, derived);
}
diff --git a/flang/test/Semantics/call14.f90 b/flang/test/Semantics/call14.f90
index e586d4eebd2535..fba11d35790b2e 100644
--- a/flang/test/Semantics/call14.f90
+++ b/flang/test/Semantics/call14.f90
@@ -9,7 +9,7 @@ module m
!ERROR: VALUE attribute may apply only to a dummy data object
subroutine C863(notData,assumedSize,coarray,coarrayComponent,assumedRank,assumedLen)
external :: notData
- !ERROR: Only a dummy argument may have an INTENT, VALUE, or OPTIONAL attribute
+ !WARNING: Only a dummy argument should have an INTENT, VALUE, or OPTIONAL attribute
real, value :: notADummy
value :: notData
!ERROR: VALUE attribute may not apply to an assumed-size array
diff --git a/flang/test/Semantics/resolve58.f90 b/flang/test/Semantics/resolve58.f90
index 2e42eb157f5b50..7686de4898404d 100644
--- a/flang/test/Semantics/resolve58.f90
+++ b/flang/test/Semantics/resolve58.f90
@@ -69,12 +69,12 @@ subroutine s6()
!ERROR: Implied-shape array 'local1' must be a named constant or a dummy argument
real, dimension (*) :: local1
- !ERROR: Only a dummy argument may have an INTENT, VALUE, or OPTIONAL attribute
+ !WARNING: Only a dummy argument should have an INTENT, VALUE, or OPTIONAL attribute
real, intent(in) :: local2
- !ERROR: Only a dummy argument may have an INTENT, VALUE, or OPTIONAL attribute
+ !WARNING: Only a dummy argument should have an INTENT, VALUE, or OPTIONAL attribute
procedure(), intent(in) :: p1
- !ERROR: Only a dummy argument may have an INTENT, VALUE, or OPTIONAL attribute
+ !WARNING: Only a dummy argument should have an INTENT, VALUE, or OPTIONAL attribute
real, optional :: local3
- !ERROR: Only a dummy argument may have an INTENT, VALUE, or OPTIONAL attribute
+ !WARNING: Only a dummy argument should have an INTENT, VALUE, or OPTIONAL attribute
procedure(), optional :: p2
end subroutine
More information about the flang-commits
mailing list