Introduction

coccigrep is a semantic grep for the C language based on coccinelle
(http://coccinelle.lip6.fr). It can be used to find where a given
structure is used in code files. coccigrep depends on the spatch
program which comes with coccinelle.

Usage

Run coccigrep -h for complete options.

Examples

To find where in a set of files the type named Packet is used, you
can run

Interesting options

In the global section, the concurrency_level is the most interesting. It codes the number of
spatch commands that will be launched in parallel. If multiple files are search, this will
increase dramatically performances at the cost of a little increase of memory usage.

If you want to add your own semantic patches, you just have to put them in a directory with
name matchting the wanted operation name (zeroed.cocci will lead to the zeroed operation).
Then add a local_cocci_dir pointing to this directory in the global section.

Other options are more explicit and are direct mapping of the associated command line option.

Running coccigrep in vim

To use coccigrep in vim, you can use the cocci-grep.vim plugin provided in
the editors directory. To do so you can simply copy it to your plugin directory
which is usually ~/.vim/plugin/. If your coccigrep script in not in your
path, you can use the coccigrep_path variable to give complete path. For
example, you can add to your .vimrc

First command will interactively ask you the value. Second one will search all
dereference of the datalink attribute for Packet structure. The last one will
look where the set operation is done on the datalink attribute of Packet. To get
the list of operations on your system, you can run coccigrep -L or look at
the list provided when input for operation is asked in interactive mode.

The matches will appear in the quickfix list and the file corresponding to first
match will be opened at the corresponding line. Note that you can use completion on
structure and attribute names based on tags (generated by make tags).

To run a search in vim on a non-named structure, you must quote the spaces and
thus run something like

:Coccigrep "struct nfq_data" s*c

Please note that, in interactive mode, quoting is not necessary.

You can also set the global variable coccigrep_files

:let g:coccigrep_files = '~/myproject/src/flist'

where flist is the file corresponding to the -l option.
And then you can run commands like

:Coccigrep Packet
:Coccigrep Packet datalink set

That is, you don’t need to provide the last argument of the previous examples.
This is particularly useful if you set vim’s autochdir option.

Running coccigrep in emacs

To use coccigrep in emacs, you need to load the cocci-grep.el module provided in the editors
directory of the source code. For example, if you copy it in ~/.emacs.d/site-lisp/, you
can do