Conditional guards

From: Ricardo Ribalda
Date: Mon May 06 2024 - 14:38:41 EST


Hi Peter

Probably a stupid question.... but I'd rather ask the expert directly :)

I am trying to fix some of the cocci warnings with guard() and there
is a use case that I do not know how to do. I have something like:

void use_hardware(priv) {
if (priv->model == 42)
mutex_lock(&priv->lock):

// DO things here

if (priv->model == 42)
mutex_unlock(&priv->lock):
}

if I replace:
if (priv->model == 42)
mutex_lock(&priv->lock):
with
if (priv->model == 42)
guard(mutex)(&priv->lock)

gcc screams at me:
drivers/media/dvb-core/dvb_frontend.c: In function ‘dvb_frontend_open’:
/include/linux/cleanup.h:119:9: error: expected expression before
‘class_mutex_t’
119 | class_##_name##_t var
__cleanup(class_##_name##_destructor) = \
| ^~~~~~
/include/linux/cleanup.h:164:9: note: in expansion of macro ‘CLASS’
164 | CLASS(_name, __UNIQUE_ID(guard))
| ^~~~~
drivers/media/dvb-core/dvb_frontend.c:2776:17: note: in expansion of
macro ‘guard’
2776 | guard(mutex)(&apadter->mfe_lock);


Is there a way to support something like this?

Regards!

--
Ricardo Ribalda