Re: [cocci] Search for the concatenation of meta variable + fixed string (similar fresh identifier)

Tobias Deiminger <[email protected]>
Newsgroups fr.inria.cocci
Message-ID <[email protected]>
Am 03.12.2025 22:47 schrieb Tobias Deiminger:
> Hello again :)
> 
> In Linux, the DEVICE_ATTR_RO macro works like this:
> 
> static ssize_t led_status_show(struct device *dev, struct
> device_attribute *attr, char *buf) { /* ... */ }
> 
> // The macro automatically concatenates led_status + _show for struct
> initialization.
> static DEVICE_ATTR_RO(led_status);
> 
> 
> I'd like to find the position of all functions assigned by
> DEVICE_ATTR_RO. So I thought to use a 1st rule to search for the
> identifier inside DEVICE_ATTR_RO, then use a 2nd rule to search for
> the inherited meta variable, concatenated with the fixed string
> "_show". Like:
> 
> @device_attr_ro@
> declarer name DEVICE_ATTR_RO;
> identifier attr_name;
> @@
>   DEVICE_ATTR_RO(attr_name);
> 
> @device_attr_ro_show_fn@
> identifier device_attr_ro.attr_name;
> position p_fn;
> @@
>   attr_name ## "_show" @ p_fn (...) {...}     // invalid syntax, but
> you get the point
> 
> 
> This doesn't work and I couldn't figure how to do it correctly.
> There's "fresh identifier" which looks close, but it only works for +,
> not for search context. Is such a search possible? How?
> 
> Tobias

Found a workaround using fresh identifier to add an intermediate helper 
construct:

@device_attr_ro@
declarer name DEVICE_ATTR_RO;
typedef __DEVICE_ATTR_RO_T;
identifier attr_name;
fresh identifier show_fn_name = attr_name ## "_show";
@@
   DEVICE_ATTR_RO(attr_name);
+ __DEVICE_ATTR_RO_T attr_name = show_fn_name;

/* Helper construct needed to get concatenated identifier for final 
search. */
@device_attr_ro_2@
identifier device_attr_ro.attr_name;
identifier show_fn;
@@
   __DEVICE_ATTR_RO_T attr_name = show_fn;

@device_attr_ro_show_fn@
identifier device_attr_ro_2.show_fn;
position p_fn;
@@
   show_fn@p_fn(...) {...}

Seems to work good enough. If there are cleaner ways, let me know.

Tobias
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.