RFC - C99 issue regarding unnamed union/structs


Flavio Ceolin
 

Hi guys,

One problem with have in Zephyr regarding C99 is that we are using a lot
of unnamed struct/unions. One example is:

"""
struct z_kernel {
/* For compatibility with pre-SMP code, union the first CPU
* record with the legacy fields so code can continue to use
* the "_kernel.XXX" expressions and assembly offsets.
*/
union {
struct _cpu cpus[CONFIG_MP_NUM_CPUS];
#ifndef CONFIG_SMP
struct {
/* nested interrupt count */
u32_t nested;

/* interrupt stack pointer base */
char *irq_stack;

/* currently scheduled thread */
struct k_thread *current;
};
#endif
};

...
"""

Named this structs/unions is OK, just a matter of text-replace around
the project. The problem is that we have some symbols being generated by
macros and I didn't find a clean way to fix it without extra changes.
The macro in question is GEN_OFFSET_SYM(). Take as example the following
code:

"""
GEN_OFFSET_SYM(_kernel_t, current);
"""
If I change it for something like:
"""
GEN_OFFSET_SYM(_kernel_t, cpu.state.current);
"""

It won't work because this macro concats parameters 1 and 2 to make a
new symbol. One solution is using a variadic parameter hack:

"""
#define GEN_OFFSET_SYM_INTERNAL_0(S, M) \
GEN_ABSOLUTE_SYM(__##S##_##M##_##OFFSET, offsetof(S, M))
#define GEN_OFFSET_SYM_INTERNAL_1(S, M, ALIAS) \
GEN_ABSOLUTE_SYM(__##S##_##ALIAS##_##OFFSET, offsetof(S, M))
#define GET_OVERRIDE(_1, _2, _3, NAME, ...) NAME

#define GEN_OFFSET_SYM(...) \
GET_OVERRIDE(__VA_ARGS__, GEN_OFFSET_SYM_INTERNAL_1, \
GEN_OFFSET_SYM_INTERNAL_0)(__VA_ARGS__)
"""

The problem with this solution is that C99 requires at least one
argument in a variadic macro. In other words, it also violates C99.

The other possible solution is change (or add a new) macro that receives three
parameters (the third is an alias).

Do you guys have other solution ? if now which one you prefer, add a new
macro or change the current one (and consequently all invocations).


Regards,
Flavio Ceolin

Join devel@lists.zephyrproject.org to automatically receive all group messages.