Reputation: 375
I am running pc lint on my code and I am getting the following warning Converting enum 'TEST_VALUE' to 'int
.
Can a macro like a function force the parameter to be a certain type or is it always just an int. I know I could just have the function configure
pass an int instead but then pc-lint also complains.
#define RESULT(x) (((x) & 0x7) << 11)
typedef enum {
RES = 0x00,
RES_TWO,
RES_THREE,
RES_FOUR
} TEST_VALUE;
int configure(TEST_VALUE values)
{
uint32_t temp = RESULT(values);
return temp;
}
Upvotes: 0
Views: 1006
Reputation: 181159
Can a macro like a function force the parameter to be a certain type or is it always just an int.
You seem to be misunderstanding the nature of the issue. It is not the macro invocation itself that pc lint is complaining about. No conversion whatever is performed by that. The problem, rather, is with the code to which the macro expands. The resulting full statement is
uint32_t temp = (((values) & 0x7) << 11);
, where values
is of type TEST_VALUE
, an enum. Evaluating that expression involves converting values
to type int
, which pc lint considers questionable enough to warrant a warning. It would issue the same warning if you used the above line directly, instead of generating it via a macro.
Now, you could put an explicit cast into your macro; something like this might be appropriate:
#define RESULT(x) (((uint32_t)(x) & 0x7) << 11)
That still involves an enum-to-integer conversion, however. Perhaps pc lint would be satisfied if you express the conversion explicitly like that, but as far as I know, it might still complain. The underlying reason for the warning is still there: it is questionable to use values of enum type for their integer values instead of for their identities. If you want named integer constants then declare variables or define macros for them.
Upvotes: 3