]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Emit a warning on inheritly limited types
authorViljar Indus <indus@adacore.com>
Thu, 1 Aug 2024 12:59:00 +0000 (15:59 +0300)
committerMarc Poulhiès <dkm@gcc.gnu.org>
Fri, 23 Aug 2024 08:51:04 +0000 (10:51 +0200)
Record types that do not have a limited keyword but have a
member with a limited type are also considered to be limited types.
This can be confusing to understand for newer Ada users. It is
better to emit a warning in this scenario and suggest that the
type should be marked with a limited keyword. This diagnostic will
be acticated when the -gnatw_l switch is used.

gcc/ada/

* sem_ch3.adb: Add method Check_Inherited_Limted_Record for
emitting the warning for an inherited limited type.
* warnsw.adb: Add processing for the -gnatw_l switch that
triggeres the inheritly limited type warning.
* warnsw.ads: same as above.
* doc/gnat_ugn/building_executable_programs_with_gnat.rst: Add
entry for -gnatw_l switch.
* gnat_ugn.texi: Regenerate.

gcc/ada/doc/gnat_ugn/building_executable_programs_with_gnat.rst
gcc/ada/gnat_ugn.texi
gcc/ada/sem_ch3.adb
gcc/ada/warnsw.adb
gcc/ada/warnsw.ads

index ce3ed0cc65a01757abb5bfae7674505414c5e039..07ca2ea22c35b2af3c6da828f345db18a1743487 100644 (file)
@@ -3430,6 +3430,23 @@ of the pragma in the :title:`GNAT_Reference_manual`).
   This switch suppresses listing of inherited aspects.
 
 
+.. index:: -gnatw_l  (gcc)
+
+:switch:`-gnatw_l`
+  *Activate warnings on inheritely limited types.*
+
+  This switch causes the compiler trigger warnings on record types that do not
+  have a limited keyword but contain a component that is a limited type.
+
+
+.. index:: -gnatw_L  (gcc)
+
+:switch:`-gnatw_L`
+  *Suppress warnings on inheritely limited types.*
+
+  This switch suppresses warnings on inheritely limited types.
+
+
 .. index:: -gnatwm  (gcc)
 
 :switch:`-gnatwm`
index 0e3ee935552d6c26f65c4c14fcd797d7ef7683a6..dcde9ea705bb7c4d17e634f2e67fd48300bab31d 100644 (file)
@@ -19,7 +19,7 @@
 
 @copying
 @quotation
-GNAT User's Guide for Native Platforms , Jul 29, 2024
+GNAT User's Guide for Native Platforms , Aug 19, 2024
 
 AdaCore
 
@@ -11671,6 +11671,31 @@ Pre’Class, and Post’Class aspects. Also list inherited subtype predicates.
 This switch suppresses listing of inherited aspects.
 @end table
 
+@geindex -gnatw_l (gcc)
+
+
+@table @asis
+
+@item @code{-gnatw_l}
+
+`Activate warnings on inheritely limited types.'
+
+This switch causes the compiler trigger warnings on record types that do not
+have a limited keyword but contain a component that is a limited type.
+@end table
+
+@geindex -gnatw_L (gcc)
+
+
+@table @asis
+
+@item @code{-gnatw_L}
+
+`Suppress warnings on inheritely limited types.'
+
+This switch suppresses warnings on inheritely limited types.
+@end table
+
 @geindex -gnatwm (gcc)
 
 
index 3b44f0a510014a759283126624326b29fdbf61bd..4dac4eec108985586ab11aa35e25facea377e844 100644 (file)
@@ -741,6 +741,11 @@ package body Sem_Ch3 is
    --  Check that an entity in a list of progenitors is an interface,
    --  emit error otherwise.
 
+   procedure Warn_On_Inherently_Limited_Type (E : Entity_Id);
+   --  Emit a warning if a record type that does not have a limited keyword in
+   --  its definition has any components that are limited (which implicitly
+   --  make the type limited).
+
    -----------------------
    -- Access_Definition --
    -----------------------
