[COMMITTED] ada: Remove GNATcheck violations

Message ID 20231130101914.3094378-1-poulhies@adacore.com
State Committed
Commit dab7e3430e76c7f23839e112f1c9676383263256
Headers
Series [COMMITTED] ada: Remove GNATcheck violations |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm warning Patch is already merged
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 warning Patch is already merged

Commit Message

Marc Poulhiès Nov. 30, 2023, 10:19 a.m. UTC
  From: Sheri Bernstein <bernstein@adacore.com>

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

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

---
 gcc/ada/libgnat/i-cstrin.adb | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)
  

Patch

diff --git a/gcc/ada/libgnat/i-cstrin.adb b/gcc/ada/libgnat/i-cstrin.adb
index afbac727e31..1eb28655004 100644
--- a/gcc/ada/libgnat/i-cstrin.adb
+++ b/gcc/ada/libgnat/i-cstrin.adb
@@ -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;