On 21 June 2012 04:22, yafo lee <[email protected]> wrote:
> hi guys,
>
> In order to convert spin_lock_init(&adpt->tx_lock);
> to mtx_init(&adpt->tx_lock, "adpt->tx_lock", "adpt->tx_lock", MTX_DEF);
>
> I wrote the rule:
> @@
> struct mtx e;
> @@
> - spin_lock_init(&e)
> + mtx_init(&e, "e", "e", MTX_DEF)
>
> But I got "e" instead of  "adpt->tx_lock", how to fix that?

But even if this was trivially simple to do with coccinelle you would
end up with a
line where adpt->tx_lock is repeated three times and not so easy to read. And
from a maintenance point of view if the adpt variable is renamed
later, the strings
might get out of sync.

I think the C pre-processor is the best tool for generating the
strings, because since
it will be done compile time there is no string sync maintenance
problem. And the
statement will be simpler to read as well.

The simplest and naive approach would be

#define MTX_INIT(mutex_, opts_) mtx_init(mutex_, #mutex_, #mutex_, opts_)
        ...
        MTX_INIT(&adpt->tx_lock, MTX_DEF); /*  -->
mtx_init(&adpt->tx_lock, "&adpt->tx_lock", "&adpt->tx_lock", MTX_DEF);
*/

however that would include the address of ampersand in the string
which I assume is not wanted. This is not difficult to fix by just starting
one character into the string generated by the pre-processor if the
strings starts with '&'.

#define MTX_INIT(mutex_, opts_) \
do { \
        const char * name_type_str_[] = #mutex_; \
        const char * ntptr_; \
        ntptr_ = name_type_str_; \
        if (*ntptr_ == '&') \
                ntptr_++; \
        mtx_init(mutex_, ntptr_, ntptr_, opts_); \
} while(0)
        ...
        MTX_INIT(&adpt->tx_lock, MTX_DEF);


BR Håkon Løvdal
_______________________________________________
Cocci mailing list
[email protected]
http://lists.diku.dk/mailman/listinfo/cocci
(Web access from inside DIKUs LAN only)

Reply via email to