[flang-commits] [flang] [Flang][Semantics] Adding support of -Wunused-dummy-argument (PR #192950)
Jean-Didier PAILLEUX via flang-commits
flang-commits at lists.llvm.org
Mon Apr 20 04:31:38 PDT 2026
https://github.com/JDPailleux created https://github.com/llvm/llvm-project/pull/192950
Adding support of new warning flags -W[no-]unused-dummy-argument.
Used in ECRAD, but not a mandatory flags for the project (https://github.com/ecmwf-ifs/ecrad).
I think it could be usefull to develop various Fortran projects and avoiding unused dummy arguments.
>From ff5ce40a665f9e4ddeadd3a5a2a3b1c1a897e7bb Mon Sep 17 00:00:00 2001
From: Jean-Didier Pailleux <jean-didier.pailleux at sipearl.com>
Date: Fri, 17 Apr 2026 16:29:19 +0200
Subject: [PATCH] [Flang][Semantics] Adding support of -Wunused-dummy-argument
---
.../include/flang/Support/Fortran-features.h | 3 +-
flang/lib/Semantics/semantics.cpp | 9 ++++
flang/test/Semantics/bind-c17.f90 | 2 +-
flang/test/Semantics/bindings03.f90 | 1 +
flang/test/Semantics/bug1491.f90 | 2 +-
flang/test/Semantics/call30.f90 | 2 +-
flang/test/Semantics/call37.f90 | 2 +-
flang/test/Semantics/call44.f90 | 2 +-
flang/test/Semantics/call45.f90 | 2 +-
flang/test/Semantics/deferred01.f90 | 2 +-
flang/test/Semantics/definable02.f90 | 2 +-
flang/test/Semantics/final03.f90 | 2 +-
.../test/Semantics/wunused-dummy-argument.f90 | 47 +++++++++++++++++++
13 files changed, 68 insertions(+), 10 deletions(-)
create mode 100644 flang/test/Semantics/wunused-dummy-argument.f90
diff --git a/flang/include/flang/Support/Fortran-features.h b/flang/include/flang/Support/Fortran-features.h
index f2bd0d21f25b6..445fe10009ba1 100644
--- a/flang/include/flang/Support/Fortran-features.h
+++ b/flang/include/flang/Support/Fortran-features.h
@@ -84,7 +84,8 @@ ENUM_CLASS(UsageWarning, Portability, PointerToUndefinable,
HostAssociatedIntentOutInSpecExpr, NonVolatilePointerToVolatile,
RealConstantWidening, VolatileOrAsynchronousTemporary, UnusedVariable,
UsedUndefinedVariable, BadValueInDeadCode, AssumedTypeSizeDummy,
- MisplacedIgnoreTKR, NamelistParameter, ImpureFinalInPure)
+ MisplacedIgnoreTKR, NamelistParameter, ImpureFinalInPure,
+ UnusedDummyArgument)
using LanguageFeatures = EnumSet<LanguageFeature, LanguageFeature_enumSize>;
using UsageWarnings = EnumSet<UsageWarning, UsageWarning_enumSize>;
diff --git a/flang/lib/Semantics/semantics.cpp b/flang/lib/Semantics/semantics.cpp
index 8bca853f01a24..3e97aee795905 100644
--- a/flang/lib/Semantics/semantics.cpp
+++ b/flang/lib/Semantics/semantics.cpp
@@ -203,6 +203,8 @@ static void WarnUnusedOrUndefinedLocal(
scope.kind() == Scope::Kind::BlockConstruct) {
for (const auto &[_, symbolRef] : scope) {
const Symbol &symbol{*symbolRef};
+ const auto *ownerSubp{symbol.detailsIf<SubprogramDetails>()};
+ bool inInterface{ownerSubp && ownerSubp->isInterface()};
if ((symbol.has<semantics::ObjectEntityDetails>() ||
(symbol.has<semantics::ProcEntityDetails>() &&
IsProcedurePointer(symbol))) &&
@@ -224,6 +226,13 @@ static void WarnUnusedOrUndefinedLocal(
}
}
}
+ if (symbol.has<semantics::ObjectEntityDetails>() && IsDummy(symbol) &&
+ !inInterface && !context.IsSymbolUsed(symbol) &&
+ scope.kind() == Scope::Kind::Subprogram) {
+ context.Warn(common::UsageWarning::UnusedDummyArgument, symbol.name(),
+ "Value of dummy argument '%s' is never used"_warn_en_US,
+ symbol.name());
+ }
}
}
if (!scope.IsModuleFile()) {
diff --git a/flang/test/Semantics/bind-c17.f90 b/flang/test/Semantics/bind-c17.f90
index 34d21865d4e4c..8609e7adc8757 100644
--- a/flang/test/Semantics/bind-c17.f90
+++ b/flang/test/Semantics/bind-c17.f90
@@ -1,4 +1,4 @@
-! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic -Werror
+! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic -Werror -Wno-unused-dummy-argument
module m
type a ! not BIND(C)
end type
diff --git a/flang/test/Semantics/bindings03.f90 b/flang/test/Semantics/bindings03.f90
index 1aa657cd667ff..4c579e1339771 100644
--- a/flang/test/Semantics/bindings03.f90
+++ b/flang/test/Semantics/bindings03.f90
@@ -7,6 +7,7 @@ module m
end type
contains
subroutine sub(x)
+ !WARNING: Value of dummy argument 'x' is never used [-Wunused-dummy-argument]
class(t), intent(in) :: x
end subroutine
end module
diff --git a/flang/test/Semantics/bug1491.f90 b/flang/test/Semantics/bug1491.f90
index ccc6eea31a1d4..f00206fa685a3 100644
--- a/flang/test/Semantics/bug1491.f90
+++ b/flang/test/Semantics/bug1491.f90
@@ -1,4 +1,4 @@
-!RUN: %python %S/test_errors.py %s %flang_fc1 -Werror -pedantic
+!RUN: %python %S/test_errors.py %s %flang_fc1 -Werror -Wno-unused-dummy-argument -pedantic
module m
interface
integer function foo1()
diff --git a/flang/test/Semantics/call30.f90 b/flang/test/Semantics/call30.f90
index 1570eb02b6755..8b8c777043a3e 100644
--- a/flang/test/Semantics/call30.f90
+++ b/flang/test/Semantics/call30.f90
@@ -1,4 +1,4 @@
-! RUN: %python %S/test_errors.py %s %flang_fc1 -Werror -pedantic
+! RUN: %python %S/test_errors.py %s %flang_fc1 -Werror -Wno-unused-dummy-argument -pedantic
! This test is responsible for checking the fix for passing non-variables as
! actual arguments to subroutines/functions whose corresponding dummy argument
! expects a VOLATILE variable
diff --git a/flang/test/Semantics/call37.f90 b/flang/test/Semantics/call37.f90
index a536f62a1a7a8..c2c13132264b7 100644
--- a/flang/test/Semantics/call37.f90
+++ b/flang/test/Semantics/call37.f90
@@ -1,4 +1,4 @@
-! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic -Werror
+! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic -Werror -Wno-unused-dummy-argument
! Test warnings on mismatching interfaces involvingCHARACTER arguments
subroutine constLen(s)
character(len = 1) s
diff --git a/flang/test/Semantics/call44.f90 b/flang/test/Semantics/call44.f90
index 7bf986eaa0b65..2bc904a063644 100644
--- a/flang/test/Semantics/call44.f90
+++ b/flang/test/Semantics/call44.f90
@@ -1,4 +1,4 @@
-! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic -Wno-portability -Werror
+! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic -Wno-portability -Wno-unused-dummy-argument -Werror
subroutine assumedshape(normal, contig)
real normal(:)
real, contiguous :: contig(:)
diff --git a/flang/test/Semantics/call45.f90 b/flang/test/Semantics/call45.f90
index 9d33fa9f70a3a..b9de04ab184b4 100644
--- a/flang/test/Semantics/call45.f90
+++ b/flang/test/Semantics/call45.f90
@@ -1,4 +1,4 @@
-! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic -Werror
+! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic -Werror -Wno-unused-dummy-argument
program call45
integer, target :: v(100) = [(i, i=1, 100)]
integer, pointer :: p(:) => v
diff --git a/flang/test/Semantics/deferred01.f90 b/flang/test/Semantics/deferred01.f90
index 8117b91eef434..b2849464943b4 100644
--- a/flang/test/Semantics/deferred01.f90
+++ b/flang/test/Semantics/deferred01.f90
@@ -1,4 +1,4 @@
-! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic -Werror
+! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic -Werror -Wno-unused-dummy-argument
! Deferred TBPs must be overridden, but when they are private, those
! overrides are required to appear in the same module. We allow overrides
! elsewhere as an extension.
diff --git a/flang/test/Semantics/definable02.f90 b/flang/test/Semantics/definable02.f90
index 559efb0dbb003..4f5f66a43282c 100644
--- a/flang/test/Semantics/definable02.f90
+++ b/flang/test/Semantics/definable02.f90
@@ -1,4 +1,4 @@
-! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic -Werror
+! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic -Werror -Wno-unused-dummy-argument
! Ensure that FINAL subroutine can be called for array with vector-valued
! subscript.
diff --git a/flang/test/Semantics/final03.f90 b/flang/test/Semantics/final03.f90
index 15335b2eeadcf..29246dfca8055 100644
--- a/flang/test/Semantics/final03.f90
+++ b/flang/test/Semantics/final03.f90
@@ -1,4 +1,4 @@
-! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic -Werror
+! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic -Werror -Wno-unused-dummy-argument
! PDT sensitivity of FINAL subroutines
module m
type :: pdt(k)
diff --git a/flang/test/Semantics/wunused-dummy-argument.f90 b/flang/test/Semantics/wunused-dummy-argument.f90
new file mode 100644
index 0000000000000..2f063b95aaf5e
--- /dev/null
+++ b/flang/test/Semantics/wunused-dummy-argument.f90
@@ -0,0 +1,47 @@
+! RUN: %flang_fc1 -Wunused-dummy-argument %s 2>&1 | FileCheck %s
+! RUN: not %flang_fc1 -Wunused-dummy-argument -Werror %s
+! CHECK: Value of dummy argument 'a4' is never used [-Wunused-dummy-argument]
+! CHECK: Value of dummy argument 'b4' is never used [-Wunused-dummy-argument]
+! CHECK: Value of dummy argument 'a6' is never used [-Wunused-dummy-argument]
+! CHECK: Value of dummy argument 'b6' is never used [-Wunused-dummy-argument]
+
+program main
+ type :: my_type
+ integer :: val
+ end type
+ integer :: not_dummy_arg
+ interface
+ subroutine subroutine_interface(a)
+ integer, intent(in) :: a
+ end subroutine
+
+ function function_interface(a2)
+ integer, intent(in) :: a2
+ end function
+ end interface
+contains
+ subroutine subroutine_all_used(a3, b3)
+ integer, intent(inout) :: a3, b3
+ a3 = a3 + b3
+ end subroutine
+
+ subroutine subroutine_unused_both(a4, b4)
+ integer, intent(inout) :: a4(10)
+ type(my_type) :: b4
+ end subroutine
+
+
+ function function_used_all(a5, b5) result(c1)
+ integer, intent(inout) :: a5(10)
+ type(my_type), intent(in) :: b5
+ integer :: c1
+ a5(1) = b5%val
+ c1 = a5(2)
+ end function
+
+ function function_unused_both(a6, b6) result(c2)
+ integer, intent(inout) :: a6(10)
+ type(my_type) :: b6
+ integer :: c2
+ end function
+end program
More information about the flang-commits
mailing list