I would like to know, if it is possible, how to generate a compiler warning based on how a function’s returned value’s typecasting corresponds to one of the passed parameters. In my example, I would like to generated a compiler warning if the function call is type-casted to anything less than what is defined by the “Bytes” parameter. This is used in a C program using IAR for the MSP430
For Example:
(INT16U)GetINTU(VarPtr, 2); // This is ok
(INT16U)GetINTU(VarPtr, 4); // generates warning
(INT32U)GetINTU(VarPtr, 4); // This is ok
(INT32U)GetINTU(VarPtr, 8); // generates warning
(INT64U)GetINTU(VarPtr, 4); // This is ok
Here is the said function:
INT64U GetINTU(INT8U* Address, INT8U Bytes)
{
INT64U Value = 0;
if(Bytes<=8)
{
do
{
Value += ((INT64U)(*Address++))<<(--Bytes<<3);
}while(Bytes);
}
return Value;
}
EDIT: I also considered returning a null pointer but that means I need a static variable. which I do not want to do
There’s one simple answer to this: You can’t. The reason being that you can’t tell what the return value will be typecasted to after the function returns.