Ignore Ada Function Return Values

Is there a way to ignore return values ​​in Ada functions?

I have a function that imports from Intrinsic.

 subtype int32 is Interfaces.Interger_32; function Intrinsic_Sync_Add_And_Fetch (P : access int32; I : int32) return int32; pragma Import( Intrinsic, Intrinsic_Sync_Add_And_Fetch, "__sync_add_and_fetch_4"); 

If I want to use this in the procedure, I need to accept the return value or I will get a compiler error:

 cannot use function Intrinsic_Sync_Add_And_Fetch in procedure call. 

But if I create a variable that simply takes the return value of the function and is never used, I get compiler warnings. Obviously, I would rather avoid this.

I cannot properly assign a value to the return value that I am adding; this would undermine the point of the add operation, which is atomic.

There is the ability to take value and do something like it:

 val := Intrinsic_Sync_Add_And_Fetch(...); if val := 0 then null; end if; 

This makes the code compile without errors or warnings, but it seems to me that this is stupid. How can I get around this language feature and safely ignore the return value?

Edit: what is __sync_add_and_fetch_4?

This is an integrated atomic operation available for Intel processors. That way, part of my Autoconf / Automake process will decide if an operation is available and use a fallback implementation that includes a critical section if it is not.

You can read about this and similar operations in the GCC section for atomic inline functions .

__sync_add_and_fetch_4 does pretty much what it says. In C it will look something like this:

 int32_t __sync_add_and_fetch_4(int32_t *ptr, int32_t value) { *ptr += value; return *ptr; } 

So, this is an atom addition operation that returns the result of an addition. In principle, this is the atomic operator += . _4 means that it takes an integer of 4 bytes.

Change I understand that I could just turn off this compiler warning, but that always seems dirty to me. If there is a solution available that allows me to continue using -Wall -Werror , then I would really like to see it.

+4
source share
4 answers
 declare dummy : constant return_type := my_function; pragma Unreferenced (dummy); begin null; end; 

or write a wrapping procedure.

+4
source

If you never want to refer to the return value, why not declare the subroutine as a procedure? The value will be returned in the register, so throwing it away will not cause much grief. (I will be fixed on this!)

 subtype int32 is Interfaces.Integer_32; procedure Intrinsic_Sync_Add_And_Fetch (P : access int32; I : int32); pragma Import( Intrinsic, Intrinsic_Sync_Add_And_Fetch, "__sync_add_and_fetch_4"); 
+3
source

You said that you were configured only for the GNAT compiler. The GNAT User Guide says:

Note that a special exception applies to variables that contain any substrings DISCARD, DUMMY, IGNORE, JUNK, UNUSED, in any enclosure. Such variables are believed to be intentionally used in situations where otherwise a warning will be given, therefore warnings of this kind are always suppressed for such variables.

So the simplest solution to your problem:

 unused := Intrinsic_Sync_Add_And_Fetch(...); 

Although you may need to wrap this in a procedure if you intend to use it more than two times:

 procedure Intrinsic_Sync_Add_And_Fetch(P : access int32; I : int32) is unused : int32; begin unused := Intrinsic_Sync_Add_And_Fetch(P : access int32; I : int32); end Intrinsic_Sync_Add_And_Fetch; 
+3
source

I don’t know how to ignore the return value of a function in Ada: the language was specifically designed to force you to store these return values.

personally, I would save the return value and ignore any warning regarding the use of the variable. in any case, the warning is rather strange, since the variable is really used to store the return value.

0
source

Source: https://habr.com/ru/post/1395551/


All Articles