How can I test which identifier is passed as the value of a macro parameter in C at compile time?
I'm writing wrapper macros for a micro-controller in C (GNU).
I have some macros that work like this:
#define _ReadBits(port, mask) (PORT ## port) & (mask)
#define ReadBits(portmask) _ReadBits(portmask)
#define SWITCH D, (1<<7)
This way I can say:
foo = ReadBits(SWITCH);
and I'll get
foo = PORTD & (1<<7);
That works great. I want to expand these to do something like this:
#define _ConfigAnalog(port, mask) BUILD_BUG_ON(port != B); AD1PCFGCLR = (mask)
#define ConfigAnalog(portmask) _ConfigAnalog(portmask)
That is, I want a compile-time error to happen if parameter port isn't B (because this micro-controller can configure only port B as analog).
Is there some way to do this in C?