Re: [cocci] Python script interacting with non-matching files?

Julia Lawall <[email protected]> Mon, 16 Mar 2026 13:58:13 +0100 (CET)
Newsgroups fr.inria.cocci
Message-ID <[email protected]>

On Mon, 16 Mar 2026, Alice Lee wrote:

>
> Hi Julia, 
>
>
> Thanks for getting back to me!
>
>
>  1. Maybe it's easier to state my overall goal: I want to count the number of matches for some rule for each file in a directory. Currently I can get a python dictionary correctly counting the number of
>     matches in files in which there is at least one match. 
>     Your particular solution wouldn't work unless I know some value is in all files. Is the best solution just to get directory information in the initialization script to populate the dictionary instead of
>     trying to do this through Coccinelle?

I think there is a way to get the list of files that will be considered
based on analysis of your semantic patch.  But if you just want all files,
I think you should get that separately.

>  2. My ideal solution would make a dictionary of all matches in each file and report files that don't parse as having a count of -1. Files aren't partially failing to parse, at least according to the --debug
>     output, but say "bad" on every line. Still no way to access that in the python script?

No, there is no way to access anything about parsing in a python script.
Python is only triggered when you match something.

It seems odd that there would be bad on every line.  When your code ends a
function, does it put the final } somewhere other than the leftmost
column?  That is what Coccinelle uses to detect that it has reached the
next top level thing, to start parsing again.

julia

> Thanks,
> Alice
>
> _______________________________________________________________________________________________________________________________________________________________________________________________________________
> From: Julia Lawall <[email protected]>
> Sent: Friday, March 13, 2026 3:15:29 PM
> To: Alice Lee
> Cc: [email protected]
> Subject: Re: [cocci] Python script interacting with non-matching files?  
> [EXTERNAL EMAIL - USE CAUTION when clicking links or attachments]
>
>
>
>
> On Thu, 12 Mar 2026, Alice Lee wrote:
>
> >
> > Hello,
> >
> > I am trying to use Coccinelle to search C code bases and I have a few
> > questions.
> >
> >  1. Can a python script within a Coccinelle file somehow access the names of
> >     files that are processed without matching the search? (Maybe there is
> >     some "match all" idiom that would let me get the name of every file and
> >     then move on to the relevant search?)
>
> I don't completely understand the question.  You can attach a position
> variable to some matched code and then print the file name associated with
> that code:
>
> @r@
> position p;
> @@
>
> foo@p();
>
> @script:python@
> p << r.p;
> @@
>
> print p[0].file  // put proper python code here
>
> >  2. Similarly, can a python script tell that parsing failed for a particular
> >     file?
>
> No, python doesn't have an access to the information about what could be
> parsed.  There is --verbose-parsing.  But parsing rarely fails for a whole
> file.  If it can't parse one thing, it will just move on to the next thing
> and try from there.
>
> julia
>
> > Thank you,
> > Alice Lee    
> >
> >
> >
>
>