Skip to contentSkip to conversation

Python script interacting with non-matching files?

cocci Alice Lee 3 participants 5 messages Mar 13 – Mar 17

lore
Share & tools
lore

Alice Lee

author
#0 lore raw

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?)
2. Similarly, can a python script tell that parsing failed for a particular file?

Thank you,
Alice Lee

#2 lore raw
In reply to Alice Lee
Quoted 10 lines · Alice Lee

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

Quoted 2 lines

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

Quoted 5 lines

Thank you,
Alice Lee

author
#3 lore raw

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]

Quoted 10 lines · Alice Lee

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

Quoted 2 lines

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

Quoted 5 lines

Thank you,
Alice Lee

#4 lore raw
Quoted 11 lines · Alice Lee

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.

Quoted 2 lines

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

Quoted 60 lines

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]

Quoted 10 lines · Alice Lee

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

Quoted 2 lines

  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

Quoted 5 lines

Thank you,
Alice Lee