I’m doing module testing of code written for a different platform and are having a problem where I need to constrict the sizes of the data types in the module being tested. Since I can’t modify the module file directly I thought of using stdint.h typedefs and replace the modules declaration using defines. In essense this:
#include <stdint.h>
#define int int16_t
int main() {
uint16_t ui = 2;
unsigned int uii = 3;
printf("Hello\n");
printf("Test %d, %d\n", ui, uii);
return 0;
}
However this fails with this message:
error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘uii’
Is there another way to do this kind of type replacement?
The
signed,unsigned,long,shortmodifiers can only be applied to the keywords that denote built-in types. They can’t be applied to a typename that happens to be an alias for a built-in type: the C syntax doesn’t allow it.As for what you can do, I think Mysticial’s answer covers it. Changing the size of
intwill conflict with whatever ABI the compiler is using to make library and system calls, so you can’t really do it without compiler support. For example suppose you have a function declared as follows:If you replace all mentions of
intwithshortin the TU that calls that function, but not the TU that implements it, then the caller will pass and receive ashort, whereas the function implementation expects and returns anint. This won’t necessarily work. You need all libraries, including the standard libraries and any system calls they make, to be compiled such that caller and callee agree what anintis.One option of course is to change all the code you’re testing, to use macros in place of
int,unsigned int, and so on. Then any function declarations in headers are left alone. There will be implicit conversions when the types won’t match, which might provoke compiler warnings and truncate values, but at least has defined behavior. Basically it’s dependency injection via the preprocessor.