3 ms·
Rather than a bunch of #if blocks, one could instead do: #define SIZE_T_BITS 64 #define PASTE3(a,b,c) a##b##c #define TYPEDEF_UINT(bits,name)
by skissane 4y ago
Rather than a bunch of #if blocks, one could instead do:
#define SIZE_T_BITS 64
#define PASTE3(a,b,c) a##b##c
#define TYPEDEF_UINT(bits,name) typedef PASTE3(uint,bits,_t) name
#define TYPEDEF_INT(bits,name) typedef PASTE3(int,bits,_t) name
TYPEDEF_UINT(SIZE_T_BITS,size_t);
TYPEDEF_INT(SIZE_T_BITS,ssize_t);
Should work for any value of SIZE_T_BITS, even something weird like 128 or 36 (PDP-10 port?), so long as you already have [u]intN_t defined.
It does require SIZE_T_BITS to be in bits rather than bytes, as your SIZEOF_SIZE_T is. But surely if your script can calculate SIZEOF_SIZE_T, it can multiply the answer by 8? (Or by CHAR_BIT, if we want to support weird platforms without 8-bit bytes – Lars Brinkhoff's PDP-10 port of GCC 3.2 has CHAR_BIT==9.)
- kazinator 4y agoThe main thing in my post is how we get that input value, in your case SIZE_T_BITS, at the preprocessing level where we can then use it to select alternative pieces of code. After that it's cosmetic. The simple, dumb #if blocks have the virtue is that they are not hostile to simple tooling, like Exuberant Ctags, Cscope, and whatnot. When we ask the editor to jump to the definition of ssize_t, it knows the three possible places where it is defined and serves them up. This alternative is possible: #define PASTE3(a,b,c) a##b##c #define UINT_TYPE(bits) PASTE3(uint,bits,_t) #define INT_TYPE(bits,name) PASTE3(int,bits,_t) typedef UINT_TYPE(SIZE_T_BITS) usize_t; typedef INT_TYPE(SIZE_T_BITS) ssize_t; I think in this form, there is a good chance the tools will grok the typedefs and index them, because we have not disguised the basic phrase structure.