c - Purpose of #define foo() do { } while (0) -
While browsing the sources of LinCAN driver, I found some macros that were bothering me.
# other / * CONFIG_PREEMPT * / #define can_preempt_disable () while doing (0) #define can_preempt_enable () while {0} #endif / * CONFIG_PREEMPT * /
I understand
{...} ... if break (position); ...} while (0); Type a throw
by using break
. I wrap the sequence of works like quote eg
#define FOO () do {foo (); times (); } While avoiding unconscious if
I understand that sometimes "no-op statements" are required for #define, but this is particularly important to (0)
Why? In particular, empty braces, false conditions, while ... do? Some syntax warnings do not quite understand me?
This is:
#if! Defined (CONFIG_PREEMPT_RT) & amp; Amp; (Defined (CONFIG_PREEMPT) || (LINUX_VERSION_CODE & gt; = KERNEL_VERSION (2,6,0)) #define can_preempt_disable preempt_disable #define can_preempt_enable preempt_enable #else/ * CONFIG_PREEMPT * / #define can_preempt_disable () Make {} while (0) # In this way, the first part is the code that you get when you ask for pre-quarling protection, otherwise you get empty, not-nothing, loop.
is also more idiotic because it is often used in these cases.I think they are common causes , I.e. to ensure that the macro is still a valid statement.
> Definition A A termination should not be semicolon, because this will be the code using it, as it starts:
int c_can_wakeup_tx (struct canchip_t * chip, struct msgobj_t * obj) {Can_preempt_disable (); ..
Therefore, the macro is used like any other function call, and semicolon is correct where the macro is applied. This is very common.
UPDATE 2 : define this as
;
leads to double semicolon which is weak, at least in my opinion aempty brace pairI think, but it < Creating code / time{}
works<3> Update : As mentioned in a comment, a blank brace pair will not work since you can not insert a semicolon after the call. Ahh. Thanks!