]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Remove GNATcheck violations
authorSheri Bernstein <bernstein@adacore.com>
Fri, 17 Nov 2023 22:14:48 +0000 (22:14 +0000)
committerMarc Poulhiès <poulhies@adacore.com>
Thu, 30 Nov 2023 10:12:48 +0000 (11:12 +0100)
Remove GNATcheck violations by refactoring code and also using
pragma Annotate to exempt them.

gcc/ada/

* libgnat/i-cstrin.adb (Free): Rewrite code so there is only one
return, to remove Improper_Returns violation.
(Position_Of_Nul): Add pragma to exempt Improper_Returns
violation.
(To_Chars_Ptr): Likewise.
(Value): Likewise

gcc/ada/libgnat/i-cstrin.adb

index afbac727e3130680fc364c9d7be076af16788a2c..1eb28655004b217c9ab9aef55ec7b0aea0f2041c 100644 (file)
@@ -92,12 +92,10 @@ is
 
    procedure Free (Item : in out chars_ptr) is
    begin
-      if Item = Null_Ptr then
-         return;
+      if Item /= Null_Ptr then
+         Memory_Free (Item);
+         Item := Null_Ptr;
       end if;
-
-      Memory_Free (Item);
-      Item := Null_Ptr;
    end Free;
 
    --------------------
@@ -187,6 +185,8 @@ is
 
    function Position_Of_Nul (Into : char_array) return size_t is
    begin
+      pragma Annotate (Gnatcheck, Exempt_On, "Improper_Returns",
+                       "early returns for performance");
       for J in Into'Range loop
          if Into (J) = nul then
             return J;
@@ -194,6 +194,8 @@ is
       end loop;
 
       return Into'Last + 1;
+
+      pragma Annotate (Gnatcheck, Exempt_Off, "Improper_Returns");
    end Position_Of_Nul;
 
    ------------
@@ -226,6 +228,8 @@ is
       Nul_Check : Boolean := False) return chars_ptr
    is
    begin
+      pragma Annotate (Gnatcheck, Exempt_On, "Improper_Returns",
+                       "early returns for performance");
       if Item = null then
          return Null_Ptr;
       elsif Nul_Check
@@ -235,6 +239,8 @@ is
       else
          return To_chars_ptr (Item (Item'First)'Address);
       end if;
+
+      pragma Annotate (Gnatcheck, Exempt_Off, "Improper_Returns");
    end To_Chars_Ptr;
 
    ------------
@@ -302,6 +308,8 @@ is
       Length : size_t) return char_array
    is
    begin
+      pragma Annotate (Gnatcheck, Exempt_On, "Improper_Returns",
+                       "early returns for performance");
       if Item = Null_Ptr then
          raise Dereference_Error;
       end if;
@@ -328,6 +336,8 @@ is
 
          return Result;
       end;
+
+      pragma Annotate (Gnatcheck, Exempt_Off, "Improper_Returns");
    end Value;
 
    function Value (Item : chars_ptr) return String is
@@ -339,6 +349,8 @@ is
       Result : char_array (0 .. Length);
 
    begin
+      pragma Annotate (Gnatcheck, Exempt_On, "Improper_Returns",
+                       "early returns for performance");
       --  As per AI-00177, this is equivalent to:
 
       --    To_Ada (Value (Item, Length) & nul);
@@ -357,6 +369,8 @@ is
 
       Result (Length) := nul;
       return To_Ada (Result);
+
+      pragma Annotate (Gnatcheck, Exempt_Off, "Improper_Returns");
    end Value;
 
 end Interfaces.C.Strings;