user5069935
user5069935

Reputation:

Ada - compiler warning about "mod 64" type definition

I require a modular integer type with valid values between 0 and 63 inclusive. Such as...

type Mix_Byte is mod 64;

This does compile and work as expected but the compiler helpfully draws my attention to a possible oversight on my part...

warning: 2 ** 64 may have been intended here

As it happens I do not intend that at all, but it's nice to know the compiler is on the look out :)

It only seems to give this warning for values 32 or 64, but not 8, 16 or 128. I understand that 32 and 64 are common integer sizes and in those cases 2 ** n would make sense.

How do I silence this specific compiler warning for this particular instance (I want to allow it globally throughout my project in case I make a genuine mistake elsewhere).

I presume I can articulate the code differently in order to be more precise with my meaning?

Upvotes: 2

Views: 139

Answers (2)

DeeDee
DeeDee

Reputation: 5941

Some additional background info (apart from egilhh's answer): the check is done in freeze.adb (see here). The warning can be enabled/disabled using -gnatw.m/.M. (see output of gnatmake --help). You can temporarily disable the warning by using the Warnings pragma (see also here and here):

main.adb

procedure Main is

   pragma Warnings (Off, "*may have been intended here");
   type Mix_Byte_1 is mod 64;
   pragma Warnings (On, "*may have been intended here");

   type Mix_Byte_2 is mod 64;   --  Line 7

begin
   null;
end Main;

output (gnat)

$ gcc -c main.adb
main.adb:7:27: warning: 2 ** 64 may have been intended here

Upvotes: 3

egilhh
egilhh

Reputation: 6430

You can try to write it as a power of two:

type Mix_Byte is mod 2**6;

Edit:

Alternatively, (based on more info in your comment) you can use a named number as the modulus:

Modulus : constant := 64;
type Mix_Byte is mod Modulus;

Upvotes: 4

Related Questions