diff mbox series

[COMMITTED,09/16] ada: Emit a warning on inheritly limited types

Message ID 20240823085359.1399344-9-poulhies@adacore.com
State New
Headers show
Series [COMMITTED,01/16] ada: First controlling parameter aspect | expand

Commit Message

Marc Poulhiès Aug. 23, 2024, 8:53 a.m. UTC
From: Viljar Indus <indus@adacore.com>

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.

Tested on x86_64-pc-linux-gnu, committed on master.

---
 ...building_executable_programs_with_gnat.rst | 17 ++++++++++
 gcc/ada/gnat_ugn.texi                         | 27 ++++++++++++++-
 gcc/ada/sem_ch3.adb                           | 34 +++++++++++++++++++
 gcc/ada/warnsw.adb                            |  3 +-
 gcc/ada/warnsw.ads                            |  7 ++++
 5 files changed, 86 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/gcc/ada/doc/gnat_ugn/building_executable_programs_with_gnat.rst b/gcc/ada/doc/gnat_ugn/building_executable_programs_with_gnat.rst
index ce3ed0cc65a..07ca2ea22c3 100644
--- a/gcc/ada/doc/gnat_ugn/building_executable_programs_with_gnat.rst
+++ b/gcc/ada/doc/gnat_ugn/building_executable_programs_with_gnat.rst
@@ -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`
diff --git a/gcc/ada/gnat_ugn.texi b/gcc/ada/gnat_ugn.texi
index 0e3ee935552..dcde9ea705b 100644
--- a/gcc/ada/gnat_ugn.texi
+++ b/gcc/ada/gnat_ugn.texi
@@ -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)
 
 
diff --git a/gcc/ada/sem_ch3.adb b/gcc/ada/sem_ch3.adb
index 3b44f0a5100..4dac4eec108 100644
--- a/gcc/ada/sem_ch3.adb
+++ b/gcc/ada/sem_ch3.adb
@@ -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;
diff --git a/gcc/ada/warnsw.adb b/gcc/ada/warnsw.adb
index ea7e94c4114..2bfb56ec513 100644
--- a/gcc/ada/warnsw.adb
+++ b/gcc/ada/warnsw.adb
@@ -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,
diff --git a/gcc/ada/warnsw.ads b/gcc/ada/warnsw.ads
index 10ec8a5700b..0ca0f68e1ec 100644
--- a/gcc/ada/warnsw.ads
+++ b/gcc/ada/warnsw.ads
@@ -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.