public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [Ada] Warn on unset objects in packages with no bodies
@ 2022-07-12 12:25 Pierre-Marie de Rodat
  0 siblings, 0 replies; only message in thread
From: Pierre-Marie de Rodat @ 2022-07-12 12:25 UTC (permalink / raw)
  To: gcc-patches; +Cc: Piotr Trojanek

[-- Attachment #1: Type: text/plain, Size: 463 bytes --]

Fix an inconsistency, where GNAT was warning about references to unset
objects inside generic packages with no bodies but not inside ordinary
packages with no bodies.

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

gcc/ada/

	* sem_ch7.adb (Analyze_Package_Declaration): Check references to
	unset objects.

gcc/testsuite/

	* gnat.dg/specs/discr5.ads: Expect new warnings.
	* gnat.dg/specs/empty_variants.ads: Likewise.
	* gnat.dg/specs/pack13.ads: Likewise.

[-- Attachment #2: patch.diff --]
[-- Type: text/x-diff, Size: 2679 bytes --]

diff --git a/gcc/ada/sem_ch7.adb b/gcc/ada/sem_ch7.adb
--- a/gcc/ada/sem_ch7.adb
+++ b/gcc/ada/sem_ch7.adb
@@ -1253,6 +1253,13 @@ package body Sem_Ch7 is
               (Context      => N,
                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);
       end if;
 
       --  Set Body_Required indication on the compilation unit node


diff --git a/gcc/testsuite/gnat.dg/specs/discr5.ads b/gcc/testsuite/gnat.dg/specs/discr5.ads
--- a/gcc/testsuite/gnat.dg/specs/discr5.ads
+++ b/gcc/testsuite/gnat.dg/specs/discr5.ads
@@ -22,7 +22,7 @@ package Discr5 is
    subtype Rt is R(True);
    subtype Rf is R(False);
 
-   type R1 (D1 : Boolean) is new R (X) with record
+   type R1 (D1 : Boolean) is new R (X) with record -- { dg-warning "\"X\" may be referenced before it has a value" }
       FF : Float;
       case D1 is
          when True =>
@@ -38,7 +38,7 @@ package Discr5 is
    subtype R1t is R1 (True);
    subtype R1f is R1 (False);
 
-   type R2 (D2 : Boolean) is new R1 (Y) with record
+   type R2 (D2 : Boolean) is new R1 (Y) with record -- { dg-warning "\"Y\" may be referenced before it has a value" }
       FFF: System.Address;
       case D2 is
          when True =>
@@ -55,3 +55,4 @@ package Discr5 is
    subtype R2f is R2 (False);
 
 end Discr5;
+


diff --git a/gcc/testsuite/gnat.dg/specs/empty_variants.ads b/gcc/testsuite/gnat.dg/specs/empty_variants.ads
--- a/gcc/testsuite/gnat.dg/specs/empty_variants.ads
+++ b/gcc/testsuite/gnat.dg/specs/empty_variants.ads
@@ -1,5 +1,4 @@
 --  { dg-do compile }
---  { dg-options "-gnatdF" }
 
 package Empty_Variants is
    
@@ -23,10 +22,11 @@ package Empty_Variants is
    
    R : Rec;
    
-   I : Integer := R.I;
+   I : Integer := R.I; -- { dg-warning "\"R\.I\" may be referenced before it has a value" }
    J : Integer := R.J;
    K : Integer := R.K;
    L : Integer := R.L;
    M : Integer := R.L;
 
 end Empty_Variants;
+


diff --git a/gcc/testsuite/gnat.dg/specs/pack13.ads b/gcc/testsuite/gnat.dg/specs/pack13.ads
--- a/gcc/testsuite/gnat.dg/specs/pack13.ads
+++ b/gcc/testsuite/gnat.dg/specs/pack13.ads
@@ -20,6 +20,6 @@ package Pack13 is
 
   A : Arr;
 
-  package My_G is new G (Boolean, A(True).B);
+  package My_G is new G (Boolean, A(True).B); -- { dg-warning "\"A\" may be referenced before it has a value" }
 
 end Pack13;



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

only message in thread, other threads:[~2022-07-12 12:25 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-12 12:25 [Ada] Warn on unset objects in packages with no bodies Pierre-Marie de Rodat

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).