[Ada] Warn about conversion with any predefined time types

Message ID 20211011133942.GA1519025@adacore.com
State New
Headers show
Series
  • [Ada] Warn about conversion with any predefined time types
Related show

Commit Message

Jason Merrill via Gcc-patches Oct. 11, 2021, 1:39 p.m.
We already had a warning for unchecked conversions that involve the
private type Ada.Calendar.Time, whose representation might differ
between releases and targets of the compiler. Now this warning is
extended to Ada.Real_Time.Time and Ada.Real_Time.Time_Span, which is
similarly non-portable.

Previously the warning message referred to Time with no quotes; now all
the type names are in quotes, both because that's how we refer to entity
names in messages and because it is actually hard to omit the quotes
with the current API for error reporting.

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

gcc/ada/

	* sem_ch13.adb (Validate_Unchecked_Conversion): Simplify code
	for detecting conversions with Ada.Calendar.Time type and extend
	it to similar types in the Ada.Real_Time package.

Patch

diff --git a/gcc/ada/sem_ch13.adb b/gcc/ada/sem_ch13.adb
--- a/gcc/ada/sem_ch13.adb
+++ b/gcc/ada/sem_ch13.adb
@@ -17335,8 +17335,32 @@  package body Sem_Ch13 is
    is
       Source : Entity_Id;
       Target : Entity_Id;
+
+      procedure Warn_Nonportable (RE : RE_Id);
+      --  Warn if either source or target of the conversion is a predefined
+      --  private type, whose representation might differ between releases and
+      --  targets of the compiler.
+
+      ----------------------
+      -- Warn_Nonportable --
+      ----------------------
+
+      procedure Warn_Nonportable (RE : RE_Id) is
+      begin
+         if Is_RTE (Source, RE) or else Is_RTE (Target, RE) then
+            pragma Assert (Is_Private_Type (RTE (RE)));
+            Error_Msg_NE
+              ("?z?representation of & values may change between "
+               & "'G'N'A'T versions", N, RTE (RE));
+         end if;
+      end Warn_Nonportable;
+
+      --  Local variables
+
       Vnode  : Node_Id;
 
+   --  Start of processing for Validate_Unchecked_Conversion
+
    begin
       --  Obtain source and target types. Note that we call Ancestor_Subtype
       --  here because the processing for generic instantiation always makes
@@ -17353,6 +17377,18 @@  package body Sem_Ch13 is
          return;
       end if;
 
+      --  Warn if one of the operands is a private type declared in
+      --  Ada.Calendar or Ada.Real_Time. Do not emit a warning when compiling
+      --  GNAT-related sources.
+
+      if Warn_On_Unchecked_Conversion
+        and then not In_Predefined_Unit (N)
+      then
+         Warn_Nonportable (RO_CA_Time);
+         Warn_Nonportable (RO_RT_Time);
+         Warn_Nonportable (RE_Time_Span);
+      end if;
+
       --  If we are dealing with private types, then do the check on their
       --  fully declared counterparts if the full declarations have been
       --  encountered (they don't have to be visible, but they must exist).
@@ -17399,32 +17435,6 @@  package body Sem_Ch13 is
          end if;
       end if;
 
-      --  Warn if one of the operands is Ada.Calendar.Time. Do not emit a
-      --  warning when compiling GNAT-related sources.
-
-      if Warn_On_Unchecked_Conversion
-        and then not In_Predefined_Unit (N)
-        and then RTU_Loaded (Ada_Calendar)
-        and then (Chars (Source) = Name_Time
-                    or else
-                  Chars (Target) = Name_Time)
-      then
-         --  If Ada.Calendar is loaded and the name of one of the operands is
-         --  Time, there is a good chance that this is Ada.Calendar.Time.
-
-         declare
-            Calendar_Time : constant Entity_Id := Full_View (RTE (RO_CA_Time));
-         begin
-            pragma Assert (Present (Calendar_Time));
-
-            if Source = Calendar_Time or else Target = Calendar_Time then
-               Error_Msg_N
-                 ("?z?representation of 'Time values may change between "
-                  & "'G'N'A'T versions", N);
-            end if;
-         end;
-      end if;
-
       --  Make entry in unchecked conversion table for later processing by
       --  Validate_Unchecked_Conversions, which will check sizes and alignments
       --  (using values set by the back end where possible). This is only done