Package: coccigrep
IntroductionCoccigrep is a semantic grep for the C and C++ languages 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.UsageRuncoccigrep -hfor complete options.ExamplesTo find where in a set of files the type namedPacketis 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) {If you want to match on structure, you need to provide the complete namecoccigrep -t 'struct seq_file' fs/seq_file.c
fs/seq_file.c:654 (struct seq_file *seq):   seq = f->private_data;
fs/seq_file.c:655 (struct seq_file *seq):   seq->private = private;
fs/seq_file.c:537 (struct seq_file *m):     if (m->count < m->size) {To find where in a set of files thedatalinkattribute is used in the structure
namedPacket, 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 issetwhich 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;InstallationThe dependencies of coccigrep are spatch which comes with coccinelle. On python side, you
need setuptools and optionally pygments (for colorized output). Happy Debian user can doaptitude install python-setuptools python-pygmentsTo install coccigrep runsudo python ./setup.py installConfigurationAs from version 0.8, coccigrep can be configured via a configuration file. A complete sample of
configuration file is available in the src/coccinelle.cfg.Hierarchical configurationThe configuration file system is hierarchical and the following files are parsed in that orderhost config in /etc/coccigrepuser config in ~/.coccigrepdirectory config in .coccigrepThus, for example, the directory config settings will overwrite host config settings.Interesting optionsIn the global section, theconcurrency_levelis 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.cocciwill lead to thezeroedoperation).
Then add alocal_cocci_dirpointing to this directory in the global section.For a description of the writing of semantic patches seecoccigrep homepage.Other options are more explicit and are direct mapping of the associated command line option.Running coccigrep in vimTo use coccigrep in vim, you can use thecocci-grep.vimplugin provided in
theeditorsdirectory. To do so you can simply copy it to your plugin directory
which is usually~/.vim/plugin/. If yourcoccigrepscript in not in your
path, you can use the coccigrep_path variable to give complete path. For
example, you can add to your.vimrclet g:coccigrep_path = '/usr/local/bin/coccigrep'And then you can run commands like:Coccigrep
:Coccigrep Packet datalink source-*.c
:Coccigrep Packet datalink set source-*.cFirst command will interactively ask you the value. Hit enter to use void
value for type and/or attribute (only if operation does not need them).
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 runcoccigrep -Lor look at the list provided when input for
operation is asked in interactive mode.The matches will appear in thequickfix listand 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 bymake 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*cPlease note that, in interactive mode, quoting is not necessary.You can also set the global variablecoccigrep_files:let g:coccigrep_files = '~/myproject/src/flist'whereflistis the file corresponding to the-loption.
And then you can run commands like:Coccigrep Packet
:Coccigrep Packet datalink setThat 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 emacsTo use coccigrep in emacs, you need to load thecocci-grep.elmodule provided in theeditorsdirectory of the source code. For example, if you copy it in~/.emacs.d/site-lisp/, you
can do(add-to-list 'load-path "~/.emacs.d/site-lisp/")
(require 'cocci-grep)And then you can run something likeMeta+x cocci-grepand answer to the questions which areType: The structure type you are searchingAttribut: The attribute in the structureOperation: The operation on the structure. The set of commands include set,used,func,test,derefFiles: A blob expression that will match the file you want to search inThe matches will appear in a buffer with mode set togrep-modeand you will thus be able to jump
on occurence. History is available on the different parameters.Current limitationsMacros: Coccigrep won’t expand the macros for you, so code contained in them won’t be matched.Nested functions: Coccinelle might not match code inside a nested function,
and so does coccigrep.