This patch removes a warning on a referenced entity with no explicit
prior assignment, if the type of the entity has
Preelaborable_Initialixation, such as Exception_Occurrence.

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

2019-07-22  Ed Schonberg  <schonb...@adacore.com>

gcc/ada/

        * sem_warn.adb (Check_References): Do not emit s warning on a
        referenced entity with no explicit assignment if the type of the
        entity has Preelaborable_Initialixation, such as
        Exception_Occurrence.

gcc/testsuite/

        * gnat.dg/warn25.adb: New testcase.
--- gcc/ada/sem_warn.adb
+++ gcc/ada/sem_warn.adb
@@ -1413,9 +1413,13 @@ package body Sem_Warn is
                   goto Continue;
                end if;
 
-               --  Check for unset reference
+               --  Check for unset reference. If type of object has
+               --  preelaborable initialization, warning is misleading.
 
-               if Warn_On_No_Value_Assigned and then Present (UR) then
+               if Warn_On_No_Value_Assigned
+                 and then Present (UR)
+                 and then not Known_To_Have_Preelab_Init (Etype (E1))
+               then
 
                   --  For other than access type, go back to original node to
                   --  deal with case where original unset reference has been

--- /dev/null
new file mode 100644
+++ gcc/testsuite/gnat.dg/warn25.adb
@@ -0,0 +1,23 @@
+--  { dg-do compile }
+--  { dg-options "-gnatwa" }
+
+with Ada.Exceptions;
+procedure Warn25 is
+    CASA_Unavailable : Ada.Exceptions.Exception_Occurrence;
+    use Ada.Exceptions;
+begin
+   while True loop
+    declare
+    begin
+       if Exception_Identity (CASA_Unavailable) = Null_Id then
+         exit;
+     end if;
+     exception
+       when E : others =>
+         Save_Occurrence (Source => E, Target => CASA_Unavailable);
+     end;
+   end loop;
+   if Exception_Identity (CASA_Unavailable) /= Null_Id then
+      Reraise_Occurrence (CASA_Unavailable);
+   end if;
+end;

Reply via email to