public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [COMMITTED] ada: Fix spurious warning emissions
@ 2023-09-05 11:08 Marc Poulhiès
  0 siblings, 0 replies; only message in thread
From: Marc Poulhiès @ 2023-09-05 11:08 UTC (permalink / raw)
  To: gcc-patches; +Cc: Ronan Desplanques

From: Ronan Desplanques <desplanques@adacore.com>

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.

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/sem_ch7.adb | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/gcc/ada/sem_ch7.adb b/gcc/ada/sem_ch7.adb
index ecb4bbe3e56..1a49a53ad63 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
-- 
2.40.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-09-05 11:08 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-05 11:08 [COMMITTED] ada: Fix spurious warning emissions Marc Poulhiès

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).