Re: [cocci] Python script interacting with non-matching files?
Alice Lee <[email protected]> Mon, 16 Mar 2026 12:50:05 +0000
| Newsgroups | fr.inria.cocci |
|---|---|
| Message-ID | <[email protected]> |
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? 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? 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 > > >