When generating code, subunits are inserted into the tree of the parent unit, and their context is added to the context of the parent. This makes it hard to determine whether any with_clause on the proper body is superfluous. This patch adds circuitry to detect these superfluous with_clauses when the subunit is compiled by itself, in -gnatc mode.
Executing: gcc -c -gnatc -gnatwu pkg-proc.adb must yield: pkg-proc.adb:1:06: warning: unit "Unused_Pkg" is not referenced pkg-proc.adb:2:06: warning: unit "Unused_Pkg_2" is not referenced pkg.adb:3:03: warning: variable "Variable" is never read and never assigned --- with Unused_Pkg; with Unused_Pkg_2; with Ada.Text_Io; separate (Pkg) procedure Proc is begin Ada.Text_Io.Put_Line ("Hello"); end Proc; --- with Unused_Pkg; package body pkg is Variable : Unused_Pkg.T; procedure Proc is separate; end Pkg; --- package Pkg is procedure Proc; end Pkg; --- package body Unused_Pkg is procedure Empty is begin null; end Empty; end Unused_Pkg; -- package Unused_Pkg is type T is (Tbd); procedure Empty; end Unused_Pkg; -- package Unused_Pkg_2 is type T is (Tbd); end Unused_Pkg_2; Tested on x86_64-pc-linux-gnu, committed on trunk 2013-10-17 Ed Schonberg <schonb...@adacore.com> * sem_warn.adb (Check_Unused_Withs): If the main unit is a subunit, apply the check to the units mentioned in its context only. This provides additional warnings on with_clauses that are superfluous.
Index: sem_warn.adb =================================================================== --- sem_warn.adb (revision 203763) +++ sem_warn.adb (working copy) @@ -2545,13 +2545,16 @@ return; end if; - -- Flag any unused with clauses, but skip this step if we are compiling - -- a subunit on its own, since we do not have enough information to - -- determine whether with's are used. We will get the relevant warnings - -- when we compile the parent. This is the normal style of GNAT - -- compilation in any case. + -- Flag any unused with clauses. For a subunit, check only the units + -- in its context, not those of the parent, which may be needed by other + -- subunits. We will get the full warnings when we compile the parent, + -- but the following is helpful when compiling a subunit by itself. if Nkind (Unit (Cunit (Main_Unit))) = N_Subunit then + if Current_Sem_Unit = Main_Unit then + Check_One_Unit (Main_Unit); + end if; + return; end if;