]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Handle GNATcheck violations
authorSheri Bernstein <bernstein@adacore.com>
Tue, 1 Aug 2023 16:36:14 +0000 (16:36 +0000)
committerMarc Poulhiès <poulhies@adacore.com>
Tue, 5 Sep 2023 11:05:11 +0000 (13:05 +0200)
For the GNATcheck rule "Improper_Returns", either use pragma Annotate
to exempt the violation with the rationale "early returns for performance",
or refactor the code by replacing multiple returns by a single return
statement with a conditional expression; this is more readable and
maintainable, and also conformant with a Highly Recommended design principle
of ISO 26262-6.  For the GNATcheck rule "Discriminated_Records", use pragma
Annotate to exempt the violation with the rationale "only variant records
are disallowed".

gcc/ada/

* libgnarl/a-reatim.adb (Time_Of): Add pragma to exempt
Discriminated_Records.
* libgnat/s-imguti.adb (Round, Set_Decimal_Digits): Likewise.
* libgnat/s-multip.adb (Number_Of_CPUs): Likewise.
* libgnarl/s-tpopsp__posix-foreign.adb (Self): Refactor multiple
returns.

gcc/ada/libgnarl/a-reatim.adb
gcc/ada/libgnarl/s-tpopsp__posix-foreign.adb
gcc/ada/libgnat/s-imguti.adb
gcc/ada/libgnat/s-multip.adb

index 56a84789729ff1e7ef7631e0cc02f2f18c4d8746..24a77311f9dfc94046b0e80fdb813cc40fa49418 100644 (file)
@@ -307,6 +307,9 @@ is
    --  Start of processing for Time_Of
 
    begin
+      pragma Annotate (Gnatcheck, Exempt_On, "Improper_Returns",
+                       "early returns for performance");
+
       --  If SC is so far out of range that there is no possibility of the
       --  addition of TS getting it back in range, raise an exception right
       --  away. That way we don't have to worry about SC values overflowing.
@@ -356,6 +359,8 @@ is
             Out_Of_Range;
          end if;
       end if;
+
+      pragma Annotate (Gnatcheck, Exempt_Off, "Improper_Returns");
    end Time_Of;
 
    -----------------
index 4b3e200150d228b78632884c341d748cf2cffbf7..ebf0f622db09fb08715f5a87b974421fb9b1ae8f 100644 (file)
@@ -95,12 +95,10 @@ package body Specific is
       Result := pthread_getspecific (ATCB_Key);
 
       --  If the key value is Null then it is a non-Ada task
-
-      if Result /= System.Null_Address then
-         return To_Task_Id (Result);
-      else
-         return Register_Foreign_Thread;
-      end if;
+      return
+         (if Result /= System.Null_Address then To_Task_Id (Result)
+          else Register_Foreign_Thread
+         );
    end Self;
 
 end Specific;
index 4c8cf5f329537a6f328473f6bf6b249f2d129ef7..2e69e630c8a5dc19c13d830cc5b087443bf076f2 100644 (file)
@@ -119,6 +119,9 @@ package body System.Img_Util is
          pragma Assert (Digs'First < Digs'Last);
 
       begin
+         pragma Annotate (Gnatcheck, Exempt_On, "Improper_Returns",
+                       "early returns for performance");
+
          --  Nothing to do if rounding past the last digit we have
 
          if N >= LD then
@@ -178,6 +181,8 @@ package body System.Img_Util is
                Digits_Before_Point := Digits_Before_Point + 1;
             end if;
          end if;
+
+         pragma Annotate (Gnatcheck, Exempt_Off, "Improper_Returns");
       end Round;
 
       ---------
@@ -246,6 +251,9 @@ package body System.Img_Util is
    --  Start of processing for Set_Decimal_Digits
 
    begin
+      pragma Annotate (Gnatcheck, Exempt_On, "Improper_Returns",
+                    "early returns for performance");
+
       --  Case of exponent given
 
       if Exp > 0 then
@@ -398,6 +406,8 @@ package body System.Img_Util is
             end if;
          end if;
       end if;
+
+      pragma Annotate (Gnatcheck, Exempt_Off, "Improper_Returns");
    end Set_Decimal_Digits;
 
    --------------------------------
index 372f1407dbfe759751688582d1fc43714315d166..96177f9fc41ef9d8230c21333dbe7b5b30c5c391 100644 (file)
@@ -36,6 +36,9 @@ package body System.Multiprocessors is
 
    function Number_Of_CPUs return CPU is
    begin
+      pragma Annotate (Gnatcheck, Exempt_On, "Improper_Returns",
+                       "early returns for performance");
+
       if CPU'Last = 1 then
          return 1;
       else
@@ -46,6 +49,8 @@ package body System.Multiprocessors is
             return CPU (Gnat_Number_Of_CPUs);
          end;
       end if;
+
+      pragma Annotate (Gnatcheck, Exempt_Off, "Improper_Returns");
    end Number_Of_CPUs;
 
 end System.Multiprocessors;