@@ -22924,6 +22929,8 @@ package body Sem_Ch3 is
          Derive_Progenitor_Subprograms (T, T);
       end if;
 
+      Warn_On_Inherently_Limited_Type (T);
+
       Check_Function_Writable_Actuals (N);
    end Record_Type_Declaration;
 
@@ -23396,4 +23403,31 @@ package body Sem_Ch3 is
       Set_Is_Constrained     (T);
    end Signed_Integer_Type_Declaration;
 
+   -------------------------------------
+   -- Warn_On_Inherently_Limited_Type --
+   -------------------------------------
+
+   procedure Warn_On_Inherently_Limited_Type (E : Entity_Id) is
+      C : Entity_Id;
+   begin
+      if Warnsw.Warn_On_Inherently_Limited_Type
+        and then not Is_Limited_Record (E)
+      then
+         C := First_Component (Base_Type (E));
+         while Present (C) loop
+            if Is_Inherently_Limited_Type (Etype (C)) then
+               Error_Msg_Node_2 := E;
+               Error_Msg_NE
+                 ("?_l?limited component & makes & limited", E, C);
+               Error_Msg_N
+                 ("\\?_l?consider annotating the record type "
+                  & "with a LIMITED keyword", E);
+               exit;
+            end if;
+
+            Next_Component (C);
+         end loop;
+      end if;
+   end Warn_On_Inherently_Limited_Type;
+
 end Sem_Ch3;
index ea7e94c411439f5cb4739aa342668b32c0109408..2bfb56ec5130a3865f2d7d7794c79d0da2173908 100644 (file)
@@ -92,13 +92,14 @@ package body Warnsw is
           'z' => X.Warn_On_Size_Alignment),
 
         '_' =>
-         ('b' | 'd' | 'e' | 'f' | 'g' | 'h' | 'i' | 'k' | 'l' | 'm' |
+         ('b' | 'd' | 'e' | 'f' | 'g' | 'h' | 'i' | 'k' | 'm' |
           'n' | 'o' | 't' | 'u' | 'v' | 'w' | 'x' | 'y' | 'z' =>
            No_Such_Warning,
 
           'a' => X.Warn_On_Anonymous_Allocators,
           'c' => X.Warn_On_Unknown_Compile_Time_Warning,
           'j' => X.Warn_On_Non_Dispatching_Primitives,
+          'l' => X.Warn_On_Inherently_Limited_Type,
           'p' => X.Warn_On_Pedantic_Checks,
           'q' => X.Warn_On_Ignored_Equality,
           'r' => X.Warn_On_Component_Order,
index 10ec8a5700b8b2fe8246042151426ff0b3c41354..0ca0f68e1ecac882e9931881b99513bd76a05201 100644 (file)
@@ -72,6 +72,7 @@ package Warnsw is
          Warn_On_Hiding,
          Warn_On_Ignored_Equality,
          Warn_On_Ineffective_Predicate_Test,
+         Warn_On_Inherently_Limited_Type,
          Warn_On_Late_Primitives,
          Warn_On_Modified_Unread,
          Warn_On_No_Value_Assigned,
@@ -158,6 +159,7 @@ package Warnsw is
       Warn_On_Hiding |
       Warn_On_Ignored_Equality |
       Warn_On_Ineffective_Predicate_Test |
+      Warn_On_Inherently_Limited_Type |
       Warn_On_Late_Primitives |
       Warn_On_Modified_Unread |
       Warn_On_Non_Dispatching_Primitives |
@@ -342,6 +344,11 @@ package Warnsw is
    --  values that do not belong to the parent subtype. Modified by use of
    --  -gnatw_s/S.
 
+   Warn_On_Inherently_Limited_Type : Boolean renames F (X.Warn_On_Inherently_Limited_Type);
+   --  Set to True to generate warnings if a record type does not have a
+   --  limited keyword, but is inherently limited. Modified by use of
+   --  -gnatw_l/L.
+
    Warn_On_Late_Primitives : Boolean renames F (X.Warn_On_Late_Primitives);
    --  Warn when tagged type public primitives are defined after its private
    --  extensions.