ada

Ignore Ada Function Return Values


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

I have a function which imports from an 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 a 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 then I get compiler warnings. Obviously, I'd rather avoid those.

I can't very well assign the value back to the value I'm adding to; this would undermine the point of the add operation being atomic.

There is the option of taking the value and doing something with it, like:

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

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

Edit: What is __sync_add_and_fetch_4?

This is a built-in atomic operation available on Intel CPUs. As such, part of my Autoconf/Automake process would be deciding if the operation is available, and use a fallback implementation, which involves a critical section, if it's not.

You can read about this and similar operations in GCC's section on atomic builtins.

The __sync_add_and_fetch_4 does pretty much exactly what it says. In C, it would look something like this:

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

So it's an atomic addition operation, which returns the result of the addition. Basically, it's an atomic += operator. The _4 means that it takes a 4-byte integer.

Edit: I understand that I could probably just switch off that particular compiler warning, but that always feels dirty to me. If there's a solution available that allows me to continue using -Wall -Werror then I'd love to see it.


Solution

  • declare
       dummy : constant return_type := my_function;
       pragma Unreferenced (dummy);
    begin null; end;
    

    or write a wrapper procedure.