From: Eric Botcazou <[email protected]>

The alignment check for address clauses had historically been performed
universally by GNAT, until a decade ago when it was disabled by default
on non-strict-alignment platforms.

This seems questionable when an alignment clause is also given for the
entity since the intent is clear, so the change restores it as well as
its associated warning.

Moreover, this alignment check had historically been subsumed into the
language-defined Range_Check, until two decades ago when it became the
implementation-defined Alignment_Check, while still being part of the
Range_Check to ease the transition.  Now is probably a good time to cut
the apron strings and make Alignment_Check standalone.

gcc/ada/ChangeLog:

        * doc/gnat_rm/representation_clauses_and_pragmas.rst (Address
        Clauses): Adjust description of alignment checks.
        * sem_ch13.adb (Address_Clause_Check_Record): Remove now unused
        Alignment_Checks_Suppressed component.
        (Alignment_Checks_Suppressed): Delete.
        (Analyze_Attribute_Definition_Clause) <Attribute_Alignment>: Do not
        consider the status of range checks to decide whether to generate an
        alignment check.  Always generate it if an alignment clause is also
        given for the entity.
        <Attribute_Alignment>: Generate an alignment check if an address
        clause is also given for the entity.
        (Register_Address_Clause_Check): Adjust for above removal.
        (Validate_Address_Clauses): Test the Check_Address_Alignment flag
        on the clause to decide whether to give the warning.
        * gnat_rm.texi: Regenerate.
        * gnat_ugn.texi: Regenerate.

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

---
 .../representation_clauses_and_pragmas.rst    |  3 +-
 gcc/ada/gnat_rm.texi                          |  5 +-
 gcc/ada/gnat_ugn.texi                         |  4 +-
 gcc/ada/sem_ch13.adb                          | 50 +++++++------------
 4 files changed, 23 insertions(+), 39 deletions(-)

diff --git a/gcc/ada/doc/gnat_rm/representation_clauses_and_pragmas.rst 
b/gcc/ada/doc/gnat_rm/representation_clauses_and_pragmas.rst
index 7250f6586ee..65bb1877f7c 100644
--- a/gcc/ada/doc/gnat_rm/representation_clauses_and_pragmas.rst
+++ b/gcc/ada/doc/gnat_rm/representation_clauses_and_pragmas.rst
@@ -1580,8 +1580,7 @@ machines with strict alignment requirements, GNAT
 checks (at compile time if possible, generating a warning, or at execution
 time with a run-time check) that the alignment is appropriate.  If the
 run-time check fails, then ``Program_Error`` is raised.  This run-time
-check is suppressed if range checks are suppressed, or if the special GNAT
-check Alignment_Check is suppressed, or if
+check is suppressed if the GNAT check Alignment_Check is suppressed, or if
 ``pragma Restrictions (No_Elaboration_Code)`` is in effect. It is also
 suppressed by default on non-strict alignment machines (such as the x86).
 
diff --git a/gcc/ada/gnat_rm.texi b/gcc/ada/gnat_rm.texi
index 68a3c143dd1..23c9977ed4c 100644
--- a/gcc/ada/gnat_rm.texi
+++ b/gcc/ada/gnat_rm.texi
@@ -19,7 +19,7 @@
 
 @copying
 @quotation
-GNAT Reference Manual , Oct 17, 2025
+GNAT Reference Manual , Oct 31, 2025
 
 AdaCore
 
@@ -20724,8 +20724,7 @@ machines with strict alignment requirements, GNAT
 checks (at compile time if possible, generating a warning, or at execution
 time with a run-time check) that the alignment is appropriate.  If the
 run-time check fails, then @code{Program_Error} is raised.  This run-time
-check is suppressed if range checks are suppressed, or if the special GNAT
-check Alignment_Check is suppressed, or if
+check is suppressed if the GNAT check Alignment_Check is suppressed, or if
 @code{pragma Restrictions (No_Elaboration_Code)} is in effect. It is also
 suppressed by default on non-strict alignment machines (such as the x86).
 
diff --git a/gcc/ada/gnat_ugn.texi b/gcc/ada/gnat_ugn.texi
index 86b2cbc5d3f..478975739f1 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 , Oct 06, 2025
+GNAT User's Guide for Native Platforms , Oct 31, 2025
 
 AdaCore
 
@@ -30333,8 +30333,8 @@ to permit their use in free software.
 
 @printindex ge
 
-@anchor{gnat_ugn/gnat_utility_programs switches-related-to-project-files}@w{   
                           }
 @anchor{d2}@w{                              }
