coccigrep is a semantic grep for the C language based on coccinelle.
coccigrep can be used to find where a given structure is used in code files. coccigrep depends on the spatch program which comes with coccinelle.
As you may have guess, the source can be accessed via github.
Examples:
To find where in a set of files the structure named Packet is used, you can run:
$ coccigrep -t Packet *c
source-af-packet.c:272: p = ptv->in_p;
source-af-packet.c:300: p->datalink = ptv->datalink;
source-af-packet.c:758: switch(p->datalink) {
To find where in a set of files the datalink attribute is used in the structure
named Packet, you can simply do:
$ coccigrep -t Packet -a datalink *c
source-af-packet.c:300: p->datalink = ptv->datalink;
source-af-packet.c:758: switch(p->datalink) {
source-erf-dag.c:525: p->datalink = LINKTYPE_ETHERNET;
If you want to be more precise and find where this attribute is set, you can use
the operation flag (-o). One of its value is set which indicate we only want
the match where the attribute is set:
$ coccigrep -t Packet -a datalink -o set source*c
source-af-packet.c:300: p->datalink = ptv->datalink;
source-erf-dag.c:525: p->datalink = LINKTYPE_ETHERNET;
coccigrep supports syntax highlighting through the pygments module. For example, running coccigrep -t Packet -a datalink -o test -c -A 3 -B 3 -f html /tmp/test.c will output to stdout some colorized HTML code:
/tmp/test.c: l.300 -3, l.300 +3, Packet *p
hdrp->sll_protocol = from.sll_protocol;
}
while (p->datalink >= ptv->datalink) {
SET_PKT_LEN(p, caplen + offset);
if (PacketCopyData(p, ptv->data, GET_PKT_LEN(p)) == -1) {
TmqhOutputPacketpool(ptv->tv, p);
Usage:
usage: coccigrep [-h] [-t TYPE] [-a ATTRIBUT] [-o {used,test,set}] [-A AFTER]
[-B BEFORE] [-c] [-f {term,html}] [-v] [--version]
file [file ...]
Semantic grep based on coccinelle
positional arguments:
file List of files
optional arguments:
-h, --help show this help message and exit
-t TYPE, --type TYPE C type where looking for
-a ATTRIBUT, --attribut ATTRIBUT
C attribut that is set
-o {used,test,set}, --operation {used,test,set}
Operation on structure
-A AFTER, --after-context AFTER
Number of line after context
-B BEFORE, --before-context BEFORE
Number of line before context
-c, --color colorize output (need pigments)
-f {term,html}, --output-format {term,html}
colorize format for output
-v, --verbose verbose output (including coccinelle error)
--version show program's version number and exit
Run coccigrep -h for up-to-date and complete list of options.
Product's homepage
Requirements:
· Python
What's New in This Release: [ read full changelog ]
· This version contains an improvement in recursive searching which now includes header files.
· It also features some bugfixes.