Skip to contentSkip to conversation

Search for the concatenation of meta variable + fixed string (similar fresh identifier)

cocci Tobias Deiminger 2 participants 9 messages Dec 3, 2025 – Dec 7, 2025

lore
Share & tools
lore
#0 lore raw

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

#1 lore raw

Am 03.12.2025 22:47 schrieb Tobias Deiminger:

Quoted 32 lines

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

Quoted 5 lines

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

#7 lore raw

Re: [cocci] Search for the concatenation of meta variable + fixed string

In reply to Tobias Deiminger
Quoted 2 lines

// The macro automatically concatenates led_status + _show for struct initialization. static DEVICE_ATTR_RO(led_status);

See also:
https://elixir.bootlin.com/linux/v6.18/source/include/linux/device.h#L193-L199

Quoted 1 line

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:

The source code search idea is reasonable.
But the indicated data processing algorithm is improvable.

Where would you like to store the identifiers which are passed to calls of the macro “DEVICE_ATTR_RO”?

The corresponding data collection can be used to apply subsequent code queries also with the help of regular expressions, can't it?

How many computation resources can you invest then?

Regards,
Markus

#2 lore raw

Re: [cocci] Search for the concatenation of meta variable + fixed string

In reply to Tobias Deiminger
Quoted 1 line

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

Please reconsider your solution approach once more.

Quoted 8 lines

@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;

Would you really like to modify source code here?

Are you more interested in an “ordinary” source code search?

Quoted 1 line

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

I suggest to take another look at algorithmic properties.

Would you need to extract identifiers from selected macro calls?

Regards,
Markus

#3 lore raw

Re: [cocci] Search for the concatenation of meta variable + fixed string

Hi,

(copy for list, sorry for again messing up To:)

Am 04.12.2025 08:54 schrieb Markus Elfring:

Quoted 14 lines
Quoted 1 line

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

Please reconsider your solution approach once more.

Quoted 8 lines

@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;

Quoted 1 line

Would you really like to modify source code here?

No. It's a temporary modification as workaround. I'm using --no-show-diff, and don't replace inline.

Quoted 1 line

Are you more interested in an “ordinary” source code search?

Correct, it's only about searching. But not "ordinary". If there's another OSS tool that could do semantic code search, can deal with C macros and supports "hops" like first search marro, then search definition of function referenced by macro please tell me. Coccinelle is the only tool capable enough I currently know.

Quoted 3 lines
Quoted 1 line

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

I suggest to take another look

I took reasonable amount of looks and found no better way than this [1]. That's why I'm posting here and hope to receive SmPL snippets that demonstrate a more idiomatic less complex way.

Quoted 1 line

at algorithmic properties.

The linux tree was searched in 44 s locally, which is fine for me.

Quoted 1 line

Would you need to extract identifiers from selected macro calls?

Yes, I print functions names and positions in python scripts. The result, verified with samples, looks complete [2].

Tobias

[1] https://github.com/haxtibal/linux/blob/tdmg/cocci/cocci/sysfs.cocci
[2] https://github.com/haxtibal/linux/actions/runs/19922697880/job/57114762347#step:4:512

#4 lore raw

Re: [cocci] Search for the concatenation of meta variable + fixed string

Quoted 3 lines
Quoted 1 line

Are you more interested in an “ordinary” source code search?

Correct, it's only about searching.

Thanks for this acknowledgement.

Quoted 1 line

But not "ordinary".

Corresponding views are varying.

Quoted 1 line

If there's another OSS tool that could do semantic code search,

There are further tools available for this purpose.

Quoted 1 line

can deal with C macros and supports "hops" like first search marro, then search definition of function referenced by macro please tell me.

The alternatives are more restricted then.
But it seems that you are also looking for help on algorithm design.

Quoted 1 line

Coccinelle is the only tool capable enough I currently know.

The semantic patch language has got a lot of potential for more convenient data processing.

Quoted 3 lines
Quoted 1 line

I suggest to take another look

I took reasonable amount of looks and found no better way than this [1].

It seems that your views can become too limited so far.

doc: Use coccinelle to identify important functions
https://github.com/haxtibal/linux/commit/ff6904ce23a022908fd8bbfc0b804900154ae48b

https://github.com/haxtibal/linux/blob/tdmg/cocci/cocci/sysfs.cocci

Quoted 3 lines

That's why I'm posting here and hope to receive SmPL snippets that demonstrate a more idiomatic less complex way.

Quoted 1 line

at algorithmic properties.

There are special development challenges to consider because you need to perform data processing especially with macros.

Quoted 1 line

The linux tree was searched in 44 s locally, which is fine for me.

It is nice that your computer is sufficiently powerful for your initial experiments.

Quoted 3 lines
Quoted 1 line

Would you need to extract identifiers from selected macro calls?

Yes, I print functions names and positions in python scripts. The result, verified with samples, looks complete [2].

Would you get further development ideas from any public SmPL script examples (which I published over the years)?

See also:
Deletion of checks before specific function calls in Linux source files
https://build.opensuse.org/package/show/home:elfring:semantic_patching:Deletion_of_checks_before_specific_function_calls/Deletion_of_checks_before_specific_function_calls_in_Linux_source_files

How do you think about to split and configure your data processing steps similarly?

Regards,
Markus

#5 lore raw

Re: [cocci] Searching for device attributes with SmPL?

In reply to Tobias Deiminger
Quoted 1 line

The linux tree was searched in 44 s locally, which is fine for me.

You might be looking for further refinements accordingly.

* Would you like to point any command parameters out for the discussed software tests?

* How do you think about to distinguish implementation details better also for the usage of serial or parallel data processing?

See also:
https://gitlab.inria.fr/coccinelle/coccinelle/-/blob/master/docs/manual/spatch_options.tex#L771-795

Regards,
Markus

#8 lore raw

Re: [cocci] Analysis of device attribute usage with SmPL?

In reply to Tobias Deiminger
Quoted 1 line

I'd like to find the position of all functions assigned by DEVICE_ATTR_RO.

I guess that such a data processing idea will trigger further collateral evolution.

Quoted 1 line

So I thought to use a 1st rule to search for the identifier inside DEVICE_ATTR_RO,

In which files would you expect such an item (device attribute?) to be found?

Quoted 1 line

then use a 2nd rule to search for the inherited meta variable, concatenated with the fixed string "_show". Like:

Will a need evolve for further data processing steps?

Quoted 5 lines

@device_attr_ro@
declarer name DEVICE_ATTR_RO;
identifier attr_name;
@@
  DEVICE_ATTR_RO(attr_name);

Can development interests eventually grow for imports of such information into databases?

…>   attr_name ## "_show" @ p_fn (...) {...}     // invalid syntax, but you get the point

Quoted 3 lines

This doesn't work and I couldn't figure how to do it correctly.

…

You probably know further programming interfaces which support the recombination of captured data in various ways.

Regards,
Markus