+@anchor{gnat_ugn/gnat_utility_programs switches-related-to-project-files}@w{   
                           }
 
 @c %**end of body
 @bye
diff --git a/gcc/ada/sem_ch13.adb b/gcc/ada/sem_ch13.adb
index 131df3d65d6..31af1bb4675 100644
--- a/gcc/ada/sem_ch13.adb
+++ b/gcc/ada/sem_ch13.adb
@@ -442,11 +442,6 @@ package body Sem_Ch13 is
 
       Off : Boolean;
       --  Whether the address is offset within Y in the second case
-
-      Alignment_Checks_Suppressed : Boolean;
-      --  Whether alignment checks are suppressed by an active scope suppress
-      --  setting. We need to save the value in order to be able to reuse it
-      --  after the back end has been run.
    end record;
 
    package Address_Clause_Checks is new Table.Table (
@@ -457,26 +452,6 @@ package body Sem_Ch13 is
      Table_Increment      => 200,
      Table_Name           => "Address_Clause_Checks");
 
-   function Alignment_Checks_Suppressed
-     (ACCR : Address_Clause_Check_Record) return Boolean;
-   --  Return whether the alignment check generated for the address clause
-   --  is suppressed.
-
-   ---------------------------------
-   -- Alignment_Checks_Suppressed --
-   ---------------------------------
-
-   function Alignment_Checks_Suppressed
-     (ACCR : Address_Clause_Check_Record) return Boolean
-   is
-   begin
-      if Checks_May_Be_Suppressed (ACCR.X) then
-         return Is_Check_Suppressed (ACCR.X, Alignment_Check);
-      else
-         return ACCR.Alignment_Checks_Suppressed;
-      end if;
-   end Alignment_Checks_Suppressed;
-
    -----------------------------------------
    -- Adjust_Record_For_Reverse_Bit_Order --
    -----------------------------------------
@@ -7086,11 +7061,15 @@ package body Sem_Ch13 is
                      end if;
                   end;
 
-                  --  Entity has delayed freeze, so we will generate an
+                  --  The entity has delayed freeze, so we will generate an
                   --  alignment check at the freeze point unless suppressed.
+                  --  We will unconditionally generate it when the alignment
+                  --  is specified in addition to the address, to compensate
+                  --  for the check being suppressed by default on machines
+                  --  that do not need strict alignment of memory accesses.
 
-                  if not Range_Checks_Suppressed (U_Ent)
-                    and then not Alignment_Checks_Suppressed (U_Ent)
+                  if not Alignment_Checks_Suppressed (U_Ent)
+                    or else Present (Alignment_Clause (U_Ent))
                   then
                      Set_Check_Address_Alignment (N);
                   end if;
@@ -7165,6 +7144,14 @@ package body Sem_Ch13 is
                if Is_Array_Type (U_Ent) then
                   Set_Alignment (Base_Type (U_Ent), Align);
                end if;
+
+               --  See the Attribute_Address case above for the rationale
+
+               if not Is_Type (U_Ent)
+                 and then Present (Address_Clause (U_Ent))
+               then
+                  Set_Check_Address_Alignment (Address_Clause (U_Ent));
+               end if;
             end if;
          end Alignment;
 
@@ -16669,9 +16656,8 @@ package body Sem_Ch13 is
       Y   : Entity_Id;
       Off : Boolean)
    is
-      ACS : constant Boolean := Scope_Suppress.Suppress (Alignment_Check);
    begin
-      Address_Clause_Checks.Append ((N, X, A, Y, Off, ACS));
+      Address_Clause_Checks.Append ((N, X, A, Y, Off));
    end Register_Address_Clause_Check;
 
    ------------------------
@@ -19121,7 +19107,7 @@ package body Sem_Ch13 is
                --  Check for known value not multiple of alignment
 
                if No (ACCR.Y) then
-                  if not Alignment_Checks_Suppressed (ACCR)
+                  if Check_Address_Alignment (ACCR.N)
                     and then X_Alignment /= 0
                     and then ACCR.A mod X_Alignment /= 0
                   then
@@ -19166,7 +19152,7 @@ package body Sem_Ch13 is
                --  Note: we do not check the alignment if we gave a size
                --  warning, since it would likely be redundant.
 
-               elsif not Alignment_Checks_Suppressed (ACCR)
+               elsif Check_Address_Alignment (ACCR.N)
                  and then Y_Alignment /= Uint_0
                  and then
                    (Y_Alignment < X_Alignment
-- 
2.51.0

Reply via email to