public inbox for cocci@systeme.lip6.fr
 help / color / mirror / Atom feed
From: Tobias Deiminger <tobias.deiminger@posteo.de>
To: cocci@inria.fr
Subject: Re: [cocci] Can we match a known macro by macro name instead of expanded function name?
Date: Sat, 29 Nov 2025 13:15:06 +0000	[thread overview]
Message-ID: <2897bda0c438111e967017f6bb3422f0@posteo.de> (raw)
In-Reply-To: <b4884a21-4a59-c549-8466-164a20a76abe@inria.fr>

Hi Julia,

Am 29.11.2025 11:56 schrieb Julia Lawall:
> On Sat, 29 Nov 2025, Tobias Deiminger wrote:
> 
>> Hi,
>> 
>> my newbie assumption was we could match the following by macro name
>> 
>>   // my.c
>>   SYSCALL_DEFINE0(foo_bar) { return; }
>> 
>> using this rules
>> 
>>   // my.cocci
>>   @r@
>>   identifier fn =~ "SYSCALL_DEFINE[0-6]+";
>>   position p;
>>   @@
>>   fn@p(...) {...}
>> 
>>   @script:python@
>>   fn << r.fn;
>>   p << r.p;
>>   @@
>>   print(f"match: {fn} at line {p[0].current_element_line}")
>> 
>> 
>> Turns out, SYSCALL_DEFINE0 is a "known macro" from
>> /usr/lib/coccinelle/standard.h. In this case coccinelle seems to match 
>> an
>> identifier on the expanded function name, not the macro name. I.e., 
>> this would
>> work:
>> 
>>   identifier fn =~ "foo.*";
>> 
>> But I really want to search all syscalls by the conventional macro 
>> names, i.e.
>> search for SYSCALL_DEFINE[0-6]+. How to do that?
> 
> The problem is that the code won't parse with the conventional macro
> name.  Maybe you could change the macro definition in standard.h to put 
> eg
> __SYSCALL_DEFINE0 in front of the function name?  That would be 
> considered
> to be an attribute and then you could put __SYSCALL_DEFINE0 as an
> attribute name in your semantic patch.

sorry for having to ask again. Your suggestion sounds very reasonable. 
But I couldn't figure out how to match on attributes. I guessed it would 
be something like this

   @r@
   attribute a;
   identifier fn;
   @@

   a fn(...) { ... }

But that gives a .cocci parse error:

   minus: parse error:
     File "my.cocci", line 6, column 2, charpos = 38
     around = 'fn',
     whole content = a fn(...) { ... }

Expected, IIUC, since attribute doesn't occur in fundecl from grammar:

   fundecl ::=  [fn_ctype] funinfo * funid ([PARAMSEQ(param, ε)]) { 
[stmt_seq] }

How else to look for functions with attributes then?

Tobias

  reply	other threads:[~2025-11-29 13:15 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-29 10:38 [cocci] Can we match a known macro by macro name instead of expanded function name? Tobias Deiminger
2025-11-29 10:56 ` Julia Lawall
2025-11-29 13:15   ` Tobias Deiminger [this message]
2025-11-29 13:32     ` Markus Elfring
2025-11-29 13:51     ` Julia Lawall
2025-11-29 14:51       ` Markus Elfring
2025-11-29 15:00         ` Julia Lawall
2025-11-29 15:10           ` [cocci] Questionable macro expansions Markus Elfring
2025-11-29 15:55       ` [cocci] Can we match a known macro by macro name instead of expanded function name? Tobias Deiminger
2025-11-29 15:59         ` Julia Lawall
     [not found]           ` <67d1e00d2e22a4655cb10ec55d1a99db@posteo.de>
     [not found]             ` <83c8a7aa-9b98-8ac1-d563-e8fe2588f9a@inria.fr>
     [not found]               ` <fd7307f8024706d4bd128d0b7f43bb96@posteo.de>
2025-11-29 17:33                 ` Tobias Deiminger
2025-11-29 17:38                   ` Markus Elfring
2025-11-29 17:59                   ` Julia Lawall
2025-11-29 18:28                     ` Markus Elfring
2025-11-29 18:41                       ` Julia Lawall
2025-11-29 19:00                         ` Markus Elfring
2025-11-29 20:06                     ` Tobias Deiminger
2025-11-29 20:10                       ` Julia Lawall
2025-11-30  8:44                         ` [cocci] Clarification for parsing capabilities? Markus Elfring
2025-11-30  8:05                       ` [cocci] Can we match a known macro by macro name instead of expanded function name? Markus Elfring
2025-11-30 19:02                         ` Tobias Deiminger
2025-12-01  8:32                           ` Tobias Deiminger
2025-12-01 14:00                             ` Markus Elfring
2025-12-01  9:42                           ` Markus Elfring
2025-12-04  8:50                           ` [cocci] Searching for system call implementations? Markus Elfring
2025-11-29 16:28         ` [cocci] Can we match a known macro by macro name instead of expanded function name? Markus Elfring
2025-12-02  9:45 ` [cocci] Searching for system call implementations with SmPL? Markus Elfring

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2897bda0c438111e967017f6bb3422f0@posteo.de \
    --to=tobias.deiminger@posteo.de \
    --cc=cocci@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox