]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Reject too-strict alignment specifications.
authorSteve Baird <baird@adacore.com>
Mon, 18 Mar 2024 21:35:33 +0000 (14:35 -0700)
committerMarc Poulhiès <poulhies@adacore.com>
Mon, 20 May 2024 07:47:03 +0000 (09:47 +0200)
For a discrete (or fixed-point) type T, GNAT requires that T'Object_Size
shall be a multiple of T'Alignment * 8 .
GNAT also requires that T'Object_Size shall be no larger than
Standard'Max_Integer_Size.
For a sufficiently-large alignment specification, these requirements can
conflict.
The conflict is resolved by rejecting such alignment specifications (which
were previously accepted in some cases).

gcc/ada/

* freeze.adb (Adjust_Esize_For_Alignment): Assert that a valid
Alignment specification cannot result in adjusting the given
type's Esize to be larger than System_Max_Integer_Size.
* sem_ch13.adb (Analyze_Attribute_Definition_Clause): In analyzing
an Alignment specification, enforce the rule that a specified
Alignment value for a discrete or fixed-point type shall not be
larger than System_Max_Integer_Size / 8 .

gcc/testsuite/ChangeLog:

* gnat.dg/specs/alignment2.ads: Adjust.
* gnat.dg/specs/alignment2_bis.ads: New test.

gcc/ada/freeze.adb
gcc/ada/sem_ch13.adb
gcc/testsuite/gnat.dg/specs/alignment2.ads
gcc/testsuite/gnat.dg/specs/alignment2_bis.ads [new file with mode: 0644]

index a980c7e5b47a1e0271c71ed9b70b7f879ff8e415..26e9d01d8b20ec817ccfccc217353579d1a72e3d 100644 (file)
@@ -303,8 +303,12 @@ package body Freeze is
       if Known_Esize (Typ) and then Known_Alignment (Typ) then
          Align := Alignment_In_Bits (Typ);
 
-         if Align > Esize (Typ) and then Align <= System_Max_Integer_Size then
-            Set_Esize (Typ, Align);
+         if Align > Esize (Typ) then
+            if Align > System_Max_Integer_Size then
+               pragma Assert (Serious_Errors_Detected > 0);
+            else
+               Set_Esize (Typ, Align);
+            end if;
          end if;
       end if;
    end Adjust_Esize_For_Alignment;
index 13bf93ca548932ebf43ee19402986cd35a6556eb..59c80022c2066add5bcfd9477c38b9c86c2915ff 100644 (file)
@@ -6573,6 +6573,21 @@ package body Sem_Ch13 is
                     ("alignment for & set to Maximum_Aligment??", Nam);
                   Set_Alignment (U_Ent, Max_Align);
 
+               --  Because Object_Size must be multiple of Alignment (in bits),
+               --  System_Max_Integer_Size limit for discrete and fixed point
+               --  types implies a limit on alignment for such types.
+
+               elsif (Is_Discrete_Type (U_Ent)
+                        or else Is_Fixed_Point_Type (U_Ent))
+                 and then Align > System_Max_Integer_Size / System_Storage_Unit
+               then
+                  Error_Msg_N
+                    ("specified alignment too large for discrete or fixed " &
+                     "point type", Expr);
+                  Set_Alignment
+                    (U_Ent, UI_From_Int (System_Max_Integer_Size /
+                                         System_Storage_Unit));
+
                --  All other cases
 
                else
index 0b6c14f1b7d4e04f0baebe3f1ab566f2c76fe380..75a002e9bee4ff3028c0bee19b2fb6dba8860f8f 100644 (file)
@@ -32,18 +32,4 @@ package Alignment2 is
   end record;
   for R4'Alignment use 32;
 
-  -- warning
-  type I1 is new Integer_32;
-  for I1'Size use 32;
-  for I1'Alignment use 32; -- { dg-warning "suspiciously large alignment" }
-
-  -- warning
-  type I2 is new Integer_32;
-  for I2'Alignment use 32; -- { dg-warning "suspiciously large alignment" }
-
-  -- OK, big size
-  type I3 is new Integer_32;
-  for I3'Size use 32 * 8; -- { dg-warning "unused" }
-  for I3'Alignment use 32;
-
 end Alignment2;
diff --git a/gcc/testsuite/gnat.dg/specs/alignment2_bis.ads b/gcc/testsuite/gnat.dg/specs/alignment2_bis.ads
new file mode 100644 (file)
index 0000000..ad31a40
--- /dev/null
@@ -0,0 +1,36 @@
+-- { dg-do compile }
+
+with Interfaces; use Interfaces;
+
+package Alignment2_Bis is
+
+  pragma Warnings (Off, "*size*");
+
+  -- OK, big size
+  type R3 is record
+    A, B, C, D : Integer_8;
+  end record;
+  for R3'Size use 32 * 8;
+  for R3'Alignment use 32;
+
+  -- OK, big size
+  type R4 is record
+    A, B, C, D, E, F, G, H : Integer_32;
+  end record;
+  for R4'Alignment use 32;
+
+  -- warning
+  type I1 is new Integer_32;
+  for I1'Size use 32;
+  for I1'Alignment use 32; -- { dg-error "error: specified alignment too large for discrete or fixed point type" }
+
+  -- warning
+  type I2 is new Integer_32;
+  for I2'Alignment use 32; -- { dg-error "error: specified alignment too large for discrete or fixed point type" }
+
+  -- OK, big size
+  type I3 is new Integer_32;
+  for I3'Size use 32 * 8;
+  for I3'Alignment use 32; -- { dg-error "error: specified alignment too large for discrete or fixed point type" }
+
+end Alignment2_Bis;