Resolving a warning in macro

187 views Asked by At

I am dealing with a few macros producing warnings. One of them is ConvToSector(x):

#define STARTSECTOR 0x0002000u
#define STARTLSB    6u
#define STARTNUM    7u
#define TRICKYLSB   3u
#define ConvToSector(x) (((x)>=STARTSECTOR) ? ((((x)-STARTSECTOR) >> STARTLSB) + STARTNUM) : ((x) >> TRICKYLSB))

producing

conversion to 'uint8_t' from 'long unsigned int' may alter its value [-Wconversion]

I know all of the constants are unsigned and input to ConvToSector(x) is unsigned integer (uint32_t). So where is long unsigned int coming from?

1

There are 1 answers

0
Keith Thompson On BEST ANSWER

The warning is correct; converting a long unsigned int value to uint8_t can alter its value.

You can inhibit the warning either by not specifying the -Wconversion option (it's not enabled by default so something must be setting it), or by using a cast rather than an implicit conversion. I can't tell exactly where the cast should be added without more information.

As for the long unsigned int type mentioned in the message, again, it's hard to tell without seeing more code. It's likely that uint32_t is a typedef for unsigned long int -- but a quick experiment shows that gcc's error messages refer to the declared type even if it's a typedef.