https://gcc.gnu.org/g:8fe64e4d9cd22f7638fc590353c34c79b07f428f

commit r16-1959-g8fe64e4d9cd22f7638fc590353c34c79b07f428f
Author: Eric Botcazou <ebotca...@adacore.com>
Date:   Thu Jun 12 22:31:06 2025 +0200

    ada: Enforce alignment constraint for large Object_Size clauses
    
    The constraint is that the Object_Size must be a multiple of the alignment
    in bits.  But it's enforced only when the value of the clause is lower than
    the Value_Size rounded up to the alignment in bits, not for larger values.
    
    gcc/ada/ChangeLog:
    
            * gcc-interface/decl.cc (gnat_to_gnu_entity): Use default messages
            for errors reported for Object_Size clauses.
            (validate_size): Give an error for stand-alone objects of composite
            types if the specified size is not a multiple of the alignment.

Diff:
---
 gcc/ada/gcc-interface/decl.cc | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/gcc/ada/gcc-interface/decl.cc b/gcc/ada/gcc-interface/decl.cc
index 1d9832d69ad4..27d2cea1f3d2 100644
--- a/gcc/ada/gcc-interface/decl.cc
+++ b/gcc/ada/gcc-interface/decl.cc
@@ -4502,7 +4502,7 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, 
bool definition)
          if (Known_Esize (gnat_entity))
            gnu_size
              = validate_size (Esize (gnat_entity), gnu_type, gnat_entity,
-                              VAR_DECL, false, false, size_s, type_s);
+                              VAR_DECL, false, false, NULL, NULL);
 
          /* ??? The test on Has_Size_Clause must be removed when "unknown" is
             no longer represented as Uint_0 (i.e. Use_New_Unknown_Rep).  */
@@ -9696,6 +9696,20 @@ validate_size (Uint uint_size, tree gnu_type, Entity_Id 
gnat_object,
       return NULL_TREE;
     }
 
+  /* The size of stand-alone objects is always a multiple of the alignment,
+     but that's already enforced for elementary types by the front-end.  */
+  if (kind == VAR_DECL
+      && !component_p
+      && RECORD_OR_UNION_TYPE_P (gnu_type)
+      && !TYPE_FAT_POINTER_P (gnu_type)
+      && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size,
+                                    bitsize_int (TYPE_ALIGN (gnu_type)))))
+    {
+      post_error_ne_num ("size for& must be multiple of alignment ^",
+                        gnat_error_node, gnat_object, TYPE_ALIGN (gnu_type));
+      return NULL_TREE;
+    }
+
   return size;
 }

Reply via email to