From: Ronan Desplanques Date: Mon, 21 Aug 2023 11:35:37 +0000 (+0200) Subject: ada: Fix spurious warning emissions X-Git-Tag: basepoints/gcc-15~6447 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9be6a698f95f94a27307208cdb90bd2018182071;p=thirdparty%2Fgcc.git ada: Fix spurious warning emissions Before this patch, warnings handled by `Sem_Warn.Check_References` were erroneously emitted in some cases. Here is an example of a program that, when compiled with the `-gnatwu` switch, triggered the bug: procedure Main is package T is A : Integer; end T; begin T.A := 7; end Main; The following message was emitted: main.adb:3:07: warning: variable "A" is never read and never assigned [-gnatwu] This patch mitigates the issue by restricting the cases in which `Sem_Warn.Check_References` is called for package specifications. Note that the recursive calls in `Sem_Warn.Check_References` can be used to convince oneself that this patch does not remove legitimate warnings for non-library-level package specifications. gcc/ada/ * sem_ch7.adb (Analyze_Package_Declaration): Restrict calls to `Sem_Warn.Check_References` and adjust comment accordingly. --- diff --git a/gcc/ada/sem_ch7.adb b/gcc/ada/sem_ch7.adb index ecb4bbe3e569..1a49a53ad63b 100644 --- a/gcc/ada/sem_ch7.adb +++ b/gcc/ada/sem_ch7.adb @@ -1267,12 +1267,17 @@ package body Sem_Ch7 is Is_Main_Unit => Parent (N) = Cunit (Main_Unit)); end if; - -- Warn about references to unset objects, which is straightforward - -- for packages with no bodies. For packages with bodies this is more - -- complicated, because some of the objects might be set between spec - -- and body elaboration, in nested or child packages, etc. - - Check_References (Id); + -- For package declarations at the library level, warn about + -- references to unset objects, which is straightforward for packages + -- with no bodies. For packages with bodies this is more complicated, + -- because some of the objects might be set between spec and body + -- elaboration, in nested or child packages, etc. Note that the + -- recursive calls in Check_References will handle nested package + -- specifications. + + if Is_Library_Level_Entity (Id) then + Check_References (Id); + end if; end if; -- Set Body_Required indication on the compilation unit node