]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Restore alignment check for address clause given with alignment clause
authorEric Botcazou <ebotcazou@adacore.com>
Tue, 21 Oct 2025 21:33:01 +0000 (23:33 +0200)
committerMarc Poulhiès <dkm@gcc.gnu.org>
Mon, 3 Nov 2025 14:15:18 +0000 (15:15 +0100)
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.

gcc/ada/doc/gnat_rm/representation_clauses_and_pragmas.rst
gcc/ada/gnat_rm.texi
gcc/ada/gnat_ugn.texi
gcc/ada/sem_ch13.adb

index 7250f6586ee100a1c68a7006478820db9d82e9f0..65bb1877f7c637fcaeb4c170003858e2f3672ebc 100644 (file)
@@ -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).
 
index 68a3c143dd13de058284e092ad88368bbf57f172..23c9977ed4c3900e05cc5b1c18418fc1c0be395a 100644 (file)
@@ -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).
 
index 86b2cbc5d3f75e23cca4f8b38b6a58f0e13b07cb..478975739f12a6df5d412a89a11a6267af694252 100644 (file)
@@ -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
index 131df3d65d6d54b054c76f8b87be4a2d8542277d..31af1bb4675cda2b46f02a749e60aa7f145ed394 100644 (file)
@@ -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