]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[Ada] Warn about conversion with any predefined time types
authorPiotr Trojanek <trojanek@adacore.com>
Wed, 17 Feb 2021 22:55:59 +0000 (23:55 +0100)
committerPierre-Marie de Rodat <derodat@adacore.com>
Mon, 11 Oct 2021 13:38:11 +0000 (13:38 +0000)
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.

gcc/ada/sem_ch13.adb

index fb1be479de1f7413f4610539a756a3d39340831b..c60dd97beaf3e3b775042ae69866c10b2eb2d96a 100644 (file)
@@ -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