[Ada] Suppress warnings on unreferenced parameters of dispatching ops
diff mbox series

Message ID 20190819083903.GA33558@adacore.com
State New
Headers show
Series
  • [Ada] Suppress warnings on unreferenced parameters of dispatching ops
Related show

Commit Message

Pierre-Marie de Rodat Aug. 19, 2019, 8:39 a.m. UTC
If the -gnatwf switch is used to activate warnings on unreferenced
formal parameters, the warning is no longer given if the subprogram is
dispatching, because such warnings tend to be noise. It is quite common
to have a parameter that is necessary just because the subprogram is
overriding, or just because we need a controlling parameter for the
dispatch.

Tested on x86_64-pc-linux-gnu, committed on trunk

2019-08-19  Bob Duff  <duff@adacore.com>

gcc/ada/

	* sem_warn.adb (Warn_On_Unreferenced_Entity): Suppress warning
	on formal parameters of dispatching operations.

gcc/testsuite/

	* gnat.dg/warn29.adb, gnat.dg/warn29.ads: New testcase.

Patch
diff mbox series

--- gcc/ada/sem_warn.adb
+++ gcc/ada/sem_warn.adb
@@ -4407,11 +4407,31 @@  package body Sem_Warn is
                         E := Body_E;
                      end if;
 
-                     if not Is_Trivial_Subprogram (Scope (E)) then
-                        Error_Msg_NE -- CODEFIX
-                          ("?u?formal parameter & is not referenced!",
-                           E, Spec_E);
-                     end if;
+                     declare
+                        B : constant Node_Id := Parent (Parent (Scope (E)));
+                        S : Entity_Id := Empty;
+                     begin
+                        if Nkind_In (B,
+                                     N_Expression_Function,
+                                     N_Subprogram_Body,
+                                     N_Subprogram_Renaming_Declaration)
+                        then
+                           S := Corresponding_Spec (B);
+                        end if;
+
+                        --  Do not warn for dispatching operations, because
+                        --  that causes too much noise. Also do not warn for
+                        --  trivial subprograms.
+
+                        if (not Present (S)
+                            or else not Is_Dispatching_Operation (S))
+                          and then not Is_Trivial_Subprogram (Scope (E))
+                        then
+                           Error_Msg_NE -- CODEFIX
+                             ("?u?formal parameter & is not referenced!",
+                              E, Spec_E);
+                        end if;
+                     end;
                   end if;
                end if;
 

--- /dev/null
new file mode 100644
+++ gcc/testsuite/gnat.dg/warn29.adb
@@ -0,0 +1,11 @@ 
+--  { dg-do compile }
+--  { dg-options "-gnatwa" }
+
+with Text_IO; use Text_IO;
+
+package body Warn29 is
+   procedure P (X : T; Y : Integer) is
+   begin
+      Put_Line ("hello");
+   end P;
+end Warn29;

--- /dev/null
new file mode 100644
+++ gcc/testsuite/gnat.dg/warn29.ads
@@ -0,0 +1,4 @@ 
+package Warn29 is
+   type T is tagged null record;
+   procedure P (X : T; Y : Integer);
+end Warn